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>
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>
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
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
)
67 case PM_EVENT_SUSPEND
:
73 case PM_EVENT_QUIESCE
:
75 case PM_EVENT_HIBERNATE
:
79 case PM_EVENT_RESTORE
:
81 case PM_EVENT_RECOVER
:
84 return "(unknown PM event)";
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
)
197 ktime_t calltime
= 0;
199 if (pm_print_times_enabled
) {
200 pr_info("calling %s+ @ %i, parent: %s\n",
201 dev_name(dev
), task_pid_nr(current
),
202 dev
->parent
? dev_name(dev
->parent
) : "none");
203 calltime
= ktime_get();
209 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
210 int error
, pm_message_t state
,
216 rettime
= ktime_get();
217 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
219 if (pm_print_times_enabled
) {
220 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
221 error
, (unsigned long long)nsecs
>> 10);
226 * dpm_wait - Wait for a PM operation to complete.
227 * @dev: Device to wait for.
228 * @async: If unset, wait only if the device's power.async_suspend flag is set.
230 static void dpm_wait(struct device
*dev
, bool async
)
235 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
236 wait_for_completion(&dev
->power
.completion
);
239 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
241 dpm_wait(dev
, *((bool *)async_ptr
));
245 static void dpm_wait_for_children(struct device
*dev
, bool async
)
247 device_for_each_child(dev
, &async
, dpm_wait_fn
);
250 static void dpm_wait_for_suppliers(struct device
*dev
, bool async
)
252 struct device_link
*link
;
255 idx
= device_links_read_lock();
258 * If the supplier goes away right after we've checked the link to it,
259 * we'll wait for its completion to change the state, but that's fine,
260 * because the only things that will block as a result are the SRCU
261 * callbacks freeing the link objects for the links in the list we're
264 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
265 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
266 dpm_wait(link
->supplier
, async
);
268 device_links_read_unlock(idx
);
271 static void dpm_wait_for_superior(struct device
*dev
, bool async
)
273 dpm_wait(dev
->parent
, async
);
274 dpm_wait_for_suppliers(dev
, async
);
277 static void dpm_wait_for_consumers(struct device
*dev
, bool async
)
279 struct device_link
*link
;
282 idx
= device_links_read_lock();
285 * The status of a device link can only be changed from "dormant" by a
286 * probe, but that cannot happen during system suspend/resume. In
287 * theory it can change to "dormant" at that time, but then it is
288 * reasonable to wait for the target device anyway (eg. if it goes
289 * away, it's better to wait for it to go away completely and then
290 * continue instead of trying to continue in parallel with its
293 list_for_each_entry_rcu(link
, &dev
->links
.consumers
, s_node
)
294 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
295 dpm_wait(link
->consumer
, async
);
297 device_links_read_unlock(idx
);
300 static void dpm_wait_for_subordinate(struct device
*dev
, bool async
)
302 dpm_wait_for_children(dev
, async
);
303 dpm_wait_for_consumers(dev
, async
);
307 * pm_op - Return the PM operation appropriate for given PM event.
308 * @ops: PM operations to choose from.
309 * @state: PM transition of the system being carried out.
311 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
313 switch (state
.event
) {
314 #ifdef CONFIG_SUSPEND
315 case PM_EVENT_SUSPEND
:
317 case PM_EVENT_RESUME
:
319 #endif /* CONFIG_SUSPEND */
320 #ifdef CONFIG_HIBERNATE_CALLBACKS
321 case PM_EVENT_FREEZE
:
322 case PM_EVENT_QUIESCE
:
324 case PM_EVENT_HIBERNATE
:
325 return ops
->poweroff
;
327 case PM_EVENT_RECOVER
:
330 case PM_EVENT_RESTORE
:
332 #endif /* CONFIG_HIBERNATE_CALLBACKS */
339 * pm_late_early_op - Return the PM operation appropriate for given PM event.
340 * @ops: PM operations to choose from.
341 * @state: PM transition of the system being carried out.
343 * Runtime PM is disabled for @dev while this function is being executed.
345 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
348 switch (state
.event
) {
349 #ifdef CONFIG_SUSPEND
350 case PM_EVENT_SUSPEND
:
351 return ops
->suspend_late
;
352 case PM_EVENT_RESUME
:
353 return ops
->resume_early
;
354 #endif /* CONFIG_SUSPEND */
355 #ifdef CONFIG_HIBERNATE_CALLBACKS
356 case PM_EVENT_FREEZE
:
357 case PM_EVENT_QUIESCE
:
358 return ops
->freeze_late
;
359 case PM_EVENT_HIBERNATE
:
360 return ops
->poweroff_late
;
362 case PM_EVENT_RECOVER
:
363 return ops
->thaw_early
;
364 case PM_EVENT_RESTORE
:
365 return ops
->restore_early
;
366 #endif /* CONFIG_HIBERNATE_CALLBACKS */
373 * pm_noirq_op - Return the PM operation appropriate for given PM event.
374 * @ops: PM operations to choose from.
375 * @state: PM transition of the system being carried out.
377 * The driver of @dev will not receive interrupts while this function is being
380 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
382 switch (state
.event
) {
383 #ifdef CONFIG_SUSPEND
384 case PM_EVENT_SUSPEND
:
385 return ops
->suspend_noirq
;
386 case PM_EVENT_RESUME
:
387 return ops
->resume_noirq
;
388 #endif /* CONFIG_SUSPEND */
389 #ifdef CONFIG_HIBERNATE_CALLBACKS
390 case PM_EVENT_FREEZE
:
391 case PM_EVENT_QUIESCE
:
392 return ops
->freeze_noirq
;
393 case PM_EVENT_HIBERNATE
:
394 return ops
->poweroff_noirq
;
396 case PM_EVENT_RECOVER
:
397 return ops
->thaw_noirq
;
398 case PM_EVENT_RESTORE
:
399 return ops
->restore_noirq
;
400 #endif /* CONFIG_HIBERNATE_CALLBACKS */
406 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, const char *info
)
408 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
409 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
410 ", may wakeup" : "");
413 static void pm_dev_err(struct device
*dev
, pm_message_t state
, const char *info
,
416 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
417 dev_name(dev
), pm_verb(state
.event
), info
, error
);
420 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, int error
,
427 calltime
= ktime_get();
428 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
429 do_div(usecs64
, NSEC_PER_USEC
);
434 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
435 info
?: "", info
? " " : "", pm_verb(state
.event
),
436 error
? "aborted" : "complete",
437 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
440 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
441 pm_message_t state
, const char *info
)
449 calltime
= initcall_debug_start(dev
);
451 pm_dev_dbg(dev
, state
, info
);
452 trace_device_pm_callback_start(dev
, info
, state
.event
);
454 trace_device_pm_callback_end(dev
, error
);
455 suspend_report_result(cb
, error
);
457 initcall_debug_report(dev
, calltime
, error
, state
, info
);
462 #ifdef CONFIG_DPM_WATCHDOG
463 struct dpm_watchdog
{
465 struct task_struct
*tsk
;
466 struct timer_list timer
;
469 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
470 struct dpm_watchdog wd
473 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
474 * @data: Watchdog object address.
476 * Called when a driver has timed out suspending or resuming.
477 * There's not much we can do here to recover so panic() to
478 * capture a crash-dump in pstore.
480 static void dpm_watchdog_handler(struct timer_list
*t
)
482 struct dpm_watchdog
*wd
= from_timer(wd
, t
, timer
);
484 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
485 show_stack(wd
->tsk
, NULL
);
486 panic("%s %s: unrecoverable failure\n",
487 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
491 * dpm_watchdog_set - Enable pm watchdog for given device.
492 * @wd: Watchdog. Must be allocated on the stack.
493 * @dev: Device to handle.
495 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
497 struct timer_list
*timer
= &wd
->timer
;
502 timer_setup_on_stack(timer
, dpm_watchdog_handler
, 0);
503 /* use same timeout value for both suspend and resume */
504 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
509 * dpm_watchdog_clear - Disable suspend/resume watchdog.
510 * @wd: Watchdog to disable.
512 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
514 struct timer_list
*timer
= &wd
->timer
;
516 del_timer_sync(timer
);
517 destroy_timer_on_stack(timer
);
520 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
521 #define dpm_watchdog_set(x, y)
522 #define dpm_watchdog_clear(x)
525 /*------------------------- Resume routines -------------------------*/
528 * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
529 * @dev: Target device.
531 * Make the core skip the "early resume" and "resume" phases for @dev.
533 * This function can be called by middle-layer code during the "noirq" phase of
534 * system resume if necessary, but not by device drivers.
536 void dev_pm_skip_next_resume_phases(struct device
*dev
)
538 dev
->power
.is_late_suspended
= false;
539 dev
->power
.is_suspended
= false;
543 * suspend_event - Return a "suspend" message for given "resume" one.
544 * @resume_msg: PM message representing a system-wide resume transition.
546 static pm_message_t
suspend_event(pm_message_t resume_msg
)
548 switch (resume_msg
.event
) {
549 case PM_EVENT_RESUME
:
552 case PM_EVENT_RESTORE
:
554 case PM_EVENT_RECOVER
:
555 return PMSG_HIBERNATE
;
561 * dev_pm_may_skip_resume - System-wide device resume optimization check.
562 * @dev: Target device.
564 * Checks whether or not the device may be left in suspend after a system-wide
565 * transition to the working state.
567 bool dev_pm_may_skip_resume(struct device
*dev
)
569 return !dev
->power
.must_resume
&& pm_transition
.event
!= PM_EVENT_RESTORE
;
572 static pm_callback_t
dpm_subsys_resume_noirq_cb(struct device
*dev
,
576 pm_callback_t callback
;
579 if (dev
->pm_domain
) {
580 info
= "noirq power domain ";
581 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
582 } else if (dev
->type
&& dev
->type
->pm
) {
583 info
= "noirq type ";
584 callback
= pm_noirq_op(dev
->type
->pm
, state
);
585 } else if (dev
->class && dev
->class->pm
) {
586 info
= "noirq class ";
587 callback
= pm_noirq_op(dev
->class->pm
, state
);
588 } else if (dev
->bus
&& dev
->bus
->pm
) {
590 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
601 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
603 const char **info_p
);
605 static pm_callback_t
dpm_subsys_suspend_late_cb(struct device
*dev
,
607 const char **info_p
);
610 * device_resume_noirq - Execute a "noirq resume" callback for given device.
611 * @dev: Device to handle.
612 * @state: PM transition of the system being carried out.
613 * @async: If true, the device is being resumed asynchronously.
615 * The driver of @dev will not receive interrupts while this function is being
618 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
620 pm_callback_t callback
;
628 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
631 if (!dev
->power
.is_noirq_suspended
)
634 dpm_wait_for_superior(dev
, async
);
636 skip_resume
= dev_pm_may_skip_resume(dev
);
638 callback
= dpm_subsys_resume_noirq_cb(dev
, state
, &info
);
645 if (dev_pm_smart_suspend_and_suspended(dev
)) {
646 pm_message_t suspend_msg
= suspend_event(state
);
649 * If "freeze" callbacks have been skipped during a transition
650 * related to hibernation, the subsequent "thaw" callbacks must
651 * be skipped too or bad things may happen. Otherwise, resume
652 * callbacks are going to be run for the device, so its runtime
653 * PM status must be changed to reflect the new state after the
654 * transition under way.
656 if (!dpm_subsys_suspend_late_cb(dev
, suspend_msg
, NULL
) &&
657 !dpm_subsys_suspend_noirq_cb(dev
, suspend_msg
, NULL
)) {
658 if (state
.event
== PM_EVENT_THAW
) {
662 pm_runtime_set_active(dev
);
667 if (dev
->driver
&& dev
->driver
->pm
) {
668 info
= "noirq driver ";
669 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
673 error
= dpm_run_callback(callback
, dev
, state
, info
);
676 dev
->power
.is_noirq_suspended
= false;
680 * The device is going to be left in suspend, but it might not
681 * have been in runtime suspend before the system suspended, so
682 * its runtime PM status needs to be updated to avoid confusing
683 * the runtime PM framework when runtime PM is enabled for the
686 pm_runtime_set_suspended(dev
);
687 dev_pm_skip_next_resume_phases(dev
);
691 complete_all(&dev
->power
.completion
);
696 static bool is_async(struct device
*dev
)
698 return dev
->power
.async_suspend
&& pm_async_enabled
699 && !pm_trace_is_enabled();
702 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
704 struct device
*dev
= (struct device
*)data
;
707 error
= device_resume_noirq(dev
, pm_transition
, true);
709 pm_dev_err(dev
, pm_transition
, " async", error
);
714 void dpm_noirq_resume_devices(pm_message_t state
)
717 ktime_t starttime
= ktime_get();
719 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
720 mutex_lock(&dpm_list_mtx
);
721 pm_transition
= state
;
724 * Advanced the async threads upfront,
725 * in case the starting of async threads is
726 * delayed by non-async resuming devices.
728 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
729 reinit_completion(&dev
->power
.completion
);
732 async_schedule(async_resume_noirq
, dev
);
736 while (!list_empty(&dpm_noirq_list
)) {
737 dev
= to_device(dpm_noirq_list
.next
);
739 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
740 mutex_unlock(&dpm_list_mtx
);
742 if (!is_async(dev
)) {
745 error
= device_resume_noirq(dev
, state
, false);
747 suspend_stats
.failed_resume_noirq
++;
748 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
749 dpm_save_failed_dev(dev_name(dev
));
750 pm_dev_err(dev
, state
, " noirq", error
);
754 mutex_lock(&dpm_list_mtx
);
757 mutex_unlock(&dpm_list_mtx
);
758 async_synchronize_full();
759 dpm_show_time(starttime
, state
, 0, "noirq");
760 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
763 void dpm_noirq_end(void)
765 resume_device_irqs();
766 device_wakeup_disarm_wake_irqs();
771 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
772 * @state: PM transition of the system being carried out.
774 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
775 * allow device drivers' interrupt handlers to be called.
777 void dpm_resume_noirq(pm_message_t state
)
779 dpm_noirq_resume_devices(state
);
783 static pm_callback_t
dpm_subsys_resume_early_cb(struct device
*dev
,
787 pm_callback_t callback
;
790 if (dev
->pm_domain
) {
791 info
= "early power domain ";
792 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
793 } else if (dev
->type
&& dev
->type
->pm
) {
794 info
= "early type ";
795 callback
= pm_late_early_op(dev
->type
->pm
, state
);
796 } else if (dev
->class && dev
->class->pm
) {
797 info
= "early class ";
798 callback
= pm_late_early_op(dev
->class->pm
, state
);
799 } else if (dev
->bus
&& dev
->bus
->pm
) {
801 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
813 * device_resume_early - Execute an "early resume" callback for given device.
814 * @dev: Device to handle.
815 * @state: PM transition of the system being carried out.
816 * @async: If true, the device is being resumed asynchronously.
818 * Runtime PM is disabled for @dev while this function is being executed.
820 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
822 pm_callback_t callback
;
829 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
832 if (!dev
->power
.is_late_suspended
)
835 dpm_wait_for_superior(dev
, async
);
837 callback
= dpm_subsys_resume_early_cb(dev
, state
, &info
);
839 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
840 info
= "early driver ";
841 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
844 error
= dpm_run_callback(callback
, dev
, state
, info
);
845 dev
->power
.is_late_suspended
= false;
850 pm_runtime_enable(dev
);
851 complete_all(&dev
->power
.completion
);
855 static void async_resume_early(void *data
, async_cookie_t cookie
)
857 struct device
*dev
= (struct device
*)data
;
860 error
= device_resume_early(dev
, pm_transition
, true);
862 pm_dev_err(dev
, pm_transition
, " async", error
);
868 * dpm_resume_early - Execute "early resume" callbacks for all devices.
869 * @state: PM transition of the system being carried out.
871 void dpm_resume_early(pm_message_t state
)
874 ktime_t starttime
= ktime_get();
876 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
877 mutex_lock(&dpm_list_mtx
);
878 pm_transition
= state
;
881 * Advanced the async threads upfront,
882 * in case the starting of async threads is
883 * delayed by non-async resuming devices.
885 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
886 reinit_completion(&dev
->power
.completion
);
889 async_schedule(async_resume_early
, dev
);
893 while (!list_empty(&dpm_late_early_list
)) {
894 dev
= to_device(dpm_late_early_list
.next
);
896 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
897 mutex_unlock(&dpm_list_mtx
);
899 if (!is_async(dev
)) {
902 error
= device_resume_early(dev
, state
, false);
904 suspend_stats
.failed_resume_early
++;
905 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
906 dpm_save_failed_dev(dev_name(dev
));
907 pm_dev_err(dev
, state
, " early", error
);
910 mutex_lock(&dpm_list_mtx
);
913 mutex_unlock(&dpm_list_mtx
);
914 async_synchronize_full();
915 dpm_show_time(starttime
, state
, 0, "early");
916 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
920 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
921 * @state: PM transition of the system being carried out.
923 void dpm_resume_start(pm_message_t state
)
925 dpm_resume_noirq(state
);
926 dpm_resume_early(state
);
928 EXPORT_SYMBOL_GPL(dpm_resume_start
);
931 * device_resume - Execute "resume" callbacks for given device.
932 * @dev: Device to handle.
933 * @state: PM transition of the system being carried out.
934 * @async: If true, the device is being resumed asynchronously.
936 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
938 pm_callback_t callback
= NULL
;
939 const char *info
= NULL
;
941 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
946 if (dev
->power
.syscore
)
949 if (dev
->power
.direct_complete
) {
950 /* Match the pm_runtime_disable() in __device_suspend(). */
951 pm_runtime_enable(dev
);
955 dpm_wait_for_superior(dev
, async
);
956 dpm_watchdog_set(&wd
, dev
);
960 * This is a fib. But we'll allow new children to be added below
961 * a resumed device, even if the device hasn't been completed yet.
963 dev
->power
.is_prepared
= false;
965 if (!dev
->power
.is_suspended
)
968 if (dev
->pm_domain
) {
969 info
= "power domain ";
970 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
974 if (dev
->type
&& dev
->type
->pm
) {
976 callback
= pm_op(dev
->type
->pm
, state
);
980 if (dev
->class && dev
->class->pm
) {
982 callback
= pm_op(dev
->class->pm
, state
);
989 callback
= pm_op(dev
->bus
->pm
, state
);
990 } else if (dev
->bus
->resume
) {
991 info
= "legacy bus ";
992 callback
= dev
->bus
->resume
;
998 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1000 callback
= pm_op(dev
->driver
->pm
, state
);
1004 error
= dpm_run_callback(callback
, dev
, state
, info
);
1005 dev
->power
.is_suspended
= false;
1009 dpm_watchdog_clear(&wd
);
1012 complete_all(&dev
->power
.completion
);
1014 TRACE_RESUME(error
);
1019 static void async_resume(void *data
, async_cookie_t cookie
)
1021 struct device
*dev
= (struct device
*)data
;
1024 error
= device_resume(dev
, pm_transition
, true);
1026 pm_dev_err(dev
, pm_transition
, " async", error
);
1031 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1032 * @state: PM transition of the system being carried out.
1034 * Execute the appropriate "resume" callback for all devices whose status
1035 * indicates that they are suspended.
1037 void dpm_resume(pm_message_t state
)
1040 ktime_t starttime
= ktime_get();
1042 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
1045 mutex_lock(&dpm_list_mtx
);
1046 pm_transition
= state
;
1049 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
1050 reinit_completion(&dev
->power
.completion
);
1051 if (is_async(dev
)) {
1053 async_schedule(async_resume
, dev
);
1057 while (!list_empty(&dpm_suspended_list
)) {
1058 dev
= to_device(dpm_suspended_list
.next
);
1060 if (!is_async(dev
)) {
1063 mutex_unlock(&dpm_list_mtx
);
1065 error
= device_resume(dev
, state
, false);
1067 suspend_stats
.failed_resume
++;
1068 dpm_save_failed_step(SUSPEND_RESUME
);
1069 dpm_save_failed_dev(dev_name(dev
));
1070 pm_dev_err(dev
, state
, "", error
);
1073 mutex_lock(&dpm_list_mtx
);
1075 if (!list_empty(&dev
->power
.entry
))
1076 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1079 mutex_unlock(&dpm_list_mtx
);
1080 async_synchronize_full();
1081 dpm_show_time(starttime
, state
, 0, NULL
);
1084 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
1088 * device_complete - Complete a PM transition for given device.
1089 * @dev: Device to handle.
1090 * @state: PM transition of the system being carried out.
1092 static void device_complete(struct device
*dev
, pm_message_t state
)
1094 void (*callback
)(struct device
*) = NULL
;
1095 const char *info
= NULL
;
1097 if (dev
->power
.syscore
)
1102 if (dev
->pm_domain
) {
1103 info
= "completing power domain ";
1104 callback
= dev
->pm_domain
->ops
.complete
;
1105 } else if (dev
->type
&& dev
->type
->pm
) {
1106 info
= "completing type ";
1107 callback
= dev
->type
->pm
->complete
;
1108 } else if (dev
->class && dev
->class->pm
) {
1109 info
= "completing class ";
1110 callback
= dev
->class->pm
->complete
;
1111 } else if (dev
->bus
&& dev
->bus
->pm
) {
1112 info
= "completing bus ";
1113 callback
= dev
->bus
->pm
->complete
;
1116 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1117 info
= "completing driver ";
1118 callback
= dev
->driver
->pm
->complete
;
1122 pm_dev_dbg(dev
, state
, info
);
1128 pm_runtime_put(dev
);
1132 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1133 * @state: PM transition of the system being carried out.
1135 * Execute the ->complete() callbacks for all devices whose PM status is not
1136 * DPM_ON (this allows new devices to be registered).
1138 void dpm_complete(pm_message_t state
)
1140 struct list_head list
;
1142 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
1145 INIT_LIST_HEAD(&list
);
1146 mutex_lock(&dpm_list_mtx
);
1147 while (!list_empty(&dpm_prepared_list
)) {
1148 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1151 dev
->power
.is_prepared
= false;
1152 list_move(&dev
->power
.entry
, &list
);
1153 mutex_unlock(&dpm_list_mtx
);
1155 trace_device_pm_callback_start(dev
, "", state
.event
);
1156 device_complete(dev
, state
);
1157 trace_device_pm_callback_end(dev
, 0);
1159 mutex_lock(&dpm_list_mtx
);
1162 list_splice(&list
, &dpm_list
);
1163 mutex_unlock(&dpm_list_mtx
);
1165 /* Allow device probing and trigger re-probing of deferred devices */
1166 device_unblock_probing();
1167 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
1171 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1172 * @state: PM transition of the system being carried out.
1174 * Execute "resume" callbacks for all devices and complete the PM transition of
1177 void dpm_resume_end(pm_message_t state
)
1180 dpm_complete(state
);
1182 EXPORT_SYMBOL_GPL(dpm_resume_end
);
1185 /*------------------------- Suspend routines -------------------------*/
1188 * resume_event - Return a "resume" message for given "suspend" sleep state.
1189 * @sleep_state: PM message representing a sleep state.
1191 * Return a PM message representing the resume event corresponding to given
1194 static pm_message_t
resume_event(pm_message_t sleep_state
)
1196 switch (sleep_state
.event
) {
1197 case PM_EVENT_SUSPEND
:
1199 case PM_EVENT_FREEZE
:
1200 case PM_EVENT_QUIESCE
:
1201 return PMSG_RECOVER
;
1202 case PM_EVENT_HIBERNATE
:
1203 return PMSG_RESTORE
;
1208 static void dpm_superior_set_must_resume(struct device
*dev
)
1210 struct device_link
*link
;
1214 dev
->parent
->power
.must_resume
= true;
1216 idx
= device_links_read_lock();
1218 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
1219 link
->supplier
->power
.must_resume
= true;
1221 device_links_read_unlock(idx
);
1224 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
1226 const char **info_p
)
1228 pm_callback_t callback
;
1231 if (dev
->pm_domain
) {
1232 info
= "noirq power domain ";
1233 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1234 } else if (dev
->type
&& dev
->type
->pm
) {
1235 info
= "noirq type ";
1236 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1237 } else if (dev
->class && dev
->class->pm
) {
1238 info
= "noirq class ";
1239 callback
= pm_noirq_op(dev
->class->pm
, state
);
1240 } else if (dev
->bus
&& dev
->bus
->pm
) {
1241 info
= "noirq bus ";
1242 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1253 static bool device_must_resume(struct device
*dev
, pm_message_t state
,
1254 bool no_subsys_suspend_noirq
)
1256 pm_message_t resume_msg
= resume_event(state
);
1259 * If all of the device driver's "noirq", "late" and "early" callbacks
1260 * are invoked directly by the core, the decision to allow the device to
1261 * stay in suspend can be based on its current runtime PM status and its
1264 if (no_subsys_suspend_noirq
&&
1265 !dpm_subsys_suspend_late_cb(dev
, state
, NULL
) &&
1266 !dpm_subsys_resume_early_cb(dev
, resume_msg
, NULL
) &&
1267 !dpm_subsys_resume_noirq_cb(dev
, resume_msg
, NULL
))
1268 return !pm_runtime_status_suspended(dev
) &&
1269 (resume_msg
.event
!= PM_EVENT_RESUME
||
1270 (device_can_wakeup(dev
) && !device_may_wakeup(dev
)));
1273 * The only safe strategy here is to require that if the device may not
1274 * be left in suspend, resume callbacks must be invoked for it.
1276 return !dev
->power
.may_skip_resume
;
1280 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1281 * @dev: Device to handle.
1282 * @state: PM transition of the system being carried out.
1283 * @async: If true, the device is being suspended asynchronously.
1285 * The driver of @dev will not receive interrupts while this function is being
1288 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1290 pm_callback_t callback
;
1292 bool no_subsys_cb
= false;
1298 dpm_wait_for_subordinate(dev
, async
);
1303 if (pm_wakeup_pending()) {
1304 async_error
= -EBUSY
;
1308 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1311 callback
= dpm_subsys_suspend_noirq_cb(dev
, state
, &info
);
1315 no_subsys_cb
= !dpm_subsys_suspend_late_cb(dev
, state
, NULL
);
1317 if (dev_pm_smart_suspend_and_suspended(dev
) && no_subsys_cb
)
1320 if (dev
->driver
&& dev
->driver
->pm
) {
1321 info
= "noirq driver ";
1322 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1326 error
= dpm_run_callback(callback
, dev
, state
, info
);
1328 async_error
= error
;
1333 dev
->power
.is_noirq_suspended
= true;
1335 if (dev_pm_test_driver_flags(dev
, DPM_FLAG_LEAVE_SUSPENDED
)) {
1336 dev
->power
.must_resume
= dev
->power
.must_resume
||
1337 atomic_read(&dev
->power
.usage_count
) > 1 ||
1338 device_must_resume(dev
, state
, no_subsys_cb
);
1340 dev
->power
.must_resume
= true;
1343 if (dev
->power
.must_resume
)
1344 dpm_superior_set_must_resume(dev
);
1347 complete_all(&dev
->power
.completion
);
1348 TRACE_SUSPEND(error
);
1352 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1354 struct device
*dev
= (struct device
*)data
;
1357 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1359 dpm_save_failed_dev(dev_name(dev
));
1360 pm_dev_err(dev
, pm_transition
, " async", error
);
1366 static int device_suspend_noirq(struct device
*dev
)
1368 reinit_completion(&dev
->power
.completion
);
1370 if (is_async(dev
)) {
1372 async_schedule(async_suspend_noirq
, dev
);
1375 return __device_suspend_noirq(dev
, pm_transition
, false);
1378 void dpm_noirq_begin(void)
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();
1390 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1391 mutex_lock(&dpm_list_mtx
);
1392 pm_transition
= state
;
1395 while (!list_empty(&dpm_late_early_list
)) {
1396 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1399 mutex_unlock(&dpm_list_mtx
);
1401 error
= device_suspend_noirq(dev
);
1403 mutex_lock(&dpm_list_mtx
);
1405 pm_dev_err(dev
, state
, " noirq", error
);
1406 dpm_save_failed_dev(dev_name(dev
));
1410 if (!list_empty(&dev
->power
.entry
))
1411 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1417 mutex_unlock(&dpm_list_mtx
);
1418 async_synchronize_full();
1420 error
= async_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);
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
)
1443 ret
= dpm_noirq_suspend_devices(state
);
1445 dpm_resume_noirq(resume_event(state
));
1450 static void dpm_propagate_wakeup_to_parent(struct device
*dev
)
1452 struct device
*parent
= dev
->parent
;
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
,
1467 const char **info_p
)
1469 pm_callback_t callback
;
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
) {
1483 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
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
;
1511 __pm_runtime_disable(dev
, false);
1513 dpm_wait_for_subordinate(dev
, async
);
1518 if (pm_wakeup_pending()) {
1519 async_error
= -EBUSY
;
1523 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1526 callback
= dpm_subsys_suspend_late_cb(dev
, state
, &info
);
1530 if (dev_pm_smart_suspend_and_suspended(dev
) &&
1531 !dpm_subsys_suspend_noirq_cb(dev
, state
, NULL
))
1534 if (dev
->driver
&& dev
->driver
->pm
) {
1535 info
= "late driver ";
1536 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1540 error
= dpm_run_callback(callback
, dev
, state
, info
);
1542 async_error
= error
;
1545 dpm_propagate_wakeup_to_parent(dev
);
1548 dev
->power
.is_late_suspended
= true;
1551 TRACE_SUSPEND(error
);
1552 complete_all(&dev
->power
.completion
);
1556 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1558 struct device
*dev
= (struct device
*)data
;
1561 error
= __device_suspend_late(dev
, pm_transition
, true);
1563 dpm_save_failed_dev(dev_name(dev
));
1564 pm_dev_err(dev
, pm_transition
, " async", error
);
1569 static int device_suspend_late(struct device
*dev
)
1571 reinit_completion(&dev
->power
.completion
);
1573 if (is_async(dev
)) {
1575 async_schedule(async_suspend_late
, dev
);
1579 return __device_suspend_late(dev
, pm_transition
, false);
1583 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1584 * @state: PM transition of the system being carried out.
1586 int dpm_suspend_late(pm_message_t state
)
1588 ktime_t starttime
= ktime_get();
1591 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1592 mutex_lock(&dpm_list_mtx
);
1593 pm_transition
= state
;
1596 while (!list_empty(&dpm_suspended_list
)) {
1597 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1600 mutex_unlock(&dpm_list_mtx
);
1602 error
= device_suspend_late(dev
);
1604 mutex_lock(&dpm_list_mtx
);
1605 if (!list_empty(&dev
->power
.entry
))
1606 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1609 pm_dev_err(dev
, state
, " late", error
);
1610 dpm_save_failed_dev(dev_name(dev
));
1619 mutex_unlock(&dpm_list_mtx
);
1620 async_synchronize_full();
1622 error
= async_error
;
1624 suspend_stats
.failed_suspend_late
++;
1625 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1626 dpm_resume_early(resume_event(state
));
1628 dpm_show_time(starttime
, state
, error
, "late");
1629 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1634 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1635 * @state: PM transition of the system being carried out.
1637 int dpm_suspend_end(pm_message_t state
)
1639 int error
= dpm_suspend_late(state
);
1643 error
= dpm_suspend_noirq(state
);
1645 dpm_resume_early(resume_event(state
));
1651 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1654 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1655 * @dev: Device to suspend.
1656 * @state: PM transition of the system being carried out.
1657 * @cb: Suspend callback to execute.
1658 * @info: string description of caller.
1660 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1661 int (*cb
)(struct device
*dev
, pm_message_t state
),
1667 calltime
= initcall_debug_start(dev
);
1669 trace_device_pm_callback_start(dev
, info
, state
.event
);
1670 error
= cb(dev
, state
);
1671 trace_device_pm_callback_end(dev
, error
);
1672 suspend_report_result(cb
, error
);
1674 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1679 static void dpm_clear_superiors_direct_complete(struct device
*dev
)
1681 struct device_link
*link
;
1685 spin_lock_irq(&dev
->parent
->power
.lock
);
1686 dev
->parent
->power
.direct_complete
= false;
1687 spin_unlock_irq(&dev
->parent
->power
.lock
);
1690 idx
= device_links_read_lock();
1692 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
) {
1693 spin_lock_irq(&link
->supplier
->power
.lock
);
1694 link
->supplier
->power
.direct_complete
= false;
1695 spin_unlock_irq(&link
->supplier
->power
.lock
);
1698 device_links_read_unlock(idx
);
1702 * __device_suspend - Execute "suspend" callbacks for given device.
1703 * @dev: Device to handle.
1704 * @state: PM transition of the system being carried out.
1705 * @async: If true, the device is being suspended asynchronously.
1707 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1709 pm_callback_t callback
= NULL
;
1710 const char *info
= NULL
;
1712 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1717 dpm_wait_for_subordinate(dev
, async
);
1723 * If a device configured to wake up the system from sleep states
1724 * has been suspended at run time and there's a resume request pending
1725 * for it, this is equivalent to the device signaling wakeup, so the
1726 * system suspend operation should be aborted.
1728 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1729 pm_wakeup_event(dev
, 0);
1731 if (pm_wakeup_pending()) {
1732 async_error
= -EBUSY
;
1736 if (dev
->power
.syscore
)
1739 if (dev
->power
.direct_complete
) {
1740 if (pm_runtime_status_suspended(dev
)) {
1741 pm_runtime_disable(dev
);
1742 if (pm_runtime_status_suspended(dev
))
1745 pm_runtime_enable(dev
);
1747 dev
->power
.direct_complete
= false;
1750 dev
->power
.may_skip_resume
= false;
1751 dev
->power
.must_resume
= false;
1753 dpm_watchdog_set(&wd
, dev
);
1756 if (dev
->pm_domain
) {
1757 info
= "power domain ";
1758 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1762 if (dev
->type
&& dev
->type
->pm
) {
1764 callback
= pm_op(dev
->type
->pm
, state
);
1768 if (dev
->class && dev
->class->pm
) {
1770 callback
= pm_op(dev
->class->pm
, state
);
1777 callback
= pm_op(dev
->bus
->pm
, state
);
1778 } else if (dev
->bus
->suspend
) {
1779 pm_dev_dbg(dev
, state
, "legacy bus ");
1780 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1787 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1789 callback
= pm_op(dev
->driver
->pm
, state
);
1792 error
= dpm_run_callback(callback
, dev
, state
, info
);
1796 dev
->power
.is_suspended
= true;
1797 if (device_may_wakeup(dev
))
1798 dev
->power
.wakeup_path
= true;
1800 dpm_propagate_wakeup_to_parent(dev
);
1801 dpm_clear_superiors_direct_complete(dev
);
1805 dpm_watchdog_clear(&wd
);
1809 async_error
= error
;
1811 complete_all(&dev
->power
.completion
);
1812 TRACE_SUSPEND(error
);
1816 static void async_suspend(void *data
, async_cookie_t cookie
)
1818 struct device
*dev
= (struct device
*)data
;
1821 error
= __device_suspend(dev
, pm_transition
, true);
1823 dpm_save_failed_dev(dev_name(dev
));
1824 pm_dev_err(dev
, pm_transition
, " async", error
);
1830 static int device_suspend(struct device
*dev
)
1832 reinit_completion(&dev
->power
.completion
);
1834 if (is_async(dev
)) {
1836 async_schedule(async_suspend
, dev
);
1840 return __device_suspend(dev
, pm_transition
, false);
1844 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1845 * @state: PM transition of the system being carried out.
1847 int dpm_suspend(pm_message_t state
)
1849 ktime_t starttime
= ktime_get();
1852 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1857 mutex_lock(&dpm_list_mtx
);
1858 pm_transition
= state
;
1860 while (!list_empty(&dpm_prepared_list
)) {
1861 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1864 mutex_unlock(&dpm_list_mtx
);
1866 error
= device_suspend(dev
);
1868 mutex_lock(&dpm_list_mtx
);
1870 pm_dev_err(dev
, state
, "", error
);
1871 dpm_save_failed_dev(dev_name(dev
));
1875 if (!list_empty(&dev
->power
.entry
))
1876 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1881 mutex_unlock(&dpm_list_mtx
);
1882 async_synchronize_full();
1884 error
= async_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);
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
;
1907 if (dev
->power
.syscore
)
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
);
1924 dev
->power
.wakeup_path
= false;
1926 if (dev
->power
.no_pm_callbacks
) {
1927 ret
= 1; /* Let device go direct_complete */
1932 callback
= dev
->pm_domain
->ops
.prepare
;
1933 else if (dev
->type
&& dev
->type
->pm
)
1934 callback
= dev
->type
->pm
->prepare
;
1935 else if (dev
->class && dev
->class->pm
)
1936 callback
= dev
->class->pm
->prepare
;
1937 else if (dev
->bus
&& dev
->bus
->pm
)
1938 callback
= dev
->bus
->pm
->prepare
;
1940 if (!callback
&& dev
->driver
&& dev
->driver
->pm
)
1941 callback
= dev
->driver
->pm
->prepare
;
1944 ret
= callback(dev
);
1950 suspend_report_result(callback
, ret
);
1951 pm_runtime_put(dev
);
1955 * A positive return value from ->prepare() means "this device appears
1956 * to be runtime-suspended and its state is fine, so if it really is
1957 * runtime-suspended, you can leave it in that state provided that you
1958 * will do the same thing with all of its descendants". This only
1959 * applies to suspend transitions, however.
1961 spin_lock_irq(&dev
->power
.lock
);
1962 dev
->power
.direct_complete
= state
.event
== PM_EVENT_SUSPEND
&&
1963 pm_runtime_suspended(dev
) && ret
> 0 &&
1964 !dev_pm_test_driver_flags(dev
, DPM_FLAG_NEVER_SKIP
);
1965 spin_unlock_irq(&dev
->power
.lock
);
1970 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1971 * @state: PM transition of the system being carried out.
1973 * Execute the ->prepare() callback(s) for all devices.
1975 int dpm_prepare(pm_message_t state
)
1979 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
1983 * Give a chance for the known devices to complete their probes, before
1984 * disable probing of devices. This sync point is important at least
1985 * at boot time + hibernation restore.
1987 wait_for_device_probe();
1989 * It is unsafe if probing of devices will happen during suspend or
1990 * hibernation and system behavior will be unpredictable in this case.
1991 * So, let's prohibit device's probing here and defer their probes
1992 * instead. The normal behavior will be restored in dpm_complete().
1994 device_block_probing();
1996 mutex_lock(&dpm_list_mtx
);
1997 while (!list_empty(&dpm_list
)) {
1998 struct device
*dev
= to_device(dpm_list
.next
);
2001 mutex_unlock(&dpm_list_mtx
);
2003 trace_device_pm_callback_start(dev
, "", state
.event
);
2004 error
= device_prepare(dev
, state
);
2005 trace_device_pm_callback_end(dev
, error
);
2007 mutex_lock(&dpm_list_mtx
);
2009 if (error
== -EAGAIN
) {
2014 printk(KERN_INFO
"PM: Device %s not prepared "
2015 "for power transition: code %d\n",
2016 dev_name(dev
), error
);
2020 dev
->power
.is_prepared
= true;
2021 if (!list_empty(&dev
->power
.entry
))
2022 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
2025 mutex_unlock(&dpm_list_mtx
);
2026 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
2031 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2032 * @state: PM transition of the system being carried out.
2034 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2035 * callbacks for them.
2037 int dpm_suspend_start(pm_message_t state
)
2041 error
= dpm_prepare(state
);
2043 suspend_stats
.failed_prepare
++;
2044 dpm_save_failed_step(SUSPEND_PREPARE
);
2046 error
= dpm_suspend(state
);
2049 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
2051 void __suspend_report_result(const char *function
, void *fn
, int ret
)
2054 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
2056 EXPORT_SYMBOL_GPL(__suspend_report_result
);
2059 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2060 * @dev: Device to wait for.
2061 * @subordinate: Device that needs to wait for @dev.
2063 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
2065 dpm_wait(dev
, subordinate
->power
.async_suspend
);
2068 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
2071 * dpm_for_each_dev - device iterator.
2072 * @data: data for the callback.
2073 * @fn: function to be called for each device.
2075 * Iterate over devices in dpm_list, and call @fn for each device,
2078 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
2086 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
2090 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);
2092 static bool pm_ops_is_empty(const struct dev_pm_ops
*ops
)
2097 return !ops
->prepare
&&
2099 !ops
->suspend_late
&&
2100 !ops
->suspend_noirq
&&
2101 !ops
->resume_noirq
&&
2102 !ops
->resume_early
&&
2107 void device_pm_check_callbacks(struct device
*dev
)
2109 spin_lock_irq(&dev
->power
.lock
);
2110 dev
->power
.no_pm_callbacks
=
2111 (!dev
->bus
|| (pm_ops_is_empty(dev
->bus
->pm
) &&
2112 !dev
->bus
->suspend
&& !dev
->bus
->resume
)) &&
2113 (!dev
->class || pm_ops_is_empty(dev
->class->pm
)) &&
2114 (!dev
->type
|| pm_ops_is_empty(dev
->type
->pm
)) &&
2115 (!dev
->pm_domain
|| pm_ops_is_empty(&dev
->pm_domain
->ops
)) &&
2116 (!dev
->driver
|| (pm_ops_is_empty(dev
->driver
->pm
) &&
2117 !dev
->driver
->suspend
&& !dev
->driver
->resume
));
2118 spin_unlock_irq(&dev
->power
.lock
);
2121 bool dev_pm_smart_suspend_and_suspended(struct device
*dev
)
2123 return dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
) &&
2124 pm_runtime_status_suspended(dev
);