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/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.
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
|| dev
->power
.direct_complete
)
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 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
548 mutex_lock(&dpm_list_mtx
);
549 pm_transition
= state
;
552 * Advanced the async threads upfront,
553 * in case the starting of async threads is
554 * delayed by non-async resuming devices.
556 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
557 reinit_completion(&dev
->power
.completion
);
560 async_schedule(async_resume_noirq
, dev
);
564 while (!list_empty(&dpm_noirq_list
)) {
565 dev
= to_device(dpm_noirq_list
.next
);
567 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
568 mutex_unlock(&dpm_list_mtx
);
570 if (!is_async(dev
)) {
573 error
= device_resume_noirq(dev
, state
, false);
575 suspend_stats
.failed_resume_noirq
++;
576 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
577 dpm_save_failed_dev(dev_name(dev
));
578 pm_dev_err(dev
, state
, " noirq", error
);
582 mutex_lock(&dpm_list_mtx
);
585 mutex_unlock(&dpm_list_mtx
);
586 async_synchronize_full();
587 dpm_show_time(starttime
, state
, "noirq");
588 resume_device_irqs();
590 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
594 * device_resume_early - Execute an "early resume" callback for given device.
595 * @dev: Device to handle.
596 * @state: PM transition of the system being carried out.
598 * Runtime PM is disabled for @dev while this function is being executed.
600 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
602 pm_callback_t callback
= NULL
;
609 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
612 if (!dev
->power
.is_late_suspended
)
615 dpm_wait(dev
->parent
, async
);
617 if (dev
->pm_domain
) {
618 info
= "early power domain ";
619 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
620 } else if (dev
->type
&& dev
->type
->pm
) {
621 info
= "early type ";
622 callback
= pm_late_early_op(dev
->type
->pm
, state
);
623 } else if (dev
->class && dev
->class->pm
) {
624 info
= "early class ";
625 callback
= pm_late_early_op(dev
->class->pm
, state
);
626 } else if (dev
->bus
&& dev
->bus
->pm
) {
628 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
631 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
632 info
= "early driver ";
633 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
636 error
= dpm_run_callback(callback
, dev
, state
, info
);
637 dev
->power
.is_late_suspended
= false;
642 pm_runtime_enable(dev
);
643 complete_all(&dev
->power
.completion
);
647 static void async_resume_early(void *data
, async_cookie_t cookie
)
649 struct device
*dev
= (struct device
*)data
;
652 error
= device_resume_early(dev
, pm_transition
, true);
654 pm_dev_err(dev
, pm_transition
, " async", error
);
660 * dpm_resume_early - Execute "early resume" callbacks for all devices.
661 * @state: PM transition of the system being carried out.
663 static void dpm_resume_early(pm_message_t state
)
666 ktime_t starttime
= ktime_get();
668 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
669 mutex_lock(&dpm_list_mtx
);
670 pm_transition
= state
;
673 * Advanced the async threads upfront,
674 * in case the starting of async threads is
675 * delayed by non-async resuming devices.
677 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
678 reinit_completion(&dev
->power
.completion
);
681 async_schedule(async_resume_early
, dev
);
685 while (!list_empty(&dpm_late_early_list
)) {
686 dev
= to_device(dpm_late_early_list
.next
);
688 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
689 mutex_unlock(&dpm_list_mtx
);
691 if (!is_async(dev
)) {
694 error
= device_resume_early(dev
, state
, false);
696 suspend_stats
.failed_resume_early
++;
697 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
698 dpm_save_failed_dev(dev_name(dev
));
699 pm_dev_err(dev
, state
, " early", error
);
702 mutex_lock(&dpm_list_mtx
);
705 mutex_unlock(&dpm_list_mtx
);
706 async_synchronize_full();
707 dpm_show_time(starttime
, state
, "early");
708 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
712 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
713 * @state: PM transition of the system being carried out.
715 void dpm_resume_start(pm_message_t state
)
717 dpm_resume_noirq(state
);
718 dpm_resume_early(state
);
720 EXPORT_SYMBOL_GPL(dpm_resume_start
);
723 * device_resume - Execute "resume" callbacks for given device.
724 * @dev: Device to handle.
725 * @state: PM transition of the system being carried out.
726 * @async: If true, the device is being resumed asynchronously.
728 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
730 pm_callback_t callback
= NULL
;
733 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
738 if (dev
->power
.syscore
)
741 if (dev
->power
.direct_complete
) {
742 /* Match the pm_runtime_disable() in __device_suspend(). */
743 pm_runtime_enable(dev
);
747 dpm_wait(dev
->parent
, async
);
748 dpm_watchdog_set(&wd
, dev
);
752 * This is a fib. But we'll allow new children to be added below
753 * a resumed device, even if the device hasn't been completed yet.
755 dev
->power
.is_prepared
= false;
757 if (!dev
->power
.is_suspended
)
760 if (dev
->pm_domain
) {
761 info
= "power domain ";
762 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
766 if (dev
->type
&& dev
->type
->pm
) {
768 callback
= pm_op(dev
->type
->pm
, state
);
773 if (dev
->class->pm
) {
775 callback
= pm_op(dev
->class->pm
, state
);
777 } else if (dev
->class->resume
) {
778 info
= "legacy class ";
779 callback
= dev
->class->resume
;
787 callback
= pm_op(dev
->bus
->pm
, state
);
788 } else if (dev
->bus
->resume
) {
789 info
= "legacy bus ";
790 callback
= dev
->bus
->resume
;
796 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
798 callback
= pm_op(dev
->driver
->pm
, state
);
802 error
= dpm_run_callback(callback
, dev
, state
, info
);
803 dev
->power
.is_suspended
= false;
807 dpm_watchdog_clear(&wd
);
810 complete_all(&dev
->power
.completion
);
817 static void async_resume(void *data
, async_cookie_t cookie
)
819 struct device
*dev
= (struct device
*)data
;
822 error
= device_resume(dev
, pm_transition
, true);
824 pm_dev_err(dev
, pm_transition
, " async", error
);
829 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
830 * @state: PM transition of the system being carried out.
832 * Execute the appropriate "resume" callback for all devices whose status
833 * indicates that they are suspended.
835 void dpm_resume(pm_message_t state
)
838 ktime_t starttime
= ktime_get();
840 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
843 mutex_lock(&dpm_list_mtx
);
844 pm_transition
= state
;
847 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
848 reinit_completion(&dev
->power
.completion
);
851 async_schedule(async_resume
, dev
);
855 while (!list_empty(&dpm_suspended_list
)) {
856 dev
= to_device(dpm_suspended_list
.next
);
858 if (!is_async(dev
)) {
861 mutex_unlock(&dpm_list_mtx
);
863 error
= device_resume(dev
, state
, false);
865 suspend_stats
.failed_resume
++;
866 dpm_save_failed_step(SUSPEND_RESUME
);
867 dpm_save_failed_dev(dev_name(dev
));
868 pm_dev_err(dev
, state
, "", error
);
871 mutex_lock(&dpm_list_mtx
);
873 if (!list_empty(&dev
->power
.entry
))
874 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
877 mutex_unlock(&dpm_list_mtx
);
878 async_synchronize_full();
879 dpm_show_time(starttime
, state
, NULL
);
882 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
886 * device_complete - Complete a PM transition for given device.
887 * @dev: Device to handle.
888 * @state: PM transition of the system being carried out.
890 static void device_complete(struct device
*dev
, pm_message_t state
)
892 void (*callback
)(struct device
*) = NULL
;
895 if (dev
->power
.syscore
)
900 if (dev
->pm_domain
) {
901 info
= "completing power domain ";
902 callback
= dev
->pm_domain
->ops
.complete
;
903 } else if (dev
->type
&& dev
->type
->pm
) {
904 info
= "completing type ";
905 callback
= dev
->type
->pm
->complete
;
906 } else if (dev
->class && dev
->class->pm
) {
907 info
= "completing class ";
908 callback
= dev
->class->pm
->complete
;
909 } else if (dev
->bus
&& dev
->bus
->pm
) {
910 info
= "completing bus ";
911 callback
= dev
->bus
->pm
->complete
;
914 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
915 info
= "completing driver ";
916 callback
= dev
->driver
->pm
->complete
;
920 pm_dev_dbg(dev
, state
, info
);
921 trace_device_pm_callback_start(dev
, info
, state
.event
);
923 trace_device_pm_callback_end(dev
, 0);
932 * dpm_complete - Complete a PM transition for all non-sysdev devices.
933 * @state: PM transition of the system being carried out.
935 * Execute the ->complete() callbacks for all devices whose PM status is not
936 * DPM_ON (this allows new devices to be registered).
938 void dpm_complete(pm_message_t state
)
940 struct list_head list
;
942 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
945 INIT_LIST_HEAD(&list
);
946 mutex_lock(&dpm_list_mtx
);
947 while (!list_empty(&dpm_prepared_list
)) {
948 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
951 dev
->power
.is_prepared
= false;
952 list_move(&dev
->power
.entry
, &list
);
953 mutex_unlock(&dpm_list_mtx
);
955 device_complete(dev
, state
);
957 mutex_lock(&dpm_list_mtx
);
960 list_splice(&list
, &dpm_list
);
961 mutex_unlock(&dpm_list_mtx
);
962 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
966 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
967 * @state: PM transition of the system being carried out.
969 * Execute "resume" callbacks for all devices and complete the PM transition of
972 void dpm_resume_end(pm_message_t state
)
977 EXPORT_SYMBOL_GPL(dpm_resume_end
);
980 /*------------------------- Suspend routines -------------------------*/
983 * resume_event - Return a "resume" message for given "suspend" sleep state.
984 * @sleep_state: PM message representing a sleep state.
986 * Return a PM message representing the resume event corresponding to given
989 static pm_message_t
resume_event(pm_message_t sleep_state
)
991 switch (sleep_state
.event
) {
992 case PM_EVENT_SUSPEND
:
994 case PM_EVENT_FREEZE
:
995 case PM_EVENT_QUIESCE
:
997 case PM_EVENT_HIBERNATE
:
1004 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1005 * @dev: Device to handle.
1006 * @state: PM transition of the system being carried out.
1008 * The driver of @dev will not receive interrupts while this function is being
1011 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1013 pm_callback_t callback
= NULL
;
1020 if (pm_wakeup_pending()) {
1021 async_error
= -EBUSY
;
1025 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1028 dpm_wait_for_children(dev
, async
);
1030 if (dev
->pm_domain
) {
1031 info
= "noirq power domain ";
1032 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1033 } else if (dev
->type
&& dev
->type
->pm
) {
1034 info
= "noirq type ";
1035 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1036 } else if (dev
->class && dev
->class->pm
) {
1037 info
= "noirq class ";
1038 callback
= pm_noirq_op(dev
->class->pm
, state
);
1039 } else if (dev
->bus
&& dev
->bus
->pm
) {
1040 info
= "noirq bus ";
1041 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1044 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1045 info
= "noirq driver ";
1046 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1049 error
= dpm_run_callback(callback
, dev
, state
, info
);
1051 dev
->power
.is_noirq_suspended
= true;
1053 async_error
= error
;
1056 complete_all(&dev
->power
.completion
);
1060 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1062 struct device
*dev
= (struct device
*)data
;
1065 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1067 dpm_save_failed_dev(dev_name(dev
));
1068 pm_dev_err(dev
, pm_transition
, " async", error
);
1074 static int device_suspend_noirq(struct device
*dev
)
1076 reinit_completion(&dev
->power
.completion
);
1078 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1080 async_schedule(async_suspend_noirq
, dev
);
1083 return __device_suspend_noirq(dev
, pm_transition
, false);
1087 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1088 * @state: PM transition of the system being carried out.
1090 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1091 * handlers for all non-sysdev devices.
1093 static int dpm_suspend_noirq(pm_message_t state
)
1095 ktime_t starttime
= ktime_get();
1098 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1100 suspend_device_irqs();
1101 mutex_lock(&dpm_list_mtx
);
1102 pm_transition
= state
;
1105 while (!list_empty(&dpm_late_early_list
)) {
1106 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1109 mutex_unlock(&dpm_list_mtx
);
1111 error
= device_suspend_noirq(dev
);
1113 mutex_lock(&dpm_list_mtx
);
1115 pm_dev_err(dev
, state
, " noirq", error
);
1116 dpm_save_failed_dev(dev_name(dev
));
1120 if (!list_empty(&dev
->power
.entry
))
1121 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1127 mutex_unlock(&dpm_list_mtx
);
1128 async_synchronize_full();
1130 error
= async_error
;
1133 suspend_stats
.failed_suspend_noirq
++;
1134 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1135 dpm_resume_noirq(resume_event(state
));
1137 dpm_show_time(starttime
, state
, "noirq");
1139 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, false);
1144 * device_suspend_late - Execute a "late suspend" callback for given device.
1145 * @dev: Device to handle.
1146 * @state: PM transition of the system being carried out.
1148 * Runtime PM is disabled for @dev while this function is being executed.
1150 static int __device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1152 pm_callback_t callback
= NULL
;
1156 __pm_runtime_disable(dev
, false);
1161 if (pm_wakeup_pending()) {
1162 async_error
= -EBUSY
;
1166 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1169 dpm_wait_for_children(dev
, async
);
1171 if (dev
->pm_domain
) {
1172 info
= "late power domain ";
1173 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1174 } else if (dev
->type
&& dev
->type
->pm
) {
1175 info
= "late type ";
1176 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1177 } else if (dev
->class && dev
->class->pm
) {
1178 info
= "late class ";
1179 callback
= pm_late_early_op(dev
->class->pm
, state
);
1180 } else if (dev
->bus
&& dev
->bus
->pm
) {
1182 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1185 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1186 info
= "late driver ";
1187 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1190 error
= dpm_run_callback(callback
, dev
, state
, info
);
1192 dev
->power
.is_late_suspended
= true;
1194 async_error
= error
;
1197 complete_all(&dev
->power
.completion
);
1201 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1203 struct device
*dev
= (struct device
*)data
;
1206 error
= __device_suspend_late(dev
, pm_transition
, true);
1208 dpm_save_failed_dev(dev_name(dev
));
1209 pm_dev_err(dev
, pm_transition
, " async", error
);
1214 static int device_suspend_late(struct device
*dev
)
1216 reinit_completion(&dev
->power
.completion
);
1218 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1220 async_schedule(async_suspend_late
, dev
);
1224 return __device_suspend_late(dev
, pm_transition
, false);
1228 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1229 * @state: PM transition of the system being carried out.
1231 static int dpm_suspend_late(pm_message_t state
)
1233 ktime_t starttime
= ktime_get();
1236 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1237 mutex_lock(&dpm_list_mtx
);
1238 pm_transition
= state
;
1241 while (!list_empty(&dpm_suspended_list
)) {
1242 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1245 mutex_unlock(&dpm_list_mtx
);
1247 error
= device_suspend_late(dev
);
1249 mutex_lock(&dpm_list_mtx
);
1251 pm_dev_err(dev
, state
, " late", error
);
1252 dpm_save_failed_dev(dev_name(dev
));
1256 if (!list_empty(&dev
->power
.entry
))
1257 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1263 mutex_unlock(&dpm_list_mtx
);
1264 async_synchronize_full();
1266 suspend_stats
.failed_suspend_late
++;
1267 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1268 dpm_resume_early(resume_event(state
));
1270 dpm_show_time(starttime
, state
, "late");
1272 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1277 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1278 * @state: PM transition of the system being carried out.
1280 int dpm_suspend_end(pm_message_t state
)
1282 int error
= dpm_suspend_late(state
);
1286 error
= dpm_suspend_noirq(state
);
1288 dpm_resume_early(resume_event(state
));
1294 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1297 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1298 * @dev: Device to suspend.
1299 * @state: PM transition of the system being carried out.
1300 * @cb: Suspend callback to execute.
1302 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1303 int (*cb
)(struct device
*dev
, pm_message_t state
),
1309 calltime
= initcall_debug_start(dev
);
1311 trace_device_pm_callback_start(dev
, info
, state
.event
);
1312 error
= cb(dev
, state
);
1313 trace_device_pm_callback_end(dev
, error
);
1314 suspend_report_result(cb
, error
);
1316 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1322 * device_suspend - Execute "suspend" callbacks for given device.
1323 * @dev: Device to handle.
1324 * @state: PM transition of the system being carried out.
1325 * @async: If true, the device is being suspended asynchronously.
1327 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1329 pm_callback_t callback
= NULL
;
1332 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1334 dpm_wait_for_children(dev
, async
);
1340 * If a device configured to wake up the system from sleep states
1341 * has been suspended at run time and there's a resume request pending
1342 * for it, this is equivalent to the device signaling wakeup, so the
1343 * system suspend operation should be aborted.
1345 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1346 pm_wakeup_event(dev
, 0);
1348 if (pm_wakeup_pending()) {
1349 async_error
= -EBUSY
;
1353 if (dev
->power
.syscore
)
1356 if (dev
->power
.direct_complete
) {
1357 if (pm_runtime_status_suspended(dev
)) {
1358 pm_runtime_disable(dev
);
1359 if (pm_runtime_suspended_if_enabled(dev
))
1362 pm_runtime_enable(dev
);
1364 dev
->power
.direct_complete
= false;
1367 dpm_watchdog_set(&wd
, dev
);
1370 if (dev
->pm_domain
) {
1371 info
= "power domain ";
1372 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1376 if (dev
->type
&& dev
->type
->pm
) {
1378 callback
= pm_op(dev
->type
->pm
, state
);
1383 if (dev
->class->pm
) {
1385 callback
= pm_op(dev
->class->pm
, state
);
1387 } else if (dev
->class->suspend
) {
1388 pm_dev_dbg(dev
, state
, "legacy class ");
1389 error
= legacy_suspend(dev
, state
, dev
->class->suspend
,
1398 callback
= pm_op(dev
->bus
->pm
, state
);
1399 } else if (dev
->bus
->suspend
) {
1400 pm_dev_dbg(dev
, state
, "legacy bus ");
1401 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1408 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1410 callback
= pm_op(dev
->driver
->pm
, state
);
1413 error
= dpm_run_callback(callback
, dev
, state
, info
);
1417 struct device
*parent
= dev
->parent
;
1419 dev
->power
.is_suspended
= true;
1421 spin_lock_irq(&parent
->power
.lock
);
1423 dev
->parent
->power
.direct_complete
= false;
1424 if (dev
->power
.wakeup_path
1425 && !dev
->parent
->power
.ignore_children
)
1426 dev
->parent
->power
.wakeup_path
= true;
1428 spin_unlock_irq(&parent
->power
.lock
);
1433 dpm_watchdog_clear(&wd
);
1436 complete_all(&dev
->power
.completion
);
1438 async_error
= error
;
1443 static void async_suspend(void *data
, async_cookie_t cookie
)
1445 struct device
*dev
= (struct device
*)data
;
1448 error
= __device_suspend(dev
, pm_transition
, true);
1450 dpm_save_failed_dev(dev_name(dev
));
1451 pm_dev_err(dev
, pm_transition
, " async", error
);
1457 static int device_suspend(struct device
*dev
)
1459 reinit_completion(&dev
->power
.completion
);
1461 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1463 async_schedule(async_suspend
, dev
);
1467 return __device_suspend(dev
, pm_transition
, false);
1471 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1472 * @state: PM transition of the system being carried out.
1474 int dpm_suspend(pm_message_t state
)
1476 ktime_t starttime
= ktime_get();
1479 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1484 mutex_lock(&dpm_list_mtx
);
1485 pm_transition
= state
;
1487 while (!list_empty(&dpm_prepared_list
)) {
1488 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1491 mutex_unlock(&dpm_list_mtx
);
1493 error
= device_suspend(dev
);
1495 mutex_lock(&dpm_list_mtx
);
1497 pm_dev_err(dev
, state
, "", error
);
1498 dpm_save_failed_dev(dev_name(dev
));
1502 if (!list_empty(&dev
->power
.entry
))
1503 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1508 mutex_unlock(&dpm_list_mtx
);
1509 async_synchronize_full();
1511 error
= async_error
;
1513 suspend_stats
.failed_suspend
++;
1514 dpm_save_failed_step(SUSPEND_SUSPEND
);
1516 dpm_show_time(starttime
, state
, NULL
);
1517 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, false);
1522 * device_prepare - Prepare a device for system power transition.
1523 * @dev: Device to handle.
1524 * @state: PM transition of the system being carried out.
1526 * Execute the ->prepare() callback(s) for given device. No new children of the
1527 * device may be registered after this function has returned.
1529 static int device_prepare(struct device
*dev
, pm_message_t state
)
1531 int (*callback
)(struct device
*) = NULL
;
1535 if (dev
->power
.syscore
)
1539 * If a device's parent goes into runtime suspend at the wrong time,
1540 * it won't be possible to resume the device. To prevent this we
1541 * block runtime suspend here, during the prepare phase, and allow
1542 * it again during the complete phase.
1544 pm_runtime_get_noresume(dev
);
1548 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1550 if (dev
->pm_domain
) {
1551 info
= "preparing power domain ";
1552 callback
= dev
->pm_domain
->ops
.prepare
;
1553 } else if (dev
->type
&& dev
->type
->pm
) {
1554 info
= "preparing type ";
1555 callback
= dev
->type
->pm
->prepare
;
1556 } else if (dev
->class && dev
->class->pm
) {
1557 info
= "preparing class ";
1558 callback
= dev
->class->pm
->prepare
;
1559 } else if (dev
->bus
&& dev
->bus
->pm
) {
1560 info
= "preparing bus ";
1561 callback
= dev
->bus
->pm
->prepare
;
1564 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1565 info
= "preparing driver ";
1566 callback
= dev
->driver
->pm
->prepare
;
1570 trace_device_pm_callback_start(dev
, info
, state
.event
);
1571 ret
= callback(dev
);
1572 trace_device_pm_callback_end(dev
, ret
);
1578 suspend_report_result(callback
, ret
);
1579 pm_runtime_put(dev
);
1583 * A positive return value from ->prepare() means "this device appears
1584 * to be runtime-suspended and its state is fine, so if it really is
1585 * runtime-suspended, you can leave it in that state provided that you
1586 * will do the same thing with all of its descendants". This only
1587 * applies to suspend transitions, however.
1589 spin_lock_irq(&dev
->power
.lock
);
1590 dev
->power
.direct_complete
= ret
> 0 && state
.event
== PM_EVENT_SUSPEND
;
1591 spin_unlock_irq(&dev
->power
.lock
);
1596 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1597 * @state: PM transition of the system being carried out.
1599 * Execute the ->prepare() callback(s) for all devices.
1601 int dpm_prepare(pm_message_t state
)
1605 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
1608 mutex_lock(&dpm_list_mtx
);
1609 while (!list_empty(&dpm_list
)) {
1610 struct device
*dev
= to_device(dpm_list
.next
);
1613 mutex_unlock(&dpm_list_mtx
);
1615 error
= device_prepare(dev
, state
);
1617 mutex_lock(&dpm_list_mtx
);
1619 if (error
== -EAGAIN
) {
1624 printk(KERN_INFO
"PM: Device %s not prepared "
1625 "for power transition: code %d\n",
1626 dev_name(dev
), error
);
1630 dev
->power
.is_prepared
= true;
1631 if (!list_empty(&dev
->power
.entry
))
1632 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1635 mutex_unlock(&dpm_list_mtx
);
1636 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
1641 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1642 * @state: PM transition of the system being carried out.
1644 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1645 * callbacks for them.
1647 int dpm_suspend_start(pm_message_t state
)
1651 error
= dpm_prepare(state
);
1653 suspend_stats
.failed_prepare
++;
1654 dpm_save_failed_step(SUSPEND_PREPARE
);
1656 error
= dpm_suspend(state
);
1659 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1661 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1664 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1666 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1669 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1670 * @dev: Device to wait for.
1671 * @subordinate: Device that needs to wait for @dev.
1673 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1675 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1678 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1681 * dpm_for_each_dev - device iterator.
1682 * @data: data for the callback.
1683 * @fn: function to be called for each device.
1685 * Iterate over devices in dpm_list, and call @fn for each device,
1688 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1696 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1700 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);