2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/resume-trace.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/async.h>
30 #include <linux/suspend.h>
31 #include <trace/events/power.h>
32 #include <linux/cpuidle.h>
33 #include <linux/timer.h>
38 typedef int (*pm_callback_t
)(struct device
*);
41 * The entries in the dpm_list list are in a depth first order, simply
42 * because children are guaranteed to be discovered after parents, and
43 * are inserted at the back of the list on discovery.
45 * Since device_pm_add() may be called with a device lock held,
46 * we must never try to acquire a device lock while holding
51 static LIST_HEAD(dpm_prepared_list
);
52 static LIST_HEAD(dpm_suspended_list
);
53 static LIST_HEAD(dpm_late_early_list
);
54 static LIST_HEAD(dpm_noirq_list
);
56 struct suspend_stats suspend_stats
;
57 static DEFINE_MUTEX(dpm_list_mtx
);
58 static pm_message_t pm_transition
;
60 static int async_error
;
62 static char *pm_verb(int event
)
65 case PM_EVENT_SUSPEND
:
71 case PM_EVENT_QUIESCE
:
73 case PM_EVENT_HIBERNATE
:
77 case PM_EVENT_RESTORE
:
79 case PM_EVENT_RECOVER
:
82 return "(unknown PM event)";
87 * device_pm_sleep_init - Initialize system suspend-related device fields.
88 * @dev: Device object being initialized.
90 void device_pm_sleep_init(struct device
*dev
)
92 dev
->power
.is_prepared
= false;
93 dev
->power
.is_suspended
= false;
94 dev
->power
.is_noirq_suspended
= false;
95 dev
->power
.is_late_suspended
= false;
96 init_completion(&dev
->power
.completion
);
97 complete_all(&dev
->power
.completion
);
98 dev
->power
.wakeup
= NULL
;
99 INIT_LIST_HEAD(&dev
->power
.entry
);
103 * device_pm_lock - Lock the list of active devices used by the PM core.
105 void device_pm_lock(void)
107 mutex_lock(&dpm_list_mtx
);
111 * device_pm_unlock - Unlock the list of active devices used by the PM core.
113 void device_pm_unlock(void)
115 mutex_unlock(&dpm_list_mtx
);
119 * device_pm_add - Add a device to the PM core's list of active devices.
120 * @dev: Device to add to the list.
122 void device_pm_add(struct device
*dev
)
124 pr_debug("PM: Adding info for %s:%s\n",
125 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
126 mutex_lock(&dpm_list_mtx
);
127 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
128 dev_warn(dev
, "parent %s should not be sleeping\n",
129 dev_name(dev
->parent
));
130 list_add_tail(&dev
->power
.entry
, &dpm_list
);
131 mutex_unlock(&dpm_list_mtx
);
135 * device_pm_remove - Remove a device from the PM core's list of active devices.
136 * @dev: Device to be removed from the list.
138 void device_pm_remove(struct device
*dev
)
140 pr_debug("PM: Removing info for %s:%s\n",
141 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
142 complete_all(&dev
->power
.completion
);
143 mutex_lock(&dpm_list_mtx
);
144 list_del_init(&dev
->power
.entry
);
145 mutex_unlock(&dpm_list_mtx
);
146 device_wakeup_disable(dev
);
147 pm_runtime_remove(dev
);
151 * device_pm_move_before - Move device in the PM core's list of active devices.
152 * @deva: Device to move in dpm_list.
153 * @devb: Device @deva should come before.
155 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
157 pr_debug("PM: Moving %s:%s before %s:%s\n",
158 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
159 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
160 /* Delete deva from dpm_list and reinsert before devb. */
161 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
165 * device_pm_move_after - 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 after.
169 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
171 pr_debug("PM: Moving %s:%s after %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 after devb. */
175 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
179 * device_pm_move_last - Move device to end of the PM core's list of devices.
180 * @dev: Device to move in dpm_list.
182 void device_pm_move_last(struct device
*dev
)
184 pr_debug("PM: Moving %s:%s to end of list\n",
185 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
186 list_move_tail(&dev
->power
.entry
, &dpm_list
);
189 static ktime_t
initcall_debug_start(struct device
*dev
)
191 ktime_t calltime
= ktime_set(0, 0);
193 if (pm_print_times_enabled
) {
194 pr_info("calling %s+ @ %i, parent: %s\n",
195 dev_name(dev
), task_pid_nr(current
),
196 dev
->parent
? dev_name(dev
->parent
) : "none");
197 calltime
= ktime_get();
203 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
204 int error
, pm_message_t state
, char *info
)
209 rettime
= ktime_get();
210 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
212 if (pm_print_times_enabled
) {
213 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
214 error
, (unsigned long long)nsecs
>> 10);
217 trace_device_pm_report_time(dev
, info
, nsecs
, pm_verb(state
.event
),
222 * dpm_wait - Wait for a PM operation to complete.
223 * @dev: Device to wait for.
224 * @async: If unset, wait only if the device's power.async_suspend flag is set.
226 static void dpm_wait(struct device
*dev
, bool async
)
231 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
232 wait_for_completion(&dev
->power
.completion
);
235 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
237 dpm_wait(dev
, *((bool *)async_ptr
));
241 static void dpm_wait_for_children(struct device
*dev
, bool async
)
243 device_for_each_child(dev
, &async
, dpm_wait_fn
);
247 * pm_op - Return the PM operation appropriate for given PM event.
248 * @ops: PM operations to choose from.
249 * @state: PM transition of the system being carried out.
251 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
253 switch (state
.event
) {
254 #ifdef CONFIG_SUSPEND
255 case PM_EVENT_SUSPEND
:
257 case PM_EVENT_RESUME
:
259 #endif /* CONFIG_SUSPEND */
260 #ifdef CONFIG_HIBERNATE_CALLBACKS
261 case PM_EVENT_FREEZE
:
262 case PM_EVENT_QUIESCE
:
264 case PM_EVENT_HIBERNATE
:
265 return ops
->poweroff
;
267 case PM_EVENT_RECOVER
:
270 case PM_EVENT_RESTORE
:
272 #endif /* CONFIG_HIBERNATE_CALLBACKS */
279 * pm_late_early_op - Return the PM operation appropriate for given PM event.
280 * @ops: PM operations to choose from.
281 * @state: PM transition of the system being carried out.
283 * Runtime PM is disabled for @dev while this function is being executed.
285 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
288 switch (state
.event
) {
289 #ifdef CONFIG_SUSPEND
290 case PM_EVENT_SUSPEND
:
291 return ops
->suspend_late
;
292 case PM_EVENT_RESUME
:
293 return ops
->resume_early
;
294 #endif /* CONFIG_SUSPEND */
295 #ifdef CONFIG_HIBERNATE_CALLBACKS
296 case PM_EVENT_FREEZE
:
297 case PM_EVENT_QUIESCE
:
298 return ops
->freeze_late
;
299 case PM_EVENT_HIBERNATE
:
300 return ops
->poweroff_late
;
302 case PM_EVENT_RECOVER
:
303 return ops
->thaw_early
;
304 case PM_EVENT_RESTORE
:
305 return ops
->restore_early
;
306 #endif /* CONFIG_HIBERNATE_CALLBACKS */
313 * pm_noirq_op - Return the PM operation appropriate for given PM event.
314 * @ops: PM operations to choose from.
315 * @state: PM transition of the system being carried out.
317 * The driver of @dev will not receive interrupts while this function is being
320 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
322 switch (state
.event
) {
323 #ifdef CONFIG_SUSPEND
324 case PM_EVENT_SUSPEND
:
325 return ops
->suspend_noirq
;
326 case PM_EVENT_RESUME
:
327 return ops
->resume_noirq
;
328 #endif /* CONFIG_SUSPEND */
329 #ifdef CONFIG_HIBERNATE_CALLBACKS
330 case PM_EVENT_FREEZE
:
331 case PM_EVENT_QUIESCE
:
332 return ops
->freeze_noirq
;
333 case PM_EVENT_HIBERNATE
:
334 return ops
->poweroff_noirq
;
336 case PM_EVENT_RECOVER
:
337 return ops
->thaw_noirq
;
338 case PM_EVENT_RESTORE
:
339 return ops
->restore_noirq
;
340 #endif /* CONFIG_HIBERNATE_CALLBACKS */
346 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
348 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
349 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
350 ", may wakeup" : "");
353 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
356 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
357 dev_name(dev
), pm_verb(state
.event
), info
, error
);
360 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
366 calltime
= ktime_get();
367 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
368 do_div(usecs64
, NSEC_PER_USEC
);
372 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
373 info
?: "", info
? " " : "", pm_verb(state
.event
),
374 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
377 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
378 pm_message_t state
, char *info
)
386 calltime
= initcall_debug_start(dev
);
388 pm_dev_dbg(dev
, state
, info
);
390 suspend_report_result(cb
, error
);
392 initcall_debug_report(dev
, calltime
, error
, state
, info
);
397 #ifdef CONFIG_DPM_WATCHDOG
398 struct dpm_watchdog
{
400 struct task_struct
*tsk
;
401 struct timer_list timer
;
404 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
405 struct dpm_watchdog wd
408 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
409 * @data: Watchdog object address.
411 * Called when a driver has timed out suspending or resuming.
412 * There's not much we can do here to recover so panic() to
413 * capture a crash-dump in pstore.
415 static void dpm_watchdog_handler(unsigned long data
)
417 struct dpm_watchdog
*wd
= (void *)data
;
419 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
420 show_stack(wd
->tsk
, NULL
);
421 panic("%s %s: unrecoverable failure\n",
422 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
426 * dpm_watchdog_set - Enable pm watchdog for given device.
427 * @wd: Watchdog. Must be allocated on the stack.
428 * @dev: Device to handle.
430 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
432 struct timer_list
*timer
= &wd
->timer
;
437 init_timer_on_stack(timer
);
438 /* use same timeout value for both suspend and resume */
439 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
440 timer
->function
= dpm_watchdog_handler
;
441 timer
->data
= (unsigned long)wd
;
446 * dpm_watchdog_clear - Disable suspend/resume watchdog.
447 * @wd: Watchdog to disable.
449 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
451 struct timer_list
*timer
= &wd
->timer
;
453 del_timer_sync(timer
);
454 destroy_timer_on_stack(timer
);
457 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
458 #define dpm_watchdog_set(x, y)
459 #define dpm_watchdog_clear(x)
462 /*------------------------- Resume routines -------------------------*/
465 * device_resume_noirq - Execute an "early resume" callback for given device.
466 * @dev: Device to handle.
467 * @state: PM transition of the system being carried out.
469 * The driver of @dev will not receive interrupts while this function is being
472 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
474 pm_callback_t callback
= NULL
;
481 if (dev
->power
.syscore
)
484 if (!dev
->power
.is_noirq_suspended
)
487 dpm_wait(dev
->parent
, async
);
489 if (dev
->pm_domain
) {
490 info
= "noirq power domain ";
491 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
492 } else if (dev
->type
&& dev
->type
->pm
) {
493 info
= "noirq type ";
494 callback
= pm_noirq_op(dev
->type
->pm
, state
);
495 } else if (dev
->class && dev
->class->pm
) {
496 info
= "noirq class ";
497 callback
= pm_noirq_op(dev
->class->pm
, state
);
498 } else if (dev
->bus
&& dev
->bus
->pm
) {
500 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
503 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
504 info
= "noirq driver ";
505 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
508 error
= dpm_run_callback(callback
, dev
, state
, info
);
509 dev
->power
.is_noirq_suspended
= false;
512 complete_all(&dev
->power
.completion
);
517 static bool is_async(struct device
*dev
)
519 return dev
->power
.async_suspend
&& pm_async_enabled
520 && !pm_trace_is_enabled();
523 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
525 struct device
*dev
= (struct device
*)data
;
528 error
= device_resume_noirq(dev
, pm_transition
, true);
530 pm_dev_err(dev
, pm_transition
, " async", error
);
536 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
537 * @state: PM transition of the system being carried out.
539 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
540 * enable device drivers to receive interrupts.
542 static void dpm_resume_noirq(pm_message_t state
)
545 ktime_t starttime
= ktime_get();
547 mutex_lock(&dpm_list_mtx
);
548 pm_transition
= state
;
551 * Advanced the async threads upfront,
552 * in case the starting of async threads is
553 * delayed by non-async resuming devices.
555 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
556 reinit_completion(&dev
->power
.completion
);
559 async_schedule(async_resume_noirq
, dev
);
563 while (!list_empty(&dpm_noirq_list
)) {
564 dev
= to_device(dpm_noirq_list
.next
);
566 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
567 mutex_unlock(&dpm_list_mtx
);
569 if (!is_async(dev
)) {
572 error
= device_resume_noirq(dev
, state
, false);
574 suspend_stats
.failed_resume_noirq
++;
575 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
576 dpm_save_failed_dev(dev_name(dev
));
577 pm_dev_err(dev
, state
, " noirq", error
);
581 mutex_lock(&dpm_list_mtx
);
584 mutex_unlock(&dpm_list_mtx
);
585 async_synchronize_full();
586 dpm_show_time(starttime
, state
, "noirq");
587 resume_device_irqs();
592 * device_resume_early - Execute an "early resume" callback for given device.
593 * @dev: Device to handle.
594 * @state: PM transition of the system being carried out.
596 * Runtime PM is disabled for @dev while this function is being executed.
598 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
600 pm_callback_t callback
= NULL
;
607 if (dev
->power
.syscore
)
610 if (!dev
->power
.is_late_suspended
)
613 dpm_wait(dev
->parent
, async
);
615 if (dev
->pm_domain
) {
616 info
= "early power domain ";
617 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
618 } else if (dev
->type
&& dev
->type
->pm
) {
619 info
= "early type ";
620 callback
= pm_late_early_op(dev
->type
->pm
, state
);
621 } else if (dev
->class && dev
->class->pm
) {
622 info
= "early class ";
623 callback
= pm_late_early_op(dev
->class->pm
, state
);
624 } else if (dev
->bus
&& dev
->bus
->pm
) {
626 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
629 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
630 info
= "early driver ";
631 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
634 error
= dpm_run_callback(callback
, dev
, state
, info
);
635 dev
->power
.is_late_suspended
= false;
640 pm_runtime_enable(dev
);
641 complete_all(&dev
->power
.completion
);
645 static void async_resume_early(void *data
, async_cookie_t cookie
)
647 struct device
*dev
= (struct device
*)data
;
650 error
= device_resume_early(dev
, pm_transition
, true);
652 pm_dev_err(dev
, pm_transition
, " async", error
);
658 * dpm_resume_early - Execute "early resume" callbacks for all devices.
659 * @state: PM transition of the system being carried out.
661 static void dpm_resume_early(pm_message_t state
)
664 ktime_t starttime
= ktime_get();
666 mutex_lock(&dpm_list_mtx
);
667 pm_transition
= state
;
670 * Advanced the async threads upfront,
671 * in case the starting of async threads is
672 * delayed by non-async resuming devices.
674 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
675 reinit_completion(&dev
->power
.completion
);
678 async_schedule(async_resume_early
, dev
);
682 while (!list_empty(&dpm_late_early_list
)) {
683 dev
= to_device(dpm_late_early_list
.next
);
685 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
686 mutex_unlock(&dpm_list_mtx
);
688 if (!is_async(dev
)) {
691 error
= device_resume_early(dev
, state
, false);
693 suspend_stats
.failed_resume_early
++;
694 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
695 dpm_save_failed_dev(dev_name(dev
));
696 pm_dev_err(dev
, state
, " early", error
);
699 mutex_lock(&dpm_list_mtx
);
702 mutex_unlock(&dpm_list_mtx
);
703 async_synchronize_full();
704 dpm_show_time(starttime
, state
, "early");
708 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
709 * @state: PM transition of the system being carried out.
711 void dpm_resume_start(pm_message_t state
)
713 dpm_resume_noirq(state
);
714 dpm_resume_early(state
);
716 EXPORT_SYMBOL_GPL(dpm_resume_start
);
719 * device_resume - Execute "resume" callbacks for given device.
720 * @dev: Device to handle.
721 * @state: PM transition of the system being carried out.
722 * @async: If true, the device is being resumed asynchronously.
724 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
726 pm_callback_t callback
= NULL
;
729 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
734 if (dev
->power
.syscore
)
737 dpm_wait(dev
->parent
, async
);
738 dpm_watchdog_set(&wd
, dev
);
742 * This is a fib. But we'll allow new children to be added below
743 * a resumed device, even if the device hasn't been completed yet.
745 dev
->power
.is_prepared
= false;
747 if (!dev
->power
.is_suspended
)
750 if (dev
->pm_domain
) {
751 info
= "power domain ";
752 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
756 if (dev
->type
&& dev
->type
->pm
) {
758 callback
= pm_op(dev
->type
->pm
, state
);
763 if (dev
->class->pm
) {
765 callback
= pm_op(dev
->class->pm
, state
);
767 } else if (dev
->class->resume
) {
768 info
= "legacy class ";
769 callback
= dev
->class->resume
;
777 callback
= pm_op(dev
->bus
->pm
, state
);
778 } else if (dev
->bus
->resume
) {
779 info
= "legacy bus ";
780 callback
= dev
->bus
->resume
;
786 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
788 callback
= pm_op(dev
->driver
->pm
, state
);
792 error
= dpm_run_callback(callback
, dev
, state
, info
);
793 dev
->power
.is_suspended
= false;
797 dpm_watchdog_clear(&wd
);
800 complete_all(&dev
->power
.completion
);
807 static void async_resume(void *data
, async_cookie_t cookie
)
809 struct device
*dev
= (struct device
*)data
;
812 error
= device_resume(dev
, pm_transition
, true);
814 pm_dev_err(dev
, pm_transition
, " async", error
);
819 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
820 * @state: PM transition of the system being carried out.
822 * Execute the appropriate "resume" callback for all devices whose status
823 * indicates that they are suspended.
825 void dpm_resume(pm_message_t state
)
828 ktime_t starttime
= ktime_get();
832 mutex_lock(&dpm_list_mtx
);
833 pm_transition
= state
;
836 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
837 reinit_completion(&dev
->power
.completion
);
840 async_schedule(async_resume
, dev
);
844 while (!list_empty(&dpm_suspended_list
)) {
845 dev
= to_device(dpm_suspended_list
.next
);
847 if (!is_async(dev
)) {
850 mutex_unlock(&dpm_list_mtx
);
852 error
= device_resume(dev
, state
, false);
854 suspend_stats
.failed_resume
++;
855 dpm_save_failed_step(SUSPEND_RESUME
);
856 dpm_save_failed_dev(dev_name(dev
));
857 pm_dev_err(dev
, state
, "", error
);
860 mutex_lock(&dpm_list_mtx
);
862 if (!list_empty(&dev
->power
.entry
))
863 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
866 mutex_unlock(&dpm_list_mtx
);
867 async_synchronize_full();
868 dpm_show_time(starttime
, state
, NULL
);
872 * device_complete - Complete a PM transition for given device.
873 * @dev: Device to handle.
874 * @state: PM transition of the system being carried out.
876 static void device_complete(struct device
*dev
, pm_message_t state
)
878 void (*callback
)(struct device
*) = NULL
;
881 if (dev
->power
.syscore
)
886 if (dev
->pm_domain
) {
887 info
= "completing power domain ";
888 callback
= dev
->pm_domain
->ops
.complete
;
889 } else if (dev
->type
&& dev
->type
->pm
) {
890 info
= "completing type ";
891 callback
= dev
->type
->pm
->complete
;
892 } else if (dev
->class && dev
->class->pm
) {
893 info
= "completing class ";
894 callback
= dev
->class->pm
->complete
;
895 } else if (dev
->bus
&& dev
->bus
->pm
) {
896 info
= "completing bus ";
897 callback
= dev
->bus
->pm
->complete
;
900 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
901 info
= "completing driver ";
902 callback
= dev
->driver
->pm
->complete
;
906 pm_dev_dbg(dev
, state
, info
);
916 * dpm_complete - Complete a PM transition for all non-sysdev devices.
917 * @state: PM transition of the system being carried out.
919 * Execute the ->complete() callbacks for all devices whose PM status is not
920 * DPM_ON (this allows new devices to be registered).
922 void dpm_complete(pm_message_t state
)
924 struct list_head list
;
928 INIT_LIST_HEAD(&list
);
929 mutex_lock(&dpm_list_mtx
);
930 while (!list_empty(&dpm_prepared_list
)) {
931 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
934 dev
->power
.is_prepared
= false;
935 list_move(&dev
->power
.entry
, &list
);
936 mutex_unlock(&dpm_list_mtx
);
938 device_complete(dev
, state
);
940 mutex_lock(&dpm_list_mtx
);
943 list_splice(&list
, &dpm_list
);
944 mutex_unlock(&dpm_list_mtx
);
948 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
949 * @state: PM transition of the system being carried out.
951 * Execute "resume" callbacks for all devices and complete the PM transition of
954 void dpm_resume_end(pm_message_t state
)
959 EXPORT_SYMBOL_GPL(dpm_resume_end
);
962 /*------------------------- Suspend routines -------------------------*/
965 * resume_event - Return a "resume" message for given "suspend" sleep state.
966 * @sleep_state: PM message representing a sleep state.
968 * Return a PM message representing the resume event corresponding to given
971 static pm_message_t
resume_event(pm_message_t sleep_state
)
973 switch (sleep_state
.event
) {
974 case PM_EVENT_SUSPEND
:
976 case PM_EVENT_FREEZE
:
977 case PM_EVENT_QUIESCE
:
979 case PM_EVENT_HIBERNATE
:
986 * device_suspend_noirq - Execute a "late suspend" callback for given device.
987 * @dev: Device to handle.
988 * @state: PM transition of the system being carried out.
990 * The driver of @dev will not receive interrupts while this function is being
993 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
995 pm_callback_t callback
= NULL
;
1002 if (pm_wakeup_pending()) {
1003 async_error
= -EBUSY
;
1007 if (dev
->power
.syscore
)
1010 dpm_wait_for_children(dev
, async
);
1012 if (dev
->pm_domain
) {
1013 info
= "noirq power domain ";
1014 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1015 } else if (dev
->type
&& dev
->type
->pm
) {
1016 info
= "noirq type ";
1017 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1018 } else if (dev
->class && dev
->class->pm
) {
1019 info
= "noirq class ";
1020 callback
= pm_noirq_op(dev
->class->pm
, state
);
1021 } else if (dev
->bus
&& dev
->bus
->pm
) {
1022 info
= "noirq bus ";
1023 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1026 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1027 info
= "noirq driver ";
1028 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1031 error
= dpm_run_callback(callback
, dev
, state
, info
);
1033 dev
->power
.is_noirq_suspended
= true;
1035 async_error
= error
;
1038 complete_all(&dev
->power
.completion
);
1042 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1044 struct device
*dev
= (struct device
*)data
;
1047 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1049 dpm_save_failed_dev(dev_name(dev
));
1050 pm_dev_err(dev
, pm_transition
, " async", error
);
1056 static int device_suspend_noirq(struct device
*dev
)
1058 reinit_completion(&dev
->power
.completion
);
1060 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1062 async_schedule(async_suspend_noirq
, dev
);
1065 return __device_suspend_noirq(dev
, pm_transition
, false);
1069 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1070 * @state: PM transition of the system being carried out.
1072 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1073 * handlers for all non-sysdev devices.
1075 static int dpm_suspend_noirq(pm_message_t state
)
1077 ktime_t starttime
= ktime_get();
1081 suspend_device_irqs();
1082 mutex_lock(&dpm_list_mtx
);
1083 pm_transition
= state
;
1086 while (!list_empty(&dpm_late_early_list
)) {
1087 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1090 mutex_unlock(&dpm_list_mtx
);
1092 error
= device_suspend_noirq(dev
);
1094 mutex_lock(&dpm_list_mtx
);
1096 pm_dev_err(dev
, state
, " noirq", error
);
1097 dpm_save_failed_dev(dev_name(dev
));
1101 if (!list_empty(&dev
->power
.entry
))
1102 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1108 mutex_unlock(&dpm_list_mtx
);
1109 async_synchronize_full();
1111 error
= async_error
;
1114 suspend_stats
.failed_suspend_noirq
++;
1115 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1116 dpm_resume_noirq(resume_event(state
));
1118 dpm_show_time(starttime
, state
, "noirq");
1124 * device_suspend_late - Execute a "late suspend" callback for given device.
1125 * @dev: Device to handle.
1126 * @state: PM transition of the system being carried out.
1128 * Runtime PM is disabled for @dev while this function is being executed.
1130 static int device_suspend_late(struct device
*dev
, pm_message_t state
)
1132 pm_callback_t callback
= NULL
;
1136 __pm_runtime_disable(dev
, false);
1138 if (dev
->power
.syscore
)
1141 if (dev
->pm_domain
) {
1142 info
= "late power domain ";
1143 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1144 } else if (dev
->type
&& dev
->type
->pm
) {
1145 info
= "late type ";
1146 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1147 } else if (dev
->class && dev
->class->pm
) {
1148 info
= "late class ";
1149 callback
= pm_late_early_op(dev
->class->pm
, state
);
1150 } else if (dev
->bus
&& dev
->bus
->pm
) {
1152 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1155 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1156 info
= "late driver ";
1157 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1160 error
= dpm_run_callback(callback
, dev
, state
, info
);
1162 dev
->power
.is_late_suspended
= true;
1168 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1169 * @state: PM transition of the system being carried out.
1171 static int dpm_suspend_late(pm_message_t state
)
1173 ktime_t starttime
= ktime_get();
1176 mutex_lock(&dpm_list_mtx
);
1177 while (!list_empty(&dpm_suspended_list
)) {
1178 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1181 mutex_unlock(&dpm_list_mtx
);
1183 error
= device_suspend_late(dev
, state
);
1185 mutex_lock(&dpm_list_mtx
);
1187 pm_dev_err(dev
, state
, " late", error
);
1188 suspend_stats
.failed_suspend_late
++;
1189 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1190 dpm_save_failed_dev(dev_name(dev
));
1194 if (!list_empty(&dev
->power
.entry
))
1195 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1198 if (pm_wakeup_pending()) {
1203 mutex_unlock(&dpm_list_mtx
);
1205 dpm_resume_early(resume_event(state
));
1207 dpm_show_time(starttime
, state
, "late");
1213 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1214 * @state: PM transition of the system being carried out.
1216 int dpm_suspend_end(pm_message_t state
)
1218 int error
= dpm_suspend_late(state
);
1222 error
= dpm_suspend_noirq(state
);
1224 dpm_resume_early(resume_event(state
));
1230 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1233 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1234 * @dev: Device to suspend.
1235 * @state: PM transition of the system being carried out.
1236 * @cb: Suspend callback to execute.
1238 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1239 int (*cb
)(struct device
*dev
, pm_message_t state
),
1245 calltime
= initcall_debug_start(dev
);
1247 error
= cb(dev
, state
);
1248 suspend_report_result(cb
, error
);
1250 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1256 * device_suspend - Execute "suspend" callbacks for given device.
1257 * @dev: Device to handle.
1258 * @state: PM transition of the system being carried out.
1259 * @async: If true, the device is being suspended asynchronously.
1261 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1263 pm_callback_t callback
= NULL
;
1266 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1268 dpm_wait_for_children(dev
, async
);
1274 * If a device configured to wake up the system from sleep states
1275 * has been suspended at run time and there's a resume request pending
1276 * for it, this is equivalent to the device signaling wakeup, so the
1277 * system suspend operation should be aborted.
1279 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1280 pm_wakeup_event(dev
, 0);
1282 if (pm_wakeup_pending()) {
1283 async_error
= -EBUSY
;
1287 if (dev
->power
.syscore
)
1290 dpm_watchdog_set(&wd
, dev
);
1293 if (dev
->pm_domain
) {
1294 info
= "power domain ";
1295 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1299 if (dev
->type
&& dev
->type
->pm
) {
1301 callback
= pm_op(dev
->type
->pm
, state
);
1306 if (dev
->class->pm
) {
1308 callback
= pm_op(dev
->class->pm
, state
);
1310 } else if (dev
->class->suspend
) {
1311 pm_dev_dbg(dev
, state
, "legacy class ");
1312 error
= legacy_suspend(dev
, state
, dev
->class->suspend
,
1321 callback
= pm_op(dev
->bus
->pm
, state
);
1322 } else if (dev
->bus
->suspend
) {
1323 pm_dev_dbg(dev
, state
, "legacy bus ");
1324 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1331 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1333 callback
= pm_op(dev
->driver
->pm
, state
);
1336 error
= dpm_run_callback(callback
, dev
, state
, info
);
1340 dev
->power
.is_suspended
= true;
1341 if (dev
->power
.wakeup_path
1342 && dev
->parent
&& !dev
->parent
->power
.ignore_children
)
1343 dev
->parent
->power
.wakeup_path
= true;
1347 dpm_watchdog_clear(&wd
);
1350 complete_all(&dev
->power
.completion
);
1352 async_error
= error
;
1357 static void async_suspend(void *data
, async_cookie_t cookie
)
1359 struct device
*dev
= (struct device
*)data
;
1362 error
= __device_suspend(dev
, pm_transition
, true);
1364 dpm_save_failed_dev(dev_name(dev
));
1365 pm_dev_err(dev
, pm_transition
, " async", error
);
1371 static int device_suspend(struct device
*dev
)
1373 reinit_completion(&dev
->power
.completion
);
1375 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1377 async_schedule(async_suspend
, dev
);
1381 return __device_suspend(dev
, pm_transition
, false);
1385 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1386 * @state: PM transition of the system being carried out.
1388 int dpm_suspend(pm_message_t state
)
1390 ktime_t starttime
= ktime_get();
1395 mutex_lock(&dpm_list_mtx
);
1396 pm_transition
= state
;
1398 while (!list_empty(&dpm_prepared_list
)) {
1399 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1402 mutex_unlock(&dpm_list_mtx
);
1404 error
= device_suspend(dev
);
1406 mutex_lock(&dpm_list_mtx
);
1408 pm_dev_err(dev
, state
, "", error
);
1409 dpm_save_failed_dev(dev_name(dev
));
1413 if (!list_empty(&dev
->power
.entry
))
1414 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1419 mutex_unlock(&dpm_list_mtx
);
1420 async_synchronize_full();
1422 error
= async_error
;
1424 suspend_stats
.failed_suspend
++;
1425 dpm_save_failed_step(SUSPEND_SUSPEND
);
1427 dpm_show_time(starttime
, state
, NULL
);
1432 * device_prepare - Prepare a device for system power transition.
1433 * @dev: Device to handle.
1434 * @state: PM transition of the system being carried out.
1436 * Execute the ->prepare() callback(s) for given device. No new children of the
1437 * device may be registered after this function has returned.
1439 static int device_prepare(struct device
*dev
, pm_message_t state
)
1441 int (*callback
)(struct device
*) = NULL
;
1445 if (dev
->power
.syscore
)
1449 * If a device's parent goes into runtime suspend at the wrong time,
1450 * it won't be possible to resume the device. To prevent this we
1451 * block runtime suspend here, during the prepare phase, and allow
1452 * it again during the complete phase.
1454 pm_runtime_get_noresume(dev
);
1458 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1460 if (dev
->pm_domain
) {
1461 info
= "preparing power domain ";
1462 callback
= dev
->pm_domain
->ops
.prepare
;
1463 } else if (dev
->type
&& dev
->type
->pm
) {
1464 info
= "preparing type ";
1465 callback
= dev
->type
->pm
->prepare
;
1466 } else if (dev
->class && dev
->class->pm
) {
1467 info
= "preparing class ";
1468 callback
= dev
->class->pm
->prepare
;
1469 } else if (dev
->bus
&& dev
->bus
->pm
) {
1470 info
= "preparing bus ";
1471 callback
= dev
->bus
->pm
->prepare
;
1474 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1475 info
= "preparing driver ";
1476 callback
= dev
->driver
->pm
->prepare
;
1480 error
= callback(dev
);
1481 suspend_report_result(callback
, error
);
1487 pm_runtime_put(dev
);
1493 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1494 * @state: PM transition of the system being carried out.
1496 * Execute the ->prepare() callback(s) for all devices.
1498 int dpm_prepare(pm_message_t state
)
1504 mutex_lock(&dpm_list_mtx
);
1505 while (!list_empty(&dpm_list
)) {
1506 struct device
*dev
= to_device(dpm_list
.next
);
1509 mutex_unlock(&dpm_list_mtx
);
1511 error
= device_prepare(dev
, state
);
1513 mutex_lock(&dpm_list_mtx
);
1515 if (error
== -EAGAIN
) {
1520 printk(KERN_INFO
"PM: Device %s not prepared "
1521 "for power transition: code %d\n",
1522 dev_name(dev
), error
);
1526 dev
->power
.is_prepared
= true;
1527 if (!list_empty(&dev
->power
.entry
))
1528 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1531 mutex_unlock(&dpm_list_mtx
);
1536 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1537 * @state: PM transition of the system being carried out.
1539 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1540 * callbacks for them.
1542 int dpm_suspend_start(pm_message_t state
)
1546 error
= dpm_prepare(state
);
1548 suspend_stats
.failed_prepare
++;
1549 dpm_save_failed_step(SUSPEND_PREPARE
);
1551 error
= dpm_suspend(state
);
1554 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1556 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1559 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1561 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1564 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1565 * @dev: Device to wait for.
1566 * @subordinate: Device that needs to wait for @dev.
1568 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1570 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1573 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1576 * dpm_for_each_dev - device iterator.
1577 * @data: data for the callback.
1578 * @fn: function to be called for each device.
1580 * Iterate over devices in dpm_list, and call @fn for each device,
1583 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1591 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1595 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);