Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / base / power / main.c
blobdcfc0a36c8f7e7408867f19c296b9897e31ecc39
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/base/power/main.c - Where the driver meets power management.
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
8 * The driver model core calls device_pm_add() when a device is registered.
9 * This will initialize the embedded device_pm_info object in the device
10 * and add it to the list of power-controlled devices. sysfs entries for
11 * controlling device power management will also be added.
13 * A separate list is used for keeping track of power info, because the power
14 * domain dependencies may differ from the ancestral dependencies that the
15 * subsystem list maintains.
18 #define pr_fmt(fmt) "PM: " fmt
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/mutex.h>
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/devfreq.h>
36 #include <linux/timer.h>
38 #include "../base.h"
39 #include "power.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
50 * dpm_list_mutex.
53 LIST_HEAD(dpm_list);
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)
67 switch (event) {
68 case PM_EVENT_SUSPEND:
69 return "suspend";
70 case PM_EVENT_RESUME:
71 return "resume";
72 case PM_EVENT_FREEZE:
73 return "freeze";
74 case PM_EVENT_QUIESCE:
75 return "quiesce";
76 case PM_EVENT_HIBERNATE:
77 return "hibernate";
78 case PM_EVENT_THAW:
79 return "thaw";
80 case PM_EVENT_RESTORE:
81 return "restore";
82 case PM_EVENT_RECOVER:
83 return "recover";
84 default:
85 return "(unknown PM event)";
89 /**
90 * device_pm_sleep_init - Initialize system suspend-related device fields.
91 * @dev: Device object being initialized.
93 void device_pm_sleep_init(struct device *dev)
95 dev->power.is_prepared = false;
96 dev->power.is_suspended = false;
97 dev->power.is_noirq_suspended = false;
98 dev->power.is_late_suspended = false;
99 init_completion(&dev->power.completion);
100 complete_all(&dev->power.completion);
101 dev->power.wakeup = NULL;
102 INIT_LIST_HEAD(&dev->power.entry);
106 * device_pm_lock - Lock the list of active devices used by the PM core.
108 void device_pm_lock(void)
110 mutex_lock(&dpm_list_mtx);
114 * device_pm_unlock - Unlock the list of active devices used by the PM core.
116 void device_pm_unlock(void)
118 mutex_unlock(&dpm_list_mtx);
122 * device_pm_add - Add a device to the PM core's list of active devices.
123 * @dev: Device to add to the list.
125 void device_pm_add(struct device *dev)
127 /* Skip PM setup/initialization. */
128 if (device_pm_not_required(dev))
129 return;
131 pr_debug("Adding info for %s:%s\n",
132 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
133 device_pm_check_callbacks(dev);
134 mutex_lock(&dpm_list_mtx);
135 if (dev->parent && dev->parent->power.is_prepared)
136 dev_warn(dev, "parent %s should not be sleeping\n",
137 dev_name(dev->parent));
138 list_add_tail(&dev->power.entry, &dpm_list);
139 dev->power.in_dpm_list = true;
140 mutex_unlock(&dpm_list_mtx);
144 * device_pm_remove - Remove a device from the PM core's list of active devices.
145 * @dev: Device to be removed from the list.
147 void device_pm_remove(struct device *dev)
149 if (device_pm_not_required(dev))
150 return;
152 pr_debug("Removing info for %s:%s\n",
153 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
154 complete_all(&dev->power.completion);
155 mutex_lock(&dpm_list_mtx);
156 list_del_init(&dev->power.entry);
157 dev->power.in_dpm_list = false;
158 mutex_unlock(&dpm_list_mtx);
159 device_wakeup_disable(dev);
160 pm_runtime_remove(dev);
161 device_pm_check_callbacks(dev);
165 * device_pm_move_before - Move device in the PM core's list of active devices.
166 * @deva: Device to move in dpm_list.
167 * @devb: Device @deva should come before.
169 void device_pm_move_before(struct device *deva, struct device *devb)
171 pr_debug("Moving %s:%s before %s:%s\n",
172 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
173 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
174 /* Delete deva from dpm_list and reinsert before devb. */
175 list_move_tail(&deva->power.entry, &devb->power.entry);
179 * device_pm_move_after - Move device in the PM core's list of active devices.
180 * @deva: Device to move in dpm_list.
181 * @devb: Device @deva should come after.
183 void device_pm_move_after(struct device *deva, struct device *devb)
185 pr_debug("Moving %s:%s after %s:%s\n",
186 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
187 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
188 /* Delete deva from dpm_list and reinsert after devb. */
189 list_move(&deva->power.entry, &devb->power.entry);
193 * device_pm_move_last - Move device to end of the PM core's list of devices.
194 * @dev: Device to move in dpm_list.
196 void device_pm_move_last(struct device *dev)
198 pr_debug("Moving %s:%s to end of list\n",
199 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
200 list_move_tail(&dev->power.entry, &dpm_list);
203 static ktime_t initcall_debug_start(struct device *dev, void *cb)
205 if (!pm_print_times_enabled)
206 return 0;
208 dev_info(dev, "calling %pS @ %i, parent: %s\n", cb,
209 task_pid_nr(current),
210 dev->parent ? dev_name(dev->parent) : "none");
211 return ktime_get();
214 static void initcall_debug_report(struct device *dev, ktime_t calltime,
215 void *cb, int error)
217 ktime_t rettime;
218 s64 nsecs;
220 if (!pm_print_times_enabled)
221 return;
223 rettime = ktime_get();
224 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
226 dev_info(dev, "%pS returned %d after %Ld usecs\n", cb, error,
227 (unsigned long long)nsecs >> 10);
231 * dpm_wait - Wait for a PM operation to complete.
232 * @dev: Device to wait for.
233 * @async: If unset, wait only if the device's power.async_suspend flag is set.
235 static void dpm_wait(struct device *dev, bool async)
237 if (!dev)
238 return;
240 if (async || (pm_async_enabled && dev->power.async_suspend))
241 wait_for_completion(&dev->power.completion);
244 static int dpm_wait_fn(struct device *dev, void *async_ptr)
246 dpm_wait(dev, *((bool *)async_ptr));
247 return 0;
250 static void dpm_wait_for_children(struct device *dev, bool async)
252 device_for_each_child(dev, &async, dpm_wait_fn);
255 static void dpm_wait_for_suppliers(struct device *dev, bool async)
257 struct device_link *link;
258 int idx;
260 idx = device_links_read_lock();
263 * If the supplier goes away right after we've checked the link to it,
264 * we'll wait for its completion to change the state, but that's fine,
265 * because the only things that will block as a result are the SRCU
266 * callbacks freeing the link objects for the links in the list we're
267 * walking.
269 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
270 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
271 dpm_wait(link->supplier, async);
273 device_links_read_unlock(idx);
276 static void dpm_wait_for_superior(struct device *dev, bool async)
278 dpm_wait(dev->parent, async);
279 dpm_wait_for_suppliers(dev, async);
282 static void dpm_wait_for_consumers(struct device *dev, bool async)
284 struct device_link *link;
285 int idx;
287 idx = device_links_read_lock();
290 * The status of a device link can only be changed from "dormant" by a
291 * probe, but that cannot happen during system suspend/resume. In
292 * theory it can change to "dormant" at that time, but then it is
293 * reasonable to wait for the target device anyway (eg. if it goes
294 * away, it's better to wait for it to go away completely and then
295 * continue instead of trying to continue in parallel with its
296 * unregistration).
298 list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
299 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
300 dpm_wait(link->consumer, async);
302 device_links_read_unlock(idx);
305 static void dpm_wait_for_subordinate(struct device *dev, bool async)
307 dpm_wait_for_children(dev, async);
308 dpm_wait_for_consumers(dev, async);
312 * pm_op - Return the PM operation appropriate for given PM event.
313 * @ops: PM operations to choose from.
314 * @state: PM transition of the system being carried out.
316 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
318 switch (state.event) {
319 #ifdef CONFIG_SUSPEND
320 case PM_EVENT_SUSPEND:
321 return ops->suspend;
322 case PM_EVENT_RESUME:
323 return ops->resume;
324 #endif /* CONFIG_SUSPEND */
325 #ifdef CONFIG_HIBERNATE_CALLBACKS
326 case PM_EVENT_FREEZE:
327 case PM_EVENT_QUIESCE:
328 return ops->freeze;
329 case PM_EVENT_HIBERNATE:
330 return ops->poweroff;
331 case PM_EVENT_THAW:
332 case PM_EVENT_RECOVER:
333 return ops->thaw;
334 break;
335 case PM_EVENT_RESTORE:
336 return ops->restore;
337 #endif /* CONFIG_HIBERNATE_CALLBACKS */
340 return NULL;
344 * pm_late_early_op - Return the PM operation appropriate for given PM event.
345 * @ops: PM operations to choose from.
346 * @state: PM transition of the system being carried out.
348 * Runtime PM is disabled for @dev while this function is being executed.
350 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
351 pm_message_t state)
353 switch (state.event) {
354 #ifdef CONFIG_SUSPEND
355 case PM_EVENT_SUSPEND:
356 return ops->suspend_late;
357 case PM_EVENT_RESUME:
358 return ops->resume_early;
359 #endif /* CONFIG_SUSPEND */
360 #ifdef CONFIG_HIBERNATE_CALLBACKS
361 case PM_EVENT_FREEZE:
362 case PM_EVENT_QUIESCE:
363 return ops->freeze_late;
364 case PM_EVENT_HIBERNATE:
365 return ops->poweroff_late;
366 case PM_EVENT_THAW:
367 case PM_EVENT_RECOVER:
368 return ops->thaw_early;
369 case PM_EVENT_RESTORE:
370 return ops->restore_early;
371 #endif /* CONFIG_HIBERNATE_CALLBACKS */
374 return NULL;
378 * pm_noirq_op - Return the PM operation appropriate for given PM event.
379 * @ops: PM operations to choose from.
380 * @state: PM transition of the system being carried out.
382 * The driver of @dev will not receive interrupts while this function is being
383 * executed.
385 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
387 switch (state.event) {
388 #ifdef CONFIG_SUSPEND
389 case PM_EVENT_SUSPEND:
390 return ops->suspend_noirq;
391 case PM_EVENT_RESUME:
392 return ops->resume_noirq;
393 #endif /* CONFIG_SUSPEND */
394 #ifdef CONFIG_HIBERNATE_CALLBACKS
395 case PM_EVENT_FREEZE:
396 case PM_EVENT_QUIESCE:
397 return ops->freeze_noirq;
398 case PM_EVENT_HIBERNATE:
399 return ops->poweroff_noirq;
400 case PM_EVENT_THAW:
401 case PM_EVENT_RECOVER:
402 return ops->thaw_noirq;
403 case PM_EVENT_RESTORE:
404 return ops->restore_noirq;
405 #endif /* CONFIG_HIBERNATE_CALLBACKS */
408 return NULL;
411 static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
413 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
414 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
415 ", may wakeup" : "");
418 static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
419 int error)
421 pr_err("Device %s failed to %s%s: error %d\n",
422 dev_name(dev), pm_verb(state.event), info, error);
425 static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
426 const char *info)
428 ktime_t calltime;
429 u64 usecs64;
430 int usecs;
432 calltime = ktime_get();
433 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
434 do_div(usecs64, NSEC_PER_USEC);
435 usecs = usecs64;
436 if (usecs == 0)
437 usecs = 1;
439 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
440 info ?: "", info ? " " : "", pm_verb(state.event),
441 error ? "aborted" : "complete",
442 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
445 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
446 pm_message_t state, const char *info)
448 ktime_t calltime;
449 int error;
451 if (!cb)
452 return 0;
454 calltime = initcall_debug_start(dev, cb);
456 pm_dev_dbg(dev, state, info);
457 trace_device_pm_callback_start(dev, info, state.event);
458 error = cb(dev);
459 trace_device_pm_callback_end(dev, error);
460 suspend_report_result(cb, error);
462 initcall_debug_report(dev, calltime, cb, error);
464 return error;
467 #ifdef CONFIG_DPM_WATCHDOG
468 struct dpm_watchdog {
469 struct device *dev;
470 struct task_struct *tsk;
471 struct timer_list timer;
474 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
475 struct dpm_watchdog wd
478 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
479 * @t: The timer that PM watchdog depends on.
481 * Called when a driver has timed out suspending or resuming.
482 * There's not much we can do here to recover so panic() to
483 * capture a crash-dump in pstore.
485 static void dpm_watchdog_handler(struct timer_list *t)
487 struct dpm_watchdog *wd = from_timer(wd, t, timer);
489 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
490 show_stack(wd->tsk, NULL);
491 panic("%s %s: unrecoverable failure\n",
492 dev_driver_string(wd->dev), dev_name(wd->dev));
496 * dpm_watchdog_set - Enable pm watchdog for given device.
497 * @wd: Watchdog. Must be allocated on the stack.
498 * @dev: Device to handle.
500 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
502 struct timer_list *timer = &wd->timer;
504 wd->dev = dev;
505 wd->tsk = current;
507 timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
508 /* use same timeout value for both suspend and resume */
509 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
510 add_timer(timer);
514 * dpm_watchdog_clear - Disable suspend/resume watchdog.
515 * @wd: Watchdog to disable.
517 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
519 struct timer_list *timer = &wd->timer;
521 del_timer_sync(timer);
522 destroy_timer_on_stack(timer);
524 #else
525 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
526 #define dpm_watchdog_set(x, y)
527 #define dpm_watchdog_clear(x)
528 #endif
530 /*------------------------- Resume routines -------------------------*/
533 * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
534 * @dev: Target device.
536 * Make the core skip the "early resume" and "resume" phases for @dev.
538 * This function can be called by middle-layer code during the "noirq" phase of
539 * system resume if necessary, but not by device drivers.
541 void dev_pm_skip_next_resume_phases(struct device *dev)
543 dev->power.is_late_suspended = false;
544 dev->power.is_suspended = false;
548 * suspend_event - Return a "suspend" message for given "resume" one.
549 * @resume_msg: PM message representing a system-wide resume transition.
551 static pm_message_t suspend_event(pm_message_t resume_msg)
553 switch (resume_msg.event) {
554 case PM_EVENT_RESUME:
555 return PMSG_SUSPEND;
556 case PM_EVENT_THAW:
557 case PM_EVENT_RESTORE:
558 return PMSG_FREEZE;
559 case PM_EVENT_RECOVER:
560 return PMSG_HIBERNATE;
562 return PMSG_ON;
566 * dev_pm_may_skip_resume - System-wide device resume optimization check.
567 * @dev: Target device.
569 * Checks whether or not the device may be left in suspend after a system-wide
570 * transition to the working state.
572 bool dev_pm_may_skip_resume(struct device *dev)
574 return !dev->power.must_resume && pm_transition.event != PM_EVENT_RESTORE;
577 static pm_callback_t dpm_subsys_resume_noirq_cb(struct device *dev,
578 pm_message_t state,
579 const char **info_p)
581 pm_callback_t callback;
582 const char *info;
584 if (dev->pm_domain) {
585 info = "noirq power domain ";
586 callback = pm_noirq_op(&dev->pm_domain->ops, state);
587 } else if (dev->type && dev->type->pm) {
588 info = "noirq type ";
589 callback = pm_noirq_op(dev->type->pm, state);
590 } else if (dev->class && dev->class->pm) {
591 info = "noirq class ";
592 callback = pm_noirq_op(dev->class->pm, state);
593 } else if (dev->bus && dev->bus->pm) {
594 info = "noirq bus ";
595 callback = pm_noirq_op(dev->bus->pm, state);
596 } else {
597 return NULL;
600 if (info_p)
601 *info_p = info;
603 return callback;
606 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
607 pm_message_t state,
608 const char **info_p);
610 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
611 pm_message_t state,
612 const char **info_p);
615 * device_resume_noirq - Execute a "noirq resume" callback for given device.
616 * @dev: Device to handle.
617 * @state: PM transition of the system being carried out.
618 * @async: If true, the device is being resumed asynchronously.
620 * The driver of @dev will not receive interrupts while this function is being
621 * executed.
623 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
625 pm_callback_t callback;
626 const char *info;
627 bool skip_resume;
628 int error = 0;
630 TRACE_DEVICE(dev);
631 TRACE_RESUME(0);
633 if (dev->power.syscore || dev->power.direct_complete)
634 goto Out;
636 if (!dev->power.is_noirq_suspended)
637 goto Out;
639 dpm_wait_for_superior(dev, async);
641 skip_resume = dev_pm_may_skip_resume(dev);
643 callback = dpm_subsys_resume_noirq_cb(dev, state, &info);
644 if (callback)
645 goto Run;
647 if (skip_resume)
648 goto Skip;
650 if (dev_pm_smart_suspend_and_suspended(dev)) {
651 pm_message_t suspend_msg = suspend_event(state);
654 * If "freeze" callbacks have been skipped during a transition
655 * related to hibernation, the subsequent "thaw" callbacks must
656 * be skipped too or bad things may happen. Otherwise, resume
657 * callbacks are going to be run for the device, so its runtime
658 * PM status must be changed to reflect the new state after the
659 * transition under way.
661 if (!dpm_subsys_suspend_late_cb(dev, suspend_msg, NULL) &&
662 !dpm_subsys_suspend_noirq_cb(dev, suspend_msg, NULL)) {
663 if (state.event == PM_EVENT_THAW) {
664 skip_resume = true;
665 goto Skip;
666 } else {
667 pm_runtime_set_active(dev);
672 if (dev->driver && dev->driver->pm) {
673 info = "noirq driver ";
674 callback = pm_noirq_op(dev->driver->pm, state);
677 Run:
678 error = dpm_run_callback(callback, dev, state, info);
680 Skip:
681 dev->power.is_noirq_suspended = false;
683 if (skip_resume) {
685 * The device is going to be left in suspend, but it might not
686 * have been in runtime suspend before the system suspended, so
687 * its runtime PM status needs to be updated to avoid confusing
688 * the runtime PM framework when runtime PM is enabled for the
689 * device again.
691 pm_runtime_set_suspended(dev);
692 dev_pm_skip_next_resume_phases(dev);
695 Out:
696 complete_all(&dev->power.completion);
697 TRACE_RESUME(error);
698 return error;
701 static bool is_async(struct device *dev)
703 return dev->power.async_suspend && pm_async_enabled
704 && !pm_trace_is_enabled();
707 static bool dpm_async_fn(struct device *dev, async_func_t func)
709 reinit_completion(&dev->power.completion);
711 if (is_async(dev)) {
712 get_device(dev);
713 async_schedule(func, dev);
714 return true;
717 return false;
720 static void async_resume_noirq(void *data, async_cookie_t cookie)
722 struct device *dev = (struct device *)data;
723 int error;
725 error = device_resume_noirq(dev, pm_transition, true);
726 if (error)
727 pm_dev_err(dev, pm_transition, " async", error);
729 put_device(dev);
732 void dpm_noirq_resume_devices(pm_message_t state)
734 struct device *dev;
735 ktime_t starttime = ktime_get();
737 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
738 mutex_lock(&dpm_list_mtx);
739 pm_transition = state;
742 * Advanced the async threads upfront,
743 * in case the starting of async threads is
744 * delayed by non-async resuming devices.
746 list_for_each_entry(dev, &dpm_noirq_list, power.entry)
747 dpm_async_fn(dev, async_resume_noirq);
749 while (!list_empty(&dpm_noirq_list)) {
750 dev = to_device(dpm_noirq_list.next);
751 get_device(dev);
752 list_move_tail(&dev->power.entry, &dpm_late_early_list);
753 mutex_unlock(&dpm_list_mtx);
755 if (!is_async(dev)) {
756 int error;
758 error = device_resume_noirq(dev, state, false);
759 if (error) {
760 suspend_stats.failed_resume_noirq++;
761 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
762 dpm_save_failed_dev(dev_name(dev));
763 pm_dev_err(dev, state, " noirq", error);
767 mutex_lock(&dpm_list_mtx);
768 put_device(dev);
770 mutex_unlock(&dpm_list_mtx);
771 async_synchronize_full();
772 dpm_show_time(starttime, state, 0, "noirq");
773 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
776 void dpm_noirq_end(void)
778 resume_device_irqs();
779 device_wakeup_disarm_wake_irqs();
780 cpuidle_resume();
784 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
785 * @state: PM transition of the system being carried out.
787 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
788 * allow device drivers' interrupt handlers to be called.
790 void dpm_resume_noirq(pm_message_t state)
792 dpm_noirq_resume_devices(state);
793 dpm_noirq_end();
796 static pm_callback_t dpm_subsys_resume_early_cb(struct device *dev,
797 pm_message_t state,
798 const char **info_p)
800 pm_callback_t callback;
801 const char *info;
803 if (dev->pm_domain) {
804 info = "early power domain ";
805 callback = pm_late_early_op(&dev->pm_domain->ops, state);
806 } else if (dev->type && dev->type->pm) {
807 info = "early type ";
808 callback = pm_late_early_op(dev->type->pm, state);
809 } else if (dev->class && dev->class->pm) {
810 info = "early class ";
811 callback = pm_late_early_op(dev->class->pm, state);
812 } else if (dev->bus && dev->bus->pm) {
813 info = "early bus ";
814 callback = pm_late_early_op(dev->bus->pm, state);
815 } else {
816 return NULL;
819 if (info_p)
820 *info_p = info;
822 return callback;
826 * device_resume_early - Execute an "early resume" callback for given device.
827 * @dev: Device to handle.
828 * @state: PM transition of the system being carried out.
829 * @async: If true, the device is being resumed asynchronously.
831 * Runtime PM is disabled for @dev while this function is being executed.
833 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
835 pm_callback_t callback;
836 const char *info;
837 int error = 0;
839 TRACE_DEVICE(dev);
840 TRACE_RESUME(0);
842 if (dev->power.syscore || dev->power.direct_complete)
843 goto Out;
845 if (!dev->power.is_late_suspended)
846 goto Out;
848 dpm_wait_for_superior(dev, async);
850 callback = dpm_subsys_resume_early_cb(dev, state, &info);
852 if (!callback && dev->driver && dev->driver->pm) {
853 info = "early driver ";
854 callback = pm_late_early_op(dev->driver->pm, state);
857 error = dpm_run_callback(callback, dev, state, info);
858 dev->power.is_late_suspended = false;
860 Out:
861 TRACE_RESUME(error);
863 pm_runtime_enable(dev);
864 complete_all(&dev->power.completion);
865 return error;
868 static void async_resume_early(void *data, async_cookie_t cookie)
870 struct device *dev = (struct device *)data;
871 int error;
873 error = device_resume_early(dev, pm_transition, true);
874 if (error)
875 pm_dev_err(dev, pm_transition, " async", error);
877 put_device(dev);
881 * dpm_resume_early - Execute "early resume" callbacks for all devices.
882 * @state: PM transition of the system being carried out.
884 void dpm_resume_early(pm_message_t state)
886 struct device *dev;
887 ktime_t starttime = ktime_get();
889 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
890 mutex_lock(&dpm_list_mtx);
891 pm_transition = state;
894 * Advanced the async threads upfront,
895 * in case the starting of async threads is
896 * delayed by non-async resuming devices.
898 list_for_each_entry(dev, &dpm_late_early_list, power.entry)
899 dpm_async_fn(dev, async_resume_early);
901 while (!list_empty(&dpm_late_early_list)) {
902 dev = to_device(dpm_late_early_list.next);
903 get_device(dev);
904 list_move_tail(&dev->power.entry, &dpm_suspended_list);
905 mutex_unlock(&dpm_list_mtx);
907 if (!is_async(dev)) {
908 int error;
910 error = device_resume_early(dev, state, false);
911 if (error) {
912 suspend_stats.failed_resume_early++;
913 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
914 dpm_save_failed_dev(dev_name(dev));
915 pm_dev_err(dev, state, " early", error);
918 mutex_lock(&dpm_list_mtx);
919 put_device(dev);
921 mutex_unlock(&dpm_list_mtx);
922 async_synchronize_full();
923 dpm_show_time(starttime, state, 0, "early");
924 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
928 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
929 * @state: PM transition of the system being carried out.
931 void dpm_resume_start(pm_message_t state)
933 dpm_resume_noirq(state);
934 dpm_resume_early(state);
936 EXPORT_SYMBOL_GPL(dpm_resume_start);
939 * device_resume - Execute "resume" callbacks for given device.
940 * @dev: Device to handle.
941 * @state: PM transition of the system being carried out.
942 * @async: If true, the device is being resumed asynchronously.
944 static int device_resume(struct device *dev, pm_message_t state, bool async)
946 pm_callback_t callback = NULL;
947 const char *info = NULL;
948 int error = 0;
949 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
951 TRACE_DEVICE(dev);
952 TRACE_RESUME(0);
954 if (dev->power.syscore)
955 goto Complete;
957 if (dev->power.direct_complete) {
958 /* Match the pm_runtime_disable() in __device_suspend(). */
959 pm_runtime_enable(dev);
960 goto Complete;
963 dpm_wait_for_superior(dev, async);
964 dpm_watchdog_set(&wd, dev);
965 device_lock(dev);
968 * This is a fib. But we'll allow new children to be added below
969 * a resumed device, even if the device hasn't been completed yet.
971 dev->power.is_prepared = false;
973 if (!dev->power.is_suspended)
974 goto Unlock;
976 if (dev->pm_domain) {
977 info = "power domain ";
978 callback = pm_op(&dev->pm_domain->ops, state);
979 goto Driver;
982 if (dev->type && dev->type->pm) {
983 info = "type ";
984 callback = pm_op(dev->type->pm, state);
985 goto Driver;
988 if (dev->class && dev->class->pm) {
989 info = "class ";
990 callback = pm_op(dev->class->pm, state);
991 goto Driver;
994 if (dev->bus) {
995 if (dev->bus->pm) {
996 info = "bus ";
997 callback = pm_op(dev->bus->pm, state);
998 } else if (dev->bus->resume) {
999 info = "legacy bus ";
1000 callback = dev->bus->resume;
1001 goto End;
1005 Driver:
1006 if (!callback && dev->driver && dev->driver->pm) {
1007 info = "driver ";
1008 callback = pm_op(dev->driver->pm, state);
1011 End:
1012 error = dpm_run_callback(callback, dev, state, info);
1013 dev->power.is_suspended = false;
1015 Unlock:
1016 device_unlock(dev);
1017 dpm_watchdog_clear(&wd);
1019 Complete:
1020 complete_all(&dev->power.completion);
1022 TRACE_RESUME(error);
1024 return error;
1027 static void async_resume(void *data, async_cookie_t cookie)
1029 struct device *dev = (struct device *)data;
1030 int error;
1032 error = device_resume(dev, pm_transition, true);
1033 if (error)
1034 pm_dev_err(dev, pm_transition, " async", error);
1035 put_device(dev);
1039 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1040 * @state: PM transition of the system being carried out.
1042 * Execute the appropriate "resume" callback for all devices whose status
1043 * indicates that they are suspended.
1045 void dpm_resume(pm_message_t state)
1047 struct device *dev;
1048 ktime_t starttime = ktime_get();
1050 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
1051 might_sleep();
1053 mutex_lock(&dpm_list_mtx);
1054 pm_transition = state;
1055 async_error = 0;
1057 list_for_each_entry(dev, &dpm_suspended_list, power.entry)
1058 dpm_async_fn(dev, async_resume);
1060 while (!list_empty(&dpm_suspended_list)) {
1061 dev = to_device(dpm_suspended_list.next);
1062 get_device(dev);
1063 if (!is_async(dev)) {
1064 int error;
1066 mutex_unlock(&dpm_list_mtx);
1068 error = device_resume(dev, state, false);
1069 if (error) {
1070 suspend_stats.failed_resume++;
1071 dpm_save_failed_step(SUSPEND_RESUME);
1072 dpm_save_failed_dev(dev_name(dev));
1073 pm_dev_err(dev, state, "", error);
1076 mutex_lock(&dpm_list_mtx);
1078 if (!list_empty(&dev->power.entry))
1079 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1080 put_device(dev);
1082 mutex_unlock(&dpm_list_mtx);
1083 async_synchronize_full();
1084 dpm_show_time(starttime, state, 0, NULL);
1086 cpufreq_resume();
1087 devfreq_resume();
1088 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
1092 * device_complete - Complete a PM transition for given device.
1093 * @dev: Device to handle.
1094 * @state: PM transition of the system being carried out.
1096 static void device_complete(struct device *dev, pm_message_t state)
1098 void (*callback)(struct device *) = NULL;
1099 const char *info = NULL;
1101 if (dev->power.syscore)
1102 return;
1104 device_lock(dev);
1106 if (dev->pm_domain) {
1107 info = "completing power domain ";
1108 callback = dev->pm_domain->ops.complete;
1109 } else if (dev->type && dev->type->pm) {
1110 info = "completing type ";
1111 callback = dev->type->pm->complete;
1112 } else if (dev->class && dev->class->pm) {
1113 info = "completing class ";
1114 callback = dev->class->pm->complete;
1115 } else if (dev->bus && dev->bus->pm) {
1116 info = "completing bus ";
1117 callback = dev->bus->pm->complete;
1120 if (!callback && dev->driver && dev->driver->pm) {
1121 info = "completing driver ";
1122 callback = dev->driver->pm->complete;
1125 if (callback) {
1126 pm_dev_dbg(dev, state, info);
1127 callback(dev);
1130 device_unlock(dev);
1132 pm_runtime_put(dev);
1136 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1137 * @state: PM transition of the system being carried out.
1139 * Execute the ->complete() callbacks for all devices whose PM status is not
1140 * DPM_ON (this allows new devices to be registered).
1142 void dpm_complete(pm_message_t state)
1144 struct list_head list;
1146 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
1147 might_sleep();
1149 INIT_LIST_HEAD(&list);
1150 mutex_lock(&dpm_list_mtx);
1151 while (!list_empty(&dpm_prepared_list)) {
1152 struct device *dev = to_device(dpm_prepared_list.prev);
1154 get_device(dev);
1155 dev->power.is_prepared = false;
1156 list_move(&dev->power.entry, &list);
1157 mutex_unlock(&dpm_list_mtx);
1159 trace_device_pm_callback_start(dev, "", state.event);
1160 device_complete(dev, state);
1161 trace_device_pm_callback_end(dev, 0);
1163 mutex_lock(&dpm_list_mtx);
1164 put_device(dev);
1166 list_splice(&list, &dpm_list);
1167 mutex_unlock(&dpm_list_mtx);
1169 /* Allow device probing and trigger re-probing of deferred devices */
1170 device_unblock_probing();
1171 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1175 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1176 * @state: PM transition of the system being carried out.
1178 * Execute "resume" callbacks for all devices and complete the PM transition of
1179 * the system.
1181 void dpm_resume_end(pm_message_t state)
1183 dpm_resume(state);
1184 dpm_complete(state);
1186 EXPORT_SYMBOL_GPL(dpm_resume_end);
1189 /*------------------------- Suspend routines -------------------------*/
1192 * resume_event - Return a "resume" message for given "suspend" sleep state.
1193 * @sleep_state: PM message representing a sleep state.
1195 * Return a PM message representing the resume event corresponding to given
1196 * sleep state.
1198 static pm_message_t resume_event(pm_message_t sleep_state)
1200 switch (sleep_state.event) {
1201 case PM_EVENT_SUSPEND:
1202 return PMSG_RESUME;
1203 case PM_EVENT_FREEZE:
1204 case PM_EVENT_QUIESCE:
1205 return PMSG_RECOVER;
1206 case PM_EVENT_HIBERNATE:
1207 return PMSG_RESTORE;
1209 return PMSG_ON;
1212 static void dpm_superior_set_must_resume(struct device *dev)
1214 struct device_link *link;
1215 int idx;
1217 if (dev->parent)
1218 dev->parent->power.must_resume = true;
1220 idx = device_links_read_lock();
1222 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
1223 link->supplier->power.must_resume = true;
1225 device_links_read_unlock(idx);
1228 static pm_callback_t dpm_subsys_suspend_noirq_cb(struct device *dev,
1229 pm_message_t state,
1230 const char **info_p)
1232 pm_callback_t callback;
1233 const char *info;
1235 if (dev->pm_domain) {
1236 info = "noirq power domain ";
1237 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1238 } else if (dev->type && dev->type->pm) {
1239 info = "noirq type ";
1240 callback = pm_noirq_op(dev->type->pm, state);
1241 } else if (dev->class && dev->class->pm) {
1242 info = "noirq class ";
1243 callback = pm_noirq_op(dev->class->pm, state);
1244 } else if (dev->bus && dev->bus->pm) {
1245 info = "noirq bus ";
1246 callback = pm_noirq_op(dev->bus->pm, state);
1247 } else {
1248 return NULL;
1251 if (info_p)
1252 *info_p = info;
1254 return callback;
1257 static bool device_must_resume(struct device *dev, pm_message_t state,
1258 bool no_subsys_suspend_noirq)
1260 pm_message_t resume_msg = resume_event(state);
1263 * If all of the device driver's "noirq", "late" and "early" callbacks
1264 * are invoked directly by the core, the decision to allow the device to
1265 * stay in suspend can be based on its current runtime PM status and its
1266 * wakeup settings.
1268 if (no_subsys_suspend_noirq &&
1269 !dpm_subsys_suspend_late_cb(dev, state, NULL) &&
1270 !dpm_subsys_resume_early_cb(dev, resume_msg, NULL) &&
1271 !dpm_subsys_resume_noirq_cb(dev, resume_msg, NULL))
1272 return !pm_runtime_status_suspended(dev) &&
1273 (resume_msg.event != PM_EVENT_RESUME ||
1274 (device_can_wakeup(dev) && !device_may_wakeup(dev)));
1277 * The only safe strategy here is to require that if the device may not
1278 * be left in suspend, resume callbacks must be invoked for it.
1280 return !dev->power.may_skip_resume;
1284 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1285 * @dev: Device to handle.
1286 * @state: PM transition of the system being carried out.
1287 * @async: If true, the device is being suspended asynchronously.
1289 * The driver of @dev will not receive interrupts while this function is being
1290 * executed.
1292 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1294 pm_callback_t callback;
1295 const char *info;
1296 bool no_subsys_cb = false;
1297 int error = 0;
1299 TRACE_DEVICE(dev);
1300 TRACE_SUSPEND(0);
1302 dpm_wait_for_subordinate(dev, async);
1304 if (async_error)
1305 goto Complete;
1307 if (pm_wakeup_pending()) {
1308 async_error = -EBUSY;
1309 goto Complete;
1312 if (dev->power.syscore || dev->power.direct_complete)
1313 goto Complete;
1315 callback = dpm_subsys_suspend_noirq_cb(dev, state, &info);
1316 if (callback)
1317 goto Run;
1319 no_subsys_cb = !dpm_subsys_suspend_late_cb(dev, state, NULL);
1321 if (dev_pm_smart_suspend_and_suspended(dev) && no_subsys_cb)
1322 goto Skip;
1324 if (dev->driver && dev->driver->pm) {
1325 info = "noirq driver ";
1326 callback = pm_noirq_op(dev->driver->pm, state);
1329 Run:
1330 error = dpm_run_callback(callback, dev, state, info);
1331 if (error) {
1332 async_error = error;
1333 goto Complete;
1336 Skip:
1337 dev->power.is_noirq_suspended = true;
1339 if (dev_pm_test_driver_flags(dev, DPM_FLAG_LEAVE_SUSPENDED)) {
1340 dev->power.must_resume = dev->power.must_resume ||
1341 atomic_read(&dev->power.usage_count) > 1 ||
1342 device_must_resume(dev, state, no_subsys_cb);
1343 } else {
1344 dev->power.must_resume = true;
1347 if (dev->power.must_resume)
1348 dpm_superior_set_must_resume(dev);
1350 Complete:
1351 complete_all(&dev->power.completion);
1352 TRACE_SUSPEND(error);
1353 return error;
1356 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1358 struct device *dev = (struct device *)data;
1359 int error;
1361 error = __device_suspend_noirq(dev, pm_transition, true);
1362 if (error) {
1363 dpm_save_failed_dev(dev_name(dev));
1364 pm_dev_err(dev, pm_transition, " async", error);
1367 put_device(dev);
1370 static int device_suspend_noirq(struct device *dev)
1372 if (dpm_async_fn(dev, async_suspend_noirq))
1373 return 0;
1375 return __device_suspend_noirq(dev, pm_transition, false);
1378 void dpm_noirq_begin(void)
1380 cpuidle_pause();
1381 device_wakeup_arm_wake_irqs();
1382 suspend_device_irqs();
1385 int dpm_noirq_suspend_devices(pm_message_t state)
1387 ktime_t starttime = ktime_get();
1388 int error = 0;
1390 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1391 mutex_lock(&dpm_list_mtx);
1392 pm_transition = state;
1393 async_error = 0;
1395 while (!list_empty(&dpm_late_early_list)) {
1396 struct device *dev = to_device(dpm_late_early_list.prev);
1398 get_device(dev);
1399 mutex_unlock(&dpm_list_mtx);
1401 error = device_suspend_noirq(dev);
1403 mutex_lock(&dpm_list_mtx);
1404 if (error) {
1405 pm_dev_err(dev, state, " noirq", error);
1406 dpm_save_failed_dev(dev_name(dev));
1407 put_device(dev);
1408 break;
1410 if (!list_empty(&dev->power.entry))
1411 list_move(&dev->power.entry, &dpm_noirq_list);
1412 put_device(dev);
1414 if (async_error)
1415 break;
1417 mutex_unlock(&dpm_list_mtx);
1418 async_synchronize_full();
1419 if (!error)
1420 error = async_error;
1422 if (error) {
1423 suspend_stats.failed_suspend_noirq++;
1424 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1426 dpm_show_time(starttime, state, error, "noirq");
1427 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1428 return error;
1432 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1433 * @state: PM transition of the system being carried out.
1435 * Prevent device drivers' interrupt handlers from being called and invoke
1436 * "noirq" suspend callbacks for all non-sysdev devices.
1438 int dpm_suspend_noirq(pm_message_t state)
1440 int ret;
1442 dpm_noirq_begin();
1443 ret = dpm_noirq_suspend_devices(state);
1444 if (ret)
1445 dpm_resume_noirq(resume_event(state));
1447 return ret;
1450 static void dpm_propagate_wakeup_to_parent(struct device *dev)
1452 struct device *parent = dev->parent;
1454 if (!parent)
1455 return;
1457 spin_lock_irq(&parent->power.lock);
1459 if (dev->power.wakeup_path && !parent->power.ignore_children)
1460 parent->power.wakeup_path = true;
1462 spin_unlock_irq(&parent->power.lock);
1465 static pm_callback_t dpm_subsys_suspend_late_cb(struct device *dev,
1466 pm_message_t state,
1467 const char **info_p)
1469 pm_callback_t callback;
1470 const char *info;
1472 if (dev->pm_domain) {
1473 info = "late power domain ";
1474 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1475 } else if (dev->type && dev->type->pm) {
1476 info = "late type ";
1477 callback = pm_late_early_op(dev->type->pm, state);
1478 } else if (dev->class && dev->class->pm) {
1479 info = "late class ";
1480 callback = pm_late_early_op(dev->class->pm, state);
1481 } else if (dev->bus && dev->bus->pm) {
1482 info = "late bus ";
1483 callback = pm_late_early_op(dev->bus->pm, state);
1484 } else {
1485 return NULL;
1488 if (info_p)
1489 *info_p = info;
1491 return callback;
1495 * __device_suspend_late - Execute a "late suspend" callback for given device.
1496 * @dev: Device to handle.
1497 * @state: PM transition of the system being carried out.
1498 * @async: If true, the device is being suspended asynchronously.
1500 * Runtime PM is disabled for @dev while this function is being executed.
1502 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1504 pm_callback_t callback;
1505 const char *info;
1506 int error = 0;
1508 TRACE_DEVICE(dev);
1509 TRACE_SUSPEND(0);
1511 __pm_runtime_disable(dev, false);
1513 dpm_wait_for_subordinate(dev, async);
1515 if (async_error)
1516 goto Complete;
1518 if (pm_wakeup_pending()) {
1519 async_error = -EBUSY;
1520 goto Complete;
1523 if (dev->power.syscore || dev->power.direct_complete)
1524 goto Complete;
1526 callback = dpm_subsys_suspend_late_cb(dev, state, &info);
1527 if (callback)
1528 goto Run;
1530 if (dev_pm_smart_suspend_and_suspended(dev) &&
1531 !dpm_subsys_suspend_noirq_cb(dev, state, NULL))
1532 goto Skip;
1534 if (dev->driver && dev->driver->pm) {
1535 info = "late driver ";
1536 callback = pm_late_early_op(dev->driver->pm, state);
1539 Run:
1540 error = dpm_run_callback(callback, dev, state, info);
1541 if (error) {
1542 async_error = error;
1543 goto Complete;
1545 dpm_propagate_wakeup_to_parent(dev);
1547 Skip:
1548 dev->power.is_late_suspended = true;
1550 Complete:
1551 TRACE_SUSPEND(error);
1552 complete_all(&dev->power.completion);
1553 return error;
1556 static void async_suspend_late(void *data, async_cookie_t cookie)
1558 struct device *dev = (struct device *)data;
1559 int error;
1561 error = __device_suspend_late(dev, pm_transition, true);
1562 if (error) {
1563 dpm_save_failed_dev(dev_name(dev));
1564 pm_dev_err(dev, pm_transition, " async", error);
1566 put_device(dev);
1569 static int device_suspend_late(struct device *dev)
1571 if (dpm_async_fn(dev, async_suspend_late))
1572 return 0;
1574 return __device_suspend_late(dev, pm_transition, false);
1578 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1579 * @state: PM transition of the system being carried out.
1581 int dpm_suspend_late(pm_message_t state)
1583 ktime_t starttime = ktime_get();
1584 int error = 0;
1586 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1587 mutex_lock(&dpm_list_mtx);
1588 pm_transition = state;
1589 async_error = 0;
1591 while (!list_empty(&dpm_suspended_list)) {
1592 struct device *dev = to_device(dpm_suspended_list.prev);
1594 get_device(dev);
1595 mutex_unlock(&dpm_list_mtx);
1597 error = device_suspend_late(dev);
1599 mutex_lock(&dpm_list_mtx);
1600 if (!list_empty(&dev->power.entry))
1601 list_move(&dev->power.entry, &dpm_late_early_list);
1603 if (error) {
1604 pm_dev_err(dev, state, " late", error);
1605 dpm_save_failed_dev(dev_name(dev));
1606 put_device(dev);
1607 break;
1609 put_device(dev);
1611 if (async_error)
1612 break;
1614 mutex_unlock(&dpm_list_mtx);
1615 async_synchronize_full();
1616 if (!error)
1617 error = async_error;
1618 if (error) {
1619 suspend_stats.failed_suspend_late++;
1620 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1621 dpm_resume_early(resume_event(state));
1623 dpm_show_time(starttime, state, error, "late");
1624 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1625 return error;
1629 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1630 * @state: PM transition of the system being carried out.
1632 int dpm_suspend_end(pm_message_t state)
1634 int error = dpm_suspend_late(state);
1635 if (error)
1636 return error;
1638 error = dpm_suspend_noirq(state);
1639 if (error) {
1640 dpm_resume_early(resume_event(state));
1641 return error;
1644 return 0;
1646 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1649 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1650 * @dev: Device to suspend.
1651 * @state: PM transition of the system being carried out.
1652 * @cb: Suspend callback to execute.
1653 * @info: string description of caller.
1655 static int legacy_suspend(struct device *dev, pm_message_t state,
1656 int (*cb)(struct device *dev, pm_message_t state),
1657 const char *info)
1659 int error;
1660 ktime_t calltime;
1662 calltime = initcall_debug_start(dev, cb);
1664 trace_device_pm_callback_start(dev, info, state.event);
1665 error = cb(dev, state);
1666 trace_device_pm_callback_end(dev, error);
1667 suspend_report_result(cb, error);
1669 initcall_debug_report(dev, calltime, cb, error);
1671 return error;
1674 static void dpm_clear_superiors_direct_complete(struct device *dev)
1676 struct device_link *link;
1677 int idx;
1679 if (dev->parent) {
1680 spin_lock_irq(&dev->parent->power.lock);
1681 dev->parent->power.direct_complete = false;
1682 spin_unlock_irq(&dev->parent->power.lock);
1685 idx = device_links_read_lock();
1687 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1688 spin_lock_irq(&link->supplier->power.lock);
1689 link->supplier->power.direct_complete = false;
1690 spin_unlock_irq(&link->supplier->power.lock);
1693 device_links_read_unlock(idx);
1697 * __device_suspend - Execute "suspend" callbacks for given device.
1698 * @dev: Device to handle.
1699 * @state: PM transition of the system being carried out.
1700 * @async: If true, the device is being suspended asynchronously.
1702 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1704 pm_callback_t callback = NULL;
1705 const char *info = NULL;
1706 int error = 0;
1707 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1709 TRACE_DEVICE(dev);
1710 TRACE_SUSPEND(0);
1712 dpm_wait_for_subordinate(dev, async);
1714 if (async_error) {
1715 dev->power.direct_complete = false;
1716 goto Complete;
1720 * If a device configured to wake up the system from sleep states
1721 * has been suspended at run time and there's a resume request pending
1722 * for it, this is equivalent to the device signaling wakeup, so the
1723 * system suspend operation should be aborted.
1725 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1726 pm_wakeup_event(dev, 0);
1728 if (pm_wakeup_pending()) {
1729 dev->power.direct_complete = false;
1730 async_error = -EBUSY;
1731 goto Complete;
1734 if (dev->power.syscore)
1735 goto Complete;
1737 /* Avoid direct_complete to let wakeup_path propagate. */
1738 if (device_may_wakeup(dev) || dev->power.wakeup_path)
1739 dev->power.direct_complete = false;
1741 if (dev->power.direct_complete) {
1742 if (pm_runtime_status_suspended(dev)) {
1743 pm_runtime_disable(dev);
1744 if (pm_runtime_status_suspended(dev)) {
1745 pm_dev_dbg(dev, state, "direct-complete ");
1746 goto Complete;
1749 pm_runtime_enable(dev);
1751 dev->power.direct_complete = false;
1754 dev->power.may_skip_resume = false;
1755 dev->power.must_resume = false;
1757 dpm_watchdog_set(&wd, dev);
1758 device_lock(dev);
1760 if (dev->pm_domain) {
1761 info = "power domain ";
1762 callback = pm_op(&dev->pm_domain->ops, state);
1763 goto Run;
1766 if (dev->type && dev->type->pm) {
1767 info = "type ";
1768 callback = pm_op(dev->type->pm, state);
1769 goto Run;
1772 if (dev->class && dev->class->pm) {
1773 info = "class ";
1774 callback = pm_op(dev->class->pm, state);
1775 goto Run;
1778 if (dev->bus) {
1779 if (dev->bus->pm) {
1780 info = "bus ";
1781 callback = pm_op(dev->bus->pm, state);
1782 } else if (dev->bus->suspend) {
1783 pm_dev_dbg(dev, state, "legacy bus ");
1784 error = legacy_suspend(dev, state, dev->bus->suspend,
1785 "legacy bus ");
1786 goto End;
1790 Run:
1791 if (!callback && dev->driver && dev->driver->pm) {
1792 info = "driver ";
1793 callback = pm_op(dev->driver->pm, state);
1796 error = dpm_run_callback(callback, dev, state, info);
1798 End:
1799 if (!error) {
1800 dev->power.is_suspended = true;
1801 if (device_may_wakeup(dev))
1802 dev->power.wakeup_path = true;
1804 dpm_propagate_wakeup_to_parent(dev);
1805 dpm_clear_superiors_direct_complete(dev);
1808 device_unlock(dev);
1809 dpm_watchdog_clear(&wd);
1811 Complete:
1812 if (error)
1813 async_error = error;
1815 complete_all(&dev->power.completion);
1816 TRACE_SUSPEND(error);
1817 return error;
1820 static void async_suspend(void *data, async_cookie_t cookie)
1822 struct device *dev = (struct device *)data;
1823 int error;
1825 error = __device_suspend(dev, pm_transition, true);
1826 if (error) {
1827 dpm_save_failed_dev(dev_name(dev));
1828 pm_dev_err(dev, pm_transition, " async", error);
1831 put_device(dev);
1834 static int device_suspend(struct device *dev)
1836 if (dpm_async_fn(dev, async_suspend))
1837 return 0;
1839 return __device_suspend(dev, pm_transition, false);
1843 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1844 * @state: PM transition of the system being carried out.
1846 int dpm_suspend(pm_message_t state)
1848 ktime_t starttime = ktime_get();
1849 int error = 0;
1851 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1852 might_sleep();
1854 devfreq_suspend();
1855 cpufreq_suspend();
1857 mutex_lock(&dpm_list_mtx);
1858 pm_transition = state;
1859 async_error = 0;
1860 while (!list_empty(&dpm_prepared_list)) {
1861 struct device *dev = to_device(dpm_prepared_list.prev);
1863 get_device(dev);
1864 mutex_unlock(&dpm_list_mtx);
1866 error = device_suspend(dev);
1868 mutex_lock(&dpm_list_mtx);
1869 if (error) {
1870 pm_dev_err(dev, state, "", error);
1871 dpm_save_failed_dev(dev_name(dev));
1872 put_device(dev);
1873 break;
1875 if (!list_empty(&dev->power.entry))
1876 list_move(&dev->power.entry, &dpm_suspended_list);
1877 put_device(dev);
1878 if (async_error)
1879 break;
1881 mutex_unlock(&dpm_list_mtx);
1882 async_synchronize_full();
1883 if (!error)
1884 error = async_error;
1885 if (error) {
1886 suspend_stats.failed_suspend++;
1887 dpm_save_failed_step(SUSPEND_SUSPEND);
1889 dpm_show_time(starttime, state, error, NULL);
1890 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1891 return error;
1895 * device_prepare - Prepare a device for system power transition.
1896 * @dev: Device to handle.
1897 * @state: PM transition of the system being carried out.
1899 * Execute the ->prepare() callback(s) for given device. No new children of the
1900 * device may be registered after this function has returned.
1902 static int device_prepare(struct device *dev, pm_message_t state)
1904 int (*callback)(struct device *) = NULL;
1905 int ret = 0;
1907 if (dev->power.syscore)
1908 return 0;
1910 WARN_ON(!pm_runtime_enabled(dev) &&
1911 dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND |
1912 DPM_FLAG_LEAVE_SUSPENDED));
1915 * If a device's parent goes into runtime suspend at the wrong time,
1916 * it won't be possible to resume the device. To prevent this we
1917 * block runtime suspend here, during the prepare phase, and allow
1918 * it again during the complete phase.
1920 pm_runtime_get_noresume(dev);
1922 device_lock(dev);
1924 dev->power.wakeup_path = false;
1926 if (dev->power.no_pm_callbacks)
1927 goto unlock;
1929 if (dev->pm_domain)
1930 callback = dev->pm_domain->ops.prepare;
1931 else if (dev->type && dev->type->pm)
1932 callback = dev->type->pm->prepare;
1933 else if (dev->class && dev->class->pm)
1934 callback = dev->class->pm->prepare;
1935 else if (dev->bus && dev->bus->pm)
1936 callback = dev->bus->pm->prepare;
1938 if (!callback && dev->driver && dev->driver->pm)
1939 callback = dev->driver->pm->prepare;
1941 if (callback)
1942 ret = callback(dev);
1944 unlock:
1945 device_unlock(dev);
1947 if (ret < 0) {
1948 suspend_report_result(callback, ret);
1949 pm_runtime_put(dev);
1950 return ret;
1953 * A positive return value from ->prepare() means "this device appears
1954 * to be runtime-suspended and its state is fine, so if it really is
1955 * runtime-suspended, you can leave it in that state provided that you
1956 * will do the same thing with all of its descendants". This only
1957 * applies to suspend transitions, however.
1959 spin_lock_irq(&dev->power.lock);
1960 dev->power.direct_complete = state.event == PM_EVENT_SUSPEND &&
1961 ((pm_runtime_suspended(dev) && ret > 0) ||
1962 dev->power.no_pm_callbacks) &&
1963 !dev_pm_test_driver_flags(dev, DPM_FLAG_NEVER_SKIP);
1964 spin_unlock_irq(&dev->power.lock);
1965 return 0;
1969 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1970 * @state: PM transition of the system being carried out.
1972 * Execute the ->prepare() callback(s) for all devices.
1974 int dpm_prepare(pm_message_t state)
1976 int error = 0;
1978 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1979 might_sleep();
1982 * Give a chance for the known devices to complete their probes, before
1983 * disable probing of devices. This sync point is important at least
1984 * at boot time + hibernation restore.
1986 wait_for_device_probe();
1988 * It is unsafe if probing of devices will happen during suspend or
1989 * hibernation and system behavior will be unpredictable in this case.
1990 * So, let's prohibit device's probing here and defer their probes
1991 * instead. The normal behavior will be restored in dpm_complete().
1993 device_block_probing();
1995 mutex_lock(&dpm_list_mtx);
1996 while (!list_empty(&dpm_list)) {
1997 struct device *dev = to_device(dpm_list.next);
1999 get_device(dev);
2000 mutex_unlock(&dpm_list_mtx);
2002 trace_device_pm_callback_start(dev, "", state.event);
2003 error = device_prepare(dev, state);
2004 trace_device_pm_callback_end(dev, error);
2006 mutex_lock(&dpm_list_mtx);
2007 if (error) {
2008 if (error == -EAGAIN) {
2009 put_device(dev);
2010 error = 0;
2011 continue;
2013 pr_info("Device %s not prepared for power transition: code %d\n",
2014 dev_name(dev), error);
2015 put_device(dev);
2016 break;
2018 dev->power.is_prepared = true;
2019 if (!list_empty(&dev->power.entry))
2020 list_move_tail(&dev->power.entry, &dpm_prepared_list);
2021 put_device(dev);
2023 mutex_unlock(&dpm_list_mtx);
2024 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
2025 return error;
2029 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2030 * @state: PM transition of the system being carried out.
2032 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2033 * callbacks for them.
2035 int dpm_suspend_start(pm_message_t state)
2037 int error;
2039 error = dpm_prepare(state);
2040 if (error) {
2041 suspend_stats.failed_prepare++;
2042 dpm_save_failed_step(SUSPEND_PREPARE);
2043 } else
2044 error = dpm_suspend(state);
2045 return error;
2047 EXPORT_SYMBOL_GPL(dpm_suspend_start);
2049 void __suspend_report_result(const char *function, void *fn, int ret)
2051 if (ret)
2052 pr_err("%s(): %pS returns %d\n", function, fn, ret);
2054 EXPORT_SYMBOL_GPL(__suspend_report_result);
2057 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2058 * @subordinate: Device that needs to wait for @dev.
2059 * @dev: Device to wait for.
2061 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
2063 dpm_wait(dev, subordinate->power.async_suspend);
2064 return async_error;
2066 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
2069 * dpm_for_each_dev - device iterator.
2070 * @data: data for the callback.
2071 * @fn: function to be called for each device.
2073 * Iterate over devices in dpm_list, and call @fn for each device,
2074 * passing it @data.
2076 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
2078 struct device *dev;
2080 if (!fn)
2081 return;
2083 device_pm_lock();
2084 list_for_each_entry(dev, &dpm_list, power.entry)
2085 fn(dev, data);
2086 device_pm_unlock();
2088 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
2090 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
2092 if (!ops)
2093 return true;
2095 return !ops->prepare &&
2096 !ops->suspend &&
2097 !ops->suspend_late &&
2098 !ops->suspend_noirq &&
2099 !ops->resume_noirq &&
2100 !ops->resume_early &&
2101 !ops->resume &&
2102 !ops->complete;
2105 void device_pm_check_callbacks(struct device *dev)
2107 spin_lock_irq(&dev->power.lock);
2108 dev->power.no_pm_callbacks =
2109 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
2110 !dev->bus->suspend && !dev->bus->resume)) &&
2111 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
2112 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
2113 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
2114 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
2115 !dev->driver->suspend && !dev->driver->resume));
2116 spin_unlock_irq(&dev->power.lock);
2119 bool dev_pm_smart_suspend_and_suspended(struct device *dev)
2121 return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) &&
2122 pm_runtime_status_suspended(dev);