Linux 4.19.133
[linux/fpc-iii.git] / drivers / base / power / main.c
blob3b382a7e07b20dcf3d0b9dc95f8168b70ddc20ea
1 /*
2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/mutex.h>
23 #include <linux/pm.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pm-trace.h>
26 #include <linux/pm_wakeirq.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/sched/debug.h>
30 #include <linux/async.h>
31 #include <linux/suspend.h>
32 #include <trace/events/power.h>
33 #include <linux/cpufreq.h>
34 #include <linux/cpuidle.h>
35 #include <linux/timer.h>
37 #include "../base.h"
38 #include "power.h"
40 typedef int (*pm_callback_t)(struct device *);
43 * The entries in the dpm_list list are in a depth first order, simply
44 * because children are guaranteed to be discovered after parents, and
45 * are inserted at the back of the list on discovery.
47 * Since device_pm_add() may be called with a device lock held,
48 * we must never try to acquire a device lock while holding
49 * dpm_list_mutex.
52 LIST_HEAD(dpm_list);
53 static LIST_HEAD(dpm_prepared_list);
54 static LIST_HEAD(dpm_suspended_list);
55 static LIST_HEAD(dpm_late_early_list);
56 static LIST_HEAD(dpm_noirq_list);
58 struct suspend_stats suspend_stats;
59 static DEFINE_MUTEX(dpm_list_mtx);
60 static pm_message_t pm_transition;
62 static int async_error;
64 static const char *pm_verb(int event)
66 switch (event) {
67 case PM_EVENT_SUSPEND:
68 return "suspend";
69 case PM_EVENT_RESUME:
70 return "resume";
71 case PM_EVENT_FREEZE:
72 return "freeze";
73 case PM_EVENT_QUIESCE:
74 return "quiesce";
75 case PM_EVENT_HIBERNATE:
76 return "hibernate";
77 case PM_EVENT_THAW:
78 return "thaw";
79 case PM_EVENT_RESTORE:
80 return "restore";
81 case PM_EVENT_RECOVER:
82 return "recover";
83 default:
84 return "(unknown PM event)";
88 /**
89 * device_pm_sleep_init - Initialize system suspend-related device fields.
90 * @dev: Device object being initialized.
92 void device_pm_sleep_init(struct device *dev)
94 dev->power.is_prepared = false;
95 dev->power.is_suspended = false;
96 dev->power.is_noirq_suspended = false;
97 dev->power.is_late_suspended = false;
98 init_completion(&dev->power.completion);
99 complete_all(&dev->power.completion);
100 dev->power.wakeup = NULL;
101 INIT_LIST_HEAD(&dev->power.entry);
105 * device_pm_lock - Lock the list of active devices used by the PM core.
107 void device_pm_lock(void)
109 mutex_lock(&dpm_list_mtx);
113 * device_pm_unlock - Unlock the list of active devices used by the PM core.
115 void device_pm_unlock(void)
117 mutex_unlock(&dpm_list_mtx);
121 * device_pm_add - Add a device to the PM core's list of active devices.
122 * @dev: Device to add to the list.
124 void device_pm_add(struct device *dev)
126 pr_debug("PM: Adding info for %s:%s\n",
127 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
128 device_pm_check_callbacks(dev);
129 mutex_lock(&dpm_list_mtx);
130 if (dev->parent && dev->parent->power.is_prepared)
131 dev_warn(dev, "parent %s should not be sleeping\n",
132 dev_name(dev->parent));
133 list_add_tail(&dev->power.entry, &dpm_list);
134 dev->power.in_dpm_list = true;
135 mutex_unlock(&dpm_list_mtx);
139 * device_pm_remove - Remove a device from the PM core's list of active devices.
140 * @dev: Device to be removed from the list.
142 void device_pm_remove(struct device *dev)
144 pr_debug("PM: Removing info for %s:%s\n",
145 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
146 complete_all(&dev->power.completion);
147 mutex_lock(&dpm_list_mtx);
148 list_del_init(&dev->power.entry);
149 dev->power.in_dpm_list = false;
150 mutex_unlock(&dpm_list_mtx);
151 device_wakeup_disable(dev);
152 pm_runtime_remove(dev);
153 device_pm_check_callbacks(dev);
157 * device_pm_move_before - Move device in the PM core's list of active devices.
158 * @deva: Device to move in dpm_list.
159 * @devb: Device @deva should come before.
161 void device_pm_move_before(struct device *deva, struct device *devb)
163 pr_debug("PM: Moving %s:%s before %s:%s\n",
164 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
165 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
166 /* Delete deva from dpm_list and reinsert before devb. */
167 list_move_tail(&deva->power.entry, &devb->power.entry);
171 * device_pm_move_after - Move device in the PM core's list of active devices.
172 * @deva: Device to move in dpm_list.
173 * @devb: Device @deva should come after.
175 void device_pm_move_after(struct device *deva, struct device *devb)
177 pr_debug("PM: Moving %s:%s after %s:%s\n",
178 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
179 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
180 /* Delete deva from dpm_list and reinsert after devb. */
181 list_move(&deva->power.entry, &devb->power.entry);
185 * device_pm_move_last - Move device to end of the PM core's list of devices.
186 * @dev: Device to move in dpm_list.
188 void device_pm_move_last(struct device *dev)
190 pr_debug("PM: Moving %s:%s to end of list\n",
191 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
192 list_move_tail(&dev->power.entry, &dpm_list);
195 static ktime_t initcall_debug_start(struct device *dev, void *cb)
197 if (!pm_print_times_enabled)
198 return 0;
200 dev_info(dev, "calling %pF @ %i, parent: %s\n", cb,
201 task_pid_nr(current),
202 dev->parent ? dev_name(dev->parent) : "none");
203 return ktime_get();
206 static void initcall_debug_report(struct device *dev, ktime_t calltime,
207 void *cb, int error)
209 ktime_t rettime;
210 s64 nsecs;
212 if (!pm_print_times_enabled)
213 return;
215 rettime = ktime_get();
216 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
218 dev_info(dev, "%pF returned %d after %Ld usecs\n", cb, error,
219 (unsigned long long)nsecs >> 10);
223 * dpm_wait - Wait for a PM operation to complete.
224 * @dev: Device to wait for.
225 * @async: If unset, wait only if the device's power.async_suspend flag is set.
227 static void dpm_wait(struct device *dev, bool async)
229 if (!dev)
230 return;
232 if (async || (pm_async_enabled && dev->power.async_suspend))
233 wait_for_completion(&dev->power.completion);
236 static int dpm_wait_fn(struct device *dev, void *async_ptr)
238 dpm_wait(dev, *((bool *)async_ptr));
239 return 0;
242 static void dpm_wait_for_children(struct device *dev, bool async)
244 device_for_each_child(dev, &async, dpm_wait_fn);
247 static void dpm_wait_for_suppliers(struct device *dev, bool async)
249 struct device_link *link;
250 int idx;
252 idx = device_links_read_lock();
255 * If the supplier goes away right after we've checked the link to it,
256 * we'll wait for its completion to change the state, but that's fine,
257 * because the only things that will block as a result are the SRCU
258 * callbacks freeing the link objects for the links in the list we're
259 * walking.
261 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
262 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
263 dpm_wait(link->supplier, async);
265 device_links_read_unlock(idx);
268 static bool dpm_wait_for_superior(struct device *dev, bool async)
270 struct device *parent;
273 * If the device is resumed asynchronously and the parent's callback
274 * deletes both the device and the parent itself, the parent object may
275 * be freed while this function is running, so avoid that by reference
276 * counting the parent once more unless the device has been deleted
277 * already (in which case return right away).
279 mutex_lock(&dpm_list_mtx);
281 if (!device_pm_initialized(dev)) {
282 mutex_unlock(&dpm_list_mtx);
283 return false;
286 parent = get_device(dev->parent);
288 mutex_unlock(&dpm_list_mtx);
290 dpm_wait(parent, async);
291 put_device(parent);
293 dpm_wait_for_suppliers(dev, async);
296 * If the parent's callback has deleted the device, attempting to resume
297 * it would be invalid, so avoid doing that then.
299 return device_pm_initialized(dev);
302 static void dpm_wait_for_consumers(struct device *dev, bool async)
304 struct device_link *link;
305 int idx;
307 idx = device_links_read_lock();
310 * The status of a device link can only be changed from "dormant" by a
311 * probe, but that cannot happen during system suspend/resume. In
312 * theory it can change to "dormant" at that time, but then it is
313 * reasonable to wait for the target device anyway (eg. if it goes
314 * away, it's better to wait for it to go away completely and then
315 * continue instead of trying to continue in parallel with its
316 * unregistration).
318 list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
319 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
320 dpm_wait(link->consumer, async);
322 device_links_read_unlock(idx);
325 static void dpm_wait_for_subordinate(struct device *dev, bool async)
327 dpm_wait_for_children(dev, async);
328 dpm_wait_for_consumers(dev, async);
332 * pm_op - Return the PM operation appropriate for given PM event.
333 * @ops: PM operations to choose from.
334 * @state: PM transition of the system being carried out.
336 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
338 switch (state.event) {
339 #ifdef CONFIG_SUSPEND
340 case PM_EVENT_SUSPEND:
341 return ops->suspend;
342 case PM_EVENT_RESUME:
343 return ops->resume;
344 #endif /* CONFIG_SUSPEND */
345 #ifdef CONFIG_HIBERNATE_CALLBACKS
346 case PM_EVENT_FREEZE:
347 case PM_EVENT_QUIESCE:
348 return ops->freeze;
349 case PM_EVENT_HIBERNATE:
350 return ops->poweroff;
351 case PM_EVENT_THAW:
352 case PM_EVENT_RECOVER:
353 return ops->thaw;
354 break;
355 case PM_EVENT_RESTORE:
356 return ops->restore;
357 #endif /* CONFIG_HIBERNATE_CALLBACKS */
360 return NULL;
364 * pm_late_early_op - Return the PM operation appropriate for given PM event.
365 * @ops: PM operations to choose from.
366 * @state: PM transition of the system being carried out.
368 * Runtime PM is disabled for @dev while this function is being executed.
370 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
371 pm_message_t state)
373 switch (state.event) {
374 #ifdef CONFIG_SUSPEND
375 case PM_EVENT_SUSPEND:
376 return ops->suspend_late;
377 case PM_EVENT_RESUME:
378 return ops->resume_early;
379 #endif /* CONFIG_SUSPEND */
380 #ifdef CONFIG_HIBERNATE_CALLBACKS
381 case PM_EVENT_FREEZE:
382 case PM_EVENT_QUIESCE:
383 return ops->freeze_late;
384 case PM_EVENT_HIBERNATE:
385 return ops->poweroff_late;
386 case PM_EVENT_THAW:
387 case PM_EVENT_RECOVER:
388 return ops->thaw_early;
389 case PM_EVENT_RESTORE:
390 return ops->restore_early;
391 #endif /* CONFIG_HIBERNATE_CALLBACKS */
394 return NULL;
398 * pm_noirq_op - Return the PM operation appropriate for given PM event.
399 * @ops: PM operations to choose from.
400 * @state: PM transition of the system being carried out.
402 * The driver of @dev will not receive interrupts while this function is being
403 * executed.
405 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
407 switch (state.event) {
408 #ifdef CONFIG_SUSPEND
409 case PM_EVENT_SUSPEND:
410 return ops->suspend_noirq;
411 case PM_EVENT_RESUME:
412 return ops->resume_noirq;
413 #endif /* CONFIG_SUSPEND */
414 #ifdef CONFIG_HIBERNATE_CALLBACKS
415 case PM_EVENT_FREEZE:
416 case PM_EVENT_QUIESCE:
417 return ops->freeze_noirq;
418 case PM_EVENT_HIBERNATE:
419 return ops->poweroff_noirq;
420 case PM_EVENT_THAW:
421 case PM_EVENT_RECOVER:
422 return ops->thaw_noirq;
423 case PM_EVENT_RESTORE:
424 return ops->restore_noirq;
425 #endif /* CONFIG_HIBERNATE_CALLBACKS */
428 return NULL;
431 static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
433 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
434 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
435 ", may wakeup" : "");
438 static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
439 int error)
441 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
442 dev_name(dev), pm_verb(state.event), info, error);
445 static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
446 const char *info)
448 ktime_t calltime;
449 u64 usecs64;
450 int usecs;
452 calltime = ktime_get();
453 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
454 do_div(usecs64, NSEC_PER_USEC);
455 usecs = usecs64;
456 if (usecs == 0)
457 usecs = 1;
459 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
460 info ?: "", info ? " " : "", pm_verb(state.event),
461 error ? "aborted" : "complete",
462 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
465 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
466 pm_message_t state, const char *info)
468 ktime_t calltime;
469 int error;
471 if (!cb)
472 return 0;
474 calltime = initcall_debug_start(dev, cb);
476 pm_dev_dbg(dev, state, info);
477 trace_device_pm_callback_start(dev, info, state.event);
478 error = cb(dev);
479 trace_device_pm_callback_end(dev, error);
480 suspend_report_result(cb, error);
482 initcall_debug_report(dev, calltime, cb, error);
484 return error;
487 #ifdef CONFIG_DPM_WATCHDOG
488 struct dpm_watchdog {
489 struct device *dev;
490 struct task_struct *tsk;
491 struct timer_list timer;
494 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
495 struct dpm_watchdog wd
498 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
499 * @data: Watchdog object address.
501 * Called when a driver has timed out suspending or resuming.
502 * There's not much we can do here to recover so panic() to
503 * capture a crash-dump in pstore.
505 static void dpm_watchdog_handler(struct timer_list *t)
507 struct dpm_watchdog *wd = from_timer(wd, t, timer);
509 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
510 show_stack(wd->tsk, NULL);
511 panic("%s %s: unrecoverable failure\n",
512 dev_driver_string(wd->dev), dev_name(wd->dev));
516 * dpm_watchdog_set - Enable pm watchdog for given device.
517 * @wd: Watchdog. Must be allocated on the stack.
518 * @dev: Device to handle.
520 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
522 struct timer_list *timer = &wd->timer;
524 wd->dev = dev;
525 wd->tsk = current;
527 timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
528 /* use same timeout value for both suspend and resume */
529 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
530 add_timer(timer);
534 * dpm_watchdog_clear - Disable suspend/resume watchdog.
535 * @wd: Watchdog to disable.
537 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
539 struct timer_list *timer = &wd->timer;
541 del_timer_sync(timer);
542 destroy_timer_on_stack(timer);
544 #else
545 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
546 #define dpm_watchdog_set(x, y)
547 #define dpm_watchdog_clear(x)
548 #endif
550 /*------------------------- Resume routines -------------------------*/
553 * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
554 * @dev: Target device.
556 * Make the core skip the "early resume" and "resume" phases for @dev.
558 * This function can be called by middle-layer code during the "noirq" phase of
559 * system resume if necessary, but not by device drivers.
561 void dev_pm_skip_next_resume_phases(struct device *dev)
563 dev->power.is_late_suspended = false;
564 dev->power.is_suspended = false;
568 * suspend_event - Return a "suspend" message for given "resume" one.
569 * @resume_msg: PM message representing a system-wide resume transition.
571 static pm_message_t suspend_event(pm_message_t resume_msg)
573 switch (resume_msg.event) {
574 case PM_EVENT_RESUME:
575 return PMSG_SUSPEND;
576 case PM_EVENT_THAW:
577 case PM_EVENT_RESTORE:
578 return PMSG_FREEZE;
579 case PM_EVENT_RECOVER:
580 return PMSG_HIBERNATE;
582 return PMSG_ON;
586 * dev_pm_may_skip_resume - System-wide device resume optimization check.
587 * @dev: Target device.
589 * Checks whether or not the device may be left in suspend after a system-wide
590 * transition to the working state.
592 bool dev_pm_may_skip_resume(struct device *dev)
594 return !dev->power.must_resume && pm_transition.event != PM_EVENT_RESTORE;
597 static pm_callback_t dpm_subsys_resume_noirq_cb(struct device *dev,
598 pm_message_t state,
599 const char **info_p)
601 pm_callback_t callback;
602 const char *info;
604 if (dev->pm_domain) {
605 info = "noirq power domain ";
606 callback = pm_noirq_op(&dev->pm_domain->ops, state);
607 } else if (dev->type && dev->type->pm) {
608 info = "noirq type ";
609 callback = pm_noirq_op(dev->type->pm, state);
610 } else if (dev->class && dev->class->pm) {
611 info = "noirq class ";
612 callback = pm_noirq_op(dev->class->pm, state);
613 } else if (dev->bus && dev->bus->pm) {
614 info = "noirq bus ";
615 callback = pm_noirq_op(dev->bus->pm, state);
616 } else {
617 return NULL;
620 if (info_p)
621 *info_p = info;
623 return callback;
626 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
627 pm_message_t state,
628 const char **info_p);
630 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
631 pm_message_t state,
632 const char **info_p);
635 * device_resume_noirq - Execute a "noirq resume" callback for given device.
636 * @dev: Device to handle.
637 * @state: PM transition of the system being carried out.
638 * @async: If true, the device is being resumed asynchronously.
640 * The driver of @dev will not receive interrupts while this function is being
641 * executed.
643 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
645 pm_callback_t callback;
646 const char *info;
647 bool skip_resume;
648 int error = 0;
650 TRACE_DEVICE(dev);
651 TRACE_RESUME(0);
653 if (dev->power.syscore || dev->power.direct_complete)
654 goto Out;
656 if (!dev->power.is_noirq_suspended)
657 goto Out;
659 if (!dpm_wait_for_superior(dev, async))
660 goto Out;
662 skip_resume = dev_pm_may_skip_resume(dev);
664 callback = dpm_subsys_resume_noirq_cb(dev, state, &info);
665 if (callback)
666 goto Run;
668 if (skip_resume)
669 goto Skip;
671 if (dev_pm_smart_suspend_and_suspended(dev)) {
672 pm_message_t suspend_msg = suspend_event(state);
675 * If "freeze" callbacks have been skipped during a transition
676 * related to hibernation, the subsequent "thaw" callbacks must
677 * be skipped too or bad things may happen. Otherwise, resume
678 * callbacks are going to be run for the device, so its runtime
679 * PM status must be changed to reflect the new state after the
680 * transition under way.
682 if (!dpm_subsys_suspend_late_cb(dev, suspend_msg, NULL) &&
683 !dpm_subsys_suspend_noirq_cb(dev, suspend_msg, NULL)) {
684 if (state.event == PM_EVENT_THAW) {
685 skip_resume = true;
686 goto Skip;
687 } else {
688 pm_runtime_set_active(dev);
693 if (dev->driver && dev->driver->pm) {
694 info = "noirq driver ";
695 callback = pm_noirq_op(dev->driver->pm, state);
698 Run:
699 error = dpm_run_callback(callback, dev, state, info);
701 Skip:
702 dev->power.is_noirq_suspended = false;
704 if (skip_resume) {
706 * The device is going to be left in suspend, but it might not
707 * have been in runtime suspend before the system suspended, so
708 * its runtime PM status needs to be updated to avoid confusing
709 * the runtime PM framework when runtime PM is enabled for the
710 * device again.
712 pm_runtime_set_suspended(dev);
713 dev_pm_skip_next_resume_phases(dev);
716 Out:
717 complete_all(&dev->power.completion);
718 TRACE_RESUME(error);
719 return error;
722 static bool is_async(struct device *dev)
724 return dev->power.async_suspend && pm_async_enabled
725 && !pm_trace_is_enabled();
728 static void async_resume_noirq(void *data, async_cookie_t cookie)
730 struct device *dev = (struct device *)data;
731 int error;
733 error = device_resume_noirq(dev, pm_transition, true);
734 if (error)
735 pm_dev_err(dev, pm_transition, " async", error);
737 put_device(dev);
740 void dpm_noirq_resume_devices(pm_message_t state)
742 struct device *dev;
743 ktime_t starttime = ktime_get();
745 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
746 mutex_lock(&dpm_list_mtx);
747 pm_transition = state;
750 * Advanced the async threads upfront,
751 * in case the starting of async threads is
752 * delayed by non-async resuming devices.
754 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
755 reinit_completion(&dev->power.completion);
756 if (is_async(dev)) {
757 get_device(dev);
758 async_schedule(async_resume_noirq, dev);
762 while (!list_empty(&dpm_noirq_list)) {
763 dev = to_device(dpm_noirq_list.next);
764 get_device(dev);
765 list_move_tail(&dev->power.entry, &dpm_late_early_list);
766 mutex_unlock(&dpm_list_mtx);
768 if (!is_async(dev)) {
769 int error;
771 error = device_resume_noirq(dev, state, false);
772 if (error) {
773 suspend_stats.failed_resume_noirq++;
774 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
775 dpm_save_failed_dev(dev_name(dev));
776 pm_dev_err(dev, state, " noirq", error);
780 mutex_lock(&dpm_list_mtx);
781 put_device(dev);
783 mutex_unlock(&dpm_list_mtx);
784 async_synchronize_full();
785 dpm_show_time(starttime, state, 0, "noirq");
786 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
789 void dpm_noirq_end(void)
791 resume_device_irqs();
792 device_wakeup_disarm_wake_irqs();
793 cpuidle_resume();
797 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
798 * @state: PM transition of the system being carried out.
800 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
801 * allow device drivers' interrupt handlers to be called.
803 void dpm_resume_noirq(pm_message_t state)
805 dpm_noirq_resume_devices(state);
806 dpm_noirq_end();
809 static pm_callback_t dpm_subsys_resume_early_cb(struct device *dev,
810 pm_message_t state,
811 const char **info_p)
813 pm_callback_t callback;
814 const char *info;
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) {
826 info = "early bus ";
827 callback = pm_late_early_op(dev->bus->pm, state);
828 } else {
829 return NULL;
832 if (info_p)
833 *info_p = info;
835 return callback;
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;
849 const char *info;
850 int error = 0;
852 TRACE_DEVICE(dev);
853 TRACE_RESUME(0);
855 if (dev->power.syscore || dev->power.direct_complete)
856 goto Out;
858 if (!dev->power.is_late_suspended)
859 goto Out;
861 if (!dpm_wait_for_superior(dev, async))
862 goto Out;
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;
874 Out:
875 TRACE_RESUME(error);
877 pm_runtime_enable(dev);
878 complete_all(&dev->power.completion);
879 return error;
882 static void async_resume_early(void *data, async_cookie_t cookie)
884 struct device *dev = (struct device *)data;
885 int error;
887 error = device_resume_early(dev, pm_transition, true);
888 if (error)
889 pm_dev_err(dev, pm_transition, " async", error);
891 put_device(dev);
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)
900 struct device *dev;
901 ktime_t starttime = ktime_get();
903 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
904 mutex_lock(&dpm_list_mtx);
905 pm_transition = state;
908 * Advanced the async threads upfront,
909 * in case the starting of async threads is
910 * delayed by non-async resuming devices.
912 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
913 reinit_completion(&dev->power.completion);
914 if (is_async(dev)) {
915 get_device(dev);
916 async_schedule(async_resume_early, dev);
920 while (!list_empty(&dpm_late_early_list)) {
921 dev = to_device(dpm_late_early_list.next);
922 get_device(dev);
923 list_move_tail(&dev->power.entry, &dpm_suspended_list);
924 mutex_unlock(&dpm_list_mtx);
926 if (!is_async(dev)) {
927 int error;
929 error = device_resume_early(dev, state, false);
930 if (error) {
931 suspend_stats.failed_resume_early++;
932 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
933 dpm_save_failed_dev(dev_name(dev));
934 pm_dev_err(dev, state, " early", error);
937 mutex_lock(&dpm_list_mtx);
938 put_device(dev);
940 mutex_unlock(&dpm_list_mtx);
941 async_synchronize_full();
942 dpm_show_time(starttime, state, 0, "early");
943 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
947 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
948 * @state: PM transition of the system being carried out.
950 void dpm_resume_start(pm_message_t state)
952 dpm_resume_noirq(state);
953 dpm_resume_early(state);
955 EXPORT_SYMBOL_GPL(dpm_resume_start);
958 * device_resume - Execute "resume" callbacks for given device.
959 * @dev: Device to handle.
960 * @state: PM transition of the system being carried out.
961 * @async: If true, the device is being resumed asynchronously.
963 static int device_resume(struct device *dev, pm_message_t state, bool async)
965 pm_callback_t callback = NULL;
966 const char *info = NULL;
967 int error = 0;
968 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
970 TRACE_DEVICE(dev);
971 TRACE_RESUME(0);
973 if (dev->power.syscore)
974 goto Complete;
976 if (dev->power.direct_complete) {
977 /* Match the pm_runtime_disable() in __device_suspend(). */
978 pm_runtime_enable(dev);
979 goto Complete;
982 if (!dpm_wait_for_superior(dev, async))
983 goto Complete;
985 dpm_watchdog_set(&wd, dev);
986 device_lock(dev);
989 * This is a fib. But we'll allow new children to be added below
990 * a resumed device, even if the device hasn't been completed yet.
992 dev->power.is_prepared = false;
994 if (!dev->power.is_suspended)
995 goto Unlock;
997 if (dev->pm_domain) {
998 info = "power domain ";
999 callback = pm_op(&dev->pm_domain->ops, state);
1000 goto Driver;
1003 if (dev->type && dev->type->pm) {
1004 info = "type ";
1005 callback = pm_op(dev->type->pm, state);
1006 goto Driver;
1009 if (dev->class && dev->class->pm) {
1010 info = "class ";
1011 callback = pm_op(dev->class->pm, state);
1012 goto Driver;
1015 if (dev->bus) {
1016 if (dev->bus->pm) {
1017 info = "bus ";
1018 callback = pm_op(dev->bus->pm, state);
1019 } else if (dev->bus->resume) {
1020 info = "legacy bus ";
1021 callback = dev->bus->resume;
1022 goto End;
1026 Driver:
1027 if (!callback && dev->driver && dev->driver->pm) {
1028 info = "driver ";
1029 callback = pm_op(dev->driver->pm, state);
1032 End:
1033 error = dpm_run_callback(callback, dev, state, info);
1034 dev->power.is_suspended = false;
1036 Unlock:
1037 device_unlock(dev);
1038 dpm_watchdog_clear(&wd);
1040 Complete:
1041 complete_all(&dev->power.completion);
1043 TRACE_RESUME(error);
1045 return error;
1048 static void async_resume(void *data, async_cookie_t cookie)
1050 struct device *dev = (struct device *)data;
1051 int error;
1053 error = device_resume(dev, pm_transition, true);
1054 if (error)
1055 pm_dev_err(dev, pm_transition, " async", error);
1056 put_device(dev);
1060 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1061 * @state: PM transition of the system being carried out.
1063 * Execute the appropriate "resume" callback for all devices whose status
1064 * indicates that they are suspended.
1066 void dpm_resume(pm_message_t state)
1068 struct device *dev;
1069 ktime_t starttime = ktime_get();
1071 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
1072 might_sleep();
1074 mutex_lock(&dpm_list_mtx);
1075 pm_transition = state;
1076 async_error = 0;
1078 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
1079 reinit_completion(&dev->power.completion);
1080 if (is_async(dev)) {
1081 get_device(dev);
1082 async_schedule(async_resume, dev);
1086 while (!list_empty(&dpm_suspended_list)) {
1087 dev = to_device(dpm_suspended_list.next);
1088 get_device(dev);
1089 if (!is_async(dev)) {
1090 int error;
1092 mutex_unlock(&dpm_list_mtx);
1094 error = device_resume(dev, state, false);
1095 if (error) {
1096 suspend_stats.failed_resume++;
1097 dpm_save_failed_step(SUSPEND_RESUME);
1098 dpm_save_failed_dev(dev_name(dev));
1099 pm_dev_err(dev, state, "", error);
1102 mutex_lock(&dpm_list_mtx);
1104 if (!list_empty(&dev->power.entry))
1105 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1106 put_device(dev);
1108 mutex_unlock(&dpm_list_mtx);
1109 async_synchronize_full();
1110 dpm_show_time(starttime, state, 0, NULL);
1112 cpufreq_resume();
1113 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
1117 * device_complete - Complete a PM transition for given device.
1118 * @dev: Device to handle.
1119 * @state: PM transition of the system being carried out.
1121 static void device_complete(struct device *dev, pm_message_t state)
1123 void (*callback)(struct device *) = NULL;
1124 const char *info = NULL;
1126 if (dev->power.syscore)
1127 return;
1129 device_lock(dev);
1131 if (dev->pm_domain) {
1132 info = "completing power domain ";
1133 callback = dev->pm_domain->ops.complete;
1134 } else if (dev->type && dev->type->pm) {
1135 info = "completing type ";
1136 callback = dev->type->pm->complete;
1137 } else if (dev->class && dev->class->pm) {
1138 info = "completing class ";
1139 callback = dev->class->pm->complete;
1140 } else if (dev->bus && dev->bus->pm) {
1141 info = "completing bus ";
1142 callback = dev->bus->pm->complete;
1145 if (!callback && dev->driver && dev->driver->pm) {
1146 info = "completing driver ";
1147 callback = dev->driver->pm->complete;
1150 if (callback) {
1151 pm_dev_dbg(dev, state, info);
1152 callback(dev);
1155 device_unlock(dev);
1157 pm_runtime_put(dev);
1161 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1162 * @state: PM transition of the system being carried out.
1164 * Execute the ->complete() callbacks for all devices whose PM status is not
1165 * DPM_ON (this allows new devices to be registered).
1167 void dpm_complete(pm_message_t state)
1169 struct list_head list;
1171 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
1172 might_sleep();
1174 INIT_LIST_HEAD(&list);
1175 mutex_lock(&dpm_list_mtx);
1176 while (!list_empty(&dpm_prepared_list)) {
1177 struct device *dev = to_device(dpm_prepared_list.prev);
1179 get_device(dev);
1180 dev->power.is_prepared = false;
1181 list_move(&dev->power.entry, &list);
1182 mutex_unlock(&dpm_list_mtx);
1184 trace_device_pm_callback_start(dev, "", state.event);
1185 device_complete(dev, state);
1186 trace_device_pm_callback_end(dev, 0);
1188 mutex_lock(&dpm_list_mtx);
1189 put_device(dev);
1191 list_splice(&list, &dpm_list);
1192 mutex_unlock(&dpm_list_mtx);
1194 /* Allow device probing and trigger re-probing of deferred devices */
1195 device_unblock_probing();
1196 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1200 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1201 * @state: PM transition of the system being carried out.
1203 * Execute "resume" callbacks for all devices and complete the PM transition of
1204 * the system.
1206 void dpm_resume_end(pm_message_t state)
1208 dpm_resume(state);
1209 dpm_complete(state);
1211 EXPORT_SYMBOL_GPL(dpm_resume_end);
1214 /*------------------------- Suspend routines -------------------------*/
1217 * resume_event - Return a "resume" message for given "suspend" sleep state.
1218 * @sleep_state: PM message representing a sleep state.
1220 * Return a PM message representing the resume event corresponding to given
1221 * sleep state.
1223 static pm_message_t resume_event(pm_message_t sleep_state)
1225 switch (sleep_state.event) {
1226 case PM_EVENT_SUSPEND:
1227 return PMSG_RESUME;
1228 case PM_EVENT_FREEZE:
1229 case PM_EVENT_QUIESCE:
1230 return PMSG_RECOVER;
1231 case PM_EVENT_HIBERNATE:
1232 return PMSG_RESTORE;
1234 return PMSG_ON;
1237 static void dpm_superior_set_must_resume(struct device *dev)
1239 struct device_link *link;
1240 int idx;
1242 if (dev->parent)
1243 dev->parent->power.must_resume = true;
1245 idx = device_links_read_lock();
1247 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
1248 link->supplier->power.must_resume = true;
1250 device_links_read_unlock(idx);
1253 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
1254 pm_message_t state,
1255 const char **info_p)
1257 pm_callback_t callback;
1258 const char *info;
1260 if (dev->pm_domain) {
1261 info = "noirq power domain ";
1262 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1263 } else if (dev->type && dev->type->pm) {
1264 info = "noirq type ";
1265 callback = pm_noirq_op(dev->type->pm, state);
1266 } else if (dev->class && dev->class->pm) {
1267 info = "noirq class ";
1268 callback = pm_noirq_op(dev->class->pm, state);
1269 } else if (dev->bus && dev->bus->pm) {
1270 info = "noirq bus ";
1271 callback = pm_noirq_op(dev->bus->pm, state);
1272 } else {
1273 return NULL;
1276 if (info_p)
1277 *info_p = info;
1279 return callback;
1282 static bool device_must_resume(struct device *dev, pm_message_t state,
1283 bool no_subsys_suspend_noirq)
1285 pm_message_t resume_msg = resume_event(state);
1288 * If all of the device driver's "noirq", "late" and "early" callbacks
1289 * are invoked directly by the core, the decision to allow the device to
1290 * stay in suspend can be based on its current runtime PM status and its
1291 * wakeup settings.
1293 if (no_subsys_suspend_noirq &&
1294 !dpm_subsys_suspend_late_cb(dev, state, NULL) &&
1295 !dpm_subsys_resume_early_cb(dev, resume_msg, NULL) &&
1296 !dpm_subsys_resume_noirq_cb(dev, resume_msg, NULL))
1297 return !pm_runtime_status_suspended(dev) &&
1298 (resume_msg.event != PM_EVENT_RESUME ||
1299 (device_can_wakeup(dev) && !device_may_wakeup(dev)));
1302 * The only safe strategy here is to require that if the device may not
1303 * be left in suspend, resume callbacks must be invoked for it.
1305 return !dev->power.may_skip_resume;
1309 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1310 * @dev: Device to handle.
1311 * @state: PM transition of the system being carried out.
1312 * @async: If true, the device is being suspended asynchronously.
1314 * The driver of @dev will not receive interrupts while this function is being
1315 * executed.
1317 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1319 pm_callback_t callback;
1320 const char *info;
1321 bool no_subsys_cb = false;
1322 int error = 0;
1324 TRACE_DEVICE(dev);
1325 TRACE_SUSPEND(0);
1327 dpm_wait_for_subordinate(dev, async);
1329 if (async_error)
1330 goto Complete;
1332 if (pm_wakeup_pending()) {
1333 async_error = -EBUSY;
1334 goto Complete;
1337 if (dev->power.syscore || dev->power.direct_complete)
1338 goto Complete;
1340 callback = dpm_subsys_suspend_noirq_cb(dev, state, &info);
1341 if (callback)
1342 goto Run;
1344 no_subsys_cb = !dpm_subsys_suspend_late_cb(dev, state, NULL);
1346 if (dev_pm_smart_suspend_and_suspended(dev) && no_subsys_cb)
1347 goto Skip;
1349 if (dev->driver && dev->driver->pm) {
1350 info = "noirq driver ";
1351 callback = pm_noirq_op(dev->driver->pm, state);
1354 Run:
1355 error = dpm_run_callback(callback, dev, state, info);
1356 if (error) {
1357 async_error = error;
1358 goto Complete;
1361 Skip:
1362 dev->power.is_noirq_suspended = true;
1364 if (dev_pm_test_driver_flags(dev, DPM_FLAG_LEAVE_SUSPENDED)) {
1365 dev->power.must_resume = dev->power.must_resume ||
1366 atomic_read(&dev->power.usage_count) > 1 ||
1367 device_must_resume(dev, state, no_subsys_cb);
1368 } else {
1369 dev->power.must_resume = true;
1372 if (dev->power.must_resume)
1373 dpm_superior_set_must_resume(dev);
1375 Complete:
1376 complete_all(&dev->power.completion);
1377 TRACE_SUSPEND(error);
1378 return error;
1381 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1383 struct device *dev = (struct device *)data;
1384 int error;
1386 error = __device_suspend_noirq(dev, pm_transition, true);
1387 if (error) {
1388 dpm_save_failed_dev(dev_name(dev));
1389 pm_dev_err(dev, pm_transition, " async", error);
1392 put_device(dev);
1395 static int device_suspend_noirq(struct device *dev)
1397 reinit_completion(&dev->power.completion);
1399 if (is_async(dev)) {
1400 get_device(dev);
1401 async_schedule(async_suspend_noirq, dev);
1402 return 0;
1404 return __device_suspend_noirq(dev, pm_transition, false);
1407 void dpm_noirq_begin(void)
1409 cpuidle_pause();
1410 device_wakeup_arm_wake_irqs();
1411 suspend_device_irqs();
1414 int dpm_noirq_suspend_devices(pm_message_t state)
1416 ktime_t starttime = ktime_get();
1417 int error = 0;
1419 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1420 mutex_lock(&dpm_list_mtx);
1421 pm_transition = state;
1422 async_error = 0;
1424 while (!list_empty(&dpm_late_early_list)) {
1425 struct device *dev = to_device(dpm_late_early_list.prev);
1427 get_device(dev);
1428 mutex_unlock(&dpm_list_mtx);
1430 error = device_suspend_noirq(dev);
1432 mutex_lock(&dpm_list_mtx);
1433 if (error) {
1434 pm_dev_err(dev, state, " noirq", error);
1435 dpm_save_failed_dev(dev_name(dev));
1436 put_device(dev);
1437 break;
1439 if (!list_empty(&dev->power.entry))
1440 list_move(&dev->power.entry, &dpm_noirq_list);
1441 put_device(dev);
1443 if (async_error)
1444 break;
1446 mutex_unlock(&dpm_list_mtx);
1447 async_synchronize_full();
1448 if (!error)
1449 error = async_error;
1451 if (error) {
1452 suspend_stats.failed_suspend_noirq++;
1453 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1455 dpm_show_time(starttime, state, error, "noirq");
1456 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1457 return error;
1461 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1462 * @state: PM transition of the system being carried out.
1464 * Prevent device drivers' interrupt handlers from being called and invoke
1465 * "noirq" suspend callbacks for all non-sysdev devices.
1467 int dpm_suspend_noirq(pm_message_t state)
1469 int ret;
1471 dpm_noirq_begin();
1472 ret = dpm_noirq_suspend_devices(state);
1473 if (ret)
1474 dpm_resume_noirq(resume_event(state));
1476 return ret;
1479 static void dpm_propagate_wakeup_to_parent(struct device *dev)
1481 struct device *parent = dev->parent;
1483 if (!parent)
1484 return;
1486 spin_lock_irq(&parent->power.lock);
1488 if (dev->power.wakeup_path && !parent->power.ignore_children)
1489 parent->power.wakeup_path = true;
1491 spin_unlock_irq(&parent->power.lock);
1494 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
1495 pm_message_t state,
1496 const char **info_p)
1498 pm_callback_t callback;
1499 const char *info;
1501 if (dev->pm_domain) {
1502 info = "late power domain ";
1503 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1504 } else if (dev->type && dev->type->pm) {
1505 info = "late type ";
1506 callback = pm_late_early_op(dev->type->pm, state);
1507 } else if (dev->class && dev->class->pm) {
1508 info = "late class ";
1509 callback = pm_late_early_op(dev->class->pm, state);
1510 } else if (dev->bus && dev->bus->pm) {
1511 info = "late bus ";
1512 callback = pm_late_early_op(dev->bus->pm, state);
1513 } else {
1514 return NULL;
1517 if (info_p)
1518 *info_p = info;
1520 return callback;
1524 * __device_suspend_late - Execute a "late suspend" callback for given device.
1525 * @dev: Device to handle.
1526 * @state: PM transition of the system being carried out.
1527 * @async: If true, the device is being suspended asynchronously.
1529 * Runtime PM is disabled for @dev while this function is being executed.
1531 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1533 pm_callback_t callback;
1534 const char *info;
1535 int error = 0;
1537 TRACE_DEVICE(dev);
1538 TRACE_SUSPEND(0);
1540 __pm_runtime_disable(dev, false);
1542 dpm_wait_for_subordinate(dev, async);
1544 if (async_error)
1545 goto Complete;
1547 if (pm_wakeup_pending()) {
1548 async_error = -EBUSY;
1549 goto Complete;
1552 if (dev->power.syscore || dev->power.direct_complete)
1553 goto Complete;
1555 callback = dpm_subsys_suspend_late_cb(dev, state, &info);
1556 if (callback)
1557 goto Run;
1559 if (dev_pm_smart_suspend_and_suspended(dev) &&
1560 !dpm_subsys_suspend_noirq_cb(dev, state, NULL))
1561 goto Skip;
1563 if (dev->driver && dev->driver->pm) {
1564 info = "late driver ";
1565 callback = pm_late_early_op(dev->driver->pm, state);
1568 Run:
1569 error = dpm_run_callback(callback, dev, state, info);
1570 if (error) {
1571 async_error = error;
1572 goto Complete;
1574 dpm_propagate_wakeup_to_parent(dev);
1576 Skip:
1577 dev->power.is_late_suspended = true;
1579 Complete:
1580 TRACE_SUSPEND(error);
1581 complete_all(&dev->power.completion);
1582 return error;
1585 static void async_suspend_late(void *data, async_cookie_t cookie)
1587 struct device *dev = (struct device *)data;
1588 int error;
1590 error = __device_suspend_late(dev, pm_transition, true);
1591 if (error) {
1592 dpm_save_failed_dev(dev_name(dev));
1593 pm_dev_err(dev, pm_transition, " async", error);
1595 put_device(dev);
1598 static int device_suspend_late(struct device *dev)
1600 reinit_completion(&dev->power.completion);
1602 if (is_async(dev)) {
1603 get_device(dev);
1604 async_schedule(async_suspend_late, dev);
1605 return 0;
1608 return __device_suspend_late(dev, pm_transition, false);
1612 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1613 * @state: PM transition of the system being carried out.
1615 int dpm_suspend_late(pm_message_t state)
1617 ktime_t starttime = ktime_get();
1618 int error = 0;
1620 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1621 mutex_lock(&dpm_list_mtx);
1622 pm_transition = state;
1623 async_error = 0;
1625 while (!list_empty(&dpm_suspended_list)) {
1626 struct device *dev = to_device(dpm_suspended_list.prev);
1628 get_device(dev);
1629 mutex_unlock(&dpm_list_mtx);
1631 error = device_suspend_late(dev);
1633 mutex_lock(&dpm_list_mtx);
1634 if (!list_empty(&dev->power.entry))
1635 list_move(&dev->power.entry, &dpm_late_early_list);
1637 if (error) {
1638 pm_dev_err(dev, state, " late", error);
1639 dpm_save_failed_dev(dev_name(dev));
1640 put_device(dev);
1641 break;
1643 put_device(dev);
1645 if (async_error)
1646 break;
1648 mutex_unlock(&dpm_list_mtx);
1649 async_synchronize_full();
1650 if (!error)
1651 error = async_error;
1652 if (error) {
1653 suspend_stats.failed_suspend_late++;
1654 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1655 dpm_resume_early(resume_event(state));
1657 dpm_show_time(starttime, state, error, "late");
1658 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1659 return error;
1663 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1664 * @state: PM transition of the system being carried out.
1666 int dpm_suspend_end(pm_message_t state)
1668 int error = dpm_suspend_late(state);
1669 if (error)
1670 return error;
1672 error = dpm_suspend_noirq(state);
1673 if (error) {
1674 dpm_resume_early(resume_event(state));
1675 return error;
1678 return 0;
1680 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1683 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1684 * @dev: Device to suspend.
1685 * @state: PM transition of the system being carried out.
1686 * @cb: Suspend callback to execute.
1687 * @info: string description of caller.
1689 static int legacy_suspend(struct device *dev, pm_message_t state,
1690 int (*cb)(struct device *dev, pm_message_t state),
1691 const char *info)
1693 int error;
1694 ktime_t calltime;
1696 calltime = initcall_debug_start(dev, cb);
1698 trace_device_pm_callback_start(dev, info, state.event);
1699 error = cb(dev, state);
1700 trace_device_pm_callback_end(dev, error);
1701 suspend_report_result(cb, error);
1703 initcall_debug_report(dev, calltime, cb, error);
1705 return error;
1708 static void dpm_clear_superiors_direct_complete(struct device *dev)
1710 struct device_link *link;
1711 int idx;
1713 if (dev->parent) {
1714 spin_lock_irq(&dev->parent->power.lock);
1715 dev->parent->power.direct_complete = false;
1716 spin_unlock_irq(&dev->parent->power.lock);
1719 idx = device_links_read_lock();
1721 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1722 spin_lock_irq(&link->supplier->power.lock);
1723 link->supplier->power.direct_complete = false;
1724 spin_unlock_irq(&link->supplier->power.lock);
1727 device_links_read_unlock(idx);
1731 * __device_suspend - Execute "suspend" callbacks for given device.
1732 * @dev: Device to handle.
1733 * @state: PM transition of the system being carried out.
1734 * @async: If true, the device is being suspended asynchronously.
1736 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1738 pm_callback_t callback = NULL;
1739 const char *info = NULL;
1740 int error = 0;
1741 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1743 TRACE_DEVICE(dev);
1744 TRACE_SUSPEND(0);
1746 dpm_wait_for_subordinate(dev, async);
1748 if (async_error) {
1749 dev->power.direct_complete = false;
1750 goto Complete;
1754 * If a device configured to wake up the system from sleep states
1755 * has been suspended at run time and there's a resume request pending
1756 * for it, this is equivalent to the device signaling wakeup, so the
1757 * system suspend operation should be aborted.
1759 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1760 pm_wakeup_event(dev, 0);
1762 if (pm_wakeup_pending()) {
1763 dev->power.direct_complete = false;
1764 async_error = -EBUSY;
1765 goto Complete;
1768 if (dev->power.syscore)
1769 goto Complete;
1771 /* Avoid direct_complete to let wakeup_path propagate. */
1772 if (device_may_wakeup(dev) || dev->power.wakeup_path)
1773 dev->power.direct_complete = false;
1775 if (dev->power.direct_complete) {
1776 if (pm_runtime_status_suspended(dev)) {
1777 pm_runtime_disable(dev);
1778 if (pm_runtime_status_suspended(dev))
1779 goto Complete;
1781 pm_runtime_enable(dev);
1783 dev->power.direct_complete = false;
1786 dev->power.may_skip_resume = false;
1787 dev->power.must_resume = false;
1789 dpm_watchdog_set(&wd, dev);
1790 device_lock(dev);
1792 if (dev->pm_domain) {
1793 info = "power domain ";
1794 callback = pm_op(&dev->pm_domain->ops, state);
1795 goto Run;
1798 if (dev->type && dev->type->pm) {
1799 info = "type ";
1800 callback = pm_op(dev->type->pm, state);
1801 goto Run;
1804 if (dev->class && dev->class->pm) {
1805 info = "class ";
1806 callback = pm_op(dev->class->pm, state);
1807 goto Run;
1810 if (dev->bus) {
1811 if (dev->bus->pm) {
1812 info = "bus ";
1813 callback = pm_op(dev->bus->pm, state);
1814 } else if (dev->bus->suspend) {
1815 pm_dev_dbg(dev, state, "legacy bus ");
1816 error = legacy_suspend(dev, state, dev->bus->suspend,
1817 "legacy bus ");
1818 goto End;
1822 Run:
1823 if (!callback && dev->driver && dev->driver->pm) {
1824 info = "driver ";
1825 callback = pm_op(dev->driver->pm, state);
1828 error = dpm_run_callback(callback, dev, state, info);
1830 End:
1831 if (!error) {
1832 dev->power.is_suspended = true;
1833 if (device_may_wakeup(dev))
1834 dev->power.wakeup_path = true;
1836 dpm_propagate_wakeup_to_parent(dev);
1837 dpm_clear_superiors_direct_complete(dev);
1840 device_unlock(dev);
1841 dpm_watchdog_clear(&wd);
1843 Complete:
1844 if (error)
1845 async_error = error;
1847 complete_all(&dev->power.completion);
1848 TRACE_SUSPEND(error);
1849 return error;
1852 static void async_suspend(void *data, async_cookie_t cookie)
1854 struct device *dev = (struct device *)data;
1855 int error;
1857 error = __device_suspend(dev, pm_transition, true);
1858 if (error) {
1859 dpm_save_failed_dev(dev_name(dev));
1860 pm_dev_err(dev, pm_transition, " async", error);
1863 put_device(dev);
1866 static int device_suspend(struct device *dev)
1868 reinit_completion(&dev->power.completion);
1870 if (is_async(dev)) {
1871 get_device(dev);
1872 async_schedule(async_suspend, dev);
1873 return 0;
1876 return __device_suspend(dev, pm_transition, false);
1880 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1881 * @state: PM transition of the system being carried out.
1883 int dpm_suspend(pm_message_t state)
1885 ktime_t starttime = ktime_get();
1886 int error = 0;
1888 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1889 might_sleep();
1891 cpufreq_suspend();
1893 mutex_lock(&dpm_list_mtx);
1894 pm_transition = state;
1895 async_error = 0;
1896 while (!list_empty(&dpm_prepared_list)) {
1897 struct device *dev = to_device(dpm_prepared_list.prev);
1899 get_device(dev);
1900 mutex_unlock(&dpm_list_mtx);
1902 error = device_suspend(dev);
1904 mutex_lock(&dpm_list_mtx);
1905 if (error) {
1906 pm_dev_err(dev, state, "", error);
1907 dpm_save_failed_dev(dev_name(dev));
1908 put_device(dev);
1909 break;
1911 if (!list_empty(&dev->power.entry))
1912 list_move(&dev->power.entry, &dpm_suspended_list);
1913 put_device(dev);
1914 if (async_error)
1915 break;
1917 mutex_unlock(&dpm_list_mtx);
1918 async_synchronize_full();
1919 if (!error)
1920 error = async_error;
1921 if (error) {
1922 suspend_stats.failed_suspend++;
1923 dpm_save_failed_step(SUSPEND_SUSPEND);
1925 dpm_show_time(starttime, state, error, NULL);
1926 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1927 return error;
1931 * device_prepare - Prepare a device for system power transition.
1932 * @dev: Device to handle.
1933 * @state: PM transition of the system being carried out.
1935 * Execute the ->prepare() callback(s) for given device. No new children of the
1936 * device may be registered after this function has returned.
1938 static int device_prepare(struct device *dev, pm_message_t state)
1940 int (*callback)(struct device *) = NULL;
1941 int ret = 0;
1943 if (dev->power.syscore)
1944 return 0;
1946 WARN_ON(!pm_runtime_enabled(dev) &&
1947 dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
1948 DPM_FLAG_LEAVE_SUSPENDED));
1951 * If a device's parent goes into runtime suspend at the wrong time,
1952 * it won't be possible to resume the device. To prevent this we
1953 * block runtime suspend here, during the prepare phase, and allow
1954 * it again during the complete phase.
1956 pm_runtime_get_noresume(dev);
1958 device_lock(dev);
1960 dev->power.wakeup_path = false;
1962 if (dev->power.no_pm_callbacks)
1963 goto unlock;
1965 if (dev->pm_domain)
1966 callback = dev->pm_domain->ops.prepare;
1967 else if (dev->type && dev->type->pm)
1968 callback = dev->type->pm->prepare;
1969 else if (dev->class && dev->class->pm)
1970 callback = dev->class->pm->prepare;
1971 else if (dev->bus && dev->bus->pm)
1972 callback = dev->bus->pm->prepare;
1974 if (!callback && dev->driver && dev->driver->pm)
1975 callback = dev->driver->pm->prepare;
1977 if (callback)
1978 ret = callback(dev);
1980 unlock:
1981 device_unlock(dev);
1983 if (ret < 0) {
1984 suspend_report_result(callback, ret);
1985 pm_runtime_put(dev);
1986 return ret;
1989 * A positive return value from ->prepare() means "this device appears
1990 * to be runtime-suspended and its state is fine, so if it really is
1991 * runtime-suspended, you can leave it in that state provided that you
1992 * will do the same thing with all of its descendants". This only
1993 * applies to suspend transitions, however.
1995 spin_lock_irq(&dev->power.lock);
1996 dev->power.direct_complete = state.event == PM_EVENT_SUSPEND &&
1997 ((pm_runtime_suspended(dev) && ret > 0) ||
1998 dev->power.no_pm_callbacks) &&
1999 !dev_pm_test_driver_flags(dev, DPM_FLAG_NEVER_SKIP);
2000 spin_unlock_irq(&dev->power.lock);
2001 return 0;
2005 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
2006 * @state: PM transition of the system being carried out.
2008 * Execute the ->prepare() callback(s) for all devices.
2010 int dpm_prepare(pm_message_t state)
2012 int error = 0;
2014 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
2015 might_sleep();
2018 * Give a chance for the known devices to complete their probes, before
2019 * disable probing of devices. This sync point is important at least
2020 * at boot time + hibernation restore.
2022 wait_for_device_probe();
2024 * It is unsafe if probing of devices will happen during suspend or
2025 * hibernation and system behavior will be unpredictable in this case.
2026 * So, let's prohibit device's probing here and defer their probes
2027 * instead. The normal behavior will be restored in dpm_complete().
2029 device_block_probing();
2031 mutex_lock(&dpm_list_mtx);
2032 while (!list_empty(&dpm_list)) {
2033 struct device *dev = to_device(dpm_list.next);
2035 get_device(dev);
2036 mutex_unlock(&dpm_list_mtx);
2038 trace_device_pm_callback_start(dev, "", state.event);
2039 error = device_prepare(dev, state);
2040 trace_device_pm_callback_end(dev, error);
2042 mutex_lock(&dpm_list_mtx);
2043 if (error) {
2044 if (error == -EAGAIN) {
2045 put_device(dev);
2046 error = 0;
2047 continue;
2049 printk(KERN_INFO "PM: Device %s not prepared "
2050 "for power transition: code %d\n",
2051 dev_name(dev), error);
2052 put_device(dev);
2053 break;
2055 dev->power.is_prepared = true;
2056 if (!list_empty(&dev->power.entry))
2057 list_move_tail(&dev->power.entry, &dpm_prepared_list);
2058 put_device(dev);
2060 mutex_unlock(&dpm_list_mtx);
2061 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
2062 return error;
2066 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2067 * @state: PM transition of the system being carried out.
2069 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2070 * callbacks for them.
2072 int dpm_suspend_start(pm_message_t state)
2074 int error;
2076 error = dpm_prepare(state);
2077 if (error) {
2078 suspend_stats.failed_prepare++;
2079 dpm_save_failed_step(SUSPEND_PREPARE);
2080 } else
2081 error = dpm_suspend(state);
2082 return error;
2084 EXPORT_SYMBOL_GPL(dpm_suspend_start);
2086 void __suspend_report_result(const char *function, void *fn, int ret)
2088 if (ret)
2089 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
2091 EXPORT_SYMBOL_GPL(__suspend_report_result);
2094 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2095 * @dev: Device to wait for.
2096 * @subordinate: Device that needs to wait for @dev.
2098 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
2100 dpm_wait(dev, subordinate->power.async_suspend);
2101 return async_error;
2103 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
2106 * dpm_for_each_dev - device iterator.
2107 * @data: data for the callback.
2108 * @fn: function to be called for each device.
2110 * Iterate over devices in dpm_list, and call @fn for each device,
2111 * passing it @data.
2113 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
2115 struct device *dev;
2117 if (!fn)
2118 return;
2120 device_pm_lock();
2121 list_for_each_entry(dev, &dpm_list, power.entry)
2122 fn(dev, data);
2123 device_pm_unlock();
2125 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
2127 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
2129 if (!ops)
2130 return true;
2132 return !ops->prepare &&
2133 !ops->suspend &&
2134 !ops->suspend_late &&
2135 !ops->suspend_noirq &&
2136 !ops->resume_noirq &&
2137 !ops->resume_early &&
2138 !ops->resume &&
2139 !ops->complete;
2142 void device_pm_check_callbacks(struct device *dev)
2144 spin_lock_irq(&dev->power.lock);
2145 dev->power.no_pm_callbacks =
2146 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
2147 !dev->bus->suspend && !dev->bus->resume)) &&
2148 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
2149 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
2150 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
2151 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
2152 !dev->driver->suspend && !dev->driver->resume));
2153 spin_unlock_irq(&dev->power.lock);
2156 bool dev_pm_smart_suspend_and_suspended(struct device *dev)
2158 return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) &&
2159 pm_runtime_status_suspended(dev);