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 init_completion(&dev
->power
.completion
);
95 complete_all(&dev
->power
.completion
);
96 dev
->power
.wakeup
= NULL
;
97 INIT_LIST_HEAD(&dev
->power
.entry
);
101 * device_pm_lock - Lock the list of active devices used by the PM core.
103 void device_pm_lock(void)
105 mutex_lock(&dpm_list_mtx
);
109 * device_pm_unlock - Unlock the list of active devices used by the PM core.
111 void device_pm_unlock(void)
113 mutex_unlock(&dpm_list_mtx
);
117 * device_pm_add - Add a device to the PM core's list of active devices.
118 * @dev: Device to add to the list.
120 void device_pm_add(struct device
*dev
)
122 pr_debug("PM: Adding info for %s:%s\n",
123 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
124 mutex_lock(&dpm_list_mtx
);
125 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
126 dev_warn(dev
, "parent %s should not be sleeping\n",
127 dev_name(dev
->parent
));
128 list_add_tail(&dev
->power
.entry
, &dpm_list
);
129 mutex_unlock(&dpm_list_mtx
);
133 * device_pm_remove - Remove a device from the PM core's list of active devices.
134 * @dev: Device to be removed from the list.
136 void device_pm_remove(struct device
*dev
)
138 pr_debug("PM: Removing info for %s:%s\n",
139 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
140 complete_all(&dev
->power
.completion
);
141 mutex_lock(&dpm_list_mtx
);
142 list_del_init(&dev
->power
.entry
);
143 mutex_unlock(&dpm_list_mtx
);
144 device_wakeup_disable(dev
);
145 pm_runtime_remove(dev
);
149 * device_pm_move_before - Move device in the PM core's list of active devices.
150 * @deva: Device to move in dpm_list.
151 * @devb: Device @deva should come before.
153 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
155 pr_debug("PM: Moving %s:%s before %s:%s\n",
156 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
157 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
158 /* Delete deva from dpm_list and reinsert before devb. */
159 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
163 * device_pm_move_after - Move device in the PM core's list of active devices.
164 * @deva: Device to move in dpm_list.
165 * @devb: Device @deva should come after.
167 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
169 pr_debug("PM: Moving %s:%s after %s:%s\n",
170 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
171 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
172 /* Delete deva from dpm_list and reinsert after devb. */
173 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
177 * device_pm_move_last - Move device to end of the PM core's list of devices.
178 * @dev: Device to move in dpm_list.
180 void device_pm_move_last(struct device
*dev
)
182 pr_debug("PM: Moving %s:%s to end of list\n",
183 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
184 list_move_tail(&dev
->power
.entry
, &dpm_list
);
187 static ktime_t
initcall_debug_start(struct device
*dev
)
189 ktime_t calltime
= ktime_set(0, 0);
191 if (pm_print_times_enabled
) {
192 pr_info("calling %s+ @ %i, parent: %s\n",
193 dev_name(dev
), task_pid_nr(current
),
194 dev
->parent
? dev_name(dev
->parent
) : "none");
195 calltime
= ktime_get();
201 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
202 int error
, pm_message_t state
, char *info
)
207 rettime
= ktime_get();
208 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
210 if (pm_print_times_enabled
) {
211 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
212 error
, (unsigned long long)nsecs
>> 10);
215 trace_device_pm_report_time(dev
, info
, nsecs
, pm_verb(state
.event
),
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
);
388 suspend_report_result(cb
, error
);
390 initcall_debug_report(dev
, calltime
, error
, state
, info
);
395 #ifdef CONFIG_DPM_WATCHDOG
396 struct dpm_watchdog
{
398 struct task_struct
*tsk
;
399 struct timer_list timer
;
402 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
403 struct dpm_watchdog wd
406 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
407 * @data: Watchdog object address.
409 * Called when a driver has timed out suspending or resuming.
410 * There's not much we can do here to recover so panic() to
411 * capture a crash-dump in pstore.
413 static void dpm_watchdog_handler(unsigned long data
)
415 struct dpm_watchdog
*wd
= (void *)data
;
417 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
418 show_stack(wd
->tsk
, NULL
);
419 panic("%s %s: unrecoverable failure\n",
420 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
424 * dpm_watchdog_set - Enable pm watchdog for given device.
425 * @wd: Watchdog. Must be allocated on the stack.
426 * @dev: Device to handle.
428 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
430 struct timer_list
*timer
= &wd
->timer
;
435 init_timer_on_stack(timer
);
436 /* use same timeout value for both suspend and resume */
437 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
438 timer
->function
= dpm_watchdog_handler
;
439 timer
->data
= (unsigned long)wd
;
444 * dpm_watchdog_clear - Disable suspend/resume watchdog.
445 * @wd: Watchdog to disable.
447 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
449 struct timer_list
*timer
= &wd
->timer
;
451 del_timer_sync(timer
);
452 destroy_timer_on_stack(timer
);
455 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
456 #define dpm_watchdog_set(x, y)
457 #define dpm_watchdog_clear(x)
460 /*------------------------- Resume routines -------------------------*/
463 * device_resume_noirq - Execute an "early resume" callback for given device.
464 * @dev: Device to handle.
465 * @state: PM transition of the system being carried out.
467 * The driver of @dev will not receive interrupts while this function is being
470 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
472 pm_callback_t callback
= NULL
;
479 if (dev
->power
.syscore
)
482 if (dev
->pm_domain
) {
483 info
= "noirq power domain ";
484 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
485 } else if (dev
->type
&& dev
->type
->pm
) {
486 info
= "noirq type ";
487 callback
= pm_noirq_op(dev
->type
->pm
, state
);
488 } else if (dev
->class && dev
->class->pm
) {
489 info
= "noirq class ";
490 callback
= pm_noirq_op(dev
->class->pm
, state
);
491 } else if (dev
->bus
&& dev
->bus
->pm
) {
493 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
496 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
497 info
= "noirq driver ";
498 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
501 error
= dpm_run_callback(callback
, dev
, state
, info
);
509 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
510 * @state: PM transition of the system being carried out.
512 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
513 * enable device drivers to receive interrupts.
515 static void dpm_resume_noirq(pm_message_t state
)
517 ktime_t starttime
= ktime_get();
519 mutex_lock(&dpm_list_mtx
);
520 while (!list_empty(&dpm_noirq_list
)) {
521 struct device
*dev
= to_device(dpm_noirq_list
.next
);
525 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
526 mutex_unlock(&dpm_list_mtx
);
528 error
= device_resume_noirq(dev
, state
);
530 suspend_stats
.failed_resume_noirq
++;
531 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
532 dpm_save_failed_dev(dev_name(dev
));
533 pm_dev_err(dev
, state
, " noirq", error
);
536 mutex_lock(&dpm_list_mtx
);
539 mutex_unlock(&dpm_list_mtx
);
540 dpm_show_time(starttime
, state
, "noirq");
541 resume_device_irqs();
546 * device_resume_early - Execute an "early resume" callback for given device.
547 * @dev: Device to handle.
548 * @state: PM transition of the system being carried out.
550 * Runtime PM is disabled for @dev while this function is being executed.
552 static int device_resume_early(struct device
*dev
, pm_message_t state
)
554 pm_callback_t callback
= NULL
;
561 if (dev
->power
.syscore
)
564 if (dev
->pm_domain
) {
565 info
= "early power domain ";
566 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
567 } else if (dev
->type
&& dev
->type
->pm
) {
568 info
= "early type ";
569 callback
= pm_late_early_op(dev
->type
->pm
, state
);
570 } else if (dev
->class && dev
->class->pm
) {
571 info
= "early class ";
572 callback
= pm_late_early_op(dev
->class->pm
, state
);
573 } else if (dev
->bus
&& dev
->bus
->pm
) {
575 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
578 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
579 info
= "early driver ";
580 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
583 error
= dpm_run_callback(callback
, dev
, state
, info
);
588 pm_runtime_enable(dev
);
593 * dpm_resume_early - Execute "early resume" callbacks for all devices.
594 * @state: PM transition of the system being carried out.
596 static void dpm_resume_early(pm_message_t state
)
598 ktime_t starttime
= ktime_get();
600 mutex_lock(&dpm_list_mtx
);
601 while (!list_empty(&dpm_late_early_list
)) {
602 struct device
*dev
= to_device(dpm_late_early_list
.next
);
606 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
607 mutex_unlock(&dpm_list_mtx
);
609 error
= device_resume_early(dev
, state
);
611 suspend_stats
.failed_resume_early
++;
612 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
613 dpm_save_failed_dev(dev_name(dev
));
614 pm_dev_err(dev
, state
, " early", error
);
617 mutex_lock(&dpm_list_mtx
);
620 mutex_unlock(&dpm_list_mtx
);
621 dpm_show_time(starttime
, state
, "early");
625 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
626 * @state: PM transition of the system being carried out.
628 void dpm_resume_start(pm_message_t state
)
630 dpm_resume_noirq(state
);
631 dpm_resume_early(state
);
633 EXPORT_SYMBOL_GPL(dpm_resume_start
);
636 * device_resume - Execute "resume" callbacks for given device.
637 * @dev: Device to handle.
638 * @state: PM transition of the system being carried out.
639 * @async: If true, the device is being resumed asynchronously.
641 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
643 pm_callback_t callback
= NULL
;
646 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
651 if (dev
->power
.syscore
)
654 dpm_wait(dev
->parent
, async
);
655 dpm_watchdog_set(&wd
, dev
);
659 * This is a fib. But we'll allow new children to be added below
660 * a resumed device, even if the device hasn't been completed yet.
662 dev
->power
.is_prepared
= false;
664 if (!dev
->power
.is_suspended
)
667 if (dev
->pm_domain
) {
668 info
= "power domain ";
669 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
673 if (dev
->type
&& dev
->type
->pm
) {
675 callback
= pm_op(dev
->type
->pm
, state
);
680 if (dev
->class->pm
) {
682 callback
= pm_op(dev
->class->pm
, state
);
684 } else if (dev
->class->resume
) {
685 info
= "legacy class ";
686 callback
= dev
->class->resume
;
694 callback
= pm_op(dev
->bus
->pm
, state
);
695 } else if (dev
->bus
->resume
) {
696 info
= "legacy bus ";
697 callback
= dev
->bus
->resume
;
703 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
705 callback
= pm_op(dev
->driver
->pm
, state
);
709 error
= dpm_run_callback(callback
, dev
, state
, info
);
710 dev
->power
.is_suspended
= false;
714 dpm_watchdog_clear(&wd
);
717 complete_all(&dev
->power
.completion
);
724 static void async_resume(void *data
, async_cookie_t cookie
)
726 struct device
*dev
= (struct device
*)data
;
729 error
= device_resume(dev
, pm_transition
, true);
731 pm_dev_err(dev
, pm_transition
, " async", error
);
735 static bool is_async(struct device
*dev
)
737 return dev
->power
.async_suspend
&& pm_async_enabled
738 && !pm_trace_is_enabled();
742 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
743 * @state: PM transition of the system being carried out.
745 * Execute the appropriate "resume" callback for all devices whose status
746 * indicates that they are suspended.
748 void dpm_resume(pm_message_t state
)
751 ktime_t starttime
= ktime_get();
755 mutex_lock(&dpm_list_mtx
);
756 pm_transition
= state
;
759 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
760 reinit_completion(&dev
->power
.completion
);
763 async_schedule(async_resume
, dev
);
767 while (!list_empty(&dpm_suspended_list
)) {
768 dev
= to_device(dpm_suspended_list
.next
);
770 if (!is_async(dev
)) {
773 mutex_unlock(&dpm_list_mtx
);
775 error
= device_resume(dev
, state
, false);
777 suspend_stats
.failed_resume
++;
778 dpm_save_failed_step(SUSPEND_RESUME
);
779 dpm_save_failed_dev(dev_name(dev
));
780 pm_dev_err(dev
, state
, "", error
);
783 mutex_lock(&dpm_list_mtx
);
785 if (!list_empty(&dev
->power
.entry
))
786 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
789 mutex_unlock(&dpm_list_mtx
);
790 async_synchronize_full();
791 dpm_show_time(starttime
, state
, NULL
);
795 * device_complete - Complete a PM transition for given device.
796 * @dev: Device to handle.
797 * @state: PM transition of the system being carried out.
799 static void device_complete(struct device
*dev
, pm_message_t state
)
801 void (*callback
)(struct device
*) = NULL
;
804 if (dev
->power
.syscore
)
809 if (dev
->pm_domain
) {
810 info
= "completing power domain ";
811 callback
= dev
->pm_domain
->ops
.complete
;
812 } else if (dev
->type
&& dev
->type
->pm
) {
813 info
= "completing type ";
814 callback
= dev
->type
->pm
->complete
;
815 } else if (dev
->class && dev
->class->pm
) {
816 info
= "completing class ";
817 callback
= dev
->class->pm
->complete
;
818 } else if (dev
->bus
&& dev
->bus
->pm
) {
819 info
= "completing bus ";
820 callback
= dev
->bus
->pm
->complete
;
823 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
824 info
= "completing driver ";
825 callback
= dev
->driver
->pm
->complete
;
829 pm_dev_dbg(dev
, state
, info
);
839 * dpm_complete - Complete a PM transition for all non-sysdev devices.
840 * @state: PM transition of the system being carried out.
842 * Execute the ->complete() callbacks for all devices whose PM status is not
843 * DPM_ON (this allows new devices to be registered).
845 void dpm_complete(pm_message_t state
)
847 struct list_head list
;
851 INIT_LIST_HEAD(&list
);
852 mutex_lock(&dpm_list_mtx
);
853 while (!list_empty(&dpm_prepared_list
)) {
854 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
857 dev
->power
.is_prepared
= false;
858 list_move(&dev
->power
.entry
, &list
);
859 mutex_unlock(&dpm_list_mtx
);
861 device_complete(dev
, state
);
863 mutex_lock(&dpm_list_mtx
);
866 list_splice(&list
, &dpm_list
);
867 mutex_unlock(&dpm_list_mtx
);
871 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
872 * @state: PM transition of the system being carried out.
874 * Execute "resume" callbacks for all devices and complete the PM transition of
877 void dpm_resume_end(pm_message_t state
)
882 EXPORT_SYMBOL_GPL(dpm_resume_end
);
885 /*------------------------- Suspend routines -------------------------*/
888 * resume_event - Return a "resume" message for given "suspend" sleep state.
889 * @sleep_state: PM message representing a sleep state.
891 * Return a PM message representing the resume event corresponding to given
894 static pm_message_t
resume_event(pm_message_t sleep_state
)
896 switch (sleep_state
.event
) {
897 case PM_EVENT_SUSPEND
:
899 case PM_EVENT_FREEZE
:
900 case PM_EVENT_QUIESCE
:
902 case PM_EVENT_HIBERNATE
:
909 * device_suspend_noirq - Execute a "late suspend" callback for given device.
910 * @dev: Device to handle.
911 * @state: PM transition of the system being carried out.
913 * The driver of @dev will not receive interrupts while this function is being
916 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
918 pm_callback_t callback
= NULL
;
921 if (dev
->power
.syscore
)
924 if (dev
->pm_domain
) {
925 info
= "noirq power domain ";
926 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
927 } else if (dev
->type
&& dev
->type
->pm
) {
928 info
= "noirq type ";
929 callback
= pm_noirq_op(dev
->type
->pm
, state
);
930 } else if (dev
->class && dev
->class->pm
) {
931 info
= "noirq class ";
932 callback
= pm_noirq_op(dev
->class->pm
, state
);
933 } else if (dev
->bus
&& dev
->bus
->pm
) {
935 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
938 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
939 info
= "noirq driver ";
940 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
943 return dpm_run_callback(callback
, dev
, state
, info
);
947 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
948 * @state: PM transition of the system being carried out.
950 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
951 * handlers for all non-sysdev devices.
953 static int dpm_suspend_noirq(pm_message_t state
)
955 ktime_t starttime
= ktime_get();
959 suspend_device_irqs();
960 mutex_lock(&dpm_list_mtx
);
961 while (!list_empty(&dpm_late_early_list
)) {
962 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
965 mutex_unlock(&dpm_list_mtx
);
967 error
= device_suspend_noirq(dev
, state
);
969 mutex_lock(&dpm_list_mtx
);
971 pm_dev_err(dev
, state
, " noirq", error
);
972 suspend_stats
.failed_suspend_noirq
++;
973 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
974 dpm_save_failed_dev(dev_name(dev
));
978 if (!list_empty(&dev
->power
.entry
))
979 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
982 if (pm_wakeup_pending()) {
987 mutex_unlock(&dpm_list_mtx
);
989 dpm_resume_noirq(resume_event(state
));
991 dpm_show_time(starttime
, state
, "noirq");
996 * device_suspend_late - Execute a "late suspend" callback for given device.
997 * @dev: Device to handle.
998 * @state: PM transition of the system being carried out.
1000 * Runtime PM is disabled for @dev while this function is being executed.
1002 static int device_suspend_late(struct device
*dev
, pm_message_t state
)
1004 pm_callback_t callback
= NULL
;
1007 __pm_runtime_disable(dev
, false);
1009 if (dev
->power
.syscore
)
1012 if (dev
->pm_domain
) {
1013 info
= "late power domain ";
1014 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1015 } else if (dev
->type
&& dev
->type
->pm
) {
1016 info
= "late type ";
1017 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1018 } else if (dev
->class && dev
->class->pm
) {
1019 info
= "late class ";
1020 callback
= pm_late_early_op(dev
->class->pm
, state
);
1021 } else if (dev
->bus
&& dev
->bus
->pm
) {
1023 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1026 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1027 info
= "late driver ";
1028 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1031 return dpm_run_callback(callback
, dev
, state
, info
);
1035 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1036 * @state: PM transition of the system being carried out.
1038 static int dpm_suspend_late(pm_message_t state
)
1040 ktime_t starttime
= ktime_get();
1043 mutex_lock(&dpm_list_mtx
);
1044 while (!list_empty(&dpm_suspended_list
)) {
1045 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1048 mutex_unlock(&dpm_list_mtx
);
1050 error
= device_suspend_late(dev
, state
);
1052 mutex_lock(&dpm_list_mtx
);
1054 pm_dev_err(dev
, state
, " late", error
);
1055 suspend_stats
.failed_suspend_late
++;
1056 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1057 dpm_save_failed_dev(dev_name(dev
));
1061 if (!list_empty(&dev
->power
.entry
))
1062 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1065 if (pm_wakeup_pending()) {
1070 mutex_unlock(&dpm_list_mtx
);
1072 dpm_resume_early(resume_event(state
));
1074 dpm_show_time(starttime
, state
, "late");
1080 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1081 * @state: PM transition of the system being carried out.
1083 int dpm_suspend_end(pm_message_t state
)
1085 int error
= dpm_suspend_late(state
);
1089 error
= dpm_suspend_noirq(state
);
1091 dpm_resume_early(resume_event(state
));
1097 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1100 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1101 * @dev: Device to suspend.
1102 * @state: PM transition of the system being carried out.
1103 * @cb: Suspend callback to execute.
1105 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1106 int (*cb
)(struct device
*dev
, pm_message_t state
),
1112 calltime
= initcall_debug_start(dev
);
1114 error
= cb(dev
, state
);
1115 suspend_report_result(cb
, error
);
1117 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1123 * device_suspend - Execute "suspend" callbacks for given device.
1124 * @dev: Device to handle.
1125 * @state: PM transition of the system being carried out.
1126 * @async: If true, the device is being suspended asynchronously.
1128 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1130 pm_callback_t callback
= NULL
;
1133 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1135 dpm_wait_for_children(dev
, async
);
1141 * If a device configured to wake up the system from sleep states
1142 * has been suspended at run time and there's a resume request pending
1143 * for it, this is equivalent to the device signaling wakeup, so the
1144 * system suspend operation should be aborted.
1146 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1147 pm_wakeup_event(dev
, 0);
1149 if (pm_wakeup_pending()) {
1150 async_error
= -EBUSY
;
1154 if (dev
->power
.syscore
)
1157 dpm_watchdog_set(&wd
, dev
);
1160 if (dev
->pm_domain
) {
1161 info
= "power domain ";
1162 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1166 if (dev
->type
&& dev
->type
->pm
) {
1168 callback
= pm_op(dev
->type
->pm
, state
);
1173 if (dev
->class->pm
) {
1175 callback
= pm_op(dev
->class->pm
, state
);
1177 } else if (dev
->class->suspend
) {
1178 pm_dev_dbg(dev
, state
, "legacy class ");
1179 error
= legacy_suspend(dev
, state
, dev
->class->suspend
,
1188 callback
= pm_op(dev
->bus
->pm
, state
);
1189 } else if (dev
->bus
->suspend
) {
1190 pm_dev_dbg(dev
, state
, "legacy bus ");
1191 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1198 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1200 callback
= pm_op(dev
->driver
->pm
, state
);
1203 error
= dpm_run_callback(callback
, dev
, state
, info
);
1207 dev
->power
.is_suspended
= true;
1208 if (dev
->power
.wakeup_path
1209 && dev
->parent
&& !dev
->parent
->power
.ignore_children
)
1210 dev
->parent
->power
.wakeup_path
= true;
1214 dpm_watchdog_clear(&wd
);
1217 complete_all(&dev
->power
.completion
);
1219 async_error
= error
;
1224 static void async_suspend(void *data
, async_cookie_t cookie
)
1226 struct device
*dev
= (struct device
*)data
;
1229 error
= __device_suspend(dev
, pm_transition
, true);
1231 dpm_save_failed_dev(dev_name(dev
));
1232 pm_dev_err(dev
, pm_transition
, " async", error
);
1238 static int device_suspend(struct device
*dev
)
1240 reinit_completion(&dev
->power
.completion
);
1242 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1244 async_schedule(async_suspend
, dev
);
1248 return __device_suspend(dev
, pm_transition
, false);
1252 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1253 * @state: PM transition of the system being carried out.
1255 int dpm_suspend(pm_message_t state
)
1257 ktime_t starttime
= ktime_get();
1262 mutex_lock(&dpm_list_mtx
);
1263 pm_transition
= state
;
1265 while (!list_empty(&dpm_prepared_list
)) {
1266 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1269 mutex_unlock(&dpm_list_mtx
);
1271 error
= device_suspend(dev
);
1273 mutex_lock(&dpm_list_mtx
);
1275 pm_dev_err(dev
, state
, "", error
);
1276 dpm_save_failed_dev(dev_name(dev
));
1280 if (!list_empty(&dev
->power
.entry
))
1281 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1286 mutex_unlock(&dpm_list_mtx
);
1287 async_synchronize_full();
1289 error
= async_error
;
1291 suspend_stats
.failed_suspend
++;
1292 dpm_save_failed_step(SUSPEND_SUSPEND
);
1294 dpm_show_time(starttime
, state
, NULL
);
1299 * device_prepare - Prepare a device for system power transition.
1300 * @dev: Device to handle.
1301 * @state: PM transition of the system being carried out.
1303 * Execute the ->prepare() callback(s) for given device. No new children of the
1304 * device may be registered after this function has returned.
1306 static int device_prepare(struct device
*dev
, pm_message_t state
)
1308 int (*callback
)(struct device
*) = NULL
;
1312 if (dev
->power
.syscore
)
1316 * If a device's parent goes into runtime suspend at the wrong time,
1317 * it won't be possible to resume the device. To prevent this we
1318 * block runtime suspend here, during the prepare phase, and allow
1319 * it again during the complete phase.
1321 pm_runtime_get_noresume(dev
);
1325 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1327 if (dev
->pm_domain
) {
1328 info
= "preparing power domain ";
1329 callback
= dev
->pm_domain
->ops
.prepare
;
1330 } else if (dev
->type
&& dev
->type
->pm
) {
1331 info
= "preparing type ";
1332 callback
= dev
->type
->pm
->prepare
;
1333 } else if (dev
->class && dev
->class->pm
) {
1334 info
= "preparing class ";
1335 callback
= dev
->class->pm
->prepare
;
1336 } else if (dev
->bus
&& dev
->bus
->pm
) {
1337 info
= "preparing bus ";
1338 callback
= dev
->bus
->pm
->prepare
;
1341 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1342 info
= "preparing driver ";
1343 callback
= dev
->driver
->pm
->prepare
;
1347 error
= callback(dev
);
1348 suspend_report_result(callback
, error
);
1354 pm_runtime_put(dev
);
1360 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1361 * @state: PM transition of the system being carried out.
1363 * Execute the ->prepare() callback(s) for all devices.
1365 int dpm_prepare(pm_message_t state
)
1371 mutex_lock(&dpm_list_mtx
);
1372 while (!list_empty(&dpm_list
)) {
1373 struct device
*dev
= to_device(dpm_list
.next
);
1376 mutex_unlock(&dpm_list_mtx
);
1378 error
= device_prepare(dev
, state
);
1380 mutex_lock(&dpm_list_mtx
);
1382 if (error
== -EAGAIN
) {
1387 printk(KERN_INFO
"PM: Device %s not prepared "
1388 "for power transition: code %d\n",
1389 dev_name(dev
), error
);
1393 dev
->power
.is_prepared
= true;
1394 if (!list_empty(&dev
->power
.entry
))
1395 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1398 mutex_unlock(&dpm_list_mtx
);
1403 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1404 * @state: PM transition of the system being carried out.
1406 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1407 * callbacks for them.
1409 int dpm_suspend_start(pm_message_t state
)
1413 error
= dpm_prepare(state
);
1415 suspend_stats
.failed_prepare
++;
1416 dpm_save_failed_step(SUSPEND_PREPARE
);
1418 error
= dpm_suspend(state
);
1421 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1423 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1426 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1428 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1431 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1432 * @dev: Device to wait for.
1433 * @subordinate: Device that needs to wait for @dev.
1435 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1437 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1440 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1443 * dpm_for_each_dev - device iterator.
1444 * @data: data for the callback.
1445 * @fn: function to be called for each device.
1447 * Iterate over devices in dpm_list, and call @fn for each device,
1450 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1458 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1462 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);