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 <linux/cpuidle.h>
35 typedef int (*pm_callback_t
)(struct device
*);
38 * The entries in the dpm_list list are in a depth first order, simply
39 * because children are guaranteed to be discovered after parents, and
40 * are inserted at the back of the list on discovery.
42 * Since device_pm_add() may be called with a device lock held,
43 * we must never try to acquire a device lock while holding
48 static LIST_HEAD(dpm_prepared_list
);
49 static LIST_HEAD(dpm_suspended_list
);
50 static LIST_HEAD(dpm_late_early_list
);
51 static LIST_HEAD(dpm_noirq_list
);
53 struct suspend_stats suspend_stats
;
54 static DEFINE_MUTEX(dpm_list_mtx
);
55 static pm_message_t pm_transition
;
57 static int async_error
;
60 * device_pm_sleep_init - Initialize system suspend-related device fields.
61 * @dev: Device object being initialized.
63 void device_pm_sleep_init(struct device
*dev
)
65 dev
->power
.is_prepared
= false;
66 dev
->power
.is_suspended
= false;
67 init_completion(&dev
->power
.completion
);
68 complete_all(&dev
->power
.completion
);
69 dev
->power
.wakeup
= NULL
;
70 INIT_LIST_HEAD(&dev
->power
.entry
);
74 * device_pm_lock - Lock the list of active devices used by the PM core.
76 void device_pm_lock(void)
78 mutex_lock(&dpm_list_mtx
);
82 * device_pm_unlock - Unlock the list of active devices used by the PM core.
84 void device_pm_unlock(void)
86 mutex_unlock(&dpm_list_mtx
);
90 * device_pm_add - Add a device to the PM core's list of active devices.
91 * @dev: Device to add to the list.
93 void device_pm_add(struct device
*dev
)
95 pr_debug("PM: Adding info for %s:%s\n",
96 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
97 mutex_lock(&dpm_list_mtx
);
98 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
99 dev_warn(dev
, "parent %s should not be sleeping\n",
100 dev_name(dev
->parent
));
101 list_add_tail(&dev
->power
.entry
, &dpm_list
);
102 dev_pm_qos_constraints_init(dev
);
103 mutex_unlock(&dpm_list_mtx
);
107 * device_pm_remove - Remove a device from the PM core's list of active devices.
108 * @dev: Device to be removed from the list.
110 void device_pm_remove(struct device
*dev
)
112 pr_debug("PM: Removing info for %s:%s\n",
113 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
114 complete_all(&dev
->power
.completion
);
115 mutex_lock(&dpm_list_mtx
);
116 dev_pm_qos_constraints_destroy(dev
);
117 list_del_init(&dev
->power
.entry
);
118 mutex_unlock(&dpm_list_mtx
);
119 device_wakeup_disable(dev
);
120 pm_runtime_remove(dev
);
124 * device_pm_move_before - Move device in the PM core's list of active devices.
125 * @deva: Device to move in dpm_list.
126 * @devb: Device @deva should come before.
128 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
130 pr_debug("PM: Moving %s:%s before %s:%s\n",
131 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
132 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
133 /* Delete deva from dpm_list and reinsert before devb. */
134 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
138 * device_pm_move_after - Move device in the PM core's list of active devices.
139 * @deva: Device to move in dpm_list.
140 * @devb: Device @deva should come after.
142 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
144 pr_debug("PM: Moving %s:%s after %s:%s\n",
145 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
146 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
147 /* Delete deva from dpm_list and reinsert after devb. */
148 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
152 * device_pm_move_last - Move device to end of the PM core's list of devices.
153 * @dev: Device to move in dpm_list.
155 void device_pm_move_last(struct device
*dev
)
157 pr_debug("PM: Moving %s:%s to end of list\n",
158 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
159 list_move_tail(&dev
->power
.entry
, &dpm_list
);
162 static ktime_t
initcall_debug_start(struct device
*dev
)
164 ktime_t calltime
= ktime_set(0, 0);
166 if (pm_print_times_enabled
) {
167 pr_info("calling %s+ @ %i, parent: %s\n",
168 dev_name(dev
), task_pid_nr(current
),
169 dev
->parent
? dev_name(dev
->parent
) : "none");
170 calltime
= ktime_get();
176 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
179 ktime_t delta
, rettime
;
181 if (pm_print_times_enabled
) {
182 rettime
= ktime_get();
183 delta
= ktime_sub(rettime
, calltime
);
184 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
185 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
190 * dpm_wait - Wait for a PM operation to complete.
191 * @dev: Device to wait for.
192 * @async: If unset, wait only if the device's power.async_suspend flag is set.
194 static void dpm_wait(struct device
*dev
, bool async
)
199 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
200 wait_for_completion(&dev
->power
.completion
);
203 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
205 dpm_wait(dev
, *((bool *)async_ptr
));
209 static void dpm_wait_for_children(struct device
*dev
, bool async
)
211 device_for_each_child(dev
, &async
, dpm_wait_fn
);
215 * pm_op - Return the PM operation appropriate for given PM event.
216 * @ops: PM operations to choose from.
217 * @state: PM transition of the system being carried out.
219 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
221 switch (state
.event
) {
222 #ifdef CONFIG_SUSPEND
223 case PM_EVENT_SUSPEND
:
225 case PM_EVENT_RESUME
:
227 #endif /* CONFIG_SUSPEND */
228 #ifdef CONFIG_HIBERNATE_CALLBACKS
229 case PM_EVENT_FREEZE
:
230 case PM_EVENT_QUIESCE
:
232 case PM_EVENT_HIBERNATE
:
233 return ops
->poweroff
;
235 case PM_EVENT_RECOVER
:
238 case PM_EVENT_RESTORE
:
240 #endif /* CONFIG_HIBERNATE_CALLBACKS */
247 * pm_late_early_op - Return the PM operation appropriate for given PM event.
248 * @ops: PM operations to choose from.
249 * @state: PM transition of the system being carried out.
251 * Runtime PM is disabled for @dev while this function is being executed.
253 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
256 switch (state
.event
) {
257 #ifdef CONFIG_SUSPEND
258 case PM_EVENT_SUSPEND
:
259 return ops
->suspend_late
;
260 case PM_EVENT_RESUME
:
261 return ops
->resume_early
;
262 #endif /* CONFIG_SUSPEND */
263 #ifdef CONFIG_HIBERNATE_CALLBACKS
264 case PM_EVENT_FREEZE
:
265 case PM_EVENT_QUIESCE
:
266 return ops
->freeze_late
;
267 case PM_EVENT_HIBERNATE
:
268 return ops
->poweroff_late
;
270 case PM_EVENT_RECOVER
:
271 return ops
->thaw_early
;
272 case PM_EVENT_RESTORE
:
273 return ops
->restore_early
;
274 #endif /* CONFIG_HIBERNATE_CALLBACKS */
281 * pm_noirq_op - Return the PM operation appropriate for given PM event.
282 * @ops: PM operations to choose from.
283 * @state: PM transition of the system being carried out.
285 * The driver of @dev will not receive interrupts while this function is being
288 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
290 switch (state
.event
) {
291 #ifdef CONFIG_SUSPEND
292 case PM_EVENT_SUSPEND
:
293 return ops
->suspend_noirq
;
294 case PM_EVENT_RESUME
:
295 return ops
->resume_noirq
;
296 #endif /* CONFIG_SUSPEND */
297 #ifdef CONFIG_HIBERNATE_CALLBACKS
298 case PM_EVENT_FREEZE
:
299 case PM_EVENT_QUIESCE
:
300 return ops
->freeze_noirq
;
301 case PM_EVENT_HIBERNATE
:
302 return ops
->poweroff_noirq
;
304 case PM_EVENT_RECOVER
:
305 return ops
->thaw_noirq
;
306 case PM_EVENT_RESTORE
:
307 return ops
->restore_noirq
;
308 #endif /* CONFIG_HIBERNATE_CALLBACKS */
314 static char *pm_verb(int event
)
317 case PM_EVENT_SUSPEND
:
319 case PM_EVENT_RESUME
:
321 case PM_EVENT_FREEZE
:
323 case PM_EVENT_QUIESCE
:
325 case PM_EVENT_HIBERNATE
:
329 case PM_EVENT_RESTORE
:
331 case PM_EVENT_RECOVER
:
334 return "(unknown PM event)";
338 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
340 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
341 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
342 ", may wakeup" : "");
345 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
348 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
349 dev_name(dev
), pm_verb(state
.event
), info
, error
);
352 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
358 calltime
= ktime_get();
359 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
360 do_div(usecs64
, NSEC_PER_USEC
);
364 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
365 info
?: "", info
? " " : "", pm_verb(state
.event
),
366 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
369 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
370 pm_message_t state
, char *info
)
378 calltime
= initcall_debug_start(dev
);
380 pm_dev_dbg(dev
, state
, info
);
382 suspend_report_result(cb
, error
);
384 initcall_debug_report(dev
, calltime
, error
);
389 /*------------------------- Resume routines -------------------------*/
392 * device_resume_noirq - Execute an "early resume" callback for given device.
393 * @dev: Device to handle.
394 * @state: PM transition of the system being carried out.
396 * The driver of @dev will not receive interrupts while this function is being
399 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
401 pm_callback_t callback
= NULL
;
408 if (dev
->power
.syscore
)
411 if (dev
->pm_domain
) {
412 info
= "noirq power domain ";
413 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
414 } else if (dev
->type
&& dev
->type
->pm
) {
415 info
= "noirq type ";
416 callback
= pm_noirq_op(dev
->type
->pm
, state
);
417 } else if (dev
->class && dev
->class->pm
) {
418 info
= "noirq class ";
419 callback
= pm_noirq_op(dev
->class->pm
, state
);
420 } else if (dev
->bus
&& dev
->bus
->pm
) {
422 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
425 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
426 info
= "noirq driver ";
427 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
430 error
= dpm_run_callback(callback
, dev
, state
, info
);
438 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
439 * @state: PM transition of the system being carried out.
441 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
442 * enable device drivers to receive interrupts.
444 static void dpm_resume_noirq(pm_message_t state
)
446 ktime_t starttime
= ktime_get();
448 mutex_lock(&dpm_list_mtx
);
449 while (!list_empty(&dpm_noirq_list
)) {
450 struct device
*dev
= to_device(dpm_noirq_list
.next
);
454 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
455 mutex_unlock(&dpm_list_mtx
);
457 error
= device_resume_noirq(dev
, state
);
459 suspend_stats
.failed_resume_noirq
++;
460 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
461 dpm_save_failed_dev(dev_name(dev
));
462 pm_dev_err(dev
, state
, " noirq", error
);
465 mutex_lock(&dpm_list_mtx
);
468 mutex_unlock(&dpm_list_mtx
);
469 dpm_show_time(starttime
, state
, "noirq");
470 resume_device_irqs();
475 * device_resume_early - Execute an "early resume" callback for given device.
476 * @dev: Device to handle.
477 * @state: PM transition of the system being carried out.
479 * Runtime PM is disabled for @dev while this function is being executed.
481 static int device_resume_early(struct device
*dev
, pm_message_t state
)
483 pm_callback_t callback
= NULL
;
490 if (dev
->power
.syscore
)
493 if (dev
->pm_domain
) {
494 info
= "early power domain ";
495 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
496 } else if (dev
->type
&& dev
->type
->pm
) {
497 info
= "early type ";
498 callback
= pm_late_early_op(dev
->type
->pm
, state
);
499 } else if (dev
->class && dev
->class->pm
) {
500 info
= "early class ";
501 callback
= pm_late_early_op(dev
->class->pm
, state
);
502 } else if (dev
->bus
&& dev
->bus
->pm
) {
504 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
507 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
508 info
= "early driver ";
509 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
512 error
= dpm_run_callback(callback
, dev
, state
, info
);
520 * dpm_resume_early - Execute "early resume" callbacks for all devices.
521 * @state: PM transition of the system being carried out.
523 static void dpm_resume_early(pm_message_t state
)
525 ktime_t starttime
= ktime_get();
527 mutex_lock(&dpm_list_mtx
);
528 while (!list_empty(&dpm_late_early_list
)) {
529 struct device
*dev
= to_device(dpm_late_early_list
.next
);
533 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
534 mutex_unlock(&dpm_list_mtx
);
536 error
= device_resume_early(dev
, state
);
538 suspend_stats
.failed_resume_early
++;
539 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
540 dpm_save_failed_dev(dev_name(dev
));
541 pm_dev_err(dev
, state
, " early", error
);
544 mutex_lock(&dpm_list_mtx
);
547 mutex_unlock(&dpm_list_mtx
);
548 dpm_show_time(starttime
, state
, "early");
552 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
553 * @state: PM transition of the system being carried out.
555 void dpm_resume_start(pm_message_t state
)
557 dpm_resume_noirq(state
);
558 dpm_resume_early(state
);
560 EXPORT_SYMBOL_GPL(dpm_resume_start
);
563 * device_resume - Execute "resume" callbacks for given device.
564 * @dev: Device to handle.
565 * @state: PM transition of the system being carried out.
566 * @async: If true, the device is being resumed asynchronously.
568 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
570 pm_callback_t callback
= NULL
;
577 if (dev
->power
.syscore
)
580 dpm_wait(dev
->parent
, async
);
584 * This is a fib. But we'll allow new children to be added below
585 * a resumed device, even if the device hasn't been completed yet.
587 dev
->power
.is_prepared
= false;
589 if (!dev
->power
.is_suspended
)
592 pm_runtime_enable(dev
);
594 if (dev
->pm_domain
) {
595 info
= "power domain ";
596 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
600 if (dev
->type
&& dev
->type
->pm
) {
602 callback
= pm_op(dev
->type
->pm
, state
);
607 if (dev
->class->pm
) {
609 callback
= pm_op(dev
->class->pm
, state
);
611 } else if (dev
->class->resume
) {
612 info
= "legacy class ";
613 callback
= dev
->class->resume
;
621 callback
= pm_op(dev
->bus
->pm
, state
);
622 } else if (dev
->bus
->resume
) {
623 info
= "legacy bus ";
624 callback
= dev
->bus
->resume
;
630 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
632 callback
= pm_op(dev
->driver
->pm
, state
);
636 error
= dpm_run_callback(callback
, dev
, state
, info
);
637 dev
->power
.is_suspended
= false;
643 complete_all(&dev
->power
.completion
);
650 static void async_resume(void *data
, async_cookie_t cookie
)
652 struct device
*dev
= (struct device
*)data
;
655 error
= device_resume(dev
, pm_transition
, true);
657 pm_dev_err(dev
, pm_transition
, " async", error
);
661 static bool is_async(struct device
*dev
)
663 return dev
->power
.async_suspend
&& pm_async_enabled
664 && !pm_trace_is_enabled();
668 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
669 * @state: PM transition of the system being carried out.
671 * Execute the appropriate "resume" callback for all devices whose status
672 * indicates that they are suspended.
674 void dpm_resume(pm_message_t state
)
677 ktime_t starttime
= ktime_get();
681 mutex_lock(&dpm_list_mtx
);
682 pm_transition
= state
;
685 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
686 INIT_COMPLETION(dev
->power
.completion
);
689 async_schedule(async_resume
, dev
);
693 while (!list_empty(&dpm_suspended_list
)) {
694 dev
= to_device(dpm_suspended_list
.next
);
696 if (!is_async(dev
)) {
699 mutex_unlock(&dpm_list_mtx
);
701 error
= device_resume(dev
, state
, false);
703 suspend_stats
.failed_resume
++;
704 dpm_save_failed_step(SUSPEND_RESUME
);
705 dpm_save_failed_dev(dev_name(dev
));
706 pm_dev_err(dev
, state
, "", error
);
709 mutex_lock(&dpm_list_mtx
);
711 if (!list_empty(&dev
->power
.entry
))
712 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
715 mutex_unlock(&dpm_list_mtx
);
716 async_synchronize_full();
717 dpm_show_time(starttime
, state
, NULL
);
721 * device_complete - Complete a PM transition for given device.
722 * @dev: Device to handle.
723 * @state: PM transition of the system being carried out.
725 static void device_complete(struct device
*dev
, pm_message_t state
)
727 void (*callback
)(struct device
*) = NULL
;
730 if (dev
->power
.syscore
)
735 if (dev
->pm_domain
) {
736 info
= "completing power domain ";
737 callback
= dev
->pm_domain
->ops
.complete
;
738 } else if (dev
->type
&& dev
->type
->pm
) {
739 info
= "completing type ";
740 callback
= dev
->type
->pm
->complete
;
741 } else if (dev
->class && dev
->class->pm
) {
742 info
= "completing class ";
743 callback
= dev
->class->pm
->complete
;
744 } else if (dev
->bus
&& dev
->bus
->pm
) {
745 info
= "completing bus ";
746 callback
= dev
->bus
->pm
->complete
;
749 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
750 info
= "completing driver ";
751 callback
= dev
->driver
->pm
->complete
;
755 pm_dev_dbg(dev
, state
, info
);
761 pm_runtime_put_sync(dev
);
765 * dpm_complete - Complete a PM transition for all non-sysdev devices.
766 * @state: PM transition of the system being carried out.
768 * Execute the ->complete() callbacks for all devices whose PM status is not
769 * DPM_ON (this allows new devices to be registered).
771 void dpm_complete(pm_message_t state
)
773 struct list_head list
;
777 INIT_LIST_HEAD(&list
);
778 mutex_lock(&dpm_list_mtx
);
779 while (!list_empty(&dpm_prepared_list
)) {
780 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
783 dev
->power
.is_prepared
= false;
784 list_move(&dev
->power
.entry
, &list
);
785 mutex_unlock(&dpm_list_mtx
);
787 device_complete(dev
, state
);
789 mutex_lock(&dpm_list_mtx
);
792 list_splice(&list
, &dpm_list
);
793 mutex_unlock(&dpm_list_mtx
);
797 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
798 * @state: PM transition of the system being carried out.
800 * Execute "resume" callbacks for all devices and complete the PM transition of
803 void dpm_resume_end(pm_message_t state
)
808 EXPORT_SYMBOL_GPL(dpm_resume_end
);
811 /*------------------------- Suspend routines -------------------------*/
814 * resume_event - Return a "resume" message for given "suspend" sleep state.
815 * @sleep_state: PM message representing a sleep state.
817 * Return a PM message representing the resume event corresponding to given
820 static pm_message_t
resume_event(pm_message_t sleep_state
)
822 switch (sleep_state
.event
) {
823 case PM_EVENT_SUSPEND
:
825 case PM_EVENT_FREEZE
:
826 case PM_EVENT_QUIESCE
:
828 case PM_EVENT_HIBERNATE
:
835 * device_suspend_noirq - Execute a "late suspend" callback for given device.
836 * @dev: Device to handle.
837 * @state: PM transition of the system being carried out.
839 * The driver of @dev will not receive interrupts while this function is being
842 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
844 pm_callback_t callback
= NULL
;
847 if (dev
->power
.syscore
)
850 if (dev
->pm_domain
) {
851 info
= "noirq power domain ";
852 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
853 } else if (dev
->type
&& dev
->type
->pm
) {
854 info
= "noirq type ";
855 callback
= pm_noirq_op(dev
->type
->pm
, state
);
856 } else if (dev
->class && dev
->class->pm
) {
857 info
= "noirq class ";
858 callback
= pm_noirq_op(dev
->class->pm
, state
);
859 } else if (dev
->bus
&& dev
->bus
->pm
) {
861 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
864 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
865 info
= "noirq driver ";
866 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
869 return dpm_run_callback(callback
, dev
, state
, info
);
873 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
874 * @state: PM transition of the system being carried out.
876 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
877 * handlers for all non-sysdev devices.
879 static int dpm_suspend_noirq(pm_message_t state
)
881 ktime_t starttime
= ktime_get();
885 suspend_device_irqs();
886 mutex_lock(&dpm_list_mtx
);
887 while (!list_empty(&dpm_late_early_list
)) {
888 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
891 mutex_unlock(&dpm_list_mtx
);
893 error
= device_suspend_noirq(dev
, state
);
895 mutex_lock(&dpm_list_mtx
);
897 pm_dev_err(dev
, state
, " noirq", error
);
898 suspend_stats
.failed_suspend_noirq
++;
899 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
900 dpm_save_failed_dev(dev_name(dev
));
904 if (!list_empty(&dev
->power
.entry
))
905 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
908 if (pm_wakeup_pending()) {
913 mutex_unlock(&dpm_list_mtx
);
915 dpm_resume_noirq(resume_event(state
));
917 dpm_show_time(starttime
, state
, "noirq");
922 * device_suspend_late - Execute a "late suspend" callback for given device.
923 * @dev: Device to handle.
924 * @state: PM transition of the system being carried out.
926 * Runtime PM is disabled for @dev while this function is being executed.
928 static int device_suspend_late(struct device
*dev
, pm_message_t state
)
930 pm_callback_t callback
= NULL
;
933 if (dev
->power
.syscore
)
936 if (dev
->pm_domain
) {
937 info
= "late power domain ";
938 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
939 } else if (dev
->type
&& dev
->type
->pm
) {
941 callback
= pm_late_early_op(dev
->type
->pm
, state
);
942 } else if (dev
->class && dev
->class->pm
) {
943 info
= "late class ";
944 callback
= pm_late_early_op(dev
->class->pm
, state
);
945 } else if (dev
->bus
&& dev
->bus
->pm
) {
947 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
950 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
951 info
= "late driver ";
952 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
955 return dpm_run_callback(callback
, dev
, state
, info
);
959 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
960 * @state: PM transition of the system being carried out.
962 static int dpm_suspend_late(pm_message_t state
)
964 ktime_t starttime
= ktime_get();
967 mutex_lock(&dpm_list_mtx
);
968 while (!list_empty(&dpm_suspended_list
)) {
969 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
972 mutex_unlock(&dpm_list_mtx
);
974 error
= device_suspend_late(dev
, state
);
976 mutex_lock(&dpm_list_mtx
);
978 pm_dev_err(dev
, state
, " late", error
);
979 suspend_stats
.failed_suspend_late
++;
980 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
981 dpm_save_failed_dev(dev_name(dev
));
985 if (!list_empty(&dev
->power
.entry
))
986 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
989 if (pm_wakeup_pending()) {
994 mutex_unlock(&dpm_list_mtx
);
996 dpm_resume_early(resume_event(state
));
998 dpm_show_time(starttime
, state
, "late");
1004 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1005 * @state: PM transition of the system being carried out.
1007 int dpm_suspend_end(pm_message_t state
)
1009 int error
= dpm_suspend_late(state
);
1013 error
= dpm_suspend_noirq(state
);
1015 dpm_resume_early(resume_event(state
));
1021 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1024 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1025 * @dev: Device to suspend.
1026 * @state: PM transition of the system being carried out.
1027 * @cb: Suspend callback to execute.
1029 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1030 int (*cb
)(struct device
*dev
, pm_message_t state
))
1035 calltime
= initcall_debug_start(dev
);
1037 error
= cb(dev
, state
);
1038 suspend_report_result(cb
, error
);
1040 initcall_debug_report(dev
, calltime
, error
);
1046 * device_suspend - Execute "suspend" callbacks for given device.
1047 * @dev: Device to handle.
1048 * @state: PM transition of the system being carried out.
1049 * @async: If true, the device is being suspended asynchronously.
1051 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1053 pm_callback_t callback
= NULL
;
1057 dpm_wait_for_children(dev
, async
);
1063 * If a device configured to wake up the system from sleep states
1064 * has been suspended at run time and there's a resume request pending
1065 * for it, this is equivalent to the device signaling wakeup, so the
1066 * system suspend operation should be aborted.
1068 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1069 pm_wakeup_event(dev
, 0);
1071 if (pm_wakeup_pending()) {
1072 async_error
= -EBUSY
;
1076 if (dev
->power
.syscore
)
1081 if (dev
->pm_domain
) {
1082 info
= "power domain ";
1083 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1087 if (dev
->type
&& dev
->type
->pm
) {
1089 callback
= pm_op(dev
->type
->pm
, state
);
1094 if (dev
->class->pm
) {
1096 callback
= pm_op(dev
->class->pm
, state
);
1098 } else if (dev
->class->suspend
) {
1099 pm_dev_dbg(dev
, state
, "legacy class ");
1100 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
1108 callback
= pm_op(dev
->bus
->pm
, state
);
1109 } else if (dev
->bus
->suspend
) {
1110 pm_dev_dbg(dev
, state
, "legacy bus ");
1111 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
1117 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1119 callback
= pm_op(dev
->driver
->pm
, state
);
1122 error
= dpm_run_callback(callback
, dev
, state
, info
);
1126 dev
->power
.is_suspended
= true;
1127 if (dev
->power
.wakeup_path
1128 && dev
->parent
&& !dev
->parent
->power
.ignore_children
)
1129 dev
->parent
->power
.wakeup_path
= true;
1135 complete_all(&dev
->power
.completion
);
1138 async_error
= error
;
1139 else if (dev
->power
.is_suspended
)
1140 __pm_runtime_disable(dev
, false);
1145 static void async_suspend(void *data
, async_cookie_t cookie
)
1147 struct device
*dev
= (struct device
*)data
;
1150 error
= __device_suspend(dev
, pm_transition
, true);
1152 dpm_save_failed_dev(dev_name(dev
));
1153 pm_dev_err(dev
, pm_transition
, " async", error
);
1159 static int device_suspend(struct device
*dev
)
1161 INIT_COMPLETION(dev
->power
.completion
);
1163 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1165 async_schedule(async_suspend
, dev
);
1169 return __device_suspend(dev
, pm_transition
, false);
1173 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1174 * @state: PM transition of the system being carried out.
1176 int dpm_suspend(pm_message_t state
)
1178 ktime_t starttime
= ktime_get();
1183 mutex_lock(&dpm_list_mtx
);
1184 pm_transition
= state
;
1186 while (!list_empty(&dpm_prepared_list
)) {
1187 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1190 mutex_unlock(&dpm_list_mtx
);
1192 error
= device_suspend(dev
);
1194 mutex_lock(&dpm_list_mtx
);
1196 pm_dev_err(dev
, state
, "", error
);
1197 dpm_save_failed_dev(dev_name(dev
));
1201 if (!list_empty(&dev
->power
.entry
))
1202 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1207 mutex_unlock(&dpm_list_mtx
);
1208 async_synchronize_full();
1210 error
= async_error
;
1212 suspend_stats
.failed_suspend
++;
1213 dpm_save_failed_step(SUSPEND_SUSPEND
);
1215 dpm_show_time(starttime
, state
, NULL
);
1220 * device_prepare - Prepare a device for system power transition.
1221 * @dev: Device to handle.
1222 * @state: PM transition of the system being carried out.
1224 * Execute the ->prepare() callback(s) for given device. No new children of the
1225 * device may be registered after this function has returned.
1227 static int device_prepare(struct device
*dev
, pm_message_t state
)
1229 int (*callback
)(struct device
*) = NULL
;
1233 if (dev
->power
.syscore
)
1237 * If a device's parent goes into runtime suspend at the wrong time,
1238 * it won't be possible to resume the device. To prevent this we
1239 * block runtime suspend here, during the prepare phase, and allow
1240 * it again during the complete phase.
1242 pm_runtime_get_noresume(dev
);
1246 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1248 if (dev
->pm_domain
) {
1249 info
= "preparing power domain ";
1250 callback
= dev
->pm_domain
->ops
.prepare
;
1251 } else if (dev
->type
&& dev
->type
->pm
) {
1252 info
= "preparing type ";
1253 callback
= dev
->type
->pm
->prepare
;
1254 } else if (dev
->class && dev
->class->pm
) {
1255 info
= "preparing class ";
1256 callback
= dev
->class->pm
->prepare
;
1257 } else if (dev
->bus
&& dev
->bus
->pm
) {
1258 info
= "preparing bus ";
1259 callback
= dev
->bus
->pm
->prepare
;
1262 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1263 info
= "preparing driver ";
1264 callback
= dev
->driver
->pm
->prepare
;
1268 error
= callback(dev
);
1269 suspend_report_result(callback
, error
);
1278 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1279 * @state: PM transition of the system being carried out.
1281 * Execute the ->prepare() callback(s) for all devices.
1283 int dpm_prepare(pm_message_t state
)
1289 mutex_lock(&dpm_list_mtx
);
1290 while (!list_empty(&dpm_list
)) {
1291 struct device
*dev
= to_device(dpm_list
.next
);
1294 mutex_unlock(&dpm_list_mtx
);
1296 error
= device_prepare(dev
, state
);
1298 mutex_lock(&dpm_list_mtx
);
1300 if (error
== -EAGAIN
) {
1305 printk(KERN_INFO
"PM: Device %s not prepared "
1306 "for power transition: code %d\n",
1307 dev_name(dev
), error
);
1311 dev
->power
.is_prepared
= true;
1312 if (!list_empty(&dev
->power
.entry
))
1313 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1316 mutex_unlock(&dpm_list_mtx
);
1321 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1322 * @state: PM transition of the system being carried out.
1324 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1325 * callbacks for them.
1327 int dpm_suspend_start(pm_message_t state
)
1331 error
= dpm_prepare(state
);
1333 suspend_stats
.failed_prepare
++;
1334 dpm_save_failed_step(SUSPEND_PREPARE
);
1336 error
= dpm_suspend(state
);
1339 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1341 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1344 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1346 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1349 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1350 * @dev: Device to wait for.
1351 * @subordinate: Device that needs to wait for @dev.
1353 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1355 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1358 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1361 * dpm_for_each_dev - device iterator.
1362 * @data: data for the callback.
1363 * @fn: function to be called for each device.
1365 * Iterate over devices in dpm_list, and call @fn for each device,
1368 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1376 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1380 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);