2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm-trace.h>
27 #include <linux/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/cpufreq.h>
33 #include <linux/cpuidle.h>
34 #include <linux/timer.h>
39 typedef int (*pm_callback_t
)(struct device
*);
42 * The entries in the dpm_list list are in a depth first order, simply
43 * because children are guaranteed to be discovered after parents, and
44 * are inserted at the back of the list on discovery.
46 * Since device_pm_add() may be called with a device lock held,
47 * we must never try to acquire a device lock while holding
52 static LIST_HEAD(dpm_prepared_list
);
53 static LIST_HEAD(dpm_suspended_list
);
54 static LIST_HEAD(dpm_late_early_list
);
55 static LIST_HEAD(dpm_noirq_list
);
57 struct suspend_stats suspend_stats
;
58 static DEFINE_MUTEX(dpm_list_mtx
);
59 static pm_message_t pm_transition
;
61 static int async_error
;
63 static char *pm_verb(int event
)
66 case PM_EVENT_SUSPEND
:
72 case PM_EVENT_QUIESCE
:
74 case PM_EVENT_HIBERNATE
:
78 case PM_EVENT_RESTORE
:
80 case PM_EVENT_RECOVER
:
83 return "(unknown PM event)";
88 * device_pm_sleep_init - Initialize system suspend-related device fields.
89 * @dev: Device object being initialized.
91 void device_pm_sleep_init(struct device
*dev
)
93 dev
->power
.is_prepared
= false;
94 dev
->power
.is_suspended
= false;
95 dev
->power
.is_noirq_suspended
= false;
96 dev
->power
.is_late_suspended
= false;
97 init_completion(&dev
->power
.completion
);
98 complete_all(&dev
->power
.completion
);
99 dev
->power
.wakeup
= NULL
;
100 INIT_LIST_HEAD(&dev
->power
.entry
);
104 * device_pm_lock - Lock the list of active devices used by the PM core.
106 void device_pm_lock(void)
108 mutex_lock(&dpm_list_mtx
);
112 * device_pm_unlock - Unlock the list of active devices used by the PM core.
114 void device_pm_unlock(void)
116 mutex_unlock(&dpm_list_mtx
);
120 * device_pm_add - Add a device to the PM core's list of active devices.
121 * @dev: Device to add to the list.
123 void device_pm_add(struct device
*dev
)
125 pr_debug("PM: Adding info for %s:%s\n",
126 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
127 mutex_lock(&dpm_list_mtx
);
128 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
129 dev_warn(dev
, "parent %s should not be sleeping\n",
130 dev_name(dev
->parent
));
131 list_add_tail(&dev
->power
.entry
, &dpm_list
);
132 mutex_unlock(&dpm_list_mtx
);
136 * device_pm_remove - Remove a device from the PM core's list of active devices.
137 * @dev: Device to be removed from the list.
139 void device_pm_remove(struct device
*dev
)
141 pr_debug("PM: Removing info for %s:%s\n",
142 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
143 complete_all(&dev
->power
.completion
);
144 mutex_lock(&dpm_list_mtx
);
145 list_del_init(&dev
->power
.entry
);
146 mutex_unlock(&dpm_list_mtx
);
147 device_wakeup_disable(dev
);
148 pm_runtime_remove(dev
);
152 * device_pm_move_before - Move device in the PM core's list of active devices.
153 * @deva: Device to move in dpm_list.
154 * @devb: Device @deva should come before.
156 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
158 pr_debug("PM: Moving %s:%s before %s:%s\n",
159 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
160 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
161 /* Delete deva from dpm_list and reinsert before devb. */
162 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
166 * device_pm_move_after - Move device in the PM core's list of active devices.
167 * @deva: Device to move in dpm_list.
168 * @devb: Device @deva should come after.
170 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
172 pr_debug("PM: Moving %s:%s after %s:%s\n",
173 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
174 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
175 /* Delete deva from dpm_list and reinsert after devb. */
176 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
180 * device_pm_move_last - Move device to end of the PM core's list of devices.
181 * @dev: Device to move in dpm_list.
183 void device_pm_move_last(struct device
*dev
)
185 pr_debug("PM: Moving %s:%s to end of list\n",
186 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
187 list_move_tail(&dev
->power
.entry
, &dpm_list
);
190 static ktime_t
initcall_debug_start(struct device
*dev
)
192 ktime_t calltime
= ktime_set(0, 0);
194 if (pm_print_times_enabled
) {
195 pr_info("calling %s+ @ %i, parent: %s\n",
196 dev_name(dev
), task_pid_nr(current
),
197 dev
->parent
? dev_name(dev
->parent
) : "none");
198 calltime
= ktime_get();
204 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
205 int error
, pm_message_t state
, char *info
)
210 rettime
= ktime_get();
211 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
213 if (pm_print_times_enabled
) {
214 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
215 error
, (unsigned long long)nsecs
>> 10);
220 * dpm_wait - Wait for a PM operation to complete.
221 * @dev: Device to wait for.
222 * @async: If unset, wait only if the device's power.async_suspend flag is set.
224 static void dpm_wait(struct device
*dev
, bool async
)
229 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
230 wait_for_completion(&dev
->power
.completion
);
233 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
235 dpm_wait(dev
, *((bool *)async_ptr
));
239 static void dpm_wait_for_children(struct device
*dev
, bool async
)
241 device_for_each_child(dev
, &async
, dpm_wait_fn
);
245 * pm_op - Return the PM operation appropriate for given PM event.
246 * @ops: PM operations to choose from.
247 * @state: PM transition of the system being carried out.
249 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
251 switch (state
.event
) {
252 #ifdef CONFIG_SUSPEND
253 case PM_EVENT_SUSPEND
:
255 case PM_EVENT_RESUME
:
257 #endif /* CONFIG_SUSPEND */
258 #ifdef CONFIG_HIBERNATE_CALLBACKS
259 case PM_EVENT_FREEZE
:
260 case PM_EVENT_QUIESCE
:
262 case PM_EVENT_HIBERNATE
:
263 return ops
->poweroff
;
265 case PM_EVENT_RECOVER
:
268 case PM_EVENT_RESTORE
:
270 #endif /* CONFIG_HIBERNATE_CALLBACKS */
277 * pm_late_early_op - Return the PM operation appropriate for given PM event.
278 * @ops: PM operations to choose from.
279 * @state: PM transition of the system being carried out.
281 * Runtime PM is disabled for @dev while this function is being executed.
283 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
286 switch (state
.event
) {
287 #ifdef CONFIG_SUSPEND
288 case PM_EVENT_SUSPEND
:
289 return ops
->suspend_late
;
290 case PM_EVENT_RESUME
:
291 return ops
->resume_early
;
292 #endif /* CONFIG_SUSPEND */
293 #ifdef CONFIG_HIBERNATE_CALLBACKS
294 case PM_EVENT_FREEZE
:
295 case PM_EVENT_QUIESCE
:
296 return ops
->freeze_late
;
297 case PM_EVENT_HIBERNATE
:
298 return ops
->poweroff_late
;
300 case PM_EVENT_RECOVER
:
301 return ops
->thaw_early
;
302 case PM_EVENT_RESTORE
:
303 return ops
->restore_early
;
304 #endif /* CONFIG_HIBERNATE_CALLBACKS */
311 * pm_noirq_op - Return the PM operation appropriate for given PM event.
312 * @ops: PM operations to choose from.
313 * @state: PM transition of the system being carried out.
315 * The driver of @dev will not receive interrupts while this function is being
318 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
320 switch (state
.event
) {
321 #ifdef CONFIG_SUSPEND
322 case PM_EVENT_SUSPEND
:
323 return ops
->suspend_noirq
;
324 case PM_EVENT_RESUME
:
325 return ops
->resume_noirq
;
326 #endif /* CONFIG_SUSPEND */
327 #ifdef CONFIG_HIBERNATE_CALLBACKS
328 case PM_EVENT_FREEZE
:
329 case PM_EVENT_QUIESCE
:
330 return ops
->freeze_noirq
;
331 case PM_EVENT_HIBERNATE
:
332 return ops
->poweroff_noirq
;
334 case PM_EVENT_RECOVER
:
335 return ops
->thaw_noirq
;
336 case PM_EVENT_RESTORE
:
337 return ops
->restore_noirq
;
338 #endif /* CONFIG_HIBERNATE_CALLBACKS */
344 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
346 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
347 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
348 ", may wakeup" : "");
351 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
354 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
355 dev_name(dev
), pm_verb(state
.event
), info
, error
);
358 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
364 calltime
= ktime_get();
365 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
366 do_div(usecs64
, NSEC_PER_USEC
);
370 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
371 info
?: "", info
? " " : "", pm_verb(state
.event
),
372 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
375 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
376 pm_message_t state
, char *info
)
384 calltime
= initcall_debug_start(dev
);
386 pm_dev_dbg(dev
, state
, info
);
387 trace_device_pm_callback_start(dev
, info
, state
.event
);
389 trace_device_pm_callback_end(dev
, error
);
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.
468 * @async: If true, the device is being resumed asynchronously.
470 * The driver of @dev will not receive interrupts while this function is being
473 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
475 pm_callback_t callback
= NULL
;
482 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
485 if (!dev
->power
.is_noirq_suspended
)
488 dpm_wait(dev
->parent
, async
);
490 if (dev
->pm_domain
) {
491 info
= "noirq power domain ";
492 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
493 } else if (dev
->type
&& dev
->type
->pm
) {
494 info
= "noirq type ";
495 callback
= pm_noirq_op(dev
->type
->pm
, state
);
496 } else if (dev
->class && dev
->class->pm
) {
497 info
= "noirq class ";
498 callback
= pm_noirq_op(dev
->class->pm
, state
);
499 } else if (dev
->bus
&& dev
->bus
->pm
) {
501 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
504 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
505 info
= "noirq driver ";
506 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
509 error
= dpm_run_callback(callback
, dev
, state
, info
);
510 dev
->power
.is_noirq_suspended
= false;
513 complete_all(&dev
->power
.completion
);
518 static bool is_async(struct device
*dev
)
520 return dev
->power
.async_suspend
&& pm_async_enabled
521 && !pm_trace_is_enabled();
524 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
526 struct device
*dev
= (struct device
*)data
;
529 error
= device_resume_noirq(dev
, pm_transition
, true);
531 pm_dev_err(dev
, pm_transition
, " async", error
);
537 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
538 * @state: PM transition of the system being carried out.
540 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
541 * enable device drivers to receive interrupts.
543 void dpm_resume_noirq(pm_message_t state
)
546 ktime_t starttime
= ktime_get();
548 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
549 mutex_lock(&dpm_list_mtx
);
550 pm_transition
= state
;
553 * Advanced the async threads upfront,
554 * in case the starting of async threads is
555 * delayed by non-async resuming devices.
557 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
558 reinit_completion(&dev
->power
.completion
);
561 async_schedule(async_resume_noirq
, dev
);
565 while (!list_empty(&dpm_noirq_list
)) {
566 dev
= to_device(dpm_noirq_list
.next
);
568 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
569 mutex_unlock(&dpm_list_mtx
);
571 if (!is_async(dev
)) {
574 error
= device_resume_noirq(dev
, state
, false);
576 suspend_stats
.failed_resume_noirq
++;
577 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
578 dpm_save_failed_dev(dev_name(dev
));
579 pm_dev_err(dev
, state
, " noirq", error
);
583 mutex_lock(&dpm_list_mtx
);
586 mutex_unlock(&dpm_list_mtx
);
587 async_synchronize_full();
588 dpm_show_time(starttime
, state
, "noirq");
589 resume_device_irqs();
591 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
595 * device_resume_early - Execute an "early resume" callback for given device.
596 * @dev: Device to handle.
597 * @state: PM transition of the system being carried out.
598 * @async: If true, the device is being resumed asynchronously.
600 * Runtime PM is disabled for @dev while this function is being executed.
602 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
604 pm_callback_t callback
= NULL
;
611 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
614 if (!dev
->power
.is_late_suspended
)
617 dpm_wait(dev
->parent
, async
);
619 if (dev
->pm_domain
) {
620 info
= "early power domain ";
621 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
622 } else if (dev
->type
&& dev
->type
->pm
) {
623 info
= "early type ";
624 callback
= pm_late_early_op(dev
->type
->pm
, state
);
625 } else if (dev
->class && dev
->class->pm
) {
626 info
= "early class ";
627 callback
= pm_late_early_op(dev
->class->pm
, state
);
628 } else if (dev
->bus
&& dev
->bus
->pm
) {
630 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
633 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
634 info
= "early driver ";
635 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
638 error
= dpm_run_callback(callback
, dev
, state
, info
);
639 dev
->power
.is_late_suspended
= false;
644 pm_runtime_enable(dev
);
645 complete_all(&dev
->power
.completion
);
649 static void async_resume_early(void *data
, async_cookie_t cookie
)
651 struct device
*dev
= (struct device
*)data
;
654 error
= device_resume_early(dev
, pm_transition
, true);
656 pm_dev_err(dev
, pm_transition
, " async", error
);
662 * dpm_resume_early - Execute "early resume" callbacks for all devices.
663 * @state: PM transition of the system being carried out.
665 void dpm_resume_early(pm_message_t state
)
668 ktime_t starttime
= ktime_get();
670 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
671 mutex_lock(&dpm_list_mtx
);
672 pm_transition
= state
;
675 * Advanced the async threads upfront,
676 * in case the starting of async threads is
677 * delayed by non-async resuming devices.
679 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
680 reinit_completion(&dev
->power
.completion
);
683 async_schedule(async_resume_early
, dev
);
687 while (!list_empty(&dpm_late_early_list
)) {
688 dev
= to_device(dpm_late_early_list
.next
);
690 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
691 mutex_unlock(&dpm_list_mtx
);
693 if (!is_async(dev
)) {
696 error
= device_resume_early(dev
, state
, false);
698 suspend_stats
.failed_resume_early
++;
699 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
700 dpm_save_failed_dev(dev_name(dev
));
701 pm_dev_err(dev
, state
, " early", error
);
704 mutex_lock(&dpm_list_mtx
);
707 mutex_unlock(&dpm_list_mtx
);
708 async_synchronize_full();
709 dpm_show_time(starttime
, state
, "early");
710 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
714 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
715 * @state: PM transition of the system being carried out.
717 void dpm_resume_start(pm_message_t state
)
719 dpm_resume_noirq(state
);
720 dpm_resume_early(state
);
722 EXPORT_SYMBOL_GPL(dpm_resume_start
);
725 * device_resume - Execute "resume" callbacks for given device.
726 * @dev: Device to handle.
727 * @state: PM transition of the system being carried out.
728 * @async: If true, the device is being resumed asynchronously.
730 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
732 pm_callback_t callback
= NULL
;
735 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
740 if (dev
->power
.syscore
)
743 if (dev
->power
.direct_complete
) {
744 /* Match the pm_runtime_disable() in __device_suspend(). */
745 pm_runtime_enable(dev
);
749 dpm_wait(dev
->parent
, async
);
750 dpm_watchdog_set(&wd
, dev
);
754 * This is a fib. But we'll allow new children to be added below
755 * a resumed device, even if the device hasn't been completed yet.
757 dev
->power
.is_prepared
= false;
759 if (!dev
->power
.is_suspended
)
762 if (dev
->pm_domain
) {
763 info
= "power domain ";
764 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
768 if (dev
->type
&& dev
->type
->pm
) {
770 callback
= pm_op(dev
->type
->pm
, state
);
775 if (dev
->class->pm
) {
777 callback
= pm_op(dev
->class->pm
, state
);
779 } else if (dev
->class->resume
) {
780 info
= "legacy class ";
781 callback
= dev
->class->resume
;
789 callback
= pm_op(dev
->bus
->pm
, state
);
790 } else if (dev
->bus
->resume
) {
791 info
= "legacy bus ";
792 callback
= dev
->bus
->resume
;
798 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
800 callback
= pm_op(dev
->driver
->pm
, state
);
804 error
= dpm_run_callback(callback
, dev
, state
, info
);
805 dev
->power
.is_suspended
= false;
809 dpm_watchdog_clear(&wd
);
812 complete_all(&dev
->power
.completion
);
819 static void async_resume(void *data
, async_cookie_t cookie
)
821 struct device
*dev
= (struct device
*)data
;
824 error
= device_resume(dev
, pm_transition
, true);
826 pm_dev_err(dev
, pm_transition
, " async", error
);
831 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
832 * @state: PM transition of the system being carried out.
834 * Execute the appropriate "resume" callback for all devices whose status
835 * indicates that they are suspended.
837 void dpm_resume(pm_message_t state
)
840 ktime_t starttime
= ktime_get();
842 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
845 mutex_lock(&dpm_list_mtx
);
846 pm_transition
= state
;
849 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
850 reinit_completion(&dev
->power
.completion
);
853 async_schedule(async_resume
, dev
);
857 while (!list_empty(&dpm_suspended_list
)) {
858 dev
= to_device(dpm_suspended_list
.next
);
860 if (!is_async(dev
)) {
863 mutex_unlock(&dpm_list_mtx
);
865 error
= device_resume(dev
, state
, false);
867 suspend_stats
.failed_resume
++;
868 dpm_save_failed_step(SUSPEND_RESUME
);
869 dpm_save_failed_dev(dev_name(dev
));
870 pm_dev_err(dev
, state
, "", error
);
873 mutex_lock(&dpm_list_mtx
);
875 if (!list_empty(&dev
->power
.entry
))
876 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
879 mutex_unlock(&dpm_list_mtx
);
880 async_synchronize_full();
881 dpm_show_time(starttime
, state
, NULL
);
884 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
888 * device_complete - Complete a PM transition for given device.
889 * @dev: Device to handle.
890 * @state: PM transition of the system being carried out.
892 static void device_complete(struct device
*dev
, pm_message_t state
)
894 void (*callback
)(struct device
*) = NULL
;
897 if (dev
->power
.syscore
)
902 if (dev
->pm_domain
) {
903 info
= "completing power domain ";
904 callback
= dev
->pm_domain
->ops
.complete
;
905 } else if (dev
->type
&& dev
->type
->pm
) {
906 info
= "completing type ";
907 callback
= dev
->type
->pm
->complete
;
908 } else if (dev
->class && dev
->class->pm
) {
909 info
= "completing class ";
910 callback
= dev
->class->pm
->complete
;
911 } else if (dev
->bus
&& dev
->bus
->pm
) {
912 info
= "completing bus ";
913 callback
= dev
->bus
->pm
->complete
;
916 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
917 info
= "completing driver ";
918 callback
= dev
->driver
->pm
->complete
;
922 pm_dev_dbg(dev
, state
, info
);
923 trace_device_pm_callback_start(dev
, info
, state
.event
);
925 trace_device_pm_callback_end(dev
, 0);
934 * dpm_complete - Complete a PM transition for all non-sysdev devices.
935 * @state: PM transition of the system being carried out.
937 * Execute the ->complete() callbacks for all devices whose PM status is not
938 * DPM_ON (this allows new devices to be registered).
940 void dpm_complete(pm_message_t state
)
942 struct list_head list
;
944 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
947 INIT_LIST_HEAD(&list
);
948 mutex_lock(&dpm_list_mtx
);
949 while (!list_empty(&dpm_prepared_list
)) {
950 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
953 dev
->power
.is_prepared
= false;
954 list_move(&dev
->power
.entry
, &list
);
955 mutex_unlock(&dpm_list_mtx
);
957 device_complete(dev
, state
);
959 mutex_lock(&dpm_list_mtx
);
962 list_splice(&list
, &dpm_list
);
963 mutex_unlock(&dpm_list_mtx
);
964 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
968 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
969 * @state: PM transition of the system being carried out.
971 * Execute "resume" callbacks for all devices and complete the PM transition of
974 void dpm_resume_end(pm_message_t state
)
979 EXPORT_SYMBOL_GPL(dpm_resume_end
);
982 /*------------------------- Suspend routines -------------------------*/
985 * resume_event - Return a "resume" message for given "suspend" sleep state.
986 * @sleep_state: PM message representing a sleep state.
988 * Return a PM message representing the resume event corresponding to given
991 static pm_message_t
resume_event(pm_message_t sleep_state
)
993 switch (sleep_state
.event
) {
994 case PM_EVENT_SUSPEND
:
996 case PM_EVENT_FREEZE
:
997 case PM_EVENT_QUIESCE
:
999 case PM_EVENT_HIBERNATE
:
1000 return PMSG_RESTORE
;
1006 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1007 * @dev: Device to handle.
1008 * @state: PM transition of the system being carried out.
1009 * @async: If true, the device is being suspended asynchronously.
1011 * The driver of @dev will not receive interrupts while this function is being
1014 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1016 pm_callback_t callback
= NULL
;
1026 if (pm_wakeup_pending()) {
1027 async_error
= -EBUSY
;
1031 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1034 dpm_wait_for_children(dev
, async
);
1036 if (dev
->pm_domain
) {
1037 info
= "noirq power domain ";
1038 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1039 } else if (dev
->type
&& dev
->type
->pm
) {
1040 info
= "noirq type ";
1041 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1042 } else if (dev
->class && dev
->class->pm
) {
1043 info
= "noirq class ";
1044 callback
= pm_noirq_op(dev
->class->pm
, state
);
1045 } else if (dev
->bus
&& dev
->bus
->pm
) {
1046 info
= "noirq bus ";
1047 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1050 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1051 info
= "noirq driver ";
1052 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1055 error
= dpm_run_callback(callback
, dev
, state
, info
);
1057 dev
->power
.is_noirq_suspended
= true;
1059 async_error
= error
;
1062 complete_all(&dev
->power
.completion
);
1063 TRACE_SUSPEND(error
);
1067 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1069 struct device
*dev
= (struct device
*)data
;
1072 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1074 dpm_save_failed_dev(dev_name(dev
));
1075 pm_dev_err(dev
, pm_transition
, " async", error
);
1081 static int device_suspend_noirq(struct device
*dev
)
1083 reinit_completion(&dev
->power
.completion
);
1085 if (is_async(dev
)) {
1087 async_schedule(async_suspend_noirq
, dev
);
1090 return __device_suspend_noirq(dev
, pm_transition
, false);
1094 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1095 * @state: PM transition of the system being carried out.
1097 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1098 * handlers for all non-sysdev devices.
1100 int dpm_suspend_noirq(pm_message_t state
)
1102 ktime_t starttime
= ktime_get();
1105 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1107 suspend_device_irqs();
1108 mutex_lock(&dpm_list_mtx
);
1109 pm_transition
= state
;
1112 while (!list_empty(&dpm_late_early_list
)) {
1113 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1116 mutex_unlock(&dpm_list_mtx
);
1118 error
= device_suspend_noirq(dev
);
1120 mutex_lock(&dpm_list_mtx
);
1122 pm_dev_err(dev
, state
, " noirq", error
);
1123 dpm_save_failed_dev(dev_name(dev
));
1127 if (!list_empty(&dev
->power
.entry
))
1128 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1134 mutex_unlock(&dpm_list_mtx
);
1135 async_synchronize_full();
1137 error
= async_error
;
1140 suspend_stats
.failed_suspend_noirq
++;
1141 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1142 dpm_resume_noirq(resume_event(state
));
1144 dpm_show_time(starttime
, state
, "noirq");
1146 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, false);
1151 * device_suspend_late - Execute a "late suspend" callback for given device.
1152 * @dev: Device to handle.
1153 * @state: PM transition of the system being carried out.
1154 * @async: If true, the device is being suspended asynchronously.
1156 * Runtime PM is disabled for @dev while this function is being executed.
1158 static int __device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1160 pm_callback_t callback
= NULL
;
1167 __pm_runtime_disable(dev
, false);
1172 if (pm_wakeup_pending()) {
1173 async_error
= -EBUSY
;
1177 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1180 dpm_wait_for_children(dev
, async
);
1182 if (dev
->pm_domain
) {
1183 info
= "late power domain ";
1184 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1185 } else if (dev
->type
&& dev
->type
->pm
) {
1186 info
= "late type ";
1187 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1188 } else if (dev
->class && dev
->class->pm
) {
1189 info
= "late class ";
1190 callback
= pm_late_early_op(dev
->class->pm
, state
);
1191 } else if (dev
->bus
&& dev
->bus
->pm
) {
1193 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1196 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1197 info
= "late driver ";
1198 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1201 error
= dpm_run_callback(callback
, dev
, state
, info
);
1203 dev
->power
.is_late_suspended
= true;
1205 async_error
= error
;
1208 TRACE_SUSPEND(error
);
1209 complete_all(&dev
->power
.completion
);
1213 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1215 struct device
*dev
= (struct device
*)data
;
1218 error
= __device_suspend_late(dev
, pm_transition
, true);
1220 dpm_save_failed_dev(dev_name(dev
));
1221 pm_dev_err(dev
, pm_transition
, " async", error
);
1226 static int device_suspend_late(struct device
*dev
)
1228 reinit_completion(&dev
->power
.completion
);
1230 if (is_async(dev
)) {
1232 async_schedule(async_suspend_late
, dev
);
1236 return __device_suspend_late(dev
, pm_transition
, false);
1240 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1241 * @state: PM transition of the system being carried out.
1243 int dpm_suspend_late(pm_message_t state
)
1245 ktime_t starttime
= ktime_get();
1248 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1249 mutex_lock(&dpm_list_mtx
);
1250 pm_transition
= state
;
1253 while (!list_empty(&dpm_suspended_list
)) {
1254 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1257 mutex_unlock(&dpm_list_mtx
);
1259 error
= device_suspend_late(dev
);
1261 mutex_lock(&dpm_list_mtx
);
1263 pm_dev_err(dev
, state
, " late", error
);
1264 dpm_save_failed_dev(dev_name(dev
));
1268 if (!list_empty(&dev
->power
.entry
))
1269 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1275 mutex_unlock(&dpm_list_mtx
);
1276 async_synchronize_full();
1278 error
= async_error
;
1280 suspend_stats
.failed_suspend_late
++;
1281 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1282 dpm_resume_early(resume_event(state
));
1284 dpm_show_time(starttime
, state
, "late");
1286 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1291 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1292 * @state: PM transition of the system being carried out.
1294 int dpm_suspend_end(pm_message_t state
)
1296 int error
= dpm_suspend_late(state
);
1300 error
= dpm_suspend_noirq(state
);
1302 dpm_resume_early(resume_event(state
));
1308 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1311 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1312 * @dev: Device to suspend.
1313 * @state: PM transition of the system being carried out.
1314 * @cb: Suspend callback to execute.
1315 * @info: string description of caller.
1317 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1318 int (*cb
)(struct device
*dev
, pm_message_t state
),
1324 calltime
= initcall_debug_start(dev
);
1326 trace_device_pm_callback_start(dev
, info
, state
.event
);
1327 error
= cb(dev
, state
);
1328 trace_device_pm_callback_end(dev
, error
);
1329 suspend_report_result(cb
, error
);
1331 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1337 * device_suspend - Execute "suspend" callbacks for given device.
1338 * @dev: Device to handle.
1339 * @state: PM transition of the system being carried out.
1340 * @async: If true, the device is being suspended asynchronously.
1342 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1344 pm_callback_t callback
= NULL
;
1347 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1352 dpm_wait_for_children(dev
, async
);
1358 * If a device configured to wake up the system from sleep states
1359 * has been suspended at run time and there's a resume request pending
1360 * for it, this is equivalent to the device signaling wakeup, so the
1361 * system suspend operation should be aborted.
1363 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1364 pm_wakeup_event(dev
, 0);
1366 if (pm_wakeup_pending()) {
1367 async_error
= -EBUSY
;
1371 if (dev
->power
.syscore
)
1374 if (dev
->power
.direct_complete
) {
1375 if (pm_runtime_status_suspended(dev
)) {
1376 pm_runtime_disable(dev
);
1377 if (pm_runtime_suspended_if_enabled(dev
))
1380 pm_runtime_enable(dev
);
1382 dev
->power
.direct_complete
= false;
1385 dpm_watchdog_set(&wd
, dev
);
1388 if (dev
->pm_domain
) {
1389 info
= "power domain ";
1390 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1394 if (dev
->type
&& dev
->type
->pm
) {
1396 callback
= pm_op(dev
->type
->pm
, state
);
1401 if (dev
->class->pm
) {
1403 callback
= pm_op(dev
->class->pm
, state
);
1405 } else if (dev
->class->suspend
) {
1406 pm_dev_dbg(dev
, state
, "legacy class ");
1407 error
= legacy_suspend(dev
, state
, dev
->class->suspend
,
1416 callback
= pm_op(dev
->bus
->pm
, state
);
1417 } else if (dev
->bus
->suspend
) {
1418 pm_dev_dbg(dev
, state
, "legacy bus ");
1419 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1426 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1428 callback
= pm_op(dev
->driver
->pm
, state
);
1431 error
= dpm_run_callback(callback
, dev
, state
, info
);
1435 struct device
*parent
= dev
->parent
;
1437 dev
->power
.is_suspended
= true;
1439 spin_lock_irq(&parent
->power
.lock
);
1441 dev
->parent
->power
.direct_complete
= false;
1442 if (dev
->power
.wakeup_path
1443 && !dev
->parent
->power
.ignore_children
)
1444 dev
->parent
->power
.wakeup_path
= true;
1446 spin_unlock_irq(&parent
->power
.lock
);
1451 dpm_watchdog_clear(&wd
);
1454 complete_all(&dev
->power
.completion
);
1456 async_error
= error
;
1458 TRACE_SUSPEND(error
);
1462 static void async_suspend(void *data
, async_cookie_t cookie
)
1464 struct device
*dev
= (struct device
*)data
;
1467 error
= __device_suspend(dev
, pm_transition
, true);
1469 dpm_save_failed_dev(dev_name(dev
));
1470 pm_dev_err(dev
, pm_transition
, " async", error
);
1476 static int device_suspend(struct device
*dev
)
1478 reinit_completion(&dev
->power
.completion
);
1480 if (is_async(dev
)) {
1482 async_schedule(async_suspend
, dev
);
1486 return __device_suspend(dev
, pm_transition
, false);
1490 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1491 * @state: PM transition of the system being carried out.
1493 int dpm_suspend(pm_message_t state
)
1495 ktime_t starttime
= ktime_get();
1498 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1503 mutex_lock(&dpm_list_mtx
);
1504 pm_transition
= state
;
1506 while (!list_empty(&dpm_prepared_list
)) {
1507 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1510 mutex_unlock(&dpm_list_mtx
);
1512 error
= device_suspend(dev
);
1514 mutex_lock(&dpm_list_mtx
);
1516 pm_dev_err(dev
, state
, "", error
);
1517 dpm_save_failed_dev(dev_name(dev
));
1521 if (!list_empty(&dev
->power
.entry
))
1522 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1527 mutex_unlock(&dpm_list_mtx
);
1528 async_synchronize_full();
1530 error
= async_error
;
1532 suspend_stats
.failed_suspend
++;
1533 dpm_save_failed_step(SUSPEND_SUSPEND
);
1535 dpm_show_time(starttime
, state
, NULL
);
1536 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, false);
1541 * device_prepare - Prepare a device for system power transition.
1542 * @dev: Device to handle.
1543 * @state: PM transition of the system being carried out.
1545 * Execute the ->prepare() callback(s) for given device. No new children of the
1546 * device may be registered after this function has returned.
1548 static int device_prepare(struct device
*dev
, pm_message_t state
)
1550 int (*callback
)(struct device
*) = NULL
;
1554 if (dev
->power
.syscore
)
1558 * If a device's parent goes into runtime suspend at the wrong time,
1559 * it won't be possible to resume the device. To prevent this we
1560 * block runtime suspend here, during the prepare phase, and allow
1561 * it again during the complete phase.
1563 pm_runtime_get_noresume(dev
);
1567 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1569 if (dev
->pm_domain
) {
1570 info
= "preparing power domain ";
1571 callback
= dev
->pm_domain
->ops
.prepare
;
1572 } else if (dev
->type
&& dev
->type
->pm
) {
1573 info
= "preparing type ";
1574 callback
= dev
->type
->pm
->prepare
;
1575 } else if (dev
->class && dev
->class->pm
) {
1576 info
= "preparing class ";
1577 callback
= dev
->class->pm
->prepare
;
1578 } else if (dev
->bus
&& dev
->bus
->pm
) {
1579 info
= "preparing bus ";
1580 callback
= dev
->bus
->pm
->prepare
;
1583 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1584 info
= "preparing driver ";
1585 callback
= dev
->driver
->pm
->prepare
;
1589 trace_device_pm_callback_start(dev
, info
, state
.event
);
1590 ret
= callback(dev
);
1591 trace_device_pm_callback_end(dev
, ret
);
1597 suspend_report_result(callback
, ret
);
1598 pm_runtime_put(dev
);
1602 * A positive return value from ->prepare() means "this device appears
1603 * to be runtime-suspended and its state is fine, so if it really is
1604 * runtime-suspended, you can leave it in that state provided that you
1605 * will do the same thing with all of its descendants". This only
1606 * applies to suspend transitions, however.
1608 spin_lock_irq(&dev
->power
.lock
);
1609 dev
->power
.direct_complete
= ret
> 0 && state
.event
== PM_EVENT_SUSPEND
;
1610 spin_unlock_irq(&dev
->power
.lock
);
1615 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1616 * @state: PM transition of the system being carried out.
1618 * Execute the ->prepare() callback(s) for all devices.
1620 int dpm_prepare(pm_message_t state
)
1624 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
1627 mutex_lock(&dpm_list_mtx
);
1628 while (!list_empty(&dpm_list
)) {
1629 struct device
*dev
= to_device(dpm_list
.next
);
1632 mutex_unlock(&dpm_list_mtx
);
1634 error
= device_prepare(dev
, state
);
1636 mutex_lock(&dpm_list_mtx
);
1638 if (error
== -EAGAIN
) {
1643 printk(KERN_INFO
"PM: Device %s not prepared "
1644 "for power transition: code %d\n",
1645 dev_name(dev
), error
);
1649 dev
->power
.is_prepared
= true;
1650 if (!list_empty(&dev
->power
.entry
))
1651 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1654 mutex_unlock(&dpm_list_mtx
);
1655 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
1660 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1661 * @state: PM transition of the system being carried out.
1663 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1664 * callbacks for them.
1666 int dpm_suspend_start(pm_message_t state
)
1670 error
= dpm_prepare(state
);
1672 suspend_stats
.failed_prepare
++;
1673 dpm_save_failed_step(SUSPEND_PREPARE
);
1675 error
= dpm_suspend(state
);
1678 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1680 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1683 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1685 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1688 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1689 * @dev: Device to wait for.
1690 * @subordinate: Device that needs to wait for @dev.
1692 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1694 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1697 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1700 * dpm_for_each_dev - device iterator.
1701 * @data: data for the callback.
1702 * @fn: function to be called for each device.
1704 * Iterate over devices in dpm_list, and call @fn for each device,
1707 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1715 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1719 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);