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
19 #define dev_fmt pr_fmt
21 #include <linux/device.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm-trace.h>
27 #include <linux/pm_wakeirq.h>
28 #include <linux/interrupt.h>
29 #include <linux/sched.h>
30 #include <linux/sched/debug.h>
31 #include <linux/async.h>
32 #include <linux/suspend.h>
33 #include <trace/events/power.h>
34 #include <linux/cpufreq.h>
35 #include <linux/devfreq.h>
36 #include <linux/timer.h>
41 typedef int (*pm_callback_t
)(struct device
*);
43 #define list_for_each_entry_rcu_locked(pos, head, member) \
44 list_for_each_entry_rcu(pos, head, member, \
45 device_links_read_lock_held())
48 * The entries in the dpm_list list are in a depth first order, simply
49 * because children are guaranteed to be discovered after parents, and
50 * are inserted at the back of the list on discovery.
52 * Since device_pm_add() may be called with a device lock held,
53 * we must never try to acquire a device lock while holding
58 static LIST_HEAD(dpm_prepared_list
);
59 static LIST_HEAD(dpm_suspended_list
);
60 static LIST_HEAD(dpm_late_early_list
);
61 static LIST_HEAD(dpm_noirq_list
);
63 static DEFINE_MUTEX(dpm_list_mtx
);
64 static pm_message_t pm_transition
;
66 static int async_error
;
68 static const char *pm_verb(int event
)
71 case PM_EVENT_SUSPEND
:
77 case PM_EVENT_QUIESCE
:
79 case PM_EVENT_HIBERNATE
:
83 case PM_EVENT_RESTORE
:
85 case PM_EVENT_RECOVER
:
88 return "(unknown PM event)";
93 * device_pm_sleep_init - Initialize system suspend-related device fields.
94 * @dev: Device object being initialized.
96 void device_pm_sleep_init(struct device
*dev
)
98 dev
->power
.is_prepared
= false;
99 dev
->power
.is_suspended
= false;
100 dev
->power
.is_noirq_suspended
= false;
101 dev
->power
.is_late_suspended
= false;
102 init_completion(&dev
->power
.completion
);
103 complete_all(&dev
->power
.completion
);
104 dev
->power
.wakeup
= NULL
;
105 INIT_LIST_HEAD(&dev
->power
.entry
);
109 * device_pm_lock - Lock the list of active devices used by the PM core.
111 void device_pm_lock(void)
113 mutex_lock(&dpm_list_mtx
);
117 * device_pm_unlock - Unlock the list of active devices used by the PM core.
119 void device_pm_unlock(void)
121 mutex_unlock(&dpm_list_mtx
);
125 * device_pm_add - Add a device to the PM core's list of active devices.
126 * @dev: Device to add to the list.
128 void device_pm_add(struct device
*dev
)
130 /* Skip PM setup/initialization. */
131 if (device_pm_not_required(dev
))
134 pr_debug("Adding info for %s:%s\n",
135 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
136 device_pm_check_callbacks(dev
);
137 mutex_lock(&dpm_list_mtx
);
138 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
139 dev_warn(dev
, "parent %s should not be sleeping\n",
140 dev_name(dev
->parent
));
141 list_add_tail(&dev
->power
.entry
, &dpm_list
);
142 dev
->power
.in_dpm_list
= true;
143 mutex_unlock(&dpm_list_mtx
);
147 * device_pm_remove - Remove a device from the PM core's list of active devices.
148 * @dev: Device to be removed from the list.
150 void device_pm_remove(struct device
*dev
)
152 if (device_pm_not_required(dev
))
155 pr_debug("Removing info for %s:%s\n",
156 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
157 complete_all(&dev
->power
.completion
);
158 mutex_lock(&dpm_list_mtx
);
159 list_del_init(&dev
->power
.entry
);
160 dev
->power
.in_dpm_list
= false;
161 mutex_unlock(&dpm_list_mtx
);
162 device_wakeup_disable(dev
);
163 pm_runtime_remove(dev
);
164 device_pm_check_callbacks(dev
);
168 * device_pm_move_before - Move device in the PM core's list of active devices.
169 * @deva: Device to move in dpm_list.
170 * @devb: Device @deva should come before.
172 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
174 pr_debug("Moving %s:%s before %s:%s\n",
175 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
176 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
177 /* Delete deva from dpm_list and reinsert before devb. */
178 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
182 * device_pm_move_after - Move device in the PM core's list of active devices.
183 * @deva: Device to move in dpm_list.
184 * @devb: Device @deva should come after.
186 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
188 pr_debug("Moving %s:%s after %s:%s\n",
189 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
190 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
191 /* Delete deva from dpm_list and reinsert after devb. */
192 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
196 * device_pm_move_last - Move device to end of the PM core's list of devices.
197 * @dev: Device to move in dpm_list.
199 void device_pm_move_last(struct device
*dev
)
201 pr_debug("Moving %s:%s to end of list\n",
202 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
203 list_move_tail(&dev
->power
.entry
, &dpm_list
);
206 static ktime_t
initcall_debug_start(struct device
*dev
, void *cb
)
208 if (!pm_print_times_enabled
)
211 dev_info(dev
, "calling %ps @ %i, parent: %s\n", cb
,
212 task_pid_nr(current
),
213 dev
->parent
? dev_name(dev
->parent
) : "none");
217 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
222 if (!pm_print_times_enabled
)
225 rettime
= ktime_get();
226 dev_info(dev
, "%ps returned %d after %Ld usecs\n", cb
, error
,
227 (unsigned long long)ktime_us_delta(rettime
, calltime
));
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_locked(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 bool dpm_wait_for_superior(struct device
*dev
, bool async
)
278 struct device
*parent
;
281 * If the device is resumed asynchronously and the parent's callback
282 * deletes both the device and the parent itself, the parent object may
283 * be freed while this function is running, so avoid that by reference
284 * counting the parent once more unless the device has been deleted
285 * already (in which case return right away).
287 mutex_lock(&dpm_list_mtx
);
289 if (!device_pm_initialized(dev
)) {
290 mutex_unlock(&dpm_list_mtx
);
294 parent
= get_device(dev
->parent
);
296 mutex_unlock(&dpm_list_mtx
);
298 dpm_wait(parent
, async
);
301 dpm_wait_for_suppliers(dev
, async
);
304 * If the parent's callback has deleted the device, attempting to resume
305 * it would be invalid, so avoid doing that then.
307 return device_pm_initialized(dev
);
310 static void dpm_wait_for_consumers(struct device
*dev
, bool async
)
312 struct device_link
*link
;
315 idx
= device_links_read_lock();
318 * The status of a device link can only be changed from "dormant" by a
319 * probe, but that cannot happen during system suspend/resume. In
320 * theory it can change to "dormant" at that time, but then it is
321 * reasonable to wait for the target device anyway (eg. if it goes
322 * away, it's better to wait for it to go away completely and then
323 * continue instead of trying to continue in parallel with its
326 list_for_each_entry_rcu_locked(link
, &dev
->links
.consumers
, s_node
)
327 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
328 dpm_wait(link
->consumer
, async
);
330 device_links_read_unlock(idx
);
333 static void dpm_wait_for_subordinate(struct device
*dev
, bool async
)
335 dpm_wait_for_children(dev
, async
);
336 dpm_wait_for_consumers(dev
, async
);
340 * pm_op - Return the PM operation appropriate for given PM event.
341 * @ops: PM operations to choose from.
342 * @state: PM transition of the system being carried out.
344 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
346 switch (state
.event
) {
347 #ifdef CONFIG_SUSPEND
348 case PM_EVENT_SUSPEND
:
350 case PM_EVENT_RESUME
:
352 #endif /* CONFIG_SUSPEND */
353 #ifdef CONFIG_HIBERNATE_CALLBACKS
354 case PM_EVENT_FREEZE
:
355 case PM_EVENT_QUIESCE
:
357 case PM_EVENT_HIBERNATE
:
358 return ops
->poweroff
;
360 case PM_EVENT_RECOVER
:
362 case PM_EVENT_RESTORE
:
364 #endif /* CONFIG_HIBERNATE_CALLBACKS */
371 * pm_late_early_op - Return the PM operation appropriate for given PM event.
372 * @ops: PM operations to choose from.
373 * @state: PM transition of the system being carried out.
375 * Runtime PM is disabled for @dev while this function is being executed.
377 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
380 switch (state
.event
) {
381 #ifdef CONFIG_SUSPEND
382 case PM_EVENT_SUSPEND
:
383 return ops
->suspend_late
;
384 case PM_EVENT_RESUME
:
385 return ops
->resume_early
;
386 #endif /* CONFIG_SUSPEND */
387 #ifdef CONFIG_HIBERNATE_CALLBACKS
388 case PM_EVENT_FREEZE
:
389 case PM_EVENT_QUIESCE
:
390 return ops
->freeze_late
;
391 case PM_EVENT_HIBERNATE
:
392 return ops
->poweroff_late
;
394 case PM_EVENT_RECOVER
:
395 return ops
->thaw_early
;
396 case PM_EVENT_RESTORE
:
397 return ops
->restore_early
;
398 #endif /* CONFIG_HIBERNATE_CALLBACKS */
405 * pm_noirq_op - Return the PM operation appropriate for given PM event.
406 * @ops: PM operations to choose from.
407 * @state: PM transition of the system being carried out.
409 * The driver of @dev will not receive interrupts while this function is being
412 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
414 switch (state
.event
) {
415 #ifdef CONFIG_SUSPEND
416 case PM_EVENT_SUSPEND
:
417 return ops
->suspend_noirq
;
418 case PM_EVENT_RESUME
:
419 return ops
->resume_noirq
;
420 #endif /* CONFIG_SUSPEND */
421 #ifdef CONFIG_HIBERNATE_CALLBACKS
422 case PM_EVENT_FREEZE
:
423 case PM_EVENT_QUIESCE
:
424 return ops
->freeze_noirq
;
425 case PM_EVENT_HIBERNATE
:
426 return ops
->poweroff_noirq
;
428 case PM_EVENT_RECOVER
:
429 return ops
->thaw_noirq
;
430 case PM_EVENT_RESTORE
:
431 return ops
->restore_noirq
;
432 #endif /* CONFIG_HIBERNATE_CALLBACKS */
438 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, const char *info
)
440 dev_dbg(dev
, "%s%s%s driver flags: %x\n", info
, pm_verb(state
.event
),
441 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
442 ", may wakeup" : "", dev
->power
.driver_flags
);
445 static void pm_dev_err(struct device
*dev
, pm_message_t state
, const char *info
,
448 dev_err(dev
, "failed to %s%s: error %d\n", pm_verb(state
.event
), info
,
452 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, int error
,
459 calltime
= ktime_get();
460 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
461 do_div(usecs64
, NSEC_PER_USEC
);
466 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
467 info
?: "", info
? " " : "", pm_verb(state
.event
),
468 error
? "aborted" : "complete",
469 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
472 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
473 pm_message_t state
, const char *info
)
481 calltime
= initcall_debug_start(dev
, cb
);
483 pm_dev_dbg(dev
, state
, info
);
484 trace_device_pm_callback_start(dev
, info
, state
.event
);
486 trace_device_pm_callback_end(dev
, error
);
487 suspend_report_result(dev
, cb
, error
);
489 initcall_debug_report(dev
, calltime
, cb
, error
);
494 #ifdef CONFIG_DPM_WATCHDOG
495 struct dpm_watchdog
{
497 struct task_struct
*tsk
;
498 struct timer_list timer
;
501 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
502 struct dpm_watchdog wd
505 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
506 * @t: The timer that PM watchdog depends on.
508 * Called when a driver has timed out suspending or resuming.
509 * There's not much we can do here to recover so panic() to
510 * capture a crash-dump in pstore.
512 static void dpm_watchdog_handler(struct timer_list
*t
)
514 struct dpm_watchdog
*wd
= from_timer(wd
, t
, timer
);
516 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
517 show_stack(wd
->tsk
, NULL
, KERN_EMERG
);
518 panic("%s %s: unrecoverable failure\n",
519 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
523 * dpm_watchdog_set - Enable pm watchdog for given device.
524 * @wd: Watchdog. Must be allocated on the stack.
525 * @dev: Device to handle.
527 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
529 struct timer_list
*timer
= &wd
->timer
;
534 timer_setup_on_stack(timer
, dpm_watchdog_handler
, 0);
535 /* use same timeout value for both suspend and resume */
536 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
541 * dpm_watchdog_clear - Disable suspend/resume watchdog.
542 * @wd: Watchdog to disable.
544 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
546 struct timer_list
*timer
= &wd
->timer
;
548 del_timer_sync(timer
);
549 destroy_timer_on_stack(timer
);
552 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
553 #define dpm_watchdog_set(x, y)
554 #define dpm_watchdog_clear(x)
557 /*------------------------- Resume routines -------------------------*/
560 * dev_pm_skip_resume - System-wide device resume optimization check.
561 * @dev: Target device.
564 * - %false if the transition under way is RESTORE.
565 * - Return value of dev_pm_skip_suspend() if the transition under way is THAW.
566 * - The logical negation of %power.must_resume otherwise (that is, when the
567 * transition under way is RESUME).
569 bool dev_pm_skip_resume(struct device
*dev
)
571 if (pm_transition
.event
== PM_EVENT_RESTORE
)
574 if (pm_transition
.event
== PM_EVENT_THAW
)
575 return dev_pm_skip_suspend(dev
);
577 return !dev
->power
.must_resume
;
580 static bool is_async(struct device
*dev
)
582 return dev
->power
.async_suspend
&& pm_async_enabled
583 && !pm_trace_is_enabled();
586 static bool dpm_async_fn(struct device
*dev
, async_func_t func
)
588 reinit_completion(&dev
->power
.completion
);
591 dev
->power
.async_in_progress
= true;
595 if (async_schedule_dev_nocall(func
, dev
))
601 * Because async_schedule_dev_nocall() above has returned false or it
602 * has not been called at all, func() is not running and it is safe to
603 * update the async_in_progress flag without extra synchronization.
605 dev
->power
.async_in_progress
= false;
610 * device_resume_noirq - Execute a "noirq resume" callback for given device.
611 * @dev: Device to handle.
612 * @state: PM transition of the system being carried out.
613 * @async: If true, the device is being resumed asynchronously.
615 * The driver of @dev will not receive interrupts while this function is being
618 static void device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
620 pm_callback_t callback
= NULL
;
621 const char *info
= NULL
;
628 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
631 if (!dev
->power
.is_noirq_suspended
)
634 if (!dpm_wait_for_superior(dev
, async
))
637 skip_resume
= dev_pm_skip_resume(dev
);
639 * If the driver callback is skipped below or by the middle layer
640 * callback and device_resume_early() also skips the driver callback for
641 * this device later, it needs to appear as "suspended" to PM-runtime,
642 * so change its status accordingly.
644 * Otherwise, the device is going to be resumed, so set its PM-runtime
645 * status to "active", but do that only if DPM_FLAG_SMART_SUSPEND is set
646 * to avoid confusing drivers that don't use it.
649 pm_runtime_set_suspended(dev
);
650 else if (dev_pm_skip_suspend(dev
))
651 pm_runtime_set_active(dev
);
653 if (dev
->pm_domain
) {
654 info
= "noirq power domain ";
655 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
656 } else if (dev
->type
&& dev
->type
->pm
) {
657 info
= "noirq type ";
658 callback
= pm_noirq_op(dev
->type
->pm
, state
);
659 } else if (dev
->class && dev
->class->pm
) {
660 info
= "noirq class ";
661 callback
= pm_noirq_op(dev
->class->pm
, state
);
662 } else if (dev
->bus
&& dev
->bus
->pm
) {
664 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
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;
684 complete_all(&dev
->power
.completion
);
689 dpm_save_failed_dev(dev_name(dev
));
690 pm_dev_err(dev
, state
, async
? " async noirq" : " noirq", error
);
694 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
696 struct device
*dev
= data
;
698 device_resume_noirq(dev
, pm_transition
, true);
702 static void dpm_noirq_resume_devices(pm_message_t state
)
705 ktime_t starttime
= ktime_get();
707 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
710 pm_transition
= state
;
712 mutex_lock(&dpm_list_mtx
);
715 * Trigger the resume of "async" devices upfront so they don't have to
716 * wait for the "non-async" ones they don't depend on.
718 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
)
719 dpm_async_fn(dev
, async_resume_noirq
);
721 while (!list_empty(&dpm_noirq_list
)) {
722 dev
= to_device(dpm_noirq_list
.next
);
723 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
725 if (!dev
->power
.async_in_progress
) {
728 mutex_unlock(&dpm_list_mtx
);
730 device_resume_noirq(dev
, state
, false);
734 mutex_lock(&dpm_list_mtx
);
737 mutex_unlock(&dpm_list_mtx
);
738 async_synchronize_full();
739 dpm_show_time(starttime
, state
, 0, "noirq");
741 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
743 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
747 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
748 * @state: PM transition of the system being carried out.
750 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
751 * allow device drivers' interrupt handlers to be called.
753 void dpm_resume_noirq(pm_message_t state
)
755 dpm_noirq_resume_devices(state
);
757 resume_device_irqs();
758 device_wakeup_disarm_wake_irqs();
762 * device_resume_early - Execute an "early resume" callback for given device.
763 * @dev: Device to handle.
764 * @state: PM transition of the system being carried out.
765 * @async: If true, the device is being resumed asynchronously.
767 * Runtime PM is disabled for @dev while this function is being executed.
769 static void device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
771 pm_callback_t callback
= NULL
;
772 const char *info
= NULL
;
778 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
781 if (!dev
->power
.is_late_suspended
)
784 if (!dpm_wait_for_superior(dev
, async
))
787 if (dev
->pm_domain
) {
788 info
= "early power domain ";
789 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
790 } else if (dev
->type
&& dev
->type
->pm
) {
791 info
= "early type ";
792 callback
= pm_late_early_op(dev
->type
->pm
, state
);
793 } else if (dev
->class && dev
->class->pm
) {
794 info
= "early class ";
795 callback
= pm_late_early_op(dev
->class->pm
, state
);
796 } else if (dev
->bus
&& dev
->bus
->pm
) {
798 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
803 if (dev_pm_skip_resume(dev
))
806 if (dev
->driver
&& dev
->driver
->pm
) {
807 info
= "early driver ";
808 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
812 error
= dpm_run_callback(callback
, dev
, state
, info
);
815 dev
->power
.is_late_suspended
= false;
820 pm_runtime_enable(dev
);
821 complete_all(&dev
->power
.completion
);
825 dpm_save_failed_dev(dev_name(dev
));
826 pm_dev_err(dev
, state
, async
? " async early" : " early", error
);
830 static void async_resume_early(void *data
, async_cookie_t cookie
)
832 struct device
*dev
= data
;
834 device_resume_early(dev
, pm_transition
, true);
839 * dpm_resume_early - Execute "early resume" callbacks for all devices.
840 * @state: PM transition of the system being carried out.
842 void dpm_resume_early(pm_message_t state
)
845 ktime_t starttime
= ktime_get();
847 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
850 pm_transition
= state
;
852 mutex_lock(&dpm_list_mtx
);
855 * Trigger the resume of "async" devices upfront so they don't have to
856 * wait for the "non-async" ones they don't depend on.
858 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
)
859 dpm_async_fn(dev
, async_resume_early
);
861 while (!list_empty(&dpm_late_early_list
)) {
862 dev
= to_device(dpm_late_early_list
.next
);
863 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
865 if (!dev
->power
.async_in_progress
) {
868 mutex_unlock(&dpm_list_mtx
);
870 device_resume_early(dev
, state
, false);
874 mutex_lock(&dpm_list_mtx
);
877 mutex_unlock(&dpm_list_mtx
);
878 async_synchronize_full();
879 dpm_show_time(starttime
, state
, 0, "early");
881 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
883 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
887 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
888 * @state: PM transition of the system being carried out.
890 void dpm_resume_start(pm_message_t state
)
892 dpm_resume_noirq(state
);
893 dpm_resume_early(state
);
895 EXPORT_SYMBOL_GPL(dpm_resume_start
);
898 * device_resume - Execute "resume" callbacks for given device.
899 * @dev: Device to handle.
900 * @state: PM transition of the system being carried out.
901 * @async: If true, the device is being resumed asynchronously.
903 static void device_resume(struct device
*dev
, pm_message_t state
, bool async
)
905 pm_callback_t callback
= NULL
;
906 const char *info
= NULL
;
908 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
913 if (dev
->power
.syscore
)
916 if (dev
->power
.direct_complete
) {
917 /* Match the pm_runtime_disable() in __device_suspend(). */
918 pm_runtime_enable(dev
);
922 if (!dpm_wait_for_superior(dev
, async
))
925 dpm_watchdog_set(&wd
, dev
);
929 * This is a fib. But we'll allow new children to be added below
930 * a resumed device, even if the device hasn't been completed yet.
932 dev
->power
.is_prepared
= false;
934 if (!dev
->power
.is_suspended
)
937 if (dev
->pm_domain
) {
938 info
= "power domain ";
939 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
943 if (dev
->type
&& dev
->type
->pm
) {
945 callback
= pm_op(dev
->type
->pm
, state
);
949 if (dev
->class && dev
->class->pm
) {
951 callback
= pm_op(dev
->class->pm
, state
);
958 callback
= pm_op(dev
->bus
->pm
, state
);
959 } else if (dev
->bus
->resume
) {
960 info
= "legacy bus ";
961 callback
= dev
->bus
->resume
;
967 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
969 callback
= pm_op(dev
->driver
->pm
, state
);
973 error
= dpm_run_callback(callback
, dev
, state
, info
);
974 dev
->power
.is_suspended
= false;
978 dpm_watchdog_clear(&wd
);
981 complete_all(&dev
->power
.completion
);
987 dpm_save_failed_dev(dev_name(dev
));
988 pm_dev_err(dev
, state
, async
? " async" : "", error
);
992 static void async_resume(void *data
, async_cookie_t cookie
)
994 struct device
*dev
= data
;
996 device_resume(dev
, pm_transition
, true);
1001 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1002 * @state: PM transition of the system being carried out.
1004 * Execute the appropriate "resume" callback for all devices whose status
1005 * indicates that they are suspended.
1007 void dpm_resume(pm_message_t state
)
1010 ktime_t starttime
= ktime_get();
1012 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
1015 pm_transition
= state
;
1018 mutex_lock(&dpm_list_mtx
);
1021 * Trigger the resume of "async" devices upfront so they don't have to
1022 * wait for the "non-async" ones they don't depend on.
1024 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
)
1025 dpm_async_fn(dev
, async_resume
);
1027 while (!list_empty(&dpm_suspended_list
)) {
1028 dev
= to_device(dpm_suspended_list
.next
);
1029 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1031 if (!dev
->power
.async_in_progress
) {
1034 mutex_unlock(&dpm_list_mtx
);
1036 device_resume(dev
, state
, false);
1040 mutex_lock(&dpm_list_mtx
);
1043 mutex_unlock(&dpm_list_mtx
);
1044 async_synchronize_full();
1045 dpm_show_time(starttime
, state
, 0, NULL
);
1047 dpm_save_failed_step(SUSPEND_RESUME
);
1051 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
1055 * device_complete - Complete a PM transition for given device.
1056 * @dev: Device to handle.
1057 * @state: PM transition of the system being carried out.
1059 static void device_complete(struct device
*dev
, pm_message_t state
)
1061 void (*callback
)(struct device
*) = NULL
;
1062 const char *info
= NULL
;
1064 if (dev
->power
.syscore
)
1069 if (dev
->pm_domain
) {
1070 info
= "completing power domain ";
1071 callback
= dev
->pm_domain
->ops
.complete
;
1072 } else if (dev
->type
&& dev
->type
->pm
) {
1073 info
= "completing type ";
1074 callback
= dev
->type
->pm
->complete
;
1075 } else if (dev
->class && dev
->class->pm
) {
1076 info
= "completing class ";
1077 callback
= dev
->class->pm
->complete
;
1078 } else if (dev
->bus
&& dev
->bus
->pm
) {
1079 info
= "completing bus ";
1080 callback
= dev
->bus
->pm
->complete
;
1083 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1084 info
= "completing driver ";
1085 callback
= dev
->driver
->pm
->complete
;
1089 pm_dev_dbg(dev
, state
, info
);
1096 pm_runtime_put(dev
);
1100 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1101 * @state: PM transition of the system being carried out.
1103 * Execute the ->complete() callbacks for all devices whose PM status is not
1104 * DPM_ON (this allows new devices to be registered).
1106 void dpm_complete(pm_message_t state
)
1108 struct list_head list
;
1110 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
1113 INIT_LIST_HEAD(&list
);
1114 mutex_lock(&dpm_list_mtx
);
1115 while (!list_empty(&dpm_prepared_list
)) {
1116 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1119 dev
->power
.is_prepared
= false;
1120 list_move(&dev
->power
.entry
, &list
);
1122 mutex_unlock(&dpm_list_mtx
);
1124 trace_device_pm_callback_start(dev
, "", state
.event
);
1125 device_complete(dev
, state
);
1126 trace_device_pm_callback_end(dev
, 0);
1130 mutex_lock(&dpm_list_mtx
);
1132 list_splice(&list
, &dpm_list
);
1133 mutex_unlock(&dpm_list_mtx
);
1135 /* Allow device probing and trigger re-probing of deferred devices */
1136 device_unblock_probing();
1137 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
1141 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1142 * @state: PM transition of the system being carried out.
1144 * Execute "resume" callbacks for all devices and complete the PM transition of
1147 void dpm_resume_end(pm_message_t state
)
1150 dpm_complete(state
);
1152 EXPORT_SYMBOL_GPL(dpm_resume_end
);
1155 /*------------------------- Suspend routines -------------------------*/
1158 * resume_event - Return a "resume" message for given "suspend" sleep state.
1159 * @sleep_state: PM message representing a sleep state.
1161 * Return a PM message representing the resume event corresponding to given
1164 static pm_message_t
resume_event(pm_message_t sleep_state
)
1166 switch (sleep_state
.event
) {
1167 case PM_EVENT_SUSPEND
:
1169 case PM_EVENT_FREEZE
:
1170 case PM_EVENT_QUIESCE
:
1171 return PMSG_RECOVER
;
1172 case PM_EVENT_HIBERNATE
:
1173 return PMSG_RESTORE
;
1178 static void dpm_superior_set_must_resume(struct device
*dev
)
1180 struct device_link
*link
;
1184 dev
->parent
->power
.must_resume
= true;
1186 idx
= device_links_read_lock();
1188 list_for_each_entry_rcu_locked(link
, &dev
->links
.suppliers
, c_node
)
1189 link
->supplier
->power
.must_resume
= true;
1191 device_links_read_unlock(idx
);
1195 * device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1196 * @dev: Device to handle.
1197 * @state: PM transition of the system being carried out.
1198 * @async: If true, the device is being suspended asynchronously.
1200 * The driver of @dev will not receive interrupts while this function is being
1203 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1205 pm_callback_t callback
= NULL
;
1206 const char *info
= NULL
;
1212 dpm_wait_for_subordinate(dev
, async
);
1217 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1220 if (dev
->pm_domain
) {
1221 info
= "noirq power domain ";
1222 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1223 } else if (dev
->type
&& dev
->type
->pm
) {
1224 info
= "noirq type ";
1225 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1226 } else if (dev
->class && dev
->class->pm
) {
1227 info
= "noirq class ";
1228 callback
= pm_noirq_op(dev
->class->pm
, state
);
1229 } else if (dev
->bus
&& dev
->bus
->pm
) {
1230 info
= "noirq bus ";
1231 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1236 if (dev_pm_skip_suspend(dev
))
1239 if (dev
->driver
&& dev
->driver
->pm
) {
1240 info
= "noirq driver ";
1241 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1245 error
= dpm_run_callback(callback
, dev
, state
, info
);
1247 async_error
= error
;
1248 dpm_save_failed_dev(dev_name(dev
));
1249 pm_dev_err(dev
, state
, async
? " async noirq" : " noirq", error
);
1254 dev
->power
.is_noirq_suspended
= true;
1257 * Skipping the resume of devices that were in use right before the
1258 * system suspend (as indicated by their PM-runtime usage counters)
1259 * would be suboptimal. Also resume them if doing that is not allowed
1262 if (atomic_read(&dev
->power
.usage_count
) > 1 ||
1263 !(dev_pm_test_driver_flags(dev
, DPM_FLAG_MAY_SKIP_RESUME
) &&
1264 dev
->power
.may_skip_resume
))
1265 dev
->power
.must_resume
= true;
1267 if (dev
->power
.must_resume
)
1268 dpm_superior_set_must_resume(dev
);
1271 complete_all(&dev
->power
.completion
);
1272 TRACE_SUSPEND(error
);
1276 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1278 struct device
*dev
= data
;
1280 device_suspend_noirq(dev
, pm_transition
, true);
1284 static int dpm_noirq_suspend_devices(pm_message_t state
)
1286 ktime_t starttime
= ktime_get();
1289 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1291 pm_transition
= state
;
1294 mutex_lock(&dpm_list_mtx
);
1296 while (!list_empty(&dpm_late_early_list
)) {
1297 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1299 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1301 if (dpm_async_fn(dev
, async_suspend_noirq
))
1306 mutex_unlock(&dpm_list_mtx
);
1308 error
= device_suspend_noirq(dev
, state
, false);
1312 mutex_lock(&dpm_list_mtx
);
1314 if (error
|| async_error
)
1318 mutex_unlock(&dpm_list_mtx
);
1320 async_synchronize_full();
1322 error
= async_error
;
1325 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1327 dpm_show_time(starttime
, state
, error
, "noirq");
1328 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, false);
1333 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1334 * @state: PM transition of the system being carried out.
1336 * Prevent device drivers' interrupt handlers from being called and invoke
1337 * "noirq" suspend callbacks for all non-sysdev devices.
1339 int dpm_suspend_noirq(pm_message_t state
)
1343 device_wakeup_arm_wake_irqs();
1344 suspend_device_irqs();
1346 ret
= dpm_noirq_suspend_devices(state
);
1348 dpm_resume_noirq(resume_event(state
));
1353 static void dpm_propagate_wakeup_to_parent(struct device
*dev
)
1355 struct device
*parent
= dev
->parent
;
1360 spin_lock_irq(&parent
->power
.lock
);
1362 if (device_wakeup_path(dev
) && !parent
->power
.ignore_children
)
1363 parent
->power
.wakeup_path
= true;
1365 spin_unlock_irq(&parent
->power
.lock
);
1369 * device_suspend_late - Execute a "late suspend" callback for given device.
1370 * @dev: Device to handle.
1371 * @state: PM transition of the system being carried out.
1372 * @async: If true, the device is being suspended asynchronously.
1374 * Runtime PM is disabled for @dev while this function is being executed.
1376 static int device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1378 pm_callback_t callback
= NULL
;
1379 const char *info
= NULL
;
1385 __pm_runtime_disable(dev
, false);
1387 dpm_wait_for_subordinate(dev
, async
);
1392 if (pm_wakeup_pending()) {
1393 async_error
= -EBUSY
;
1397 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1400 if (dev
->pm_domain
) {
1401 info
= "late power domain ";
1402 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1403 } else if (dev
->type
&& dev
->type
->pm
) {
1404 info
= "late type ";
1405 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1406 } else if (dev
->class && dev
->class->pm
) {
1407 info
= "late class ";
1408 callback
= pm_late_early_op(dev
->class->pm
, state
);
1409 } else if (dev
->bus
&& dev
->bus
->pm
) {
1411 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1416 if (dev_pm_skip_suspend(dev
))
1419 if (dev
->driver
&& dev
->driver
->pm
) {
1420 info
= "late driver ";
1421 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1425 error
= dpm_run_callback(callback
, dev
, state
, info
);
1427 async_error
= error
;
1428 dpm_save_failed_dev(dev_name(dev
));
1429 pm_dev_err(dev
, state
, async
? " async late" : " late", error
);
1432 dpm_propagate_wakeup_to_parent(dev
);
1435 dev
->power
.is_late_suspended
= true;
1438 TRACE_SUSPEND(error
);
1439 complete_all(&dev
->power
.completion
);
1443 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1445 struct device
*dev
= data
;
1447 device_suspend_late(dev
, pm_transition
, true);
1452 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1453 * @state: PM transition of the system being carried out.
1455 int dpm_suspend_late(pm_message_t state
)
1457 ktime_t starttime
= ktime_get();
1460 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1462 pm_transition
= state
;
1465 wake_up_all_idle_cpus();
1467 mutex_lock(&dpm_list_mtx
);
1469 while (!list_empty(&dpm_suspended_list
)) {
1470 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1472 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1474 if (dpm_async_fn(dev
, async_suspend_late
))
1479 mutex_unlock(&dpm_list_mtx
);
1481 error
= device_suspend_late(dev
, state
, false);
1485 mutex_lock(&dpm_list_mtx
);
1487 if (error
|| async_error
)
1491 mutex_unlock(&dpm_list_mtx
);
1493 async_synchronize_full();
1495 error
= async_error
;
1498 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1499 dpm_resume_early(resume_event(state
));
1501 dpm_show_time(starttime
, state
, error
, "late");
1502 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1507 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1508 * @state: PM transition of the system being carried out.
1510 int dpm_suspend_end(pm_message_t state
)
1512 ktime_t starttime
= ktime_get();
1515 error
= dpm_suspend_late(state
);
1519 error
= dpm_suspend_noirq(state
);
1521 dpm_resume_early(resume_event(state
));
1524 dpm_show_time(starttime
, state
, error
, "end");
1527 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1530 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1531 * @dev: Device to suspend.
1532 * @state: PM transition of the system being carried out.
1533 * @cb: Suspend callback to execute.
1534 * @info: string description of caller.
1536 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1537 int (*cb
)(struct device
*dev
, pm_message_t state
),
1543 calltime
= initcall_debug_start(dev
, cb
);
1545 trace_device_pm_callback_start(dev
, info
, state
.event
);
1546 error
= cb(dev
, state
);
1547 trace_device_pm_callback_end(dev
, error
);
1548 suspend_report_result(dev
, cb
, error
);
1550 initcall_debug_report(dev
, calltime
, cb
, error
);
1555 static void dpm_clear_superiors_direct_complete(struct device
*dev
)
1557 struct device_link
*link
;
1561 spin_lock_irq(&dev
->parent
->power
.lock
);
1562 dev
->parent
->power
.direct_complete
= false;
1563 spin_unlock_irq(&dev
->parent
->power
.lock
);
1566 idx
= device_links_read_lock();
1568 list_for_each_entry_rcu_locked(link
, &dev
->links
.suppliers
, c_node
) {
1569 spin_lock_irq(&link
->supplier
->power
.lock
);
1570 link
->supplier
->power
.direct_complete
= false;
1571 spin_unlock_irq(&link
->supplier
->power
.lock
);
1574 device_links_read_unlock(idx
);
1578 * device_suspend - Execute "suspend" callbacks for given device.
1579 * @dev: Device to handle.
1580 * @state: PM transition of the system being carried out.
1581 * @async: If true, the device is being suspended asynchronously.
1583 static int device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1585 pm_callback_t callback
= NULL
;
1586 const char *info
= NULL
;
1588 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1593 dpm_wait_for_subordinate(dev
, async
);
1596 dev
->power
.direct_complete
= false;
1601 * Wait for possible runtime PM transitions of the device in progress
1602 * to complete and if there's a runtime resume request pending for it,
1603 * resume it before proceeding with invoking the system-wide suspend
1606 * If the system-wide suspend callbacks below change the configuration
1607 * of the device, they must disable runtime PM for it or otherwise
1608 * ensure that its runtime-resume callbacks will not be confused by that
1609 * change in case they are invoked going forward.
1611 pm_runtime_barrier(dev
);
1613 if (pm_wakeup_pending()) {
1614 dev
->power
.direct_complete
= false;
1615 async_error
= -EBUSY
;
1619 if (dev
->power
.syscore
)
1622 /* Avoid direct_complete to let wakeup_path propagate. */
1623 if (device_may_wakeup(dev
) || device_wakeup_path(dev
))
1624 dev
->power
.direct_complete
= false;
1626 if (dev
->power
.direct_complete
) {
1627 if (pm_runtime_status_suspended(dev
)) {
1628 pm_runtime_disable(dev
);
1629 if (pm_runtime_status_suspended(dev
)) {
1630 pm_dev_dbg(dev
, state
, "direct-complete ");
1634 pm_runtime_enable(dev
);
1636 dev
->power
.direct_complete
= false;
1639 dev
->power
.may_skip_resume
= true;
1640 dev
->power
.must_resume
= !dev_pm_test_driver_flags(dev
, DPM_FLAG_MAY_SKIP_RESUME
);
1642 dpm_watchdog_set(&wd
, dev
);
1645 if (dev
->pm_domain
) {
1646 info
= "power domain ";
1647 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1651 if (dev
->type
&& dev
->type
->pm
) {
1653 callback
= pm_op(dev
->type
->pm
, state
);
1657 if (dev
->class && dev
->class->pm
) {
1659 callback
= pm_op(dev
->class->pm
, state
);
1666 callback
= pm_op(dev
->bus
->pm
, state
);
1667 } else if (dev
->bus
->suspend
) {
1668 pm_dev_dbg(dev
, state
, "legacy bus ");
1669 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1676 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1678 callback
= pm_op(dev
->driver
->pm
, state
);
1681 error
= dpm_run_callback(callback
, dev
, state
, info
);
1685 dev
->power
.is_suspended
= true;
1686 if (device_may_wakeup(dev
))
1687 dev
->power
.wakeup_path
= true;
1689 dpm_propagate_wakeup_to_parent(dev
);
1690 dpm_clear_superiors_direct_complete(dev
);
1694 dpm_watchdog_clear(&wd
);
1698 async_error
= error
;
1699 dpm_save_failed_dev(dev_name(dev
));
1700 pm_dev_err(dev
, state
, async
? " async" : "", error
);
1703 complete_all(&dev
->power
.completion
);
1704 TRACE_SUSPEND(error
);
1708 static void async_suspend(void *data
, async_cookie_t cookie
)
1710 struct device
*dev
= data
;
1712 device_suspend(dev
, pm_transition
, true);
1717 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1718 * @state: PM transition of the system being carried out.
1720 int dpm_suspend(pm_message_t state
)
1722 ktime_t starttime
= ktime_get();
1725 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1731 pm_transition
= state
;
1734 mutex_lock(&dpm_list_mtx
);
1736 while (!list_empty(&dpm_prepared_list
)) {
1737 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1739 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1741 if (dpm_async_fn(dev
, async_suspend
))
1746 mutex_unlock(&dpm_list_mtx
);
1748 error
= device_suspend(dev
, state
, false);
1752 mutex_lock(&dpm_list_mtx
);
1754 if (error
|| async_error
)
1758 mutex_unlock(&dpm_list_mtx
);
1760 async_synchronize_full();
1762 error
= async_error
;
1765 dpm_save_failed_step(SUSPEND_SUSPEND
);
1767 dpm_show_time(starttime
, state
, error
, NULL
);
1768 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, false);
1773 * device_prepare - Prepare a device for system power transition.
1774 * @dev: Device to handle.
1775 * @state: PM transition of the system being carried out.
1777 * Execute the ->prepare() callback(s) for given device. No new children of the
1778 * device may be registered after this function has returned.
1780 static int device_prepare(struct device
*dev
, pm_message_t state
)
1782 int (*callback
)(struct device
*) = NULL
;
1786 * If a device's parent goes into runtime suspend at the wrong time,
1787 * it won't be possible to resume the device. To prevent this we
1788 * block runtime suspend here, during the prepare phase, and allow
1789 * it again during the complete phase.
1791 pm_runtime_get_noresume(dev
);
1793 if (dev
->power
.syscore
)
1798 dev
->power
.wakeup_path
= false;
1800 if (dev
->power
.no_pm_callbacks
)
1804 callback
= dev
->pm_domain
->ops
.prepare
;
1805 else if (dev
->type
&& dev
->type
->pm
)
1806 callback
= dev
->type
->pm
->prepare
;
1807 else if (dev
->class && dev
->class->pm
)
1808 callback
= dev
->class->pm
->prepare
;
1809 else if (dev
->bus
&& dev
->bus
->pm
)
1810 callback
= dev
->bus
->pm
->prepare
;
1812 if (!callback
&& dev
->driver
&& dev
->driver
->pm
)
1813 callback
= dev
->driver
->pm
->prepare
;
1816 ret
= callback(dev
);
1822 suspend_report_result(dev
, callback
, ret
);
1823 pm_runtime_put(dev
);
1827 * A positive return value from ->prepare() means "this device appears
1828 * to be runtime-suspended and its state is fine, so if it really is
1829 * runtime-suspended, you can leave it in that state provided that you
1830 * will do the same thing with all of its descendants". This only
1831 * applies to suspend transitions, however.
1833 spin_lock_irq(&dev
->power
.lock
);
1834 dev
->power
.direct_complete
= state
.event
== PM_EVENT_SUSPEND
&&
1835 (ret
> 0 || dev
->power
.no_pm_callbacks
) &&
1836 !dev_pm_test_driver_flags(dev
, DPM_FLAG_NO_DIRECT_COMPLETE
);
1837 spin_unlock_irq(&dev
->power
.lock
);
1842 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1843 * @state: PM transition of the system being carried out.
1845 * Execute the ->prepare() callback(s) for all devices.
1847 int dpm_prepare(pm_message_t state
)
1851 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
1855 * Give a chance for the known devices to complete their probes, before
1856 * disable probing of devices. This sync point is important at least
1857 * at boot time + hibernation restore.
1859 wait_for_device_probe();
1861 * It is unsafe if probing of devices will happen during suspend or
1862 * hibernation and system behavior will be unpredictable in this case.
1863 * So, let's prohibit device's probing here and defer their probes
1864 * instead. The normal behavior will be restored in dpm_complete().
1866 device_block_probing();
1868 mutex_lock(&dpm_list_mtx
);
1869 while (!list_empty(&dpm_list
) && !error
) {
1870 struct device
*dev
= to_device(dpm_list
.next
);
1874 mutex_unlock(&dpm_list_mtx
);
1876 trace_device_pm_callback_start(dev
, "", state
.event
);
1877 error
= device_prepare(dev
, state
);
1878 trace_device_pm_callback_end(dev
, error
);
1880 mutex_lock(&dpm_list_mtx
);
1883 dev
->power
.is_prepared
= true;
1884 if (!list_empty(&dev
->power
.entry
))
1885 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1886 } else if (error
== -EAGAIN
) {
1889 dev_info(dev
, "not prepared for power transition: code %d\n",
1893 mutex_unlock(&dpm_list_mtx
);
1897 mutex_lock(&dpm_list_mtx
);
1899 mutex_unlock(&dpm_list_mtx
);
1900 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
1905 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1906 * @state: PM transition of the system being carried out.
1908 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1909 * callbacks for them.
1911 int dpm_suspend_start(pm_message_t state
)
1913 ktime_t starttime
= ktime_get();
1916 error
= dpm_prepare(state
);
1918 dpm_save_failed_step(SUSPEND_PREPARE
);
1920 error
= dpm_suspend(state
);
1922 dpm_show_time(starttime
, state
, error
, "start");
1925 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1927 void __suspend_report_result(const char *function
, struct device
*dev
, void *fn
, int ret
)
1930 dev_err(dev
, "%s(): %ps returns %d\n", function
, fn
, ret
);
1932 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1935 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1936 * @subordinate: Device that needs to wait for @dev.
1937 * @dev: Device to wait for.
1939 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1941 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1944 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1947 * dpm_for_each_dev - device iterator.
1948 * @data: data for the callback.
1949 * @fn: function to be called for each device.
1951 * Iterate over devices in dpm_list, and call @fn for each device,
1954 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1962 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1966 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);
1968 static bool pm_ops_is_empty(const struct dev_pm_ops
*ops
)
1973 return !ops
->prepare
&&
1975 !ops
->suspend_late
&&
1976 !ops
->suspend_noirq
&&
1977 !ops
->resume_noirq
&&
1978 !ops
->resume_early
&&
1983 void device_pm_check_callbacks(struct device
*dev
)
1985 unsigned long flags
;
1987 spin_lock_irqsave(&dev
->power
.lock
, flags
);
1988 dev
->power
.no_pm_callbacks
=
1989 (!dev
->bus
|| (pm_ops_is_empty(dev
->bus
->pm
) &&
1990 !dev
->bus
->suspend
&& !dev
->bus
->resume
)) &&
1991 (!dev
->class || pm_ops_is_empty(dev
->class->pm
)) &&
1992 (!dev
->type
|| pm_ops_is_empty(dev
->type
->pm
)) &&
1993 (!dev
->pm_domain
|| pm_ops_is_empty(&dev
->pm_domain
->ops
)) &&
1994 (!dev
->driver
|| (pm_ops_is_empty(dev
->driver
->pm
) &&
1995 !dev
->driver
->suspend
&& !dev
->driver
->resume
));
1996 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
1999 bool dev_pm_skip_suspend(struct device
*dev
)
2001 return dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
) &&
2002 pm_runtime_status_suspended(dev
);