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 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(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:
363 case PM_EVENT_RESTORE:
365 #endif /* CONFIG_HIBERNATE_CALLBACKS */
372 * pm_late_early_op - Return the PM operation appropriate for given PM event.
373 * @ops: PM operations to choose from.
374 * @state: PM transition of the system being carried out.
376 * Runtime PM is disabled for @dev while this function is being executed.
378 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
381 switch (state.event) {
382 #ifdef CONFIG_SUSPEND
383 case PM_EVENT_SUSPEND:
384 return ops->suspend_late;
385 case PM_EVENT_RESUME:
386 return ops->resume_early;
387 #endif /* CONFIG_SUSPEND */
388 #ifdef CONFIG_HIBERNATE_CALLBACKS
389 case PM_EVENT_FREEZE:
390 case PM_EVENT_QUIESCE:
391 return ops->freeze_late;
392 case PM_EVENT_HIBERNATE:
393 return ops->poweroff_late;
395 case PM_EVENT_RECOVER:
396 return ops->thaw_early;
397 case PM_EVENT_RESTORE:
398 return ops->restore_early;
399 #endif /* CONFIG_HIBERNATE_CALLBACKS */
406 * pm_noirq_op - Return the PM operation appropriate for given PM event.
407 * @ops: PM operations to choose from.
408 * @state: PM transition of the system being carried out.
410 * The driver of @dev will not receive interrupts while this function is being
413 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
415 switch (state.event) {
416 #ifdef CONFIG_SUSPEND
417 case PM_EVENT_SUSPEND:
418 return ops->suspend_noirq;
419 case PM_EVENT_RESUME:
420 return ops->resume_noirq;
421 #endif /* CONFIG_SUSPEND */
422 #ifdef CONFIG_HIBERNATE_CALLBACKS
423 case PM_EVENT_FREEZE:
424 case PM_EVENT_QUIESCE:
425 return ops->freeze_noirq;
426 case PM_EVENT_HIBERNATE:
427 return ops->poweroff_noirq;
429 case PM_EVENT_RECOVER:
430 return ops->thaw_noirq;
431 case PM_EVENT_RESTORE:
432 return ops->restore_noirq;
433 #endif /* CONFIG_HIBERNATE_CALLBACKS */
439 static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
441 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
442 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
443 ", may wakeup" : "");
446 static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
449 pr_err("Device %s failed to %s%s: error %d\n",
450 dev_name(dev), pm_verb(state.event), info, error);
453 static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
460 calltime = ktime_get();
461 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
462 do_div(usecs64, NSEC_PER_USEC);
467 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
468 info ?: "", info ? " " : "", pm_verb(state.event),
469 error ? "aborted" : "complete",
470 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
473 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
474 pm_message_t state, const char *info)
482 calltime = initcall_debug_start(dev, cb);
484 pm_dev_dbg(dev, state, info);
485 trace_device_pm_callback_start(dev, info, state.event);
487 trace_device_pm_callback_end(dev, error);
488 suspend_report_result(cb, error);
490 initcall_debug_report(dev, calltime, cb, error);
495 #ifdef CONFIG_DPM_WATCHDOG
496 struct dpm_watchdog {
498 struct task_struct *tsk;
499 struct timer_list timer;
502 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
503 struct dpm_watchdog wd
506 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
507 * @t: The timer that PM watchdog depends on.
509 * Called when a driver has timed out suspending or resuming.
510 * There's not much we can do here to recover so panic() to
511 * capture a crash-dump in pstore.
513 static void dpm_watchdog_handler(struct timer_list *t)
515 struct dpm_watchdog *wd = from_timer(wd, t, timer);
517 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
518 show_stack(wd->tsk, NULL);
519 panic("%s %s: unrecoverable failure\n",
520 dev_driver_string(wd->dev), dev_name(wd->dev));
524 * dpm_watchdog_set - Enable pm watchdog for given device.
525 * @wd: Watchdog. Must be allocated on the stack.
526 * @dev: Device to handle.
528 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
530 struct timer_list *timer = &wd->timer;
535 timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
536 /* use same timeout value for both suspend and resume */
537 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
542 * dpm_watchdog_clear - Disable suspend/resume watchdog.
543 * @wd: Watchdog to disable.
545 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
547 struct timer_list *timer = &wd->timer;
549 del_timer_sync(timer);
550 destroy_timer_on_stack(timer);
553 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
554 #define dpm_watchdog_set(x, y)
555 #define dpm_watchdog_clear(x)
558 /*------------------------- Resume routines -------------------------*/
561 * suspend_event - Return a "suspend" message for given "resume" one.
562 * @resume_msg: PM message representing a system-wide resume transition.
564 static pm_message_t suspend_event(pm_message_t resume_msg)
566 switch (resume_msg.event) {
567 case PM_EVENT_RESUME:
570 case PM_EVENT_RESTORE:
572 case PM_EVENT_RECOVER:
573 return PMSG_HIBERNATE;
579 * dev_pm_may_skip_resume - System-wide device resume optimization check.
580 * @dev: Target device.
582 * Checks whether or not the device may be left in suspend after a system-wide
583 * transition to the working state.
585 bool dev_pm_may_skip_resume(struct device *dev)
587 return !dev->power.must_resume && pm_transition.event != PM_EVENT_RESTORE;
590 static pm_callback_t dpm_subsys_resume_noirq_cb(struct device *dev,
594 pm_callback_t callback;
597 if (dev->pm_domain) {
598 info = "noirq power domain ";
599 callback = pm_noirq_op(&dev->pm_domain->ops, state);
600 } else if (dev->type && dev->type->pm) {
601 info = "noirq type ";
602 callback = pm_noirq_op(dev->type->pm, state);
603 } else if (dev->class && dev->class->pm) {
604 info = "noirq class ";
605 callback = pm_noirq_op(dev->class->pm, state);
606 } else if (dev->bus && dev->bus->pm) {
608 callback = pm_noirq_op(dev->bus->pm, state);
619 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
621 const char **info_p);
623 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
625 const char **info_p);
628 * device_resume_noirq - Execute a "noirq resume" callback for given device.
629 * @dev: Device to handle.
630 * @state: PM transition of the system being carried out.
631 * @async: If true, the device is being resumed asynchronously.
633 * The driver of @dev will not receive interrupts while this function is being
636 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
638 pm_callback_t callback;
646 if (dev->power.syscore || dev->power.direct_complete)
649 if (!dev->power.is_noirq_suspended)
652 if (!dpm_wait_for_superior(dev, async))
655 skip_resume = dev_pm_may_skip_resume(dev);
657 callback = dpm_subsys_resume_noirq_cb(dev, state, &info);
664 if (dev_pm_smart_suspend_and_suspended(dev)) {
665 pm_message_t suspend_msg = suspend_event(state);
668 * If "freeze" callbacks have been skipped during a transition
669 * related to hibernation, the subsequent "thaw" callbacks must
670 * be skipped too or bad things may happen. Otherwise, resume
671 * callbacks are going to be run for the device, so its runtime
672 * PM status must be changed to reflect the new state after the
673 * transition under way.
675 if (!dpm_subsys_suspend_late_cb(dev, suspend_msg, NULL) &&
676 !dpm_subsys_suspend_noirq_cb(dev, suspend_msg, NULL)) {
677 if (state.event == PM_EVENT_THAW) {
681 pm_runtime_set_active(dev);
686 if (dev->driver && dev->driver->pm) {
687 info = "noirq driver ";
688 callback = pm_noirq_op(dev->driver->pm, state);
692 error = dpm_run_callback(callback, dev, state, info);
695 dev->power.is_noirq_suspended = false;
698 /* Make the next phases of resume skip the device. */
699 dev->power.is_late_suspended = false;
700 dev->power.is_suspended = false;
702 * The device is going to be left in suspend, but it might not
703 * have been in runtime suspend before the system suspended, so
704 * its runtime PM status needs to be updated to avoid confusing
705 * the runtime PM framework when runtime PM is enabled for the
708 pm_runtime_set_suspended(dev);
712 complete_all(&dev->power.completion);
717 static bool is_async(struct device *dev)
719 return dev->power.async_suspend && pm_async_enabled
720 && !pm_trace_is_enabled();
723 static bool dpm_async_fn(struct device *dev, async_func_t func)
725 reinit_completion(&dev->power.completion);
729 async_schedule(func, dev);
736 static void async_resume_noirq(void *data, async_cookie_t cookie)
738 struct device *dev = (struct device *)data;
741 error = device_resume_noirq(dev, pm_transition, true);
743 pm_dev_err(dev, pm_transition, " async", error);
748 static void dpm_noirq_resume_devices(pm_message_t state)
751 ktime_t starttime = ktime_get();
753 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
754 mutex_lock(&dpm_list_mtx);
755 pm_transition = state;
758 * Advanced the async threads upfront,
759 * in case the starting of async threads is
760 * delayed by non-async resuming devices.
762 list_for_each_entry(dev, &dpm_noirq_list, power.entry)
763 dpm_async_fn(dev, async_resume_noirq);
765 while (!list_empty(&dpm_noirq_list)) {
766 dev = to_device(dpm_noirq_list.next);
768 list_move_tail(&dev->power.entry, &dpm_late_early_list);
769 mutex_unlock(&dpm_list_mtx);
771 if (!is_async(dev)) {
774 error = device_resume_noirq(dev, state, false);
776 suspend_stats.failed_resume_noirq++;
777 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
778 dpm_save_failed_dev(dev_name(dev));
779 pm_dev_err(dev, state, " noirq", error);
783 mutex_lock(&dpm_list_mtx);
786 mutex_unlock(&dpm_list_mtx);
787 async_synchronize_full();
788 dpm_show_time(starttime, state, 0, "noirq");
789 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
793 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
794 * @state: PM transition of the system being carried out.
796 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
797 * allow device drivers' interrupt handlers to be called.
799 void dpm_resume_noirq(pm_message_t state)
801 dpm_noirq_resume_devices(state);
803 resume_device_irqs();
804 device_wakeup_disarm_wake_irqs();
809 static pm_callback_t dpm_subsys_resume_early_cb(struct device *dev,
813 pm_callback_t callback;
816 if (dev->pm_domain) {
817 info = "early power domain ";
818 callback = pm_late_early_op(&dev->pm_domain->ops, state);
819 } else if (dev->type && dev->type->pm) {
820 info = "early type ";
821 callback = pm_late_early_op(dev->type->pm, state);
822 } else if (dev->class && dev->class->pm) {
823 info = "early class ";
824 callback = pm_late_early_op(dev->class->pm, state);
825 } else if (dev->bus && dev->bus->pm) {
827 callback = pm_late_early_op(dev->bus->pm, state);
839 * device_resume_early - Execute an "early resume" callback for given device.
840 * @dev: Device to handle.
841 * @state: PM transition of the system being carried out.
842 * @async: If true, the device is being resumed asynchronously.
844 * Runtime PM is disabled for @dev while this function is being executed.
846 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
848 pm_callback_t callback;
855 if (dev->power.syscore || dev->power.direct_complete)
858 if (!dev->power.is_late_suspended)
861 if (!dpm_wait_for_superior(dev, async))
864 callback = dpm_subsys_resume_early_cb(dev, state, &info);
866 if (!callback && dev->driver && dev->driver->pm) {
867 info = "early driver ";
868 callback = pm_late_early_op(dev->driver->pm, state);
871 error = dpm_run_callback(callback, dev, state, info);
872 dev->power.is_late_suspended = false;
877 pm_runtime_enable(dev);
878 complete_all(&dev->power.completion);
882 static void async_resume_early(void *data, async_cookie_t cookie)
884 struct device *dev = (struct device *)data;
887 error = device_resume_early(dev, pm_transition, true);
889 pm_dev_err(dev, pm_transition, " async", error);
895 * dpm_resume_early - Execute "early resume" callbacks for all devices.
896 * @state: PM transition of the system being carried out.
898 void dpm_resume_early(pm_message_t state)
901 ktime_t starttime = ktime_get();
903 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
904 mutex_lock(&dpm_list_mtx);
905 pm_transition = state;
908 * Advanced the async threads upfront,
909 * in case the starting of async threads is
910 * delayed by non-async resuming devices.
912 list_for_each_entry(dev, &dpm_late_early_list, power.entry)
913 dpm_async_fn(dev, async_resume_early);
915 while (!list_empty(&dpm_late_early_list)) {
916 dev = to_device(dpm_late_early_list.next);
918 list_move_tail(&dev->power.entry, &dpm_suspended_list);
919 mutex_unlock(&dpm_list_mtx);
921 if (!is_async(dev)) {
924 error = device_resume_early(dev, state, false);
926 suspend_stats.failed_resume_early++;
927 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
928 dpm_save_failed_dev(dev_name(dev));
929 pm_dev_err(dev, state, " early", error);
932 mutex_lock(&dpm_list_mtx);
935 mutex_unlock(&dpm_list_mtx);
936 async_synchronize_full();
937 dpm_show_time(starttime, state, 0, "early");
938 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
942 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
943 * @state: PM transition of the system being carried out.
945 void dpm_resume_start(pm_message_t state)
947 dpm_resume_noirq(state);
948 dpm_resume_early(state);
950 EXPORT_SYMBOL_GPL(dpm_resume_start);
953 * device_resume - Execute "resume" callbacks for given device.
954 * @dev: Device to handle.
955 * @state: PM transition of the system being carried out.
956 * @async: If true, the device is being resumed asynchronously.
958 static int device_resume(struct device *dev, pm_message_t state, bool async)
960 pm_callback_t callback = NULL;
961 const char *info = NULL;
963 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
968 if (dev->power.syscore)
971 if (dev->power.direct_complete) {
972 /* Match the pm_runtime_disable() in __device_suspend(). */
973 pm_runtime_enable(dev);
977 if (!dpm_wait_for_superior(dev, async))
980 dpm_watchdog_set(&wd, dev);
984 * This is a fib. But we'll allow new children to be added below
985 * a resumed device, even if the device hasn't been completed yet.
987 dev->power.is_prepared = false;
989 if (!dev->power.is_suspended)
992 if (dev->pm_domain) {
993 info = "power domain ";
994 callback = pm_op(&dev->pm_domain->ops, state);
998 if (dev->type && dev->type->pm) {
1000 callback = pm_op(dev->type->pm, state);
1004 if (dev->class && dev->class->pm) {
1006 callback = pm_op(dev->class->pm, state);
1013 callback = pm_op(dev->bus->pm, state);
1014 } else if (dev->bus->resume) {
1015 info = "legacy bus ";
1016 callback = dev->bus->resume;
1022 if (!callback && dev->driver && dev->driver->pm) {
1024 callback = pm_op(dev->driver->pm, state);
1028 error = dpm_run_callback(callback, dev, state, info);
1029 dev->power.is_suspended = false;
1033 dpm_watchdog_clear(&wd);
1036 complete_all(&dev->power.completion);
1038 TRACE_RESUME(error);
1043 static void async_resume(void *data, async_cookie_t cookie)
1045 struct device *dev = (struct device *)data;
1048 error = device_resume(dev, pm_transition, true);
1050 pm_dev_err(dev, pm_transition, " async", error);
1055 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1056 * @state: PM transition of the system being carried out.
1058 * Execute the appropriate "resume" callback for all devices whose status
1059 * indicates that they are suspended.
1061 void dpm_resume(pm_message_t state)
1064 ktime_t starttime = ktime_get();
1066 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
1069 mutex_lock(&dpm_list_mtx);
1070 pm_transition = state;
1073 list_for_each_entry(dev, &dpm_suspended_list, power.entry)
1074 dpm_async_fn(dev, async_resume);
1076 while (!list_empty(&dpm_suspended_list)) {
1077 dev = to_device(dpm_suspended_list.next);
1079 if (!is_async(dev)) {
1082 mutex_unlock(&dpm_list_mtx);
1084 error = device_resume(dev, state, false);
1086 suspend_stats.failed_resume++;
1087 dpm_save_failed_step(SUSPEND_RESUME);
1088 dpm_save_failed_dev(dev_name(dev));
1089 pm_dev_err(dev, state, "", error);
1092 mutex_lock(&dpm_list_mtx);
1094 if (!list_empty(&dev->power.entry))
1095 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1098 mutex_unlock(&dpm_list_mtx);
1099 async_synchronize_full();
1100 dpm_show_time(starttime, state, 0, NULL);
1104 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
1108 * device_complete - Complete a PM transition for given device.
1109 * @dev: Device to handle.
1110 * @state: PM transition of the system being carried out.
1112 static void device_complete(struct device *dev, pm_message_t state)
1114 void (*callback)(struct device *) = NULL;
1115 const char *info = NULL;
1117 if (dev->power.syscore)
1122 if (dev->pm_domain) {
1123 info = "completing power domain ";
1124 callback = dev->pm_domain->ops.complete;
1125 } else if (dev->type && dev->type->pm) {
1126 info = "completing type ";
1127 callback = dev->type->pm->complete;
1128 } else if (dev->class && dev->class->pm) {
1129 info = "completing class ";
1130 callback = dev->class->pm->complete;
1131 } else if (dev->bus && dev->bus->pm) {
1132 info = "completing bus ";
1133 callback = dev->bus->pm->complete;
1136 if (!callback && dev->driver && dev->driver->pm) {
1137 info = "completing driver ";
1138 callback = dev->driver->pm->complete;
1142 pm_dev_dbg(dev, state, info);
1148 pm_runtime_put(dev);
1152 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1153 * @state: PM transition of the system being carried out.
1155 * Execute the ->complete() callbacks for all devices whose PM status is not
1156 * DPM_ON (this allows new devices to be registered).
1158 void dpm_complete(pm_message_t state)
1160 struct list_head list;
1162 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
1165 INIT_LIST_HEAD(&list);
1166 mutex_lock(&dpm_list_mtx);
1167 while (!list_empty(&dpm_prepared_list)) {
1168 struct device *dev = to_device(dpm_prepared_list.prev);
1171 dev->power.is_prepared = false;
1172 list_move(&dev->power.entry, &list);
1173 mutex_unlock(&dpm_list_mtx);
1175 trace_device_pm_callback_start(dev, "", state.event);
1176 device_complete(dev, state);
1177 trace_device_pm_callback_end(dev, 0);
1179 mutex_lock(&dpm_list_mtx);
1182 list_splice(&list, &dpm_list);
1183 mutex_unlock(&dpm_list_mtx);
1185 /* Allow device probing and trigger re-probing of deferred devices */
1186 device_unblock_probing();
1187 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1191 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1192 * @state: PM transition of the system being carried out.
1194 * Execute "resume" callbacks for all devices and complete the PM transition of
1197 void dpm_resume_end(pm_message_t state)
1200 dpm_complete(state);
1202 EXPORT_SYMBOL_GPL(dpm_resume_end);
1205 /*------------------------- Suspend routines -------------------------*/
1208 * resume_event - Return a "resume" message for given "suspend" sleep state.
1209 * @sleep_state: PM message representing a sleep state.
1211 * Return a PM message representing the resume event corresponding to given
1214 static pm_message_t resume_event(pm_message_t sleep_state)
1216 switch (sleep_state.event) {
1217 case PM_EVENT_SUSPEND:
1219 case PM_EVENT_FREEZE:
1220 case PM_EVENT_QUIESCE:
1221 return PMSG_RECOVER;
1222 case PM_EVENT_HIBERNATE:
1223 return PMSG_RESTORE;
1228 static void dpm_superior_set_must_resume(struct device *dev)
1230 struct device_link *link;
1234 dev->parent->power.must_resume = true;
1236 idx = device_links_read_lock();
1238 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
1239 link->supplier->power.must_resume = true;
1241 device_links_read_unlock(idx);
1244 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
1246 const char **info_p)
1248 pm_callback_t callback;
1251 if (dev->pm_domain) {
1252 info = "noirq power domain ";
1253 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1254 } else if (dev->type && dev->type->pm) {
1255 info = "noirq type ";
1256 callback = pm_noirq_op(dev->type->pm, state);
1257 } else if (dev->class && dev->class->pm) {
1258 info = "noirq class ";
1259 callback = pm_noirq_op(dev->class->pm, state);
1260 } else if (dev->bus && dev->bus->pm) {
1261 info = "noirq bus ";
1262 callback = pm_noirq_op(dev->bus->pm, state);
1273 static bool device_must_resume(struct device *dev, pm_message_t state,
1274 bool no_subsys_suspend_noirq)
1276 pm_message_t resume_msg = resume_event(state);
1279 * If all of the device driver's "noirq", "late" and "early" callbacks
1280 * are invoked directly by the core, the decision to allow the device to
1281 * stay in suspend can be based on its current runtime PM status and its
1284 if (no_subsys_suspend_noirq &&
1285 !dpm_subsys_suspend_late_cb(dev, state, NULL) &&
1286 !dpm_subsys_resume_early_cb(dev, resume_msg, NULL) &&
1287 !dpm_subsys_resume_noirq_cb(dev, resume_msg, NULL))
1288 return !pm_runtime_status_suspended(dev) &&
1289 (resume_msg.event != PM_EVENT_RESUME ||
1290 (device_can_wakeup(dev) && !device_may_wakeup(dev)));
1293 * The only safe strategy here is to require that if the device may not
1294 * be left in suspend, resume callbacks must be invoked for it.
1296 return !dev->power.may_skip_resume;
1300 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1301 * @dev: Device to handle.
1302 * @state: PM transition of the system being carried out.
1303 * @async: If true, the device is being suspended asynchronously.
1305 * The driver of @dev will not receive interrupts while this function is being
1308 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1310 pm_callback_t callback;
1312 bool no_subsys_cb = false;
1318 dpm_wait_for_subordinate(dev, async);
1323 if (dev->power.syscore || dev->power.direct_complete)
1326 callback = dpm_subsys_suspend_noirq_cb(dev, state, &info);
1330 no_subsys_cb = !dpm_subsys_suspend_late_cb(dev, state, NULL);
1332 if (dev_pm_smart_suspend_and_suspended(dev) && no_subsys_cb)
1335 if (dev->driver && dev->driver->pm) {
1336 info = "noirq driver ";
1337 callback = pm_noirq_op(dev->driver->pm, state);
1341 error = dpm_run_callback(callback, dev, state, info);
1343 async_error = error;
1348 dev->power.is_noirq_suspended = true;
1350 if (dev_pm_test_driver_flags(dev, DPM_FLAG_LEAVE_SUSPENDED)) {
1351 dev->power.must_resume = dev->power.must_resume ||
1352 atomic_read(&dev->power.usage_count) > 1 ||
1353 device_must_resume(dev, state, no_subsys_cb);
1355 dev->power.must_resume = true;
1358 if (dev->power.must_resume)
1359 dpm_superior_set_must_resume(dev);
1362 complete_all(&dev->power.completion);
1363 TRACE_SUSPEND(error);
1367 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1369 struct device *dev = (struct device *)data;
1372 error = __device_suspend_noirq(dev, pm_transition, true);
1374 dpm_save_failed_dev(dev_name(dev));
1375 pm_dev_err(dev, pm_transition, " async", error);
1381 static int device_suspend_noirq(struct device *dev)
1383 if (dpm_async_fn(dev, async_suspend_noirq))
1386 return __device_suspend_noirq(dev, pm_transition, false);
1389 static int dpm_noirq_suspend_devices(pm_message_t state)
1391 ktime_t starttime = ktime_get();
1394 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1395 mutex_lock(&dpm_list_mtx);
1396 pm_transition = state;
1399 while (!list_empty(&dpm_late_early_list)) {
1400 struct device *dev = to_device(dpm_late_early_list.prev);
1403 mutex_unlock(&dpm_list_mtx);
1405 error = device_suspend_noirq(dev);
1407 mutex_lock(&dpm_list_mtx);
1409 pm_dev_err(dev, state, " noirq", error);
1410 dpm_save_failed_dev(dev_name(dev));
1414 if (!list_empty(&dev->power.entry))
1415 list_move(&dev->power.entry, &dpm_noirq_list);
1421 mutex_unlock(&dpm_list_mtx);
1422 async_synchronize_full();
1424 error = async_error;
1427 suspend_stats.failed_suspend_noirq++;
1428 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1430 dpm_show_time(starttime, state, error, "noirq");
1431 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1436 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1437 * @state: PM transition of the system being carried out.
1439 * Prevent device drivers' interrupt handlers from being called and invoke
1440 * "noirq" suspend callbacks for all non-sysdev devices.
1442 int dpm_suspend_noirq(pm_message_t state)
1448 device_wakeup_arm_wake_irqs();
1449 suspend_device_irqs();
1451 ret = dpm_noirq_suspend_devices(state);
1453 dpm_resume_noirq(resume_event(state));
1458 static void dpm_propagate_wakeup_to_parent(struct device *dev)
1460 struct device *parent = dev->parent;
1465 spin_lock_irq(&parent->power.lock);
1467 if (dev->power.wakeup_path && !parent->power.ignore_children)
1468 parent->power.wakeup_path = true;
1470 spin_unlock_irq(&parent->power.lock);
1473 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
1475 const char **info_p)
1477 pm_callback_t callback;
1480 if (dev->pm_domain) {
1481 info = "late power domain ";
1482 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1483 } else if (dev->type && dev->type->pm) {
1484 info = "late type ";
1485 callback = pm_late_early_op(dev->type->pm, state);
1486 } else if (dev->class && dev->class->pm) {
1487 info = "late class ";
1488 callback = pm_late_early_op(dev->class->pm, state);
1489 } else if (dev->bus && dev->bus->pm) {
1491 callback = pm_late_early_op(dev->bus->pm, state);
1503 * __device_suspend_late - Execute a "late suspend" callback for given device.
1504 * @dev: Device to handle.
1505 * @state: PM transition of the system being carried out.
1506 * @async: If true, the device is being suspended asynchronously.
1508 * Runtime PM is disabled for @dev while this function is being executed.
1510 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1512 pm_callback_t callback;
1519 __pm_runtime_disable(dev, false);
1521 dpm_wait_for_subordinate(dev, async);
1526 if (pm_wakeup_pending()) {
1527 async_error = -EBUSY;
1531 if (dev->power.syscore || dev->power.direct_complete)
1534 callback = dpm_subsys_suspend_late_cb(dev, state, &info);
1538 if (dev_pm_smart_suspend_and_suspended(dev) &&
1539 !dpm_subsys_suspend_noirq_cb(dev, state, NULL))
1542 if (dev->driver && dev->driver->pm) {
1543 info = "late driver ";
1544 callback = pm_late_early_op(dev->driver->pm, state);
1548 error = dpm_run_callback(callback, dev, state, info);
1550 async_error = error;
1553 dpm_propagate_wakeup_to_parent(dev);
1556 dev->power.is_late_suspended = true;
1559 TRACE_SUSPEND(error);
1560 complete_all(&dev->power.completion);
1564 static void async_suspend_late(void *data, async_cookie_t cookie)
1566 struct device *dev = (struct device *)data;
1569 error = __device_suspend_late(dev, pm_transition, true);
1571 dpm_save_failed_dev(dev_name(dev));
1572 pm_dev_err(dev, pm_transition, " async", error);
1577 static int device_suspend_late(struct device *dev)
1579 if (dpm_async_fn(dev, async_suspend_late))
1582 return __device_suspend_late(dev, pm_transition, false);
1586 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1587 * @state: PM transition of the system being carried out.
1589 int dpm_suspend_late(pm_message_t state)
1591 ktime_t starttime = ktime_get();
1594 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1595 mutex_lock(&dpm_list_mtx);
1596 pm_transition = state;
1599 while (!list_empty(&dpm_suspended_list)) {
1600 struct device *dev = to_device(dpm_suspended_list.prev);
1603 mutex_unlock(&dpm_list_mtx);
1605 error = device_suspend_late(dev);
1607 mutex_lock(&dpm_list_mtx);
1608 if (!list_empty(&dev->power.entry))
1609 list_move(&dev->power.entry, &dpm_late_early_list);
1612 pm_dev_err(dev, state, " late", error);
1613 dpm_save_failed_dev(dev_name(dev));
1622 mutex_unlock(&dpm_list_mtx);
1623 async_synchronize_full();
1625 error = async_error;
1627 suspend_stats.failed_suspend_late++;
1628 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1629 dpm_resume_early(resume_event(state));
1631 dpm_show_time(starttime, state, error, "late");
1632 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1637 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1638 * @state: PM transition of the system being carried out.
1640 int dpm_suspend_end(pm_message_t state)
1642 ktime_t starttime = ktime_get();
1645 error = dpm_suspend_late(state);
1649 error = dpm_suspend_noirq(state);
1651 dpm_resume_early(resume_event(state));
1654 dpm_show_time(starttime, state, error, "end");
1657 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1660 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1661 * @dev: Device to suspend.
1662 * @state: PM transition of the system being carried out.
1663 * @cb: Suspend callback to execute.
1664 * @info: string description of caller.
1666 static int legacy_suspend(struct device *dev, pm_message_t state,
1667 int (*cb)(struct device *dev, pm_message_t state),
1673 calltime = initcall_debug_start(dev, cb);
1675 trace_device_pm_callback_start(dev, info, state.event);
1676 error = cb(dev, state);
1677 trace_device_pm_callback_end(dev, error);
1678 suspend_report_result(cb, error);
1680 initcall_debug_report(dev, calltime, cb, error);
1685 static void dpm_clear_superiors_direct_complete(struct device *dev)
1687 struct device_link *link;
1691 spin_lock_irq(&dev->parent->power.lock);
1692 dev->parent->power.direct_complete = false;
1693 spin_unlock_irq(&dev->parent->power.lock);
1696 idx = device_links_read_lock();
1698 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1699 spin_lock_irq(&link->supplier->power.lock);
1700 link->supplier->power.direct_complete = false;
1701 spin_unlock_irq(&link->supplier->power.lock);
1704 device_links_read_unlock(idx);
1708 * __device_suspend - Execute "suspend" callbacks for given device.
1709 * @dev: Device to handle.
1710 * @state: PM transition of the system being carried out.
1711 * @async: If true, the device is being suspended asynchronously.
1713 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1715 pm_callback_t callback = NULL;
1716 const char *info = NULL;
1718 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1723 dpm_wait_for_subordinate(dev, async);
1726 dev->power.direct_complete = false;
1731 * If a device configured to wake up the system from sleep states
1732 * has been suspended at run time and there's a resume request pending
1733 * for it, this is equivalent to the device signaling wakeup, so the
1734 * system suspend operation should be aborted.
1736 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1737 pm_wakeup_event(dev, 0);
1739 if (pm_wakeup_pending()) {
1740 dev->power.direct_complete = false;
1741 async_error = -EBUSY;
1745 if (dev->power.syscore)
1748 /* Avoid direct_complete to let wakeup_path propagate. */
1749 if (device_may_wakeup(dev) || dev->power.wakeup_path)
1750 dev->power.direct_complete = false;
1752 if (dev->power.direct_complete) {
1753 if (pm_runtime_status_suspended(dev)) {
1754 pm_runtime_disable(dev);
1755 if (pm_runtime_status_suspended(dev)) {
1756 pm_dev_dbg(dev, state, "direct-complete ");
1760 pm_runtime_enable(dev);
1762 dev->power.direct_complete = false;
1765 dev->power.may_skip_resume = false;
1766 dev->power.must_resume = false;
1768 dpm_watchdog_set(&wd, dev);
1771 if (dev->pm_domain) {
1772 info = "power domain ";
1773 callback = pm_op(&dev->pm_domain->ops, state);
1777 if (dev->type && dev->type->pm) {
1779 callback = pm_op(dev->type->pm, state);
1783 if (dev->class && dev->class->pm) {
1785 callback = pm_op(dev->class->pm, state);
1792 callback = pm_op(dev->bus->pm, state);
1793 } else if (dev->bus->suspend) {
1794 pm_dev_dbg(dev, state, "legacy bus ");
1795 error = legacy_suspend(dev, state, dev->bus->suspend,
1802 if (!callback && dev->driver && dev->driver->pm) {
1804 callback = pm_op(dev->driver->pm, state);
1807 error = dpm_run_callback(callback, dev, state, info);
1811 dev->power.is_suspended = true;
1812 if (device_may_wakeup(dev))
1813 dev->power.wakeup_path = true;
1815 dpm_propagate_wakeup_to_parent(dev);
1816 dpm_clear_superiors_direct_complete(dev);
1820 dpm_watchdog_clear(&wd);
1824 async_error = error;
1826 complete_all(&dev->power.completion);
1827 TRACE_SUSPEND(error);
1831 static void async_suspend(void *data, async_cookie_t cookie)
1833 struct device *dev = (struct device *)data;
1836 error = __device_suspend(dev, pm_transition, true);
1838 dpm_save_failed_dev(dev_name(dev));
1839 pm_dev_err(dev, pm_transition, " async", error);
1845 static int device_suspend(struct device *dev)
1847 if (dpm_async_fn(dev, async_suspend))
1850 return __device_suspend(dev, pm_transition, false);
1854 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1855 * @state: PM transition of the system being carried out.
1857 int dpm_suspend(pm_message_t state)
1859 ktime_t starttime = ktime_get();
1862 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1868 mutex_lock(&dpm_list_mtx);
1869 pm_transition = state;
1871 while (!list_empty(&dpm_prepared_list)) {
1872 struct device *dev = to_device(dpm_prepared_list.prev);
1875 mutex_unlock(&dpm_list_mtx);
1877 error = device_suspend(dev);
1879 mutex_lock(&dpm_list_mtx);
1881 pm_dev_err(dev, state, "", error);
1882 dpm_save_failed_dev(dev_name(dev));
1886 if (!list_empty(&dev->power.entry))
1887 list_move(&dev->power.entry, &dpm_suspended_list);
1892 mutex_unlock(&dpm_list_mtx);
1893 async_synchronize_full();
1895 error = async_error;
1897 suspend_stats.failed_suspend++;
1898 dpm_save_failed_step(SUSPEND_SUSPEND);
1900 dpm_show_time(starttime, state, error, NULL);
1901 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1906 * device_prepare - Prepare a device for system power transition.
1907 * @dev: Device to handle.
1908 * @state: PM transition of the system being carried out.
1910 * Execute the ->prepare() callback(s) for given device. No new children of the
1911 * device may be registered after this function has returned.
1913 static int device_prepare(struct device *dev, pm_message_t state)
1915 int (*callback)(struct device *) = NULL;
1918 if (dev->power.syscore)
1921 WARN_ON(!pm_runtime_enabled(dev) &&
1922 dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
1923 DPM_FLAG_LEAVE_SUSPENDED));
1926 * If a device's parent goes into runtime suspend at the wrong time,
1927 * it won't be possible to resume the device. To prevent this we
1928 * block runtime suspend here, during the prepare phase, and allow
1929 * it again during the complete phase.
1931 pm_runtime_get_noresume(dev);
1935 dev->power.wakeup_path = false;
1937 if (dev->power.no_pm_callbacks)
1941 callback = dev->pm_domain->ops.prepare;
1942 else if (dev->type && dev->type->pm)
1943 callback = dev->type->pm->prepare;
1944 else if (dev->class && dev->class->pm)
1945 callback = dev->class->pm->prepare;
1946 else if (dev->bus && dev->bus->pm)
1947 callback = dev->bus->pm->prepare;
1949 if (!callback && dev->driver && dev->driver->pm)
1950 callback = dev->driver->pm->prepare;
1953 ret = callback(dev);
1959 suspend_report_result(callback, ret);
1960 pm_runtime_put(dev);
1964 * A positive return value from ->prepare() means "this device appears
1965 * to be runtime-suspended and its state is fine, so if it really is
1966 * runtime-suspended, you can leave it in that state provided that you
1967 * will do the same thing with all of its descendants". This only
1968 * applies to suspend transitions, however.
1970 spin_lock_irq(&dev->power.lock);
1971 dev->power.direct_complete = state.event == PM_EVENT_SUSPEND &&
1972 ((pm_runtime_suspended(dev) && ret > 0) ||
1973 dev->power.no_pm_callbacks) &&
1974 !dev_pm_test_driver_flags(dev, DPM_FLAG_NEVER_SKIP);
1975 spin_unlock_irq(&dev->power.lock);
1980 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1981 * @state: PM transition of the system being carried out.
1983 * Execute the ->prepare() callback(s) for all devices.
1985 int dpm_prepare(pm_message_t state)
1989 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1993 * Give a chance for the known devices to complete their probes, before
1994 * disable probing of devices. This sync point is important at least
1995 * at boot time + hibernation restore.
1997 wait_for_device_probe();
1999 * It is unsafe if probing of devices will happen during suspend or
2000 * hibernation and system behavior will be unpredictable in this case.
2001 * So, let's prohibit device's probing here and defer their probes
2002 * instead. The normal behavior will be restored in dpm_complete().
2004 device_block_probing();
2006 mutex_lock(&dpm_list_mtx);
2007 while (!list_empty(&dpm_list)) {
2008 struct device *dev = to_device(dpm_list.next);
2011 mutex_unlock(&dpm_list_mtx);
2013 trace_device_pm_callback_start(dev, "", state.event);
2014 error = device_prepare(dev, state);
2015 trace_device_pm_callback_end(dev, error);
2017 mutex_lock(&dpm_list_mtx);
2019 if (error == -EAGAIN) {
2024 pr_info("Device %s not prepared for power transition: code %d\n",
2025 dev_name(dev), error);
2029 dev->power.is_prepared = true;
2030 if (!list_empty(&dev->power.entry))
2031 list_move_tail(&dev->power.entry, &dpm_prepared_list);
2034 mutex_unlock(&dpm_list_mtx);
2035 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
2040 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2041 * @state: PM transition of the system being carried out.
2043 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2044 * callbacks for them.
2046 int dpm_suspend_start(pm_message_t state)
2048 ktime_t starttime = ktime_get();
2051 error = dpm_prepare(state);
2053 suspend_stats.failed_prepare++;
2054 dpm_save_failed_step(SUSPEND_PREPARE);
2056 error = dpm_suspend(state);
2057 dpm_show_time(starttime, state, error, "start");
2060 EXPORT_SYMBOL_GPL(dpm_suspend_start);
2062 void __suspend_report_result(const char *function, void *fn, int ret)
2065 pr_err("%s(): %pS returns %d\n", function, fn, ret);
2067 EXPORT_SYMBOL_GPL(__suspend_report_result);
2070 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2071 * @subordinate: Device that needs to wait for @dev.
2072 * @dev: Device to wait for.
2074 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
2076 dpm_wait(dev, subordinate->power.async_suspend);
2079 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
2082 * dpm_for_each_dev - device iterator.
2083 * @data: data for the callback.
2084 * @fn: function to be called for each device.
2086 * Iterate over devices in dpm_list, and call @fn for each device,
2089 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
2097 list_for_each_entry(dev, &dpm_list, power.entry)
2101 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
2103 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
2108 return !ops->prepare &&
2110 !ops->suspend_late &&
2111 !ops->suspend_noirq &&
2112 !ops->resume_noirq &&
2113 !ops->resume_early &&
2118 void device_pm_check_callbacks(struct device *dev)
2120 spin_lock_irq(&dev->power.lock);
2121 dev->power.no_pm_callbacks =
2122 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
2123 !dev->bus->suspend && !dev->bus->resume)) &&
2124 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
2125 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
2126 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
2127 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
2128 !dev->driver->suspend && !dev->driver->resume));
2129 spin_unlock_irq(&dev->power.lock);
2132 bool dev_pm_smart_suspend_and_suspended(struct device *dev)
2134 return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) &&
2135 pm_runtime_status_suspended(dev);