2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/resume-trace.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/async.h>
30 #include <linux/suspend.h>
31 #include <trace/events/power.h>
32 #include <linux/cpufreq.h>
33 #include <linux/cpuidle.h>
37 typedef int (*pm_callback_t)(struct device *);
40 * The entries in the dpm_list list are in a depth first order, simply
41 * because children are guaranteed to be discovered after parents, and
42 * are inserted at the back of the list on discovery.
44 * Since device_pm_add() may be called with a device lock held,
45 * we must never try to acquire a device lock while holding
50 static LIST_HEAD(dpm_prepared_list);
51 static LIST_HEAD(dpm_suspended_list);
52 static LIST_HEAD(dpm_late_early_list);
53 static LIST_HEAD(dpm_noirq_list);
55 struct suspend_stats suspend_stats;
56 static DEFINE_MUTEX(dpm_list_mtx);
57 static pm_message_t pm_transition;
59 static int async_error;
61 static char *pm_verb(int event)
64 case PM_EVENT_SUSPEND:
70 case PM_EVENT_QUIESCE:
72 case PM_EVENT_HIBERNATE:
76 case PM_EVENT_RESTORE:
78 case PM_EVENT_RECOVER:
81 return "(unknown PM event)";
86 * device_pm_sleep_init - Initialize system suspend-related device fields.
87 * @dev: Device object being initialized.
89 void device_pm_sleep_init(struct device *dev)
91 dev->power.is_prepared = false;
92 dev->power.is_suspended = false;
93 init_completion(&dev->power.completion);
94 complete_all(&dev->power.completion);
95 dev->power.wakeup = NULL;
96 INIT_LIST_HEAD(&dev->power.entry);
100 * device_pm_lock - Lock the list of active devices used by the PM core.
102 void device_pm_lock(void)
104 mutex_lock(&dpm_list_mtx);
108 * device_pm_unlock - Unlock the list of active devices used by the PM core.
110 void device_pm_unlock(void)
112 mutex_unlock(&dpm_list_mtx);
116 * device_pm_add - Add a device to the PM core's list of active devices.
117 * @dev: Device to add to the list.
119 void device_pm_add(struct device *dev)
121 pr_debug("PM: Adding info for %s:%s\n",
122 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
123 mutex_lock(&dpm_list_mtx);
124 if (dev->parent && dev->parent->power.is_prepared)
125 dev_warn(dev, "parent %s should not be sleeping\n",
126 dev_name(dev->parent));
127 list_add_tail(&dev->power.entry, &dpm_list);
128 mutex_unlock(&dpm_list_mtx);
132 * device_pm_remove - Remove a device from the PM core's list of active devices.
133 * @dev: Device to be removed from the list.
135 void device_pm_remove(struct device *dev)
137 pr_debug("PM: Removing info for %s:%s\n",
138 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
139 complete_all(&dev->power.completion);
140 mutex_lock(&dpm_list_mtx);
141 list_del_init(&dev->power.entry);
142 mutex_unlock(&dpm_list_mtx);
143 device_wakeup_disable(dev);
144 pm_runtime_remove(dev);
148 * device_pm_move_before - Move device in the PM core's list of active devices.
149 * @deva: Device to move in dpm_list.
150 * @devb: Device @deva should come before.
152 void device_pm_move_before(struct device *deva, struct device *devb)
154 pr_debug("PM: Moving %s:%s before %s:%s\n",
155 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
156 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
157 /* Delete deva from dpm_list and reinsert before devb. */
158 list_move_tail(&deva->power.entry, &devb->power.entry);
162 * device_pm_move_after - Move device in the PM core's list of active devices.
163 * @deva: Device to move in dpm_list.
164 * @devb: Device @deva should come after.
166 void device_pm_move_after(struct device *deva, struct device *devb)
168 pr_debug("PM: Moving %s:%s after %s:%s\n",
169 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
170 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
171 /* Delete deva from dpm_list and reinsert after devb. */
172 list_move(&deva->power.entry, &devb->power.entry);
176 * device_pm_move_last - Move device to end of the PM core's list of devices.
177 * @dev: Device to move in dpm_list.
179 void device_pm_move_last(struct device *dev)
181 pr_debug("PM: Moving %s:%s to end of list\n",
182 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
183 list_move_tail(&dev->power.entry, &dpm_list);
186 static ktime_t initcall_debug_start(struct device *dev)
188 ktime_t calltime = ktime_set(0, 0);
190 if (pm_print_times_enabled) {
191 pr_info("calling %s+ @ %i, parent: %s\n",
192 dev_name(dev), task_pid_nr(current),
193 dev->parent ? dev_name(dev->parent) : "none");
194 calltime = ktime_get();
200 static void initcall_debug_report(struct device *dev, ktime_t calltime,
201 int error, pm_message_t state, char *info)
206 rettime = ktime_get();
207 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
209 if (pm_print_times_enabled) {
210 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
211 error, (unsigned long long)nsecs >> 10);
214 trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event),
219 * dpm_wait - Wait for a PM operation to complete.
220 * @dev: Device to wait for.
221 * @async: If unset, wait only if the device's power.async_suspend flag is set.
223 static void dpm_wait(struct device *dev, bool async)
228 if (async || (pm_async_enabled && dev->power.async_suspend))
229 wait_for_completion(&dev->power.completion);
232 static int dpm_wait_fn(struct device *dev, void *async_ptr)
234 dpm_wait(dev, *((bool *)async_ptr));
238 static void dpm_wait_for_children(struct device *dev, bool async)
240 device_for_each_child(dev, &async, dpm_wait_fn);
244 * pm_op - Return the PM operation appropriate for given PM event.
245 * @ops: PM operations to choose from.
246 * @state: PM transition of the system being carried out.
248 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
250 switch (state.event) {
251 #ifdef CONFIG_SUSPEND
252 case PM_EVENT_SUSPEND:
254 case PM_EVENT_RESUME:
256 #endif /* CONFIG_SUSPEND */
257 #ifdef CONFIG_HIBERNATE_CALLBACKS
258 case PM_EVENT_FREEZE:
259 case PM_EVENT_QUIESCE:
261 case PM_EVENT_HIBERNATE:
262 return ops->poweroff;
264 case PM_EVENT_RECOVER:
267 case PM_EVENT_RESTORE:
269 #endif /* CONFIG_HIBERNATE_CALLBACKS */
276 * pm_late_early_op - Return the PM operation appropriate for given PM event.
277 * @ops: PM operations to choose from.
278 * @state: PM transition of the system being carried out.
280 * Runtime PM is disabled for @dev while this function is being executed.
282 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
285 switch (state.event) {
286 #ifdef CONFIG_SUSPEND
287 case PM_EVENT_SUSPEND:
288 return ops->suspend_late;
289 case PM_EVENT_RESUME:
290 return ops->resume_early;
291 #endif /* CONFIG_SUSPEND */
292 #ifdef CONFIG_HIBERNATE_CALLBACKS
293 case PM_EVENT_FREEZE:
294 case PM_EVENT_QUIESCE:
295 return ops->freeze_late;
296 case PM_EVENT_HIBERNATE:
297 return ops->poweroff_late;
299 case PM_EVENT_RECOVER:
300 return ops->thaw_early;
301 case PM_EVENT_RESTORE:
302 return ops->restore_early;
303 #endif /* CONFIG_HIBERNATE_CALLBACKS */
310 * pm_noirq_op - Return the PM operation appropriate for given PM event.
311 * @ops: PM operations to choose from.
312 * @state: PM transition of the system being carried out.
314 * The driver of @dev will not receive interrupts while this function is being
317 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
319 switch (state.event) {
320 #ifdef CONFIG_SUSPEND
321 case PM_EVENT_SUSPEND:
322 return ops->suspend_noirq;
323 case PM_EVENT_RESUME:
324 return ops->resume_noirq;
325 #endif /* CONFIG_SUSPEND */
326 #ifdef CONFIG_HIBERNATE_CALLBACKS
327 case PM_EVENT_FREEZE:
328 case PM_EVENT_QUIESCE:
329 return ops->freeze_noirq;
330 case PM_EVENT_HIBERNATE:
331 return ops->poweroff_noirq;
333 case PM_EVENT_RECOVER:
334 return ops->thaw_noirq;
335 case PM_EVENT_RESTORE:
336 return ops->restore_noirq;
337 #endif /* CONFIG_HIBERNATE_CALLBACKS */
343 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
345 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
346 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
347 ", may wakeup" : "");
350 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
353 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
354 dev_name(dev), pm_verb(state.event), info, error);
357 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
363 calltime = ktime_get();
364 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
365 do_div(usecs64, NSEC_PER_USEC);
369 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
370 info ?: "", info ? " " : "", pm_verb(state.event),
371 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
374 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
375 pm_message_t state, char *info)
383 calltime = initcall_debug_start(dev);
385 pm_dev_dbg(dev, state, info);
387 suspend_report_result(cb, error);
389 initcall_debug_report(dev, calltime, error, state, info);
394 /*------------------------- Resume routines -------------------------*/
397 * device_resume_noirq - Execute an "early resume" callback for given device.
398 * @dev: Device to handle.
399 * @state: PM transition of the system being carried out.
401 * The driver of @dev will not receive interrupts while this function is being
404 static int device_resume_noirq(struct device *dev, pm_message_t state)
406 pm_callback_t callback = NULL;
413 if (dev->power.syscore)
416 if (dev->pm_domain) {
417 info = "noirq power domain ";
418 callback = pm_noirq_op(&dev->pm_domain->ops, state);
419 } else if (dev->type && dev->type->pm) {
420 info = "noirq type ";
421 callback = pm_noirq_op(dev->type->pm, state);
422 } else if (dev->class && dev->class->pm) {
423 info = "noirq class ";
424 callback = pm_noirq_op(dev->class->pm, state);
425 } else if (dev->bus && dev->bus->pm) {
427 callback = pm_noirq_op(dev->bus->pm, state);
430 if (!callback && dev->driver && dev->driver->pm) {
431 info = "noirq driver ";
432 callback = pm_noirq_op(dev->driver->pm, state);
435 error = dpm_run_callback(callback, dev, state, info);
443 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
444 * @state: PM transition of the system being carried out.
446 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
447 * enable device drivers to receive interrupts.
449 static void dpm_resume_noirq(pm_message_t state)
451 ktime_t starttime = ktime_get();
453 mutex_lock(&dpm_list_mtx);
454 while (!list_empty(&dpm_noirq_list)) {
455 struct device *dev = to_device(dpm_noirq_list.next);
459 list_move_tail(&dev->power.entry, &dpm_late_early_list);
460 mutex_unlock(&dpm_list_mtx);
462 error = device_resume_noirq(dev, state);
464 suspend_stats.failed_resume_noirq++;
465 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
466 dpm_save_failed_dev(dev_name(dev));
467 pm_dev_err(dev, state, " noirq", error);
470 mutex_lock(&dpm_list_mtx);
473 mutex_unlock(&dpm_list_mtx);
474 dpm_show_time(starttime, state, "noirq");
475 resume_device_irqs();
481 * device_resume_early - Execute an "early resume" callback for given device.
482 * @dev: Device to handle.
483 * @state: PM transition of the system being carried out.
485 * Runtime PM is disabled for @dev while this function is being executed.
487 static int device_resume_early(struct device *dev, pm_message_t state)
489 pm_callback_t callback = NULL;
496 if (dev->power.syscore)
499 if (dev->pm_domain) {
500 info = "early power domain ";
501 callback = pm_late_early_op(&dev->pm_domain->ops, state);
502 } else if (dev->type && dev->type->pm) {
503 info = "early type ";
504 callback = pm_late_early_op(dev->type->pm, state);
505 } else if (dev->class && dev->class->pm) {
506 info = "early class ";
507 callback = pm_late_early_op(dev->class->pm, state);
508 } else if (dev->bus && dev->bus->pm) {
510 callback = pm_late_early_op(dev->bus->pm, state);
513 if (!callback && dev->driver && dev->driver->pm) {
514 info = "early driver ";
515 callback = pm_late_early_op(dev->driver->pm, state);
518 error = dpm_run_callback(callback, dev, state, info);
523 pm_runtime_enable(dev);
528 * dpm_resume_early - Execute "early resume" callbacks for all devices.
529 * @state: PM transition of the system being carried out.
531 static void dpm_resume_early(pm_message_t state)
533 ktime_t starttime = ktime_get();
535 mutex_lock(&dpm_list_mtx);
536 while (!list_empty(&dpm_late_early_list)) {
537 struct device *dev = to_device(dpm_late_early_list.next);
541 list_move_tail(&dev->power.entry, &dpm_suspended_list);
542 mutex_unlock(&dpm_list_mtx);
544 error = device_resume_early(dev, state);
546 suspend_stats.failed_resume_early++;
547 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
548 dpm_save_failed_dev(dev_name(dev));
549 pm_dev_err(dev, state, " early", error);
552 mutex_lock(&dpm_list_mtx);
555 mutex_unlock(&dpm_list_mtx);
556 dpm_show_time(starttime, state, "early");
560 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
561 * @state: PM transition of the system being carried out.
563 void dpm_resume_start(pm_message_t state)
565 dpm_resume_noirq(state);
566 dpm_resume_early(state);
568 EXPORT_SYMBOL_GPL(dpm_resume_start);
571 * device_resume - Execute "resume" callbacks for given device.
572 * @dev: Device to handle.
573 * @state: PM transition of the system being carried out.
574 * @async: If true, the device is being resumed asynchronously.
576 static int device_resume(struct device *dev, pm_message_t state, bool async)
578 pm_callback_t callback = NULL;
585 if (dev->power.syscore)
588 dpm_wait(dev->parent, async);
592 * This is a fib. But we'll allow new children to be added below
593 * a resumed device, even if the device hasn't been completed yet.
595 dev->power.is_prepared = false;
597 if (!dev->power.is_suspended)
600 if (dev->pm_domain) {
601 info = "power domain ";
602 callback = pm_op(&dev->pm_domain->ops, state);
606 if (dev->type && dev->type->pm) {
608 callback = pm_op(dev->type->pm, state);
613 if (dev->class->pm) {
615 callback = pm_op(dev->class->pm, state);
617 } else if (dev->class->resume) {
618 info = "legacy class ";
619 callback = dev->class->resume;
627 callback = pm_op(dev->bus->pm, state);
628 } else if (dev->bus->resume) {
629 info = "legacy bus ";
630 callback = dev->bus->resume;
636 if (!callback && dev->driver && dev->driver->pm) {
638 callback = pm_op(dev->driver->pm, state);
642 error = dpm_run_callback(callback, dev, state, info);
643 dev->power.is_suspended = false;
649 complete_all(&dev->power.completion);
656 static void async_resume(void *data, async_cookie_t cookie)
658 struct device *dev = (struct device *)data;
661 error = device_resume(dev, pm_transition, true);
663 pm_dev_err(dev, pm_transition, " async", error);
667 static bool is_async(struct device *dev)
669 return dev->power.async_suspend && pm_async_enabled
670 && !pm_trace_is_enabled();
674 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
675 * @state: PM transition of the system being carried out.
677 * Execute the appropriate "resume" callback for all devices whose status
678 * indicates that they are suspended.
680 void dpm_resume(pm_message_t state)
683 ktime_t starttime = ktime_get();
687 mutex_lock(&dpm_list_mtx);
688 pm_transition = state;
691 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
692 INIT_COMPLETION(dev->power.completion);
695 async_schedule(async_resume, dev);
699 while (!list_empty(&dpm_suspended_list)) {
700 dev = to_device(dpm_suspended_list.next);
702 if (!is_async(dev)) {
705 mutex_unlock(&dpm_list_mtx);
707 error = device_resume(dev, state, false);
709 suspend_stats.failed_resume++;
710 dpm_save_failed_step(SUSPEND_RESUME);
711 dpm_save_failed_dev(dev_name(dev));
712 pm_dev_err(dev, state, "", error);
715 mutex_lock(&dpm_list_mtx);
717 if (!list_empty(&dev->power.entry))
718 list_move_tail(&dev->power.entry, &dpm_prepared_list);
721 mutex_unlock(&dpm_list_mtx);
722 async_synchronize_full();
723 dpm_show_time(starttime, state, NULL);
727 * device_complete - Complete a PM transition for given device.
728 * @dev: Device to handle.
729 * @state: PM transition of the system being carried out.
731 static void device_complete(struct device *dev, pm_message_t state)
733 void (*callback)(struct device *) = NULL;
736 if (dev->power.syscore)
741 if (dev->pm_domain) {
742 info = "completing power domain ";
743 callback = dev->pm_domain->ops.complete;
744 } else if (dev->type && dev->type->pm) {
745 info = "completing type ";
746 callback = dev->type->pm->complete;
747 } else if (dev->class && dev->class->pm) {
748 info = "completing class ";
749 callback = dev->class->pm->complete;
750 } else if (dev->bus && dev->bus->pm) {
751 info = "completing bus ";
752 callback = dev->bus->pm->complete;
755 if (!callback && dev->driver && dev->driver->pm) {
756 info = "completing driver ";
757 callback = dev->driver->pm->complete;
761 pm_dev_dbg(dev, state, info);
771 * dpm_complete - Complete a PM transition for all non-sysdev devices.
772 * @state: PM transition of the system being carried out.
774 * Execute the ->complete() callbacks for all devices whose PM status is not
775 * DPM_ON (this allows new devices to be registered).
777 void dpm_complete(pm_message_t state)
779 struct list_head list;
783 INIT_LIST_HEAD(&list);
784 mutex_lock(&dpm_list_mtx);
785 while (!list_empty(&dpm_prepared_list)) {
786 struct device *dev = to_device(dpm_prepared_list.prev);
789 dev->power.is_prepared = false;
790 list_move(&dev->power.entry, &list);
791 mutex_unlock(&dpm_list_mtx);
793 device_complete(dev, state);
795 mutex_lock(&dpm_list_mtx);
798 list_splice(&list, &dpm_list);
799 mutex_unlock(&dpm_list_mtx);
803 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
804 * @state: PM transition of the system being carried out.
806 * Execute "resume" callbacks for all devices and complete the PM transition of
809 void dpm_resume_end(pm_message_t state)
814 EXPORT_SYMBOL_GPL(dpm_resume_end);
817 /*------------------------- Suspend routines -------------------------*/
820 * resume_event - Return a "resume" message for given "suspend" sleep state.
821 * @sleep_state: PM message representing a sleep state.
823 * Return a PM message representing the resume event corresponding to given
826 static pm_message_t resume_event(pm_message_t sleep_state)
828 switch (sleep_state.event) {
829 case PM_EVENT_SUSPEND:
831 case PM_EVENT_FREEZE:
832 case PM_EVENT_QUIESCE:
834 case PM_EVENT_HIBERNATE:
841 * device_suspend_noirq - Execute a "late suspend" callback for given device.
842 * @dev: Device to handle.
843 * @state: PM transition of the system being carried out.
845 * The driver of @dev will not receive interrupts while this function is being
848 static int device_suspend_noirq(struct device *dev, pm_message_t state)
850 pm_callback_t callback = NULL;
853 if (dev->power.syscore)
856 if (dev->pm_domain) {
857 info = "noirq power domain ";
858 callback = pm_noirq_op(&dev->pm_domain->ops, state);
859 } else if (dev->type && dev->type->pm) {
860 info = "noirq type ";
861 callback = pm_noirq_op(dev->type->pm, state);
862 } else if (dev->class && dev->class->pm) {
863 info = "noirq class ";
864 callback = pm_noirq_op(dev->class->pm, state);
865 } else if (dev->bus && dev->bus->pm) {
867 callback = pm_noirq_op(dev->bus->pm, state);
870 if (!callback && dev->driver && dev->driver->pm) {
871 info = "noirq driver ";
872 callback = pm_noirq_op(dev->driver->pm, state);
875 return dpm_run_callback(callback, dev, state, info);
879 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
880 * @state: PM transition of the system being carried out.
882 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
883 * handlers for all non-sysdev devices.
885 static int dpm_suspend_noirq(pm_message_t state)
887 ktime_t starttime = ktime_get();
892 suspend_device_irqs();
893 mutex_lock(&dpm_list_mtx);
894 while (!list_empty(&dpm_late_early_list)) {
895 struct device *dev = to_device(dpm_late_early_list.prev);
898 mutex_unlock(&dpm_list_mtx);
900 error = device_suspend_noirq(dev, state);
902 mutex_lock(&dpm_list_mtx);
904 pm_dev_err(dev, state, " noirq", error);
905 suspend_stats.failed_suspend_noirq++;
906 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
907 dpm_save_failed_dev(dev_name(dev));
911 if (!list_empty(&dev->power.entry))
912 list_move(&dev->power.entry, &dpm_noirq_list);
915 if (pm_wakeup_pending()) {
920 mutex_unlock(&dpm_list_mtx);
922 dpm_resume_noirq(resume_event(state));
924 dpm_show_time(starttime, state, "noirq");
929 * device_suspend_late - Execute a "late suspend" callback for given device.
930 * @dev: Device to handle.
931 * @state: PM transition of the system being carried out.
933 * Runtime PM is disabled for @dev while this function is being executed.
935 static int device_suspend_late(struct device *dev, pm_message_t state)
937 pm_callback_t callback = NULL;
940 __pm_runtime_disable(dev, false);
942 if (dev->power.syscore)
945 if (dev->pm_domain) {
946 info = "late power domain ";
947 callback = pm_late_early_op(&dev->pm_domain->ops, state);
948 } else if (dev->type && dev->type->pm) {
950 callback = pm_late_early_op(dev->type->pm, state);
951 } else if (dev->class && dev->class->pm) {
952 info = "late class ";
953 callback = pm_late_early_op(dev->class->pm, state);
954 } else if (dev->bus && dev->bus->pm) {
956 callback = pm_late_early_op(dev->bus->pm, state);
959 if (!callback && dev->driver && dev->driver->pm) {
960 info = "late driver ";
961 callback = pm_late_early_op(dev->driver->pm, state);
964 return dpm_run_callback(callback, dev, state, info);
968 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
969 * @state: PM transition of the system being carried out.
971 static int dpm_suspend_late(pm_message_t state)
973 ktime_t starttime = ktime_get();
976 mutex_lock(&dpm_list_mtx);
977 while (!list_empty(&dpm_suspended_list)) {
978 struct device *dev = to_device(dpm_suspended_list.prev);
981 mutex_unlock(&dpm_list_mtx);
983 error = device_suspend_late(dev, state);
985 mutex_lock(&dpm_list_mtx);
987 pm_dev_err(dev, state, " late", error);
988 suspend_stats.failed_suspend_late++;
989 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
990 dpm_save_failed_dev(dev_name(dev));
994 if (!list_empty(&dev->power.entry))
995 list_move(&dev->power.entry, &dpm_late_early_list);
998 if (pm_wakeup_pending()) {
1003 mutex_unlock(&dpm_list_mtx);
1005 dpm_resume_early(resume_event(state));
1007 dpm_show_time(starttime, state, "late");
1013 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1014 * @state: PM transition of the system being carried out.
1016 int dpm_suspend_end(pm_message_t state)
1018 int error = dpm_suspend_late(state);
1022 error = dpm_suspend_noirq(state);
1024 dpm_resume_early(resume_event(state));
1030 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1033 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1034 * @dev: Device to suspend.
1035 * @state: PM transition of the system being carried out.
1036 * @cb: Suspend callback to execute.
1038 static int legacy_suspend(struct device *dev, pm_message_t state,
1039 int (*cb)(struct device *dev, pm_message_t state),
1045 calltime = initcall_debug_start(dev);
1047 error = cb(dev, state);
1048 suspend_report_result(cb, error);
1050 initcall_debug_report(dev, calltime, error, state, info);
1056 * device_suspend - Execute "suspend" callbacks for given device.
1057 * @dev: Device to handle.
1058 * @state: PM transition of the system being carried out.
1059 * @async: If true, the device is being suspended asynchronously.
1061 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1063 pm_callback_t callback = NULL;
1067 dpm_wait_for_children(dev, async);
1073 * If a device configured to wake up the system from sleep states
1074 * has been suspended at run time and there's a resume request pending
1075 * for it, this is equivalent to the device signaling wakeup, so the
1076 * system suspend operation should be aborted.
1078 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1079 pm_wakeup_event(dev, 0);
1081 if (pm_wakeup_pending()) {
1082 async_error = -EBUSY;
1086 if (dev->power.syscore)
1091 if (dev->pm_domain) {
1092 info = "power domain ";
1093 callback = pm_op(&dev->pm_domain->ops, state);
1097 if (dev->type && dev->type->pm) {
1099 callback = pm_op(dev->type->pm, state);
1104 if (dev->class->pm) {
1106 callback = pm_op(dev->class->pm, state);
1108 } else if (dev->class->suspend) {
1109 pm_dev_dbg(dev, state, "legacy class ");
1110 error = legacy_suspend(dev, state, dev->class->suspend,
1119 callback = pm_op(dev->bus->pm, state);
1120 } else if (dev->bus->suspend) {
1121 pm_dev_dbg(dev, state, "legacy bus ");
1122 error = legacy_suspend(dev, state, dev->bus->suspend,
1129 if (!callback && dev->driver && dev->driver->pm) {
1131 callback = pm_op(dev->driver->pm, state);
1134 error = dpm_run_callback(callback, dev, state, info);
1138 dev->power.is_suspended = true;
1139 if (dev->power.wakeup_path
1140 && dev->parent && !dev->parent->power.ignore_children)
1141 dev->parent->power.wakeup_path = true;
1147 complete_all(&dev->power.completion);
1149 async_error = error;
1154 static void async_suspend(void *data, async_cookie_t cookie)
1156 struct device *dev = (struct device *)data;
1159 error = __device_suspend(dev, pm_transition, true);
1161 dpm_save_failed_dev(dev_name(dev));
1162 pm_dev_err(dev, pm_transition, " async", error);
1168 static int device_suspend(struct device *dev)
1170 INIT_COMPLETION(dev->power.completion);
1172 if (pm_async_enabled && dev->power.async_suspend) {
1174 async_schedule(async_suspend, dev);
1178 return __device_suspend(dev, pm_transition, false);
1182 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1183 * @state: PM transition of the system being carried out.
1185 int dpm_suspend(pm_message_t state)
1187 ktime_t starttime = ktime_get();
1192 mutex_lock(&dpm_list_mtx);
1193 pm_transition = state;
1195 while (!list_empty(&dpm_prepared_list)) {
1196 struct device *dev = to_device(dpm_prepared_list.prev);
1199 mutex_unlock(&dpm_list_mtx);
1201 error = device_suspend(dev);
1203 mutex_lock(&dpm_list_mtx);
1205 pm_dev_err(dev, state, "", error);
1206 dpm_save_failed_dev(dev_name(dev));
1210 if (!list_empty(&dev->power.entry))
1211 list_move(&dev->power.entry, &dpm_suspended_list);
1216 mutex_unlock(&dpm_list_mtx);
1217 async_synchronize_full();
1219 error = async_error;
1221 suspend_stats.failed_suspend++;
1222 dpm_save_failed_step(SUSPEND_SUSPEND);
1224 dpm_show_time(starttime, state, NULL);
1229 * device_prepare - Prepare a device for system power transition.
1230 * @dev: Device to handle.
1231 * @state: PM transition of the system being carried out.
1233 * Execute the ->prepare() callback(s) for given device. No new children of the
1234 * device may be registered after this function has returned.
1236 static int device_prepare(struct device *dev, pm_message_t state)
1238 int (*callback)(struct device *) = NULL;
1242 if (dev->power.syscore)
1246 * If a device's parent goes into runtime suspend at the wrong time,
1247 * it won't be possible to resume the device. To prevent this we
1248 * block runtime suspend here, during the prepare phase, and allow
1249 * it again during the complete phase.
1251 pm_runtime_get_noresume(dev);
1255 dev->power.wakeup_path = device_may_wakeup(dev);
1257 if (dev->pm_domain) {
1258 info = "preparing power domain ";
1259 callback = dev->pm_domain->ops.prepare;
1260 } else if (dev->type && dev->type->pm) {
1261 info = "preparing type ";
1262 callback = dev->type->pm->prepare;
1263 } else if (dev->class && dev->class->pm) {
1264 info = "preparing class ";
1265 callback = dev->class->pm->prepare;
1266 } else if (dev->bus && dev->bus->pm) {
1267 info = "preparing bus ";
1268 callback = dev->bus->pm->prepare;
1271 if (!callback && dev->driver && dev->driver->pm) {
1272 info = "preparing driver ";
1273 callback = dev->driver->pm->prepare;
1277 error = callback(dev);
1278 suspend_report_result(callback, error);
1287 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1288 * @state: PM transition of the system being carried out.
1290 * Execute the ->prepare() callback(s) for all devices.
1292 int dpm_prepare(pm_message_t state)
1298 mutex_lock(&dpm_list_mtx);
1299 while (!list_empty(&dpm_list)) {
1300 struct device *dev = to_device(dpm_list.next);
1303 mutex_unlock(&dpm_list_mtx);
1305 error = device_prepare(dev, state);
1307 mutex_lock(&dpm_list_mtx);
1309 if (error == -EAGAIN) {
1314 printk(KERN_INFO "PM: Device %s not prepared "
1315 "for power transition: code %d\n",
1316 dev_name(dev), error);
1320 dev->power.is_prepared = true;
1321 if (!list_empty(&dev->power.entry))
1322 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1325 mutex_unlock(&dpm_list_mtx);
1330 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1331 * @state: PM transition of the system being carried out.
1333 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1334 * callbacks for them.
1336 int dpm_suspend_start(pm_message_t state)
1340 error = dpm_prepare(state);
1342 suspend_stats.failed_prepare++;
1343 dpm_save_failed_step(SUSPEND_PREPARE);
1345 error = dpm_suspend(state);
1348 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1350 void __suspend_report_result(const char *function, void *fn, int ret)
1353 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1355 EXPORT_SYMBOL_GPL(__suspend_report_result);
1358 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1359 * @dev: Device to wait for.
1360 * @subordinate: Device that needs to wait for @dev.
1362 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1364 dpm_wait(dev, subordinate->power.async_suspend);
1367 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1370 * dpm_for_each_dev - device iterator.
1371 * @data: data for the callback.
1372 * @fn: function to be called for each device.
1374 * Iterate over devices in dpm_list, and call @fn for each device,
1377 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1385 list_for_each_entry(dev, &dpm_list, power.entry)
1389 EXPORT_SYMBOL_GPL(dpm_for_each_dev);