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_init - Initialize the PM-related part of a device object.
61 * @dev: Device object being initialized.
63 void device_pm_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 spin_lock_init(&dev
->power
.lock
);
72 INIT_LIST_HEAD(&dev
->power
.entry
);
73 dev
->power
.power_state
= PMSG_INVALID
;
77 * device_pm_lock - Lock the list of active devices used by the PM core.
79 void device_pm_lock(void)
81 mutex_lock(&dpm_list_mtx
);
85 * device_pm_unlock - Unlock the list of active devices used by the PM core.
87 void device_pm_unlock(void)
89 mutex_unlock(&dpm_list_mtx
);
93 * device_pm_add - Add a device to the PM core's list of active devices.
94 * @dev: Device to add to the list.
96 void device_pm_add(struct device
*dev
)
98 pr_debug("PM: Adding info for %s:%s\n",
99 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
100 mutex_lock(&dpm_list_mtx
);
101 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
102 dev_warn(dev
, "parent %s should not be sleeping\n",
103 dev_name(dev
->parent
));
104 list_add_tail(&dev
->power
.entry
, &dpm_list
);
105 dev_pm_qos_constraints_init(dev
);
106 mutex_unlock(&dpm_list_mtx
);
110 * device_pm_remove - Remove a device from the PM core's list of active devices.
111 * @dev: Device to be removed from the list.
113 void device_pm_remove(struct device
*dev
)
115 pr_debug("PM: Removing info for %s:%s\n",
116 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
117 complete_all(&dev
->power
.completion
);
118 mutex_lock(&dpm_list_mtx
);
119 dev_pm_qos_constraints_destroy(dev
);
120 list_del_init(&dev
->power
.entry
);
121 mutex_unlock(&dpm_list_mtx
);
122 device_wakeup_disable(dev
);
123 pm_runtime_remove(dev
);
127 * device_pm_move_before - Move device in the PM core's list of active devices.
128 * @deva: Device to move in dpm_list.
129 * @devb: Device @deva should come before.
131 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
133 pr_debug("PM: Moving %s:%s before %s:%s\n",
134 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
135 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
136 /* Delete deva from dpm_list and reinsert before devb. */
137 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
141 * device_pm_move_after - Move device in the PM core's list of active devices.
142 * @deva: Device to move in dpm_list.
143 * @devb: Device @deva should come after.
145 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
147 pr_debug("PM: Moving %s:%s after %s:%s\n",
148 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
149 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
150 /* Delete deva from dpm_list and reinsert after devb. */
151 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
155 * device_pm_move_last - Move device to end of the PM core's list of devices.
156 * @dev: Device to move in dpm_list.
158 void device_pm_move_last(struct device
*dev
)
160 pr_debug("PM: Moving %s:%s to end of list\n",
161 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
162 list_move_tail(&dev
->power
.entry
, &dpm_list
);
165 static ktime_t
initcall_debug_start(struct device
*dev
)
167 ktime_t calltime
= ktime_set(0, 0);
169 if (pm_print_times_enabled
) {
170 pr_info("calling %s+ @ %i, parent: %s\n",
171 dev_name(dev
), task_pid_nr(current
),
172 dev
->parent
? dev_name(dev
->parent
) : "none");
173 calltime
= ktime_get();
179 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
182 ktime_t delta
, rettime
;
184 if (pm_print_times_enabled
) {
185 rettime
= ktime_get();
186 delta
= ktime_sub(rettime
, calltime
);
187 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
188 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
193 * dpm_wait - Wait for a PM operation to complete.
194 * @dev: Device to wait for.
195 * @async: If unset, wait only if the device's power.async_suspend flag is set.
197 static void dpm_wait(struct device
*dev
, bool async
)
202 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
203 wait_for_completion(&dev
->power
.completion
);
206 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
208 dpm_wait(dev
, *((bool *)async_ptr
));
212 static void dpm_wait_for_children(struct device
*dev
, bool async
)
214 device_for_each_child(dev
, &async
, dpm_wait_fn
);
218 * pm_op - Return the PM operation appropriate for given PM event.
219 * @ops: PM operations to choose from.
220 * @state: PM transition of the system being carried out.
222 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
224 switch (state
.event
) {
225 #ifdef CONFIG_SUSPEND
226 case PM_EVENT_SUSPEND
:
228 case PM_EVENT_RESUME
:
230 #endif /* CONFIG_SUSPEND */
231 #ifdef CONFIG_HIBERNATE_CALLBACKS
232 case PM_EVENT_FREEZE
:
233 case PM_EVENT_QUIESCE
:
235 case PM_EVENT_HIBERNATE
:
236 return ops
->poweroff
;
238 case PM_EVENT_RECOVER
:
241 case PM_EVENT_RESTORE
:
243 #endif /* CONFIG_HIBERNATE_CALLBACKS */
250 * pm_late_early_op - Return the PM operation appropriate for given PM event.
251 * @ops: PM operations to choose from.
252 * @state: PM transition of the system being carried out.
254 * Runtime PM is disabled for @dev while this function is being executed.
256 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
259 switch (state
.event
) {
260 #ifdef CONFIG_SUSPEND
261 case PM_EVENT_SUSPEND
:
262 return ops
->suspend_late
;
263 case PM_EVENT_RESUME
:
264 return ops
->resume_early
;
265 #endif /* CONFIG_SUSPEND */
266 #ifdef CONFIG_HIBERNATE_CALLBACKS
267 case PM_EVENT_FREEZE
:
268 case PM_EVENT_QUIESCE
:
269 return ops
->freeze_late
;
270 case PM_EVENT_HIBERNATE
:
271 return ops
->poweroff_late
;
273 case PM_EVENT_RECOVER
:
274 return ops
->thaw_early
;
275 case PM_EVENT_RESTORE
:
276 return ops
->restore_early
;
277 #endif /* CONFIG_HIBERNATE_CALLBACKS */
284 * pm_noirq_op - Return the PM operation appropriate for given PM event.
285 * @ops: PM operations to choose from.
286 * @state: PM transition of the system being carried out.
288 * The driver of @dev will not receive interrupts while this function is being
291 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
293 switch (state
.event
) {
294 #ifdef CONFIG_SUSPEND
295 case PM_EVENT_SUSPEND
:
296 return ops
->suspend_noirq
;
297 case PM_EVENT_RESUME
:
298 return ops
->resume_noirq
;
299 #endif /* CONFIG_SUSPEND */
300 #ifdef CONFIG_HIBERNATE_CALLBACKS
301 case PM_EVENT_FREEZE
:
302 case PM_EVENT_QUIESCE
:
303 return ops
->freeze_noirq
;
304 case PM_EVENT_HIBERNATE
:
305 return ops
->poweroff_noirq
;
307 case PM_EVENT_RECOVER
:
308 return ops
->thaw_noirq
;
309 case PM_EVENT_RESTORE
:
310 return ops
->restore_noirq
;
311 #endif /* CONFIG_HIBERNATE_CALLBACKS */
317 static char *pm_verb(int event
)
320 case PM_EVENT_SUSPEND
:
322 case PM_EVENT_RESUME
:
324 case PM_EVENT_FREEZE
:
326 case PM_EVENT_QUIESCE
:
328 case PM_EVENT_HIBERNATE
:
332 case PM_EVENT_RESTORE
:
334 case PM_EVENT_RECOVER
:
337 return "(unknown PM event)";
341 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
343 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
344 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
345 ", may wakeup" : "");
348 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
351 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
352 dev_name(dev
), pm_verb(state
.event
), info
, error
);
355 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
361 calltime
= ktime_get();
362 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
363 do_div(usecs64
, NSEC_PER_USEC
);
367 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
368 info
?: "", info
? " " : "", pm_verb(state
.event
),
369 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
372 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
373 pm_message_t state
, char *info
)
381 calltime
= initcall_debug_start(dev
);
383 pm_dev_dbg(dev
, state
, info
);
385 suspend_report_result(cb
, error
);
387 initcall_debug_report(dev
, calltime
, error
);
392 /*------------------------- Resume routines -------------------------*/
395 * device_resume_noirq - Execute an "early resume" callback for given device.
396 * @dev: Device to handle.
397 * @state: PM transition of the system being carried out.
399 * The driver of @dev will not receive interrupts while this function is being
402 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
404 pm_callback_t callback
= NULL
;
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
);
437 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
438 * @state: PM transition of the system being carried out.
440 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
441 * enable device drivers to receive interrupts.
443 static void dpm_resume_noirq(pm_message_t state
)
445 ktime_t starttime
= ktime_get();
447 mutex_lock(&dpm_list_mtx
);
448 while (!list_empty(&dpm_noirq_list
)) {
449 struct device
*dev
= to_device(dpm_noirq_list
.next
);
453 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
454 mutex_unlock(&dpm_list_mtx
);
456 error
= device_resume_noirq(dev
, state
);
458 suspend_stats
.failed_resume_noirq
++;
459 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
460 dpm_save_failed_dev(dev_name(dev
));
461 pm_dev_err(dev
, state
, " noirq", error
);
464 mutex_lock(&dpm_list_mtx
);
467 mutex_unlock(&dpm_list_mtx
);
468 dpm_show_time(starttime
, state
, "noirq");
469 resume_device_irqs();
474 * device_resume_early - Execute an "early resume" callback for given device.
475 * @dev: Device to handle.
476 * @state: PM transition of the system being carried out.
478 * Runtime PM is disabled for @dev while this function is being executed.
480 static int device_resume_early(struct device
*dev
, pm_message_t state
)
482 pm_callback_t callback
= NULL
;
489 if (dev
->pm_domain
) {
490 info
= "early power domain ";
491 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
492 } else if (dev
->type
&& dev
->type
->pm
) {
493 info
= "early type ";
494 callback
= pm_late_early_op(dev
->type
->pm
, state
);
495 } else if (dev
->class && dev
->class->pm
) {
496 info
= "early class ";
497 callback
= pm_late_early_op(dev
->class->pm
, state
);
498 } else if (dev
->bus
&& dev
->bus
->pm
) {
500 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
503 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
504 info
= "early driver ";
505 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
508 error
= dpm_run_callback(callback
, dev
, state
, info
);
515 * dpm_resume_early - Execute "early resume" callbacks for all devices.
516 * @state: PM transition of the system being carried out.
518 static void dpm_resume_early(pm_message_t state
)
520 ktime_t starttime
= ktime_get();
522 mutex_lock(&dpm_list_mtx
);
523 while (!list_empty(&dpm_late_early_list
)) {
524 struct device
*dev
= to_device(dpm_late_early_list
.next
);
528 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
529 mutex_unlock(&dpm_list_mtx
);
531 error
= device_resume_early(dev
, state
);
533 suspend_stats
.failed_resume_early
++;
534 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
535 dpm_save_failed_dev(dev_name(dev
));
536 pm_dev_err(dev
, state
, " early", error
);
539 mutex_lock(&dpm_list_mtx
);
542 mutex_unlock(&dpm_list_mtx
);
543 dpm_show_time(starttime
, state
, "early");
547 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
548 * @state: PM transition of the system being carried out.
550 void dpm_resume_start(pm_message_t state
)
552 dpm_resume_noirq(state
);
553 dpm_resume_early(state
);
555 EXPORT_SYMBOL_GPL(dpm_resume_start
);
558 * device_resume - Execute "resume" callbacks for given device.
559 * @dev: Device to handle.
560 * @state: PM transition of the system being carried out.
561 * @async: If true, the device is being resumed asynchronously.
563 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
565 pm_callback_t callback
= NULL
;
573 dpm_wait(dev
->parent
, async
);
577 * This is a fib. But we'll allow new children to be added below
578 * a resumed device, even if the device hasn't been completed yet.
580 dev
->power
.is_prepared
= false;
582 if (!dev
->power
.is_suspended
)
585 pm_runtime_enable(dev
);
588 if (dev
->pm_domain
) {
589 info
= "power domain ";
590 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
594 if (dev
->type
&& dev
->type
->pm
) {
596 callback
= pm_op(dev
->type
->pm
, state
);
601 if (dev
->class->pm
) {
603 callback
= pm_op(dev
->class->pm
, state
);
605 } else if (dev
->class->resume
) {
606 info
= "legacy class ";
607 callback
= dev
->class->resume
;
615 callback
= pm_op(dev
->bus
->pm
, state
);
616 } else if (dev
->bus
->resume
) {
617 info
= "legacy bus ";
618 callback
= dev
->bus
->resume
;
624 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
626 callback
= pm_op(dev
->driver
->pm
, state
);
630 error
= dpm_run_callback(callback
, dev
, state
, info
);
631 dev
->power
.is_suspended
= false;
635 complete_all(&dev
->power
.completion
);
640 pm_runtime_put_sync(dev
);
645 static void async_resume(void *data
, async_cookie_t cookie
)
647 struct device
*dev
= (struct device
*)data
;
650 error
= device_resume(dev
, pm_transition
, true);
652 pm_dev_err(dev
, pm_transition
, " async", error
);
656 static bool is_async(struct device
*dev
)
658 return dev
->power
.async_suspend
&& pm_async_enabled
659 && !pm_trace_is_enabled();
663 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
664 * @state: PM transition of the system being carried out.
666 * Execute the appropriate "resume" callback for all devices whose status
667 * indicates that they are suspended.
669 void dpm_resume(pm_message_t state
)
672 ktime_t starttime
= ktime_get();
676 mutex_lock(&dpm_list_mtx
);
677 pm_transition
= state
;
680 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
681 INIT_COMPLETION(dev
->power
.completion
);
684 async_schedule(async_resume
, dev
);
688 while (!list_empty(&dpm_suspended_list
)) {
689 dev
= to_device(dpm_suspended_list
.next
);
691 if (!is_async(dev
)) {
694 mutex_unlock(&dpm_list_mtx
);
696 error
= device_resume(dev
, state
, false);
698 suspend_stats
.failed_resume
++;
699 dpm_save_failed_step(SUSPEND_RESUME
);
700 dpm_save_failed_dev(dev_name(dev
));
701 pm_dev_err(dev
, state
, "", error
);
704 mutex_lock(&dpm_list_mtx
);
706 if (!list_empty(&dev
->power
.entry
))
707 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
710 mutex_unlock(&dpm_list_mtx
);
711 async_synchronize_full();
712 dpm_show_time(starttime
, state
, NULL
);
716 * device_complete - Complete a PM transition for given device.
717 * @dev: Device to handle.
718 * @state: PM transition of the system being carried out.
720 static void device_complete(struct device
*dev
, pm_message_t state
)
722 void (*callback
)(struct device
*) = NULL
;
727 if (dev
->pm_domain
) {
728 info
= "completing power domain ";
729 callback
= dev
->pm_domain
->ops
.complete
;
730 } else if (dev
->type
&& dev
->type
->pm
) {
731 info
= "completing type ";
732 callback
= dev
->type
->pm
->complete
;
733 } else if (dev
->class && dev
->class->pm
) {
734 info
= "completing class ";
735 callback
= dev
->class->pm
->complete
;
736 } else if (dev
->bus
&& dev
->bus
->pm
) {
737 info
= "completing bus ";
738 callback
= dev
->bus
->pm
->complete
;
741 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
742 info
= "completing driver ";
743 callback
= dev
->driver
->pm
->complete
;
747 pm_dev_dbg(dev
, state
, info
);
755 * dpm_complete - Complete a PM transition for all non-sysdev devices.
756 * @state: PM transition of the system being carried out.
758 * Execute the ->complete() callbacks for all devices whose PM status is not
759 * DPM_ON (this allows new devices to be registered).
761 void dpm_complete(pm_message_t state
)
763 struct list_head list
;
767 INIT_LIST_HEAD(&list
);
768 mutex_lock(&dpm_list_mtx
);
769 while (!list_empty(&dpm_prepared_list
)) {
770 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
773 dev
->power
.is_prepared
= false;
774 list_move(&dev
->power
.entry
, &list
);
775 mutex_unlock(&dpm_list_mtx
);
777 device_complete(dev
, state
);
779 mutex_lock(&dpm_list_mtx
);
782 list_splice(&list
, &dpm_list
);
783 mutex_unlock(&dpm_list_mtx
);
787 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
788 * @state: PM transition of the system being carried out.
790 * Execute "resume" callbacks for all devices and complete the PM transition of
793 void dpm_resume_end(pm_message_t state
)
798 EXPORT_SYMBOL_GPL(dpm_resume_end
);
801 /*------------------------- Suspend routines -------------------------*/
804 * resume_event - Return a "resume" message for given "suspend" sleep state.
805 * @sleep_state: PM message representing a sleep state.
807 * Return a PM message representing the resume event corresponding to given
810 static pm_message_t
resume_event(pm_message_t sleep_state
)
812 switch (sleep_state
.event
) {
813 case PM_EVENT_SUSPEND
:
815 case PM_EVENT_FREEZE
:
816 case PM_EVENT_QUIESCE
:
818 case PM_EVENT_HIBERNATE
:
825 * device_suspend_noirq - Execute a "late suspend" callback for given device.
826 * @dev: Device to handle.
827 * @state: PM transition of the system being carried out.
829 * The driver of @dev will not receive interrupts while this function is being
832 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
834 pm_callback_t callback
= NULL
;
837 if (dev
->pm_domain
) {
838 info
= "noirq power domain ";
839 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
840 } else if (dev
->type
&& dev
->type
->pm
) {
841 info
= "noirq type ";
842 callback
= pm_noirq_op(dev
->type
->pm
, state
);
843 } else if (dev
->class && dev
->class->pm
) {
844 info
= "noirq class ";
845 callback
= pm_noirq_op(dev
->class->pm
, state
);
846 } else if (dev
->bus
&& dev
->bus
->pm
) {
848 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
851 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
852 info
= "noirq driver ";
853 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
856 return dpm_run_callback(callback
, dev
, state
, info
);
860 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
861 * @state: PM transition of the system being carried out.
863 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
864 * handlers for all non-sysdev devices.
866 static int dpm_suspend_noirq(pm_message_t state
)
868 ktime_t starttime
= ktime_get();
872 suspend_device_irqs();
873 mutex_lock(&dpm_list_mtx
);
874 while (!list_empty(&dpm_late_early_list
)) {
875 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
878 mutex_unlock(&dpm_list_mtx
);
880 error
= device_suspend_noirq(dev
, state
);
882 mutex_lock(&dpm_list_mtx
);
884 pm_dev_err(dev
, state
, " noirq", error
);
885 suspend_stats
.failed_suspend_noirq
++;
886 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
887 dpm_save_failed_dev(dev_name(dev
));
891 if (!list_empty(&dev
->power
.entry
))
892 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
895 if (pm_wakeup_pending()) {
900 mutex_unlock(&dpm_list_mtx
);
902 dpm_resume_noirq(resume_event(state
));
904 dpm_show_time(starttime
, state
, "noirq");
909 * device_suspend_late - Execute a "late suspend" callback for given device.
910 * @dev: Device to handle.
911 * @state: PM transition of the system being carried out.
913 * Runtime PM is disabled for @dev while this function is being executed.
915 static int device_suspend_late(struct device
*dev
, pm_message_t state
)
917 pm_callback_t callback
= NULL
;
920 if (dev
->pm_domain
) {
921 info
= "late power domain ";
922 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
923 } else if (dev
->type
&& dev
->type
->pm
) {
925 callback
= pm_late_early_op(dev
->type
->pm
, state
);
926 } else if (dev
->class && dev
->class->pm
) {
927 info
= "late class ";
928 callback
= pm_late_early_op(dev
->class->pm
, state
);
929 } else if (dev
->bus
&& dev
->bus
->pm
) {
931 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
934 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
935 info
= "late driver ";
936 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
939 return dpm_run_callback(callback
, dev
, state
, info
);
943 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
944 * @state: PM transition of the system being carried out.
946 static int dpm_suspend_late(pm_message_t state
)
948 ktime_t starttime
= ktime_get();
951 mutex_lock(&dpm_list_mtx
);
952 while (!list_empty(&dpm_suspended_list
)) {
953 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
956 mutex_unlock(&dpm_list_mtx
);
958 error
= device_suspend_late(dev
, state
);
960 mutex_lock(&dpm_list_mtx
);
962 pm_dev_err(dev
, state
, " late", error
);
963 suspend_stats
.failed_suspend_late
++;
964 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
965 dpm_save_failed_dev(dev_name(dev
));
969 if (!list_empty(&dev
->power
.entry
))
970 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
973 if (pm_wakeup_pending()) {
978 mutex_unlock(&dpm_list_mtx
);
980 dpm_resume_early(resume_event(state
));
982 dpm_show_time(starttime
, state
, "late");
988 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
989 * @state: PM transition of the system being carried out.
991 int dpm_suspend_end(pm_message_t state
)
993 int error
= dpm_suspend_late(state
);
997 error
= dpm_suspend_noirq(state
);
999 dpm_resume_early(resume_event(state
));
1005 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1008 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1009 * @dev: Device to suspend.
1010 * @state: PM transition of the system being carried out.
1011 * @cb: Suspend callback to execute.
1013 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1014 int (*cb
)(struct device
*dev
, pm_message_t state
))
1019 calltime
= initcall_debug_start(dev
);
1021 error
= cb(dev
, state
);
1022 suspend_report_result(cb
, error
);
1024 initcall_debug_report(dev
, calltime
, error
);
1030 * device_suspend - Execute "suspend" callbacks for given device.
1031 * @dev: Device to handle.
1032 * @state: PM transition of the system being carried out.
1033 * @async: If true, the device is being suspended asynchronously.
1035 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1037 pm_callback_t callback
= NULL
;
1041 dpm_wait_for_children(dev
, async
);
1046 pm_runtime_get_noresume(dev
);
1047 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1048 pm_wakeup_event(dev
, 0);
1050 if (pm_wakeup_pending()) {
1051 pm_runtime_put_sync(dev
);
1052 async_error
= -EBUSY
;
1058 if (dev
->pm_domain
) {
1059 info
= "power domain ";
1060 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1064 if (dev
->type
&& dev
->type
->pm
) {
1066 callback
= pm_op(dev
->type
->pm
, state
);
1071 if (dev
->class->pm
) {
1073 callback
= pm_op(dev
->class->pm
, state
);
1075 } else if (dev
->class->suspend
) {
1076 pm_dev_dbg(dev
, state
, "legacy class ");
1077 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
1085 callback
= pm_op(dev
->bus
->pm
, state
);
1086 } else if (dev
->bus
->suspend
) {
1087 pm_dev_dbg(dev
, state
, "legacy bus ");
1088 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
1094 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1096 callback
= pm_op(dev
->driver
->pm
, state
);
1099 error
= dpm_run_callback(callback
, dev
, state
, info
);
1103 dev
->power
.is_suspended
= true;
1104 if (dev
->power
.wakeup_path
1105 && dev
->parent
&& !dev
->parent
->power
.ignore_children
)
1106 dev
->parent
->power
.wakeup_path
= true;
1112 complete_all(&dev
->power
.completion
);
1115 pm_runtime_put_sync(dev
);
1116 async_error
= error
;
1117 } else if (dev
->power
.is_suspended
) {
1118 __pm_runtime_disable(dev
, false);
1124 static void async_suspend(void *data
, async_cookie_t cookie
)
1126 struct device
*dev
= (struct device
*)data
;
1129 error
= __device_suspend(dev
, pm_transition
, true);
1131 dpm_save_failed_dev(dev_name(dev
));
1132 pm_dev_err(dev
, pm_transition
, " async", error
);
1138 static int device_suspend(struct device
*dev
)
1140 INIT_COMPLETION(dev
->power
.completion
);
1142 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1144 async_schedule(async_suspend
, dev
);
1148 return __device_suspend(dev
, pm_transition
, false);
1152 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1153 * @state: PM transition of the system being carried out.
1155 int dpm_suspend(pm_message_t state
)
1157 ktime_t starttime
= ktime_get();
1162 mutex_lock(&dpm_list_mtx
);
1163 pm_transition
= state
;
1165 while (!list_empty(&dpm_prepared_list
)) {
1166 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1169 mutex_unlock(&dpm_list_mtx
);
1171 error
= device_suspend(dev
);
1173 mutex_lock(&dpm_list_mtx
);
1175 pm_dev_err(dev
, state
, "", error
);
1176 dpm_save_failed_dev(dev_name(dev
));
1180 if (!list_empty(&dev
->power
.entry
))
1181 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1186 mutex_unlock(&dpm_list_mtx
);
1187 async_synchronize_full();
1189 error
= async_error
;
1191 suspend_stats
.failed_suspend
++;
1192 dpm_save_failed_step(SUSPEND_SUSPEND
);
1194 dpm_show_time(starttime
, state
, NULL
);
1199 * device_prepare - Prepare a device for system power transition.
1200 * @dev: Device to handle.
1201 * @state: PM transition of the system being carried out.
1203 * Execute the ->prepare() callback(s) for given device. No new children of the
1204 * device may be registered after this function has returned.
1206 static int device_prepare(struct device
*dev
, pm_message_t state
)
1208 int (*callback
)(struct device
*) = NULL
;
1214 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1216 if (dev
->pm_domain
) {
1217 info
= "preparing power domain ";
1218 callback
= dev
->pm_domain
->ops
.prepare
;
1219 } else if (dev
->type
&& dev
->type
->pm
) {
1220 info
= "preparing type ";
1221 callback
= dev
->type
->pm
->prepare
;
1222 } else if (dev
->class && dev
->class->pm
) {
1223 info
= "preparing class ";
1224 callback
= dev
->class->pm
->prepare
;
1225 } else if (dev
->bus
&& dev
->bus
->pm
) {
1226 info
= "preparing bus ";
1227 callback
= dev
->bus
->pm
->prepare
;
1230 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1231 info
= "preparing driver ";
1232 callback
= dev
->driver
->pm
->prepare
;
1236 error
= callback(dev
);
1237 suspend_report_result(callback
, error
);
1246 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1247 * @state: PM transition of the system being carried out.
1249 * Execute the ->prepare() callback(s) for all devices.
1251 int dpm_prepare(pm_message_t state
)
1257 mutex_lock(&dpm_list_mtx
);
1258 while (!list_empty(&dpm_list
)) {
1259 struct device
*dev
= to_device(dpm_list
.next
);
1262 mutex_unlock(&dpm_list_mtx
);
1264 error
= device_prepare(dev
, state
);
1266 mutex_lock(&dpm_list_mtx
);
1268 if (error
== -EAGAIN
) {
1273 printk(KERN_INFO
"PM: Device %s not prepared "
1274 "for power transition: code %d\n",
1275 dev_name(dev
), error
);
1279 dev
->power
.is_prepared
= true;
1280 if (!list_empty(&dev
->power
.entry
))
1281 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1284 mutex_unlock(&dpm_list_mtx
);
1289 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1290 * @state: PM transition of the system being carried out.
1292 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1293 * callbacks for them.
1295 int dpm_suspend_start(pm_message_t state
)
1299 error
= dpm_prepare(state
);
1301 suspend_stats
.failed_prepare
++;
1302 dpm_save_failed_step(SUSPEND_PREPARE
);
1304 error
= dpm_suspend(state
);
1307 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1309 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1312 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1314 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1317 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1318 * @dev: Device to wait for.
1319 * @subordinate: Device that needs to wait for @dev.
1321 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1323 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1326 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);