2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/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/cpuidle.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 pr_debug("PM: Adding info for %s:%s\n",
128 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
129 device_pm_check_callbacks(dev);
130 mutex_lock(&dpm_list_mtx);
131 if (dev->parent && dev->parent->power.is_prepared)
132 dev_warn(dev, "parent %s should not be sleeping\n",
133 dev_name(dev->parent));
134 list_add_tail(&dev->power.entry, &dpm_list);
135 dev->power.in_dpm_list = true;
136 mutex_unlock(&dpm_list_mtx);
140 * device_pm_remove - Remove a device from the PM core's list of active devices.
141 * @dev: Device to be removed from the list.
143 void device_pm_remove(struct device *dev)
145 pr_debug("PM: Removing info for %s:%s\n",
146 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
147 complete_all(&dev->power.completion);
148 mutex_lock(&dpm_list_mtx);
149 list_del_init(&dev->power.entry);
150 dev->power.in_dpm_list = false;
151 mutex_unlock(&dpm_list_mtx);
152 device_wakeup_disable(dev);
153 pm_runtime_remove(dev);
154 device_pm_check_callbacks(dev);
158 * device_pm_move_before - Move device in the PM core's list of active devices.
159 * @deva: Device to move in dpm_list.
160 * @devb: Device @deva should come before.
162 void device_pm_move_before(struct device *deva, struct device *devb)
164 pr_debug("PM: Moving %s:%s before %s:%s\n",
165 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
166 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
167 /* Delete deva from dpm_list and reinsert before devb. */
168 list_move_tail(&deva->power.entry, &devb->power.entry);
172 * device_pm_move_after - Move device in the PM core's list of active devices.
173 * @deva: Device to move in dpm_list.
174 * @devb: Device @deva should come after.
176 void device_pm_move_after(struct device *deva, struct device *devb)
178 pr_debug("PM: Moving %s:%s after %s:%s\n",
179 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
180 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
181 /* Delete deva from dpm_list and reinsert after devb. */
182 list_move(&deva->power.entry, &devb->power.entry);
186 * device_pm_move_last - Move device to end of the PM core's list of devices.
187 * @dev: Device to move in dpm_list.
189 void device_pm_move_last(struct device *dev)
191 pr_debug("PM: Moving %s:%s to end of list\n",
192 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
193 list_move_tail(&dev->power.entry, &dpm_list);
196 static ktime_t initcall_debug_start(struct device *dev)
198 ktime_t calltime = 0;
200 if (pm_print_times_enabled) {
201 pr_info("calling %s+ @ %i, parent: %s\n",
202 dev_name(dev), task_pid_nr(current),
203 dev->parent ? dev_name(dev->parent) : "none");
204 calltime = ktime_get();
210 static void initcall_debug_report(struct device *dev, ktime_t calltime,
211 int error, pm_message_t state,
217 rettime = ktime_get();
218 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
220 if (pm_print_times_enabled) {
221 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
222 error, (unsigned long long)nsecs >> 10);
227 * dpm_wait - Wait for a PM operation to complete.
228 * @dev: Device to wait for.
229 * @async: If unset, wait only if the device's power.async_suspend flag is set.
231 static void dpm_wait(struct device *dev, bool async)
236 if (async || (pm_async_enabled && dev->power.async_suspend))
237 wait_for_completion(&dev->power.completion);
240 static int dpm_wait_fn(struct device *dev, void *async_ptr)
242 dpm_wait(dev, *((bool *)async_ptr));
246 static void dpm_wait_for_children(struct device *dev, bool async)
248 device_for_each_child(dev, &async, dpm_wait_fn);
251 static void dpm_wait_for_suppliers(struct device *dev, bool async)
253 struct device_link *link;
256 idx = device_links_read_lock();
259 * If the supplier goes away right after we've checked the link to it,
260 * we'll wait for its completion to change the state, but that's fine,
261 * because the only things that will block as a result are the SRCU
262 * callbacks freeing the link objects for the links in the list we're
265 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
266 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
267 dpm_wait(link->supplier, async);
269 device_links_read_unlock(idx);
272 static void dpm_wait_for_superior(struct device *dev, bool async)
274 dpm_wait(dev->parent, async);
275 dpm_wait_for_suppliers(dev, async);
278 static void dpm_wait_for_consumers(struct device *dev, bool async)
280 struct device_link *link;
283 idx = device_links_read_lock();
286 * The status of a device link can only be changed from "dormant" by a
287 * probe, but that cannot happen during system suspend/resume. In
288 * theory it can change to "dormant" at that time, but then it is
289 * reasonable to wait for the target device anyway (eg. if it goes
290 * away, it's better to wait for it to go away completely and then
291 * continue instead of trying to continue in parallel with its
294 list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
295 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
296 dpm_wait(link->consumer, async);
298 device_links_read_unlock(idx);
301 static void dpm_wait_for_subordinate(struct device *dev, bool async)
303 dpm_wait_for_children(dev, async);
304 dpm_wait_for_consumers(dev, async);
308 * pm_op - Return the PM operation appropriate for given PM event.
309 * @ops: PM operations to choose from.
310 * @state: PM transition of the system being carried out.
312 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
314 switch (state.event) {
315 #ifdef CONFIG_SUSPEND
316 case PM_EVENT_SUSPEND:
318 case PM_EVENT_RESUME:
320 #endif /* CONFIG_SUSPEND */
321 #ifdef CONFIG_HIBERNATE_CALLBACKS
322 case PM_EVENT_FREEZE:
323 case PM_EVENT_QUIESCE:
325 case PM_EVENT_HIBERNATE:
326 return ops->poweroff;
328 case PM_EVENT_RECOVER:
331 case PM_EVENT_RESTORE:
333 #endif /* CONFIG_HIBERNATE_CALLBACKS */
340 * pm_late_early_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 * Runtime PM is disabled for @dev while this function is being executed.
346 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
349 switch (state.event) {
350 #ifdef CONFIG_SUSPEND
351 case PM_EVENT_SUSPEND:
352 return ops->suspend_late;
353 case PM_EVENT_RESUME:
354 return ops->resume_early;
355 #endif /* CONFIG_SUSPEND */
356 #ifdef CONFIG_HIBERNATE_CALLBACKS
357 case PM_EVENT_FREEZE:
358 case PM_EVENT_QUIESCE:
359 return ops->freeze_late;
360 case PM_EVENT_HIBERNATE:
361 return ops->poweroff_late;
363 case PM_EVENT_RECOVER:
364 return ops->thaw_early;
365 case PM_EVENT_RESTORE:
366 return ops->restore_early;
367 #endif /* CONFIG_HIBERNATE_CALLBACKS */
374 * pm_noirq_op - Return the PM operation appropriate for given PM event.
375 * @ops: PM operations to choose from.
376 * @state: PM transition of the system being carried out.
378 * The driver of @dev will not receive interrupts while this function is being
381 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
383 switch (state.event) {
384 #ifdef CONFIG_SUSPEND
385 case PM_EVENT_SUSPEND:
386 return ops->suspend_noirq;
387 case PM_EVENT_RESUME:
388 return ops->resume_noirq;
389 #endif /* CONFIG_SUSPEND */
390 #ifdef CONFIG_HIBERNATE_CALLBACKS
391 case PM_EVENT_FREEZE:
392 case PM_EVENT_QUIESCE:
393 return ops->freeze_noirq;
394 case PM_EVENT_HIBERNATE:
395 return ops->poweroff_noirq;
397 case PM_EVENT_RECOVER:
398 return ops->thaw_noirq;
399 case PM_EVENT_RESTORE:
400 return ops->restore_noirq;
401 #endif /* CONFIG_HIBERNATE_CALLBACKS */
407 static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
409 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
410 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
411 ", may wakeup" : "");
414 static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
417 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
418 dev_name(dev), pm_verb(state.event), info, error);
421 #ifdef CONFIG_PM_DEBUG
422 static void dpm_show_time(ktime_t starttime, pm_message_t state,
429 calltime = ktime_get();
430 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
431 do_div(usecs64, NSEC_PER_USEC);
435 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
436 info ?: "", info ? " " : "", pm_verb(state.event),
437 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
440 static inline void dpm_show_time(ktime_t starttime, pm_message_t state,
442 #endif /* CONFIG_PM_DEBUG */
444 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
445 pm_message_t state, const char *info)
453 calltime = initcall_debug_start(dev);
455 pm_dev_dbg(dev, state, info);
456 trace_device_pm_callback_start(dev, info, state.event);
458 trace_device_pm_callback_end(dev, error);
459 suspend_report_result(cb, error);
461 initcall_debug_report(dev, calltime, error, state, info);
466 #ifdef CONFIG_DPM_WATCHDOG
467 struct dpm_watchdog {
469 struct task_struct *tsk;
470 struct timer_list timer;
473 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
474 struct dpm_watchdog wd
477 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
478 * @data: Watchdog object address.
480 * Called when a driver has timed out suspending or resuming.
481 * There's not much we can do here to recover so panic() to
482 * capture a crash-dump in pstore.
484 static void dpm_watchdog_handler(unsigned long data)
486 struct dpm_watchdog *wd = (void *)data;
488 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
489 show_stack(wd->tsk, NULL);
490 panic("%s %s: unrecoverable failure\n",
491 dev_driver_string(wd->dev), dev_name(wd->dev));
495 * dpm_watchdog_set - Enable pm watchdog for given device.
496 * @wd: Watchdog. Must be allocated on the stack.
497 * @dev: Device to handle.
499 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
501 struct timer_list *timer = &wd->timer;
506 init_timer_on_stack(timer);
507 /* use same timeout value for both suspend and resume */
508 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
509 timer->function = dpm_watchdog_handler;
510 timer->data = (unsigned long)wd;
515 * dpm_watchdog_clear - Disable suspend/resume watchdog.
516 * @wd: Watchdog to disable.
518 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
520 struct timer_list *timer = &wd->timer;
522 del_timer_sync(timer);
523 destroy_timer_on_stack(timer);
526 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
527 #define dpm_watchdog_set(x, y)
528 #define dpm_watchdog_clear(x)
531 /*------------------------- Resume routines -------------------------*/
534 * device_resume_noirq - Execute an "early resume" callback for given device.
535 * @dev: Device to handle.
536 * @state: PM transition of the system being carried out.
537 * @async: If true, the device is being resumed asynchronously.
539 * The driver of @dev will not receive interrupts while this function is being
542 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
544 pm_callback_t callback = NULL;
545 const char *info = NULL;
551 if (dev->power.syscore || dev->power.direct_complete)
554 if (!dev->power.is_noirq_suspended)
557 dpm_wait_for_superior(dev, async);
559 if (dev->pm_domain) {
560 info = "noirq power domain ";
561 callback = pm_noirq_op(&dev->pm_domain->ops, state);
562 } else if (dev->type && dev->type->pm) {
563 info = "noirq type ";
564 callback = pm_noirq_op(dev->type->pm, state);
565 } else if (dev->class && dev->class->pm) {
566 info = "noirq class ";
567 callback = pm_noirq_op(dev->class->pm, state);
568 } else if (dev->bus && dev->bus->pm) {
570 callback = pm_noirq_op(dev->bus->pm, state);
573 if (!callback && dev->driver && dev->driver->pm) {
574 info = "noirq driver ";
575 callback = pm_noirq_op(dev->driver->pm, state);
578 error = dpm_run_callback(callback, dev, state, info);
579 dev->power.is_noirq_suspended = false;
582 complete_all(&dev->power.completion);
587 static bool is_async(struct device *dev)
589 return dev->power.async_suspend && pm_async_enabled
590 && !pm_trace_is_enabled();
593 static void async_resume_noirq(void *data, async_cookie_t cookie)
595 struct device *dev = (struct device *)data;
598 error = device_resume_noirq(dev, pm_transition, true);
600 pm_dev_err(dev, pm_transition, " async", error);
606 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
607 * @state: PM transition of the system being carried out.
609 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
610 * enable device drivers to receive interrupts.
612 void dpm_resume_noirq(pm_message_t state)
615 ktime_t starttime = ktime_get();
617 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
618 mutex_lock(&dpm_list_mtx);
619 pm_transition = state;
622 * Advanced the async threads upfront,
623 * in case the starting of async threads is
624 * delayed by non-async resuming devices.
626 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
627 reinit_completion(&dev->power.completion);
630 async_schedule(async_resume_noirq, dev);
634 while (!list_empty(&dpm_noirq_list)) {
635 dev = to_device(dpm_noirq_list.next);
637 list_move_tail(&dev->power.entry, &dpm_late_early_list);
638 mutex_unlock(&dpm_list_mtx);
640 if (!is_async(dev)) {
643 error = device_resume_noirq(dev, state, false);
645 suspend_stats.failed_resume_noirq++;
646 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
647 dpm_save_failed_dev(dev_name(dev));
648 pm_dev_err(dev, state, " noirq", error);
652 mutex_lock(&dpm_list_mtx);
655 mutex_unlock(&dpm_list_mtx);
656 async_synchronize_full();
657 dpm_show_time(starttime, state, "noirq");
658 resume_device_irqs();
659 device_wakeup_disarm_wake_irqs();
661 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
665 * device_resume_early - Execute an "early resume" callback for given device.
666 * @dev: Device to handle.
667 * @state: PM transition of the system being carried out.
668 * @async: If true, the device is being resumed asynchronously.
670 * Runtime PM is disabled for @dev while this function is being executed.
672 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
674 pm_callback_t callback = NULL;
675 const char *info = NULL;
681 if (dev->power.syscore || dev->power.direct_complete)
684 if (!dev->power.is_late_suspended)
687 dpm_wait_for_superior(dev, async);
689 if (dev->pm_domain) {
690 info = "early power domain ";
691 callback = pm_late_early_op(&dev->pm_domain->ops, state);
692 } else if (dev->type && dev->type->pm) {
693 info = "early type ";
694 callback = pm_late_early_op(dev->type->pm, state);
695 } else if (dev->class && dev->class->pm) {
696 info = "early class ";
697 callback = pm_late_early_op(dev->class->pm, state);
698 } else if (dev->bus && dev->bus->pm) {
700 callback = pm_late_early_op(dev->bus->pm, state);
703 if (!callback && dev->driver && dev->driver->pm) {
704 info = "early driver ";
705 callback = pm_late_early_op(dev->driver->pm, state);
708 error = dpm_run_callback(callback, dev, state, info);
709 dev->power.is_late_suspended = false;
714 pm_runtime_enable(dev);
715 complete_all(&dev->power.completion);
719 static void async_resume_early(void *data, async_cookie_t cookie)
721 struct device *dev = (struct device *)data;
724 error = device_resume_early(dev, pm_transition, true);
726 pm_dev_err(dev, pm_transition, " async", error);
732 * dpm_resume_early - Execute "early resume" callbacks for all devices.
733 * @state: PM transition of the system being carried out.
735 void dpm_resume_early(pm_message_t state)
738 ktime_t starttime = ktime_get();
740 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
741 mutex_lock(&dpm_list_mtx);
742 pm_transition = state;
745 * Advanced the async threads upfront,
746 * in case the starting of async threads is
747 * delayed by non-async resuming devices.
749 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
750 reinit_completion(&dev->power.completion);
753 async_schedule(async_resume_early, dev);
757 while (!list_empty(&dpm_late_early_list)) {
758 dev = to_device(dpm_late_early_list.next);
760 list_move_tail(&dev->power.entry, &dpm_suspended_list);
761 mutex_unlock(&dpm_list_mtx);
763 if (!is_async(dev)) {
766 error = device_resume_early(dev, state, false);
768 suspend_stats.failed_resume_early++;
769 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
770 dpm_save_failed_dev(dev_name(dev));
771 pm_dev_err(dev, state, " early", error);
774 mutex_lock(&dpm_list_mtx);
777 mutex_unlock(&dpm_list_mtx);
778 async_synchronize_full();
779 dpm_show_time(starttime, state, "early");
780 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
784 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
785 * @state: PM transition of the system being carried out.
787 void dpm_resume_start(pm_message_t state)
789 dpm_resume_noirq(state);
790 dpm_resume_early(state);
792 EXPORT_SYMBOL_GPL(dpm_resume_start);
795 * device_resume - Execute "resume" callbacks for given device.
796 * @dev: Device to handle.
797 * @state: PM transition of the system being carried out.
798 * @async: If true, the device is being resumed asynchronously.
800 static int device_resume(struct device *dev, pm_message_t state, bool async)
802 pm_callback_t callback = NULL;
803 const char *info = NULL;
805 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
810 if (dev->power.syscore)
813 if (dev->power.direct_complete) {
814 /* Match the pm_runtime_disable() in __device_suspend(). */
815 pm_runtime_enable(dev);
819 dpm_wait_for_superior(dev, async);
820 dpm_watchdog_set(&wd, dev);
824 * This is a fib. But we'll allow new children to be added below
825 * a resumed device, even if the device hasn't been completed yet.
827 dev->power.is_prepared = false;
829 if (!dev->power.is_suspended)
832 if (dev->pm_domain) {
833 info = "power domain ";
834 callback = pm_op(&dev->pm_domain->ops, state);
838 if (dev->type && dev->type->pm) {
840 callback = pm_op(dev->type->pm, state);
845 if (dev->class->pm) {
847 callback = pm_op(dev->class->pm, state);
849 } else if (dev->class->resume) {
850 info = "legacy class ";
851 callback = dev->class->resume;
859 callback = pm_op(dev->bus->pm, state);
860 } else if (dev->bus->resume) {
861 info = "legacy bus ";
862 callback = dev->bus->resume;
868 if (!callback && dev->driver && dev->driver->pm) {
870 callback = pm_op(dev->driver->pm, state);
874 error = dpm_run_callback(callback, dev, state, info);
875 dev->power.is_suspended = false;
879 dpm_watchdog_clear(&wd);
882 complete_all(&dev->power.completion);
889 static void async_resume(void *data, async_cookie_t cookie)
891 struct device *dev = (struct device *)data;
894 error = device_resume(dev, pm_transition, true);
896 pm_dev_err(dev, pm_transition, " async", error);
901 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
902 * @state: PM transition of the system being carried out.
904 * Execute the appropriate "resume" callback for all devices whose status
905 * indicates that they are suspended.
907 void dpm_resume(pm_message_t state)
910 ktime_t starttime = ktime_get();
912 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
915 mutex_lock(&dpm_list_mtx);
916 pm_transition = state;
919 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
920 reinit_completion(&dev->power.completion);
923 async_schedule(async_resume, dev);
927 while (!list_empty(&dpm_suspended_list)) {
928 dev = to_device(dpm_suspended_list.next);
930 if (!is_async(dev)) {
933 mutex_unlock(&dpm_list_mtx);
935 error = device_resume(dev, state, false);
937 suspend_stats.failed_resume++;
938 dpm_save_failed_step(SUSPEND_RESUME);
939 dpm_save_failed_dev(dev_name(dev));
940 pm_dev_err(dev, state, "", error);
943 mutex_lock(&dpm_list_mtx);
945 if (!list_empty(&dev->power.entry))
946 list_move_tail(&dev->power.entry, &dpm_prepared_list);
949 mutex_unlock(&dpm_list_mtx);
950 async_synchronize_full();
951 dpm_show_time(starttime, state, NULL);
954 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
958 * device_complete - Complete a PM transition for given device.
959 * @dev: Device to handle.
960 * @state: PM transition of the system being carried out.
962 static void device_complete(struct device *dev, pm_message_t state)
964 void (*callback)(struct device *) = NULL;
965 const char *info = NULL;
967 if (dev->power.syscore)
972 if (dev->pm_domain) {
973 info = "completing power domain ";
974 callback = dev->pm_domain->ops.complete;
975 } else if (dev->type && dev->type->pm) {
976 info = "completing type ";
977 callback = dev->type->pm->complete;
978 } else if (dev->class && dev->class->pm) {
979 info = "completing class ";
980 callback = dev->class->pm->complete;
981 } else if (dev->bus && dev->bus->pm) {
982 info = "completing bus ";
983 callback = dev->bus->pm->complete;
986 if (!callback && dev->driver && dev->driver->pm) {
987 info = "completing driver ";
988 callback = dev->driver->pm->complete;
992 pm_dev_dbg(dev, state, info);
1002 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1003 * @state: PM transition of the system being carried out.
1005 * Execute the ->complete() callbacks for all devices whose PM status is not
1006 * DPM_ON (this allows new devices to be registered).
1008 void dpm_complete(pm_message_t state)
1010 struct list_head list;
1012 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
1015 INIT_LIST_HEAD(&list);
1016 mutex_lock(&dpm_list_mtx);
1017 while (!list_empty(&dpm_prepared_list)) {
1018 struct device *dev = to_device(dpm_prepared_list.prev);
1021 dev->power.is_prepared = false;
1022 list_move(&dev->power.entry, &list);
1023 mutex_unlock(&dpm_list_mtx);
1025 trace_device_pm_callback_start(dev, "", state.event);
1026 device_complete(dev, state);
1027 trace_device_pm_callback_end(dev, 0);
1029 mutex_lock(&dpm_list_mtx);
1032 list_splice(&list, &dpm_list);
1033 mutex_unlock(&dpm_list_mtx);
1035 /* Allow device probing and trigger re-probing of deferred devices */
1036 device_unblock_probing();
1037 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1041 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1042 * @state: PM transition of the system being carried out.
1044 * Execute "resume" callbacks for all devices and complete the PM transition of
1047 void dpm_resume_end(pm_message_t state)
1050 dpm_complete(state);
1052 EXPORT_SYMBOL_GPL(dpm_resume_end);
1055 /*------------------------- Suspend routines -------------------------*/
1058 * resume_event - Return a "resume" message for given "suspend" sleep state.
1059 * @sleep_state: PM message representing a sleep state.
1061 * Return a PM message representing the resume event corresponding to given
1064 static pm_message_t resume_event(pm_message_t sleep_state)
1066 switch (sleep_state.event) {
1067 case PM_EVENT_SUSPEND:
1069 case PM_EVENT_FREEZE:
1070 case PM_EVENT_QUIESCE:
1071 return PMSG_RECOVER;
1072 case PM_EVENT_HIBERNATE:
1073 return PMSG_RESTORE;
1079 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1080 * @dev: Device to handle.
1081 * @state: PM transition of the system being carried out.
1082 * @async: If true, the device is being suspended asynchronously.
1084 * The driver of @dev will not receive interrupts while this function is being
1087 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1089 pm_callback_t callback = NULL;
1090 const char *info = NULL;
1096 dpm_wait_for_subordinate(dev, async);
1101 if (dev->power.syscore || dev->power.direct_complete)
1104 if (dev->pm_domain) {
1105 info = "noirq power domain ";
1106 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1107 } else if (dev->type && dev->type->pm) {
1108 info = "noirq type ";
1109 callback = pm_noirq_op(dev->type->pm, state);
1110 } else if (dev->class && dev->class->pm) {
1111 info = "noirq class ";
1112 callback = pm_noirq_op(dev->class->pm, state);
1113 } else if (dev->bus && dev->bus->pm) {
1114 info = "noirq bus ";
1115 callback = pm_noirq_op(dev->bus->pm, state);
1118 if (!callback && dev->driver && dev->driver->pm) {
1119 info = "noirq driver ";
1120 callback = pm_noirq_op(dev->driver->pm, state);
1123 error = dpm_run_callback(callback, dev, state, info);
1125 dev->power.is_noirq_suspended = true;
1127 async_error = error;
1130 complete_all(&dev->power.completion);
1131 TRACE_SUSPEND(error);
1135 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1137 struct device *dev = (struct device *)data;
1140 error = __device_suspend_noirq(dev, pm_transition, true);
1142 dpm_save_failed_dev(dev_name(dev));
1143 pm_dev_err(dev, pm_transition, " async", error);
1149 static int device_suspend_noirq(struct device *dev)
1151 reinit_completion(&dev->power.completion);
1153 if (is_async(dev)) {
1155 async_schedule(async_suspend_noirq, dev);
1158 return __device_suspend_noirq(dev, pm_transition, false);
1162 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1163 * @state: PM transition of the system being carried out.
1165 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1166 * handlers for all non-sysdev devices.
1168 int dpm_suspend_noirq(pm_message_t state)
1170 ktime_t starttime = ktime_get();
1173 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1175 device_wakeup_arm_wake_irqs();
1176 suspend_device_irqs();
1177 mutex_lock(&dpm_list_mtx);
1178 pm_transition = state;
1181 while (!list_empty(&dpm_late_early_list)) {
1182 struct device *dev = to_device(dpm_late_early_list.prev);
1185 mutex_unlock(&dpm_list_mtx);
1187 error = device_suspend_noirq(dev);
1189 mutex_lock(&dpm_list_mtx);
1191 pm_dev_err(dev, state, " noirq", error);
1192 dpm_save_failed_dev(dev_name(dev));
1196 if (!list_empty(&dev->power.entry))
1197 list_move(&dev->power.entry, &dpm_noirq_list);
1203 mutex_unlock(&dpm_list_mtx);
1204 async_synchronize_full();
1206 error = async_error;
1209 suspend_stats.failed_suspend_noirq++;
1210 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1211 dpm_resume_noirq(resume_event(state));
1213 dpm_show_time(starttime, state, "noirq");
1215 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1220 * device_suspend_late - Execute a "late suspend" callback for given device.
1221 * @dev: Device to handle.
1222 * @state: PM transition of the system being carried out.
1223 * @async: If true, the device is being suspended asynchronously.
1225 * Runtime PM is disabled for @dev while this function is being executed.
1227 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1229 pm_callback_t callback = NULL;
1230 const char *info = NULL;
1236 __pm_runtime_disable(dev, false);
1238 dpm_wait_for_subordinate(dev, async);
1243 if (pm_wakeup_pending()) {
1244 async_error = -EBUSY;
1248 if (dev->power.syscore || dev->power.direct_complete)
1251 if (dev->pm_domain) {
1252 info = "late power domain ";
1253 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1254 } else if (dev->type && dev->type->pm) {
1255 info = "late type ";
1256 callback = pm_late_early_op(dev->type->pm, state);
1257 } else if (dev->class && dev->class->pm) {
1258 info = "late class ";
1259 callback = pm_late_early_op(dev->class->pm, state);
1260 } else if (dev->bus && dev->bus->pm) {
1262 callback = pm_late_early_op(dev->bus->pm, state);
1265 if (!callback && dev->driver && dev->driver->pm) {
1266 info = "late driver ";
1267 callback = pm_late_early_op(dev->driver->pm, state);
1270 error = dpm_run_callback(callback, dev, state, info);
1272 dev->power.is_late_suspended = true;
1274 async_error = error;
1277 TRACE_SUSPEND(error);
1278 complete_all(&dev->power.completion);
1282 static void async_suspend_late(void *data, async_cookie_t cookie)
1284 struct device *dev = (struct device *)data;
1287 error = __device_suspend_late(dev, pm_transition, true);
1289 dpm_save_failed_dev(dev_name(dev));
1290 pm_dev_err(dev, pm_transition, " async", error);
1295 static int device_suspend_late(struct device *dev)
1297 reinit_completion(&dev->power.completion);
1299 if (is_async(dev)) {
1301 async_schedule(async_suspend_late, dev);
1305 return __device_suspend_late(dev, pm_transition, false);
1309 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1310 * @state: PM transition of the system being carried out.
1312 int dpm_suspend_late(pm_message_t state)
1314 ktime_t starttime = ktime_get();
1317 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1318 mutex_lock(&dpm_list_mtx);
1319 pm_transition = state;
1322 while (!list_empty(&dpm_suspended_list)) {
1323 struct device *dev = to_device(dpm_suspended_list.prev);
1326 mutex_unlock(&dpm_list_mtx);
1328 error = device_suspend_late(dev);
1330 mutex_lock(&dpm_list_mtx);
1331 if (!list_empty(&dev->power.entry))
1332 list_move(&dev->power.entry, &dpm_late_early_list);
1335 pm_dev_err(dev, state, " late", error);
1336 dpm_save_failed_dev(dev_name(dev));
1345 mutex_unlock(&dpm_list_mtx);
1346 async_synchronize_full();
1348 error = async_error;
1350 suspend_stats.failed_suspend_late++;
1351 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1352 dpm_resume_early(resume_event(state));
1354 dpm_show_time(starttime, state, "late");
1356 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1361 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1362 * @state: PM transition of the system being carried out.
1364 int dpm_suspend_end(pm_message_t state)
1366 int error = dpm_suspend_late(state);
1370 error = dpm_suspend_noirq(state);
1372 dpm_resume_early(resume_event(state));
1378 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1381 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1382 * @dev: Device to suspend.
1383 * @state: PM transition of the system being carried out.
1384 * @cb: Suspend callback to execute.
1385 * @info: string description of caller.
1387 static int legacy_suspend(struct device *dev, pm_message_t state,
1388 int (*cb)(struct device *dev, pm_message_t state),
1394 calltime = initcall_debug_start(dev);
1396 trace_device_pm_callback_start(dev, info, state.event);
1397 error = cb(dev, state);
1398 trace_device_pm_callback_end(dev, error);
1399 suspend_report_result(cb, error);
1401 initcall_debug_report(dev, calltime, error, state, info);
1406 static void dpm_clear_suppliers_direct_complete(struct device *dev)
1408 struct device_link *link;
1411 idx = device_links_read_lock();
1413 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1414 spin_lock_irq(&link->supplier->power.lock);
1415 link->supplier->power.direct_complete = false;
1416 spin_unlock_irq(&link->supplier->power.lock);
1419 device_links_read_unlock(idx);
1423 * device_suspend - Execute "suspend" callbacks for given device.
1424 * @dev: Device to handle.
1425 * @state: PM transition of the system being carried out.
1426 * @async: If true, the device is being suspended asynchronously.
1428 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1430 pm_callback_t callback = NULL;
1431 const char *info = NULL;
1433 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1438 dpm_wait_for_subordinate(dev, async);
1444 * If a device configured to wake up the system from sleep states
1445 * has been suspended at run time and there's a resume request pending
1446 * for it, this is equivalent to the device signaling wakeup, so the
1447 * system suspend operation should be aborted.
1449 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1450 pm_wakeup_event(dev, 0);
1452 if (pm_wakeup_pending()) {
1453 async_error = -EBUSY;
1457 if (dev->power.syscore)
1460 if (dev->power.direct_complete) {
1461 if (pm_runtime_status_suspended(dev)) {
1462 pm_runtime_disable(dev);
1463 if (pm_runtime_status_suspended(dev))
1466 pm_runtime_enable(dev);
1468 dev->power.direct_complete = false;
1471 dpm_watchdog_set(&wd, dev);
1474 if (dev->pm_domain) {
1475 info = "power domain ";
1476 callback = pm_op(&dev->pm_domain->ops, state);
1480 if (dev->type && dev->type->pm) {
1482 callback = pm_op(dev->type->pm, state);
1487 if (dev->class->pm) {
1489 callback = pm_op(dev->class->pm, state);
1491 } else if (dev->class->suspend) {
1492 pm_dev_dbg(dev, state, "legacy class ");
1493 error = legacy_suspend(dev, state, dev->class->suspend,
1502 callback = pm_op(dev->bus->pm, state);
1503 } else if (dev->bus->suspend) {
1504 pm_dev_dbg(dev, state, "legacy bus ");
1505 error = legacy_suspend(dev, state, dev->bus->suspend,
1512 if (!callback && dev->driver && dev->driver->pm) {
1514 callback = pm_op(dev->driver->pm, state);
1517 error = dpm_run_callback(callback, dev, state, info);
1521 struct device *parent = dev->parent;
1523 dev->power.is_suspended = true;
1525 spin_lock_irq(&parent->power.lock);
1527 dev->parent->power.direct_complete = false;
1528 if (dev->power.wakeup_path
1529 && !dev->parent->power.ignore_children)
1530 dev->parent->power.wakeup_path = true;
1532 spin_unlock_irq(&parent->power.lock);
1534 dpm_clear_suppliers_direct_complete(dev);
1538 dpm_watchdog_clear(&wd);
1542 async_error = error;
1544 complete_all(&dev->power.completion);
1545 TRACE_SUSPEND(error);
1549 static void async_suspend(void *data, async_cookie_t cookie)
1551 struct device *dev = (struct device *)data;
1554 error = __device_suspend(dev, pm_transition, true);
1556 dpm_save_failed_dev(dev_name(dev));
1557 pm_dev_err(dev, pm_transition, " async", error);
1563 static int device_suspend(struct device *dev)
1565 reinit_completion(&dev->power.completion);
1567 if (is_async(dev)) {
1569 async_schedule(async_suspend, dev);
1573 return __device_suspend(dev, pm_transition, false);
1577 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1578 * @state: PM transition of the system being carried out.
1580 int dpm_suspend(pm_message_t state)
1582 ktime_t starttime = ktime_get();
1585 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1590 mutex_lock(&dpm_list_mtx);
1591 pm_transition = state;
1593 while (!list_empty(&dpm_prepared_list)) {
1594 struct device *dev = to_device(dpm_prepared_list.prev);
1597 mutex_unlock(&dpm_list_mtx);
1599 error = device_suspend(dev);
1601 mutex_lock(&dpm_list_mtx);
1603 pm_dev_err(dev, state, "", error);
1604 dpm_save_failed_dev(dev_name(dev));
1608 if (!list_empty(&dev->power.entry))
1609 list_move(&dev->power.entry, &dpm_suspended_list);
1614 mutex_unlock(&dpm_list_mtx);
1615 async_synchronize_full();
1617 error = async_error;
1619 suspend_stats.failed_suspend++;
1620 dpm_save_failed_step(SUSPEND_SUSPEND);
1622 dpm_show_time(starttime, state, NULL);
1623 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1628 * device_prepare - Prepare a device for system power transition.
1629 * @dev: Device to handle.
1630 * @state: PM transition of the system being carried out.
1632 * Execute the ->prepare() callback(s) for given device. No new children of the
1633 * device may be registered after this function has returned.
1635 static int device_prepare(struct device *dev, pm_message_t state)
1637 int (*callback)(struct device *) = NULL;
1640 if (dev->power.syscore)
1644 * If a device's parent goes into runtime suspend at the wrong time,
1645 * it won't be possible to resume the device. To prevent this we
1646 * block runtime suspend here, during the prepare phase, and allow
1647 * it again during the complete phase.
1649 pm_runtime_get_noresume(dev);
1653 dev->power.wakeup_path = device_may_wakeup(dev);
1655 if (dev->power.no_pm_callbacks) {
1656 ret = 1; /* Let device go direct_complete */
1661 callback = dev->pm_domain->ops.prepare;
1662 else if (dev->type && dev->type->pm)
1663 callback = dev->type->pm->prepare;
1664 else if (dev->class && dev->class->pm)
1665 callback = dev->class->pm->prepare;
1666 else if (dev->bus && dev->bus->pm)
1667 callback = dev->bus->pm->prepare;
1669 if (!callback && dev->driver && dev->driver->pm)
1670 callback = dev->driver->pm->prepare;
1673 ret = callback(dev);
1679 suspend_report_result(callback, ret);
1680 pm_runtime_put(dev);
1684 * A positive return value from ->prepare() means "this device appears
1685 * to be runtime-suspended and its state is fine, so if it really is
1686 * runtime-suspended, you can leave it in that state provided that you
1687 * will do the same thing with all of its descendants". This only
1688 * applies to suspend transitions, however.
1690 spin_lock_irq(&dev->power.lock);
1691 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1692 spin_unlock_irq(&dev->power.lock);
1697 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1698 * @state: PM transition of the system being carried out.
1700 * Execute the ->prepare() callback(s) for all devices.
1702 int dpm_prepare(pm_message_t state)
1706 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1710 * Give a chance for the known devices to complete their probes, before
1711 * disable probing of devices. This sync point is important at least
1712 * at boot time + hibernation restore.
1714 wait_for_device_probe();
1716 * It is unsafe if probing of devices will happen during suspend or
1717 * hibernation and system behavior will be unpredictable in this case.
1718 * So, let's prohibit device's probing here and defer their probes
1719 * instead. The normal behavior will be restored in dpm_complete().
1721 device_block_probing();
1723 mutex_lock(&dpm_list_mtx);
1724 while (!list_empty(&dpm_list)) {
1725 struct device *dev = to_device(dpm_list.next);
1728 mutex_unlock(&dpm_list_mtx);
1730 trace_device_pm_callback_start(dev, "", state.event);
1731 error = device_prepare(dev, state);
1732 trace_device_pm_callback_end(dev, error);
1734 mutex_lock(&dpm_list_mtx);
1736 if (error == -EAGAIN) {
1741 printk(KERN_INFO "PM: Device %s not prepared "
1742 "for power transition: code %d\n",
1743 dev_name(dev), error);
1747 dev->power.is_prepared = true;
1748 if (!list_empty(&dev->power.entry))
1749 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1752 mutex_unlock(&dpm_list_mtx);
1753 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
1758 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1759 * @state: PM transition of the system being carried out.
1761 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1762 * callbacks for them.
1764 int dpm_suspend_start(pm_message_t state)
1768 error = dpm_prepare(state);
1770 suspend_stats.failed_prepare++;
1771 dpm_save_failed_step(SUSPEND_PREPARE);
1773 error = dpm_suspend(state);
1776 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1778 void __suspend_report_result(const char *function, void *fn, int ret)
1781 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1783 EXPORT_SYMBOL_GPL(__suspend_report_result);
1786 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1787 * @dev: Device to wait for.
1788 * @subordinate: Device that needs to wait for @dev.
1790 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1792 dpm_wait(dev, subordinate->power.async_suspend);
1795 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1798 * dpm_for_each_dev - device iterator.
1799 * @data: data for the callback.
1800 * @fn: function to be called for each device.
1802 * Iterate over devices in dpm_list, and call @fn for each device,
1805 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1813 list_for_each_entry(dev, &dpm_list, power.entry)
1817 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
1819 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1824 return !ops->prepare &&
1826 !ops->suspend_late &&
1827 !ops->suspend_noirq &&
1828 !ops->resume_noirq &&
1829 !ops->resume_early &&
1834 void device_pm_check_callbacks(struct device *dev)
1836 spin_lock_irq(&dev->power.lock);
1837 dev->power.no_pm_callbacks =
1838 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1839 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1840 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1841 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1842 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1843 spin_unlock_irq(&dev->power.lock);