usb: xhci-plat: properly handle probe deferral for devm_clk_get()
[linux/fpc-iii.git] / drivers / base / power / main.c
blob27aea96036c4df89e6bdcfb2e5f5e402e3e6c6f1
1 /*
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>
24 #include <linux/pm.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm-trace.h>
27 #include <linux/pm_wakeirq.h>
28 #include <linux/interrupt.h>
29 #include <linux/sched.h>
30 #include <linux/async.h>
31 #include <linux/suspend.h>
32 #include <trace/events/power.h>
33 #include <linux/cpufreq.h>
34 #include <linux/cpuidle.h>
35 #include <linux/timer.h>
37 #include "../base.h"
38 #include "power.h"
40 typedef int (*pm_callback_t)(struct device *);
43 * The entries in the dpm_list list are in a depth first order, simply
44 * because children are guaranteed to be discovered after parents, and
45 * are inserted at the back of the list on discovery.
47 * Since device_pm_add() may be called with a device lock held,
48 * we must never try to acquire a device lock while holding
49 * dpm_list_mutex.
52 LIST_HEAD(dpm_list);
53 static LIST_HEAD(dpm_prepared_list);
54 static LIST_HEAD(dpm_suspended_list);
55 static LIST_HEAD(dpm_late_early_list);
56 static LIST_HEAD(dpm_noirq_list);
58 struct suspend_stats suspend_stats;
59 static DEFINE_MUTEX(dpm_list_mtx);
60 static pm_message_t pm_transition;
62 static int async_error;
64 static char *pm_verb(int event)
66 switch (event) {
67 case PM_EVENT_SUSPEND:
68 return "suspend";
69 case PM_EVENT_RESUME:
70 return "resume";
71 case PM_EVENT_FREEZE:
72 return "freeze";
73 case PM_EVENT_QUIESCE:
74 return "quiesce";
75 case PM_EVENT_HIBERNATE:
76 return "hibernate";
77 case PM_EVENT_THAW:
78 return "thaw";
79 case PM_EVENT_RESTORE:
80 return "restore";
81 case PM_EVENT_RECOVER:
82 return "recover";
83 default:
84 return "(unknown PM event)";
88 /**
89 * device_pm_sleep_init - Initialize system suspend-related device fields.
90 * @dev: Device object being initialized.
92 void device_pm_sleep_init(struct device *dev)
94 dev->power.is_prepared = false;
95 dev->power.is_suspended = false;
96 dev->power.is_noirq_suspended = false;
97 dev->power.is_late_suspended = false;
98 init_completion(&dev->power.completion);
99 complete_all(&dev->power.completion);
100 dev->power.wakeup = NULL;
101 INIT_LIST_HEAD(&dev->power.entry);
105 * device_pm_lock - Lock the list of active devices used by the PM core.
107 void device_pm_lock(void)
109 mutex_lock(&dpm_list_mtx);
113 * device_pm_unlock - Unlock the list of active devices used by the PM core.
115 void device_pm_unlock(void)
117 mutex_unlock(&dpm_list_mtx);
121 * device_pm_add - Add a device to the PM core's list of active devices.
122 * @dev: Device to add to the list.
124 void device_pm_add(struct device *dev)
126 pr_debug("PM: Adding info for %s:%s\n",
127 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
128 device_pm_check_callbacks(dev);
129 mutex_lock(&dpm_list_mtx);
130 if (dev->parent && dev->parent->power.is_prepared)
131 dev_warn(dev, "parent %s should not be sleeping\n",
132 dev_name(dev->parent));
133 list_add_tail(&dev->power.entry, &dpm_list);
134 mutex_unlock(&dpm_list_mtx);
138 * device_pm_remove - Remove a device from the PM core's list of active devices.
139 * @dev: Device to be removed from the list.
141 void device_pm_remove(struct device *dev)
143 pr_debug("PM: Removing info for %s:%s\n",
144 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
145 complete_all(&dev->power.completion);
146 mutex_lock(&dpm_list_mtx);
147 list_del_init(&dev->power.entry);
148 mutex_unlock(&dpm_list_mtx);
149 device_wakeup_disable(dev);
150 pm_runtime_remove(dev);
151 device_pm_check_callbacks(dev);
155 * device_pm_move_before - Move device in the PM core's list of active devices.
156 * @deva: Device to move in dpm_list.
157 * @devb: Device @deva should come before.
159 void device_pm_move_before(struct device *deva, struct device *devb)
161 pr_debug("PM: Moving %s:%s before %s:%s\n",
162 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
163 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
164 /* Delete deva from dpm_list and reinsert before devb. */
165 list_move_tail(&deva->power.entry, &devb->power.entry);
169 * device_pm_move_after - Move device in the PM core's list of active devices.
170 * @deva: Device to move in dpm_list.
171 * @devb: Device @deva should come after.
173 void device_pm_move_after(struct device *deva, struct device *devb)
175 pr_debug("PM: Moving %s:%s after %s:%s\n",
176 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
177 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
178 /* Delete deva from dpm_list and reinsert after devb. */
179 list_move(&deva->power.entry, &devb->power.entry);
183 * device_pm_move_last - Move device to end of the PM core's list of devices.
184 * @dev: Device to move in dpm_list.
186 void device_pm_move_last(struct device *dev)
188 pr_debug("PM: Moving %s:%s to end of list\n",
189 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
190 list_move_tail(&dev->power.entry, &dpm_list);
193 static ktime_t initcall_debug_start(struct device *dev)
195 ktime_t calltime = ktime_set(0, 0);
197 if (pm_print_times_enabled) {
198 pr_info("calling %s+ @ %i, parent: %s\n",
199 dev_name(dev), task_pid_nr(current),
200 dev->parent ? dev_name(dev->parent) : "none");
201 calltime = ktime_get();
204 return calltime;
207 static void initcall_debug_report(struct device *dev, ktime_t calltime,
208 int error, pm_message_t state, char *info)
210 ktime_t rettime;
211 s64 nsecs;
213 rettime = ktime_get();
214 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
216 if (pm_print_times_enabled) {
217 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
218 error, (unsigned long long)nsecs >> 10);
223 * dpm_wait - Wait for a PM operation to complete.
224 * @dev: Device to wait for.
225 * @async: If unset, wait only if the device's power.async_suspend flag is set.
227 static void dpm_wait(struct device *dev, bool async)
229 if (!dev)
230 return;
232 if (async || (pm_async_enabled && dev->power.async_suspend))
233 wait_for_completion(&dev->power.completion);
236 static int dpm_wait_fn(struct device *dev, void *async_ptr)
238 dpm_wait(dev, *((bool *)async_ptr));
239 return 0;
242 static void dpm_wait_for_children(struct device *dev, bool async)
244 device_for_each_child(dev, &async, dpm_wait_fn);
248 * pm_op - Return the PM operation appropriate for given PM event.
249 * @ops: PM operations to choose from.
250 * @state: PM transition of the system being carried out.
252 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
254 switch (state.event) {
255 #ifdef CONFIG_SUSPEND
256 case PM_EVENT_SUSPEND:
257 return ops->suspend;
258 case PM_EVENT_RESUME:
259 return ops->resume;
260 #endif /* CONFIG_SUSPEND */
261 #ifdef CONFIG_HIBERNATE_CALLBACKS
262 case PM_EVENT_FREEZE:
263 case PM_EVENT_QUIESCE:
264 return ops->freeze;
265 case PM_EVENT_HIBERNATE:
266 return ops->poweroff;
267 case PM_EVENT_THAW:
268 case PM_EVENT_RECOVER:
269 return ops->thaw;
270 break;
271 case PM_EVENT_RESTORE:
272 return ops->restore;
273 #endif /* CONFIG_HIBERNATE_CALLBACKS */
276 return NULL;
280 * pm_late_early_op - Return the PM operation appropriate for given PM event.
281 * @ops: PM operations to choose from.
282 * @state: PM transition of the system being carried out.
284 * Runtime PM is disabled for @dev while this function is being executed.
286 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
287 pm_message_t state)
289 switch (state.event) {
290 #ifdef CONFIG_SUSPEND
291 case PM_EVENT_SUSPEND:
292 return ops->suspend_late;
293 case PM_EVENT_RESUME:
294 return ops->resume_early;
295 #endif /* CONFIG_SUSPEND */
296 #ifdef CONFIG_HIBERNATE_CALLBACKS
297 case PM_EVENT_FREEZE:
298 case PM_EVENT_QUIESCE:
299 return ops->freeze_late;
300 case PM_EVENT_HIBERNATE:
301 return ops->poweroff_late;
302 case PM_EVENT_THAW:
303 case PM_EVENT_RECOVER:
304 return ops->thaw_early;
305 case PM_EVENT_RESTORE:
306 return ops->restore_early;
307 #endif /* CONFIG_HIBERNATE_CALLBACKS */
310 return NULL;
314 * pm_noirq_op - Return the PM operation appropriate for given PM event.
315 * @ops: PM operations to choose from.
316 * @state: PM transition of the system being carried out.
318 * The driver of @dev will not receive interrupts while this function is being
319 * executed.
321 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
323 switch (state.event) {
324 #ifdef CONFIG_SUSPEND
325 case PM_EVENT_SUSPEND:
326 return ops->suspend_noirq;
327 case PM_EVENT_RESUME:
328 return ops->resume_noirq;
329 #endif /* CONFIG_SUSPEND */
330 #ifdef CONFIG_HIBERNATE_CALLBACKS
331 case PM_EVENT_FREEZE:
332 case PM_EVENT_QUIESCE:
333 return ops->freeze_noirq;
334 case PM_EVENT_HIBERNATE:
335 return ops->poweroff_noirq;
336 case PM_EVENT_THAW:
337 case PM_EVENT_RECOVER:
338 return ops->thaw_noirq;
339 case PM_EVENT_RESTORE:
340 return ops->restore_noirq;
341 #endif /* CONFIG_HIBERNATE_CALLBACKS */
344 return NULL;
347 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
349 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
350 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
351 ", may wakeup" : "");
354 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
355 int error)
357 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
358 dev_name(dev), pm_verb(state.event), info, error);
361 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
363 ktime_t calltime;
364 u64 usecs64;
365 int usecs;
367 calltime = ktime_get();
368 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
369 do_div(usecs64, NSEC_PER_USEC);
370 usecs = usecs64;
371 if (usecs == 0)
372 usecs = 1;
373 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
374 info ?: "", info ? " " : "", pm_verb(state.event),
375 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
378 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
379 pm_message_t state, char *info)
381 ktime_t calltime;
382 int error;
384 if (!cb)
385 return 0;
387 calltime = initcall_debug_start(dev);
389 pm_dev_dbg(dev, state, info);
390 trace_device_pm_callback_start(dev, info, state.event);
391 error = cb(dev);
392 trace_device_pm_callback_end(dev, error);
393 suspend_report_result(cb, error);
395 initcall_debug_report(dev, calltime, error, state, info);
397 return error;
400 #ifdef CONFIG_DPM_WATCHDOG
401 struct dpm_watchdog {
402 struct device *dev;
403 struct task_struct *tsk;
404 struct timer_list timer;
407 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
408 struct dpm_watchdog wd
411 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
412 * @data: Watchdog object address.
414 * Called when a driver has timed out suspending or resuming.
415 * There's not much we can do here to recover so panic() to
416 * capture a crash-dump in pstore.
418 static void dpm_watchdog_handler(unsigned long data)
420 struct dpm_watchdog *wd = (void *)data;
422 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
423 show_stack(wd->tsk, NULL);
424 panic("%s %s: unrecoverable failure\n",
425 dev_driver_string(wd->dev), dev_name(wd->dev));
429 * dpm_watchdog_set - Enable pm watchdog for given device.
430 * @wd: Watchdog. Must be allocated on the stack.
431 * @dev: Device to handle.
433 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
435 struct timer_list *timer = &wd->timer;
437 wd->dev = dev;
438 wd->tsk = current;
440 init_timer_on_stack(timer);
441 /* use same timeout value for both suspend and resume */
442 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
443 timer->function = dpm_watchdog_handler;
444 timer->data = (unsigned long)wd;
445 add_timer(timer);
449 * dpm_watchdog_clear - Disable suspend/resume watchdog.
450 * @wd: Watchdog to disable.
452 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
454 struct timer_list *timer = &wd->timer;
456 del_timer_sync(timer);
457 destroy_timer_on_stack(timer);
459 #else
460 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
461 #define dpm_watchdog_set(x, y)
462 #define dpm_watchdog_clear(x)
463 #endif
465 /*------------------------- Resume routines -------------------------*/
468 * device_resume_noirq - Execute an "early resume" callback for given device.
469 * @dev: Device to handle.
470 * @state: PM transition of the system being carried out.
471 * @async: If true, the device is being resumed asynchronously.
473 * The driver of @dev will not receive interrupts while this function is being
474 * executed.
476 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
478 pm_callback_t callback = NULL;
479 char *info = NULL;
480 int error = 0;
482 TRACE_DEVICE(dev);
483 TRACE_RESUME(0);
485 if (dev->power.syscore || dev->power.direct_complete)
486 goto Out;
488 if (!dev->power.is_noirq_suspended)
489 goto Out;
491 dpm_wait(dev->parent, async);
493 if (dev->pm_domain) {
494 info = "noirq power domain ";
495 callback = pm_noirq_op(&dev->pm_domain->ops, state);
496 } else if (dev->type && dev->type->pm) {
497 info = "noirq type ";
498 callback = pm_noirq_op(dev->type->pm, state);
499 } else if (dev->class && dev->class->pm) {
500 info = "noirq class ";
501 callback = pm_noirq_op(dev->class->pm, state);
502 } else if (dev->bus && dev->bus->pm) {
503 info = "noirq bus ";
504 callback = pm_noirq_op(dev->bus->pm, state);
507 if (!callback && dev->driver && dev->driver->pm) {
508 info = "noirq driver ";
509 callback = pm_noirq_op(dev->driver->pm, state);
512 error = dpm_run_callback(callback, dev, state, info);
513 dev->power.is_noirq_suspended = false;
515 Out:
516 complete_all(&dev->power.completion);
517 TRACE_RESUME(error);
518 return error;
521 static bool is_async(struct device *dev)
523 return dev->power.async_suspend && pm_async_enabled
524 && !pm_trace_is_enabled();
527 static void async_resume_noirq(void *data, async_cookie_t cookie)
529 struct device *dev = (struct device *)data;
530 int error;
532 error = device_resume_noirq(dev, pm_transition, true);
533 if (error)
534 pm_dev_err(dev, pm_transition, " async", error);
536 put_device(dev);
540 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
541 * @state: PM transition of the system being carried out.
543 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
544 * enable device drivers to receive interrupts.
546 void dpm_resume_noirq(pm_message_t state)
548 struct device *dev;
549 ktime_t starttime = ktime_get();
551 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
552 mutex_lock(&dpm_list_mtx);
553 pm_transition = state;
556 * Advanced the async threads upfront,
557 * in case the starting of async threads is
558 * delayed by non-async resuming devices.
560 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
561 reinit_completion(&dev->power.completion);
562 if (is_async(dev)) {
563 get_device(dev);
564 async_schedule(async_resume_noirq, dev);
568 while (!list_empty(&dpm_noirq_list)) {
569 dev = to_device(dpm_noirq_list.next);
570 get_device(dev);
571 list_move_tail(&dev->power.entry, &dpm_late_early_list);
572 mutex_unlock(&dpm_list_mtx);
574 if (!is_async(dev)) {
575 int error;
577 error = device_resume_noirq(dev, state, false);
578 if (error) {
579 suspend_stats.failed_resume_noirq++;
580 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
581 dpm_save_failed_dev(dev_name(dev));
582 pm_dev_err(dev, state, " noirq", error);
586 mutex_lock(&dpm_list_mtx);
587 put_device(dev);
589 mutex_unlock(&dpm_list_mtx);
590 async_synchronize_full();
591 dpm_show_time(starttime, state, "noirq");
592 resume_device_irqs();
593 device_wakeup_disarm_wake_irqs();
594 cpuidle_resume();
595 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
599 * device_resume_early - Execute an "early resume" callback for given device.
600 * @dev: Device to handle.
601 * @state: PM transition of the system being carried out.
602 * @async: If true, the device is being resumed asynchronously.
604 * Runtime PM is disabled for @dev while this function is being executed.
606 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
608 pm_callback_t callback = NULL;
609 char *info = NULL;
610 int error = 0;
612 TRACE_DEVICE(dev);
613 TRACE_RESUME(0);
615 if (dev->power.syscore || dev->power.direct_complete)
616 goto Out;
618 if (!dev->power.is_late_suspended)
619 goto Out;
621 dpm_wait(dev->parent, async);
623 if (dev->pm_domain) {
624 info = "early power domain ";
625 callback = pm_late_early_op(&dev->pm_domain->ops, state);
626 } else if (dev->type && dev->type->pm) {
627 info = "early type ";
628 callback = pm_late_early_op(dev->type->pm, state);
629 } else if (dev->class && dev->class->pm) {
630 info = "early class ";
631 callback = pm_late_early_op(dev->class->pm, state);
632 } else if (dev->bus && dev->bus->pm) {
633 info = "early bus ";
634 callback = pm_late_early_op(dev->bus->pm, state);
637 if (!callback && dev->driver && dev->driver->pm) {
638 info = "early driver ";
639 callback = pm_late_early_op(dev->driver->pm, state);
642 error = dpm_run_callback(callback, dev, state, info);
643 dev->power.is_late_suspended = false;
645 Out:
646 TRACE_RESUME(error);
648 pm_runtime_enable(dev);
649 complete_all(&dev->power.completion);
650 return error;
653 static void async_resume_early(void *data, async_cookie_t cookie)
655 struct device *dev = (struct device *)data;
656 int error;
658 error = device_resume_early(dev, pm_transition, true);
659 if (error)
660 pm_dev_err(dev, pm_transition, " async", error);
662 put_device(dev);
666 * dpm_resume_early - Execute "early resume" callbacks for all devices.
667 * @state: PM transition of the system being carried out.
669 void dpm_resume_early(pm_message_t state)
671 struct device *dev;
672 ktime_t starttime = ktime_get();
674 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
675 mutex_lock(&dpm_list_mtx);
676 pm_transition = state;
679 * Advanced the async threads upfront,
680 * in case the starting of async threads is
681 * delayed by non-async resuming devices.
683 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
684 reinit_completion(&dev->power.completion);
685 if (is_async(dev)) {
686 get_device(dev);
687 async_schedule(async_resume_early, dev);
691 while (!list_empty(&dpm_late_early_list)) {
692 dev = to_device(dpm_late_early_list.next);
693 get_device(dev);
694 list_move_tail(&dev->power.entry, &dpm_suspended_list);
695 mutex_unlock(&dpm_list_mtx);
697 if (!is_async(dev)) {
698 int error;
700 error = device_resume_early(dev, state, false);
701 if (error) {
702 suspend_stats.failed_resume_early++;
703 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
704 dpm_save_failed_dev(dev_name(dev));
705 pm_dev_err(dev, state, " early", error);
708 mutex_lock(&dpm_list_mtx);
709 put_device(dev);
711 mutex_unlock(&dpm_list_mtx);
712 async_synchronize_full();
713 dpm_show_time(starttime, state, "early");
714 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
718 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
719 * @state: PM transition of the system being carried out.
721 void dpm_resume_start(pm_message_t state)
723 dpm_resume_noirq(state);
724 dpm_resume_early(state);
726 EXPORT_SYMBOL_GPL(dpm_resume_start);
729 * device_resume - Execute "resume" callbacks for given device.
730 * @dev: Device to handle.
731 * @state: PM transition of the system being carried out.
732 * @async: If true, the device is being resumed asynchronously.
734 static int device_resume(struct device *dev, pm_message_t state, bool async)
736 pm_callback_t callback = NULL;
737 char *info = NULL;
738 int error = 0;
739 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
741 TRACE_DEVICE(dev);
742 TRACE_RESUME(0);
744 if (dev->power.syscore)
745 goto Complete;
747 if (dev->power.direct_complete) {
748 /* Match the pm_runtime_disable() in __device_suspend(). */
749 pm_runtime_enable(dev);
750 goto Complete;
753 dpm_wait(dev->parent, async);
754 dpm_watchdog_set(&wd, dev);
755 device_lock(dev);
758 * This is a fib. But we'll allow new children to be added below
759 * a resumed device, even if the device hasn't been completed yet.
761 dev->power.is_prepared = false;
763 if (!dev->power.is_suspended)
764 goto Unlock;
766 if (dev->pm_domain) {
767 info = "power domain ";
768 callback = pm_op(&dev->pm_domain->ops, state);
769 goto Driver;
772 if (dev->type && dev->type->pm) {
773 info = "type ";
774 callback = pm_op(dev->type->pm, state);
775 goto Driver;
778 if (dev->class) {
779 if (dev->class->pm) {
780 info = "class ";
781 callback = pm_op(dev->class->pm, state);
782 goto Driver;
783 } else if (dev->class->resume) {
784 info = "legacy class ";
785 callback = dev->class->resume;
786 goto End;
790 if (dev->bus) {
791 if (dev->bus->pm) {
792 info = "bus ";
793 callback = pm_op(dev->bus->pm, state);
794 } else if (dev->bus->resume) {
795 info = "legacy bus ";
796 callback = dev->bus->resume;
797 goto End;
801 Driver:
802 if (!callback && dev->driver && dev->driver->pm) {
803 info = "driver ";
804 callback = pm_op(dev->driver->pm, state);
807 End:
808 error = dpm_run_callback(callback, dev, state, info);
809 dev->power.is_suspended = false;
811 Unlock:
812 device_unlock(dev);
813 dpm_watchdog_clear(&wd);
815 Complete:
816 complete_all(&dev->power.completion);
818 TRACE_RESUME(error);
820 return error;
823 static void async_resume(void *data, async_cookie_t cookie)
825 struct device *dev = (struct device *)data;
826 int error;
828 error = device_resume(dev, pm_transition, true);
829 if (error)
830 pm_dev_err(dev, pm_transition, " async", error);
831 put_device(dev);
835 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
836 * @state: PM transition of the system being carried out.
838 * Execute the appropriate "resume" callback for all devices whose status
839 * indicates that they are suspended.
841 void dpm_resume(pm_message_t state)
843 struct device *dev;
844 ktime_t starttime = ktime_get();
846 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
847 might_sleep();
849 mutex_lock(&dpm_list_mtx);
850 pm_transition = state;
851 async_error = 0;
853 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
854 reinit_completion(&dev->power.completion);
855 if (is_async(dev)) {
856 get_device(dev);
857 async_schedule(async_resume, dev);
861 while (!list_empty(&dpm_suspended_list)) {
862 dev = to_device(dpm_suspended_list.next);
863 get_device(dev);
864 if (!is_async(dev)) {
865 int error;
867 mutex_unlock(&dpm_list_mtx);
869 error = device_resume(dev, state, false);
870 if (error) {
871 suspend_stats.failed_resume++;
872 dpm_save_failed_step(SUSPEND_RESUME);
873 dpm_save_failed_dev(dev_name(dev));
874 pm_dev_err(dev, state, "", error);
877 mutex_lock(&dpm_list_mtx);
879 if (!list_empty(&dev->power.entry))
880 list_move_tail(&dev->power.entry, &dpm_prepared_list);
881 put_device(dev);
883 mutex_unlock(&dpm_list_mtx);
884 async_synchronize_full();
885 dpm_show_time(starttime, state, NULL);
887 cpufreq_resume();
888 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
892 * device_complete - Complete a PM transition for given device.
893 * @dev: Device to handle.
894 * @state: PM transition of the system being carried out.
896 static void device_complete(struct device *dev, pm_message_t state)
898 void (*callback)(struct device *) = NULL;
899 char *info = NULL;
901 if (dev->power.syscore)
902 return;
904 device_lock(dev);
906 if (dev->pm_domain) {
907 info = "completing power domain ";
908 callback = dev->pm_domain->ops.complete;
909 } else if (dev->type && dev->type->pm) {
910 info = "completing type ";
911 callback = dev->type->pm->complete;
912 } else if (dev->class && dev->class->pm) {
913 info = "completing class ";
914 callback = dev->class->pm->complete;
915 } else if (dev->bus && dev->bus->pm) {
916 info = "completing bus ";
917 callback = dev->bus->pm->complete;
920 if (!callback && dev->driver && dev->driver->pm) {
921 info = "completing driver ";
922 callback = dev->driver->pm->complete;
925 if (callback) {
926 pm_dev_dbg(dev, state, info);
927 callback(dev);
930 device_unlock(dev);
932 pm_runtime_put(dev);
936 * dpm_complete - Complete a PM transition for all non-sysdev devices.
937 * @state: PM transition of the system being carried out.
939 * Execute the ->complete() callbacks for all devices whose PM status is not
940 * DPM_ON (this allows new devices to be registered).
942 void dpm_complete(pm_message_t state)
944 struct list_head list;
946 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
947 might_sleep();
949 INIT_LIST_HEAD(&list);
950 mutex_lock(&dpm_list_mtx);
951 while (!list_empty(&dpm_prepared_list)) {
952 struct device *dev = to_device(dpm_prepared_list.prev);
954 get_device(dev);
955 dev->power.is_prepared = false;
956 list_move(&dev->power.entry, &list);
957 mutex_unlock(&dpm_list_mtx);
959 trace_device_pm_callback_start(dev, "", state.event);
960 device_complete(dev, state);
961 trace_device_pm_callback_end(dev, 0);
963 mutex_lock(&dpm_list_mtx);
964 put_device(dev);
966 list_splice(&list, &dpm_list);
967 mutex_unlock(&dpm_list_mtx);
969 /* Allow device probing and trigger re-probing of deferred devices */
970 device_unblock_probing();
971 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
975 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
976 * @state: PM transition of the system being carried out.
978 * Execute "resume" callbacks for all devices and complete the PM transition of
979 * the system.
981 void dpm_resume_end(pm_message_t state)
983 dpm_resume(state);
984 dpm_complete(state);
986 EXPORT_SYMBOL_GPL(dpm_resume_end);
989 /*------------------------- Suspend routines -------------------------*/
992 * resume_event - Return a "resume" message for given "suspend" sleep state.
993 * @sleep_state: PM message representing a sleep state.
995 * Return a PM message representing the resume event corresponding to given
996 * sleep state.
998 static pm_message_t resume_event(pm_message_t sleep_state)
1000 switch (sleep_state.event) {
1001 case PM_EVENT_SUSPEND:
1002 return PMSG_RESUME;
1003 case PM_EVENT_FREEZE:
1004 case PM_EVENT_QUIESCE:
1005 return PMSG_RECOVER;
1006 case PM_EVENT_HIBERNATE:
1007 return PMSG_RESTORE;
1009 return PMSG_ON;
1013 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1014 * @dev: Device to handle.
1015 * @state: PM transition of the system being carried out.
1016 * @async: If true, the device is being suspended asynchronously.
1018 * The driver of @dev will not receive interrupts while this function is being
1019 * executed.
1021 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1023 pm_callback_t callback = NULL;
1024 char *info = NULL;
1025 int error = 0;
1027 TRACE_DEVICE(dev);
1028 TRACE_SUSPEND(0);
1030 if (async_error)
1031 goto Complete;
1033 if (pm_wakeup_pending()) {
1034 async_error = -EBUSY;
1035 goto Complete;
1038 if (dev->power.syscore || dev->power.direct_complete)
1039 goto Complete;
1041 dpm_wait_for_children(dev, async);
1043 if (dev->pm_domain) {
1044 info = "noirq power domain ";
1045 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1046 } else if (dev->type && dev->type->pm) {
1047 info = "noirq type ";
1048 callback = pm_noirq_op(dev->type->pm, state);
1049 } else if (dev->class && dev->class->pm) {
1050 info = "noirq class ";
1051 callback = pm_noirq_op(dev->class->pm, state);
1052 } else if (dev->bus && dev->bus->pm) {
1053 info = "noirq bus ";
1054 callback = pm_noirq_op(dev->bus->pm, state);
1057 if (!callback && dev->driver && dev->driver->pm) {
1058 info = "noirq driver ";
1059 callback = pm_noirq_op(dev->driver->pm, state);
1062 error = dpm_run_callback(callback, dev, state, info);
1063 if (!error)
1064 dev->power.is_noirq_suspended = true;
1065 else
1066 async_error = error;
1068 Complete:
1069 complete_all(&dev->power.completion);
1070 TRACE_SUSPEND(error);
1071 return error;
1074 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1076 struct device *dev = (struct device *)data;
1077 int error;
1079 error = __device_suspend_noirq(dev, pm_transition, true);
1080 if (error) {
1081 dpm_save_failed_dev(dev_name(dev));
1082 pm_dev_err(dev, pm_transition, " async", error);
1085 put_device(dev);
1088 static int device_suspend_noirq(struct device *dev)
1090 reinit_completion(&dev->power.completion);
1092 if (is_async(dev)) {
1093 get_device(dev);
1094 async_schedule(async_suspend_noirq, dev);
1095 return 0;
1097 return __device_suspend_noirq(dev, pm_transition, false);
1101 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1102 * @state: PM transition of the system being carried out.
1104 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1105 * handlers for all non-sysdev devices.
1107 int dpm_suspend_noirq(pm_message_t state)
1109 ktime_t starttime = ktime_get();
1110 int error = 0;
1112 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1113 cpuidle_pause();
1114 device_wakeup_arm_wake_irqs();
1115 suspend_device_irqs();
1116 mutex_lock(&dpm_list_mtx);
1117 pm_transition = state;
1118 async_error = 0;
1120 while (!list_empty(&dpm_late_early_list)) {
1121 struct device *dev = to_device(dpm_late_early_list.prev);
1123 get_device(dev);
1124 mutex_unlock(&dpm_list_mtx);
1126 error = device_suspend_noirq(dev);
1128 mutex_lock(&dpm_list_mtx);
1129 if (error) {
1130 pm_dev_err(dev, state, " noirq", error);
1131 dpm_save_failed_dev(dev_name(dev));
1132 put_device(dev);
1133 break;
1135 if (!list_empty(&dev->power.entry))
1136 list_move(&dev->power.entry, &dpm_noirq_list);
1137 put_device(dev);
1139 if (async_error)
1140 break;
1142 mutex_unlock(&dpm_list_mtx);
1143 async_synchronize_full();
1144 if (!error)
1145 error = async_error;
1147 if (error) {
1148 suspend_stats.failed_suspend_noirq++;
1149 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1150 dpm_resume_noirq(resume_event(state));
1151 } else {
1152 dpm_show_time(starttime, state, "noirq");
1154 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1155 return error;
1159 * device_suspend_late - Execute a "late suspend" callback for given device.
1160 * @dev: Device to handle.
1161 * @state: PM transition of the system being carried out.
1162 * @async: If true, the device is being suspended asynchronously.
1164 * Runtime PM is disabled for @dev while this function is being executed.
1166 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1168 pm_callback_t callback = NULL;
1169 char *info = NULL;
1170 int error = 0;
1172 TRACE_DEVICE(dev);
1173 TRACE_SUSPEND(0);
1175 __pm_runtime_disable(dev, false);
1177 if (async_error)
1178 goto Complete;
1180 if (pm_wakeup_pending()) {
1181 async_error = -EBUSY;
1182 goto Complete;
1185 if (dev->power.syscore || dev->power.direct_complete)
1186 goto Complete;
1188 dpm_wait_for_children(dev, async);
1190 if (dev->pm_domain) {
1191 info = "late power domain ";
1192 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1193 } else if (dev->type && dev->type->pm) {
1194 info = "late type ";
1195 callback = pm_late_early_op(dev->type->pm, state);
1196 } else if (dev->class && dev->class->pm) {
1197 info = "late class ";
1198 callback = pm_late_early_op(dev->class->pm, state);
1199 } else if (dev->bus && dev->bus->pm) {
1200 info = "late bus ";
1201 callback = pm_late_early_op(dev->bus->pm, state);
1204 if (!callback && dev->driver && dev->driver->pm) {
1205 info = "late driver ";
1206 callback = pm_late_early_op(dev->driver->pm, state);
1209 error = dpm_run_callback(callback, dev, state, info);
1210 if (!error)
1211 dev->power.is_late_suspended = true;
1212 else
1213 async_error = error;
1215 Complete:
1216 TRACE_SUSPEND(error);
1217 complete_all(&dev->power.completion);
1218 return error;
1221 static void async_suspend_late(void *data, async_cookie_t cookie)
1223 struct device *dev = (struct device *)data;
1224 int error;
1226 error = __device_suspend_late(dev, pm_transition, true);
1227 if (error) {
1228 dpm_save_failed_dev(dev_name(dev));
1229 pm_dev_err(dev, pm_transition, " async", error);
1231 put_device(dev);
1234 static int device_suspend_late(struct device *dev)
1236 reinit_completion(&dev->power.completion);
1238 if (is_async(dev)) {
1239 get_device(dev);
1240 async_schedule(async_suspend_late, dev);
1241 return 0;
1244 return __device_suspend_late(dev, pm_transition, false);
1248 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1249 * @state: PM transition of the system being carried out.
1251 int dpm_suspend_late(pm_message_t state)
1253 ktime_t starttime = ktime_get();
1254 int error = 0;
1256 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1257 mutex_lock(&dpm_list_mtx);
1258 pm_transition = state;
1259 async_error = 0;
1261 while (!list_empty(&dpm_suspended_list)) {
1262 struct device *dev = to_device(dpm_suspended_list.prev);
1264 get_device(dev);
1265 mutex_unlock(&dpm_list_mtx);
1267 error = device_suspend_late(dev);
1269 mutex_lock(&dpm_list_mtx);
1270 if (!list_empty(&dev->power.entry))
1271 list_move(&dev->power.entry, &dpm_late_early_list);
1273 if (error) {
1274 pm_dev_err(dev, state, " late", error);
1275 dpm_save_failed_dev(dev_name(dev));
1276 put_device(dev);
1277 break;
1279 put_device(dev);
1281 if (async_error)
1282 break;
1284 mutex_unlock(&dpm_list_mtx);
1285 async_synchronize_full();
1286 if (!error)
1287 error = async_error;
1288 if (error) {
1289 suspend_stats.failed_suspend_late++;
1290 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1291 dpm_resume_early(resume_event(state));
1292 } else {
1293 dpm_show_time(starttime, state, "late");
1295 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1296 return error;
1300 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1301 * @state: PM transition of the system being carried out.
1303 int dpm_suspend_end(pm_message_t state)
1305 int error = dpm_suspend_late(state);
1306 if (error)
1307 return error;
1309 error = dpm_suspend_noirq(state);
1310 if (error) {
1311 dpm_resume_early(resume_event(state));
1312 return error;
1315 return 0;
1317 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1320 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1321 * @dev: Device to suspend.
1322 * @state: PM transition of the system being carried out.
1323 * @cb: Suspend callback to execute.
1324 * @info: string description of caller.
1326 static int legacy_suspend(struct device *dev, pm_message_t state,
1327 int (*cb)(struct device *dev, pm_message_t state),
1328 char *info)
1330 int error;
1331 ktime_t calltime;
1333 calltime = initcall_debug_start(dev);
1335 trace_device_pm_callback_start(dev, info, state.event);
1336 error = cb(dev, state);
1337 trace_device_pm_callback_end(dev, error);
1338 suspend_report_result(cb, error);
1340 initcall_debug_report(dev, calltime, error, state, info);
1342 return error;
1346 * device_suspend - Execute "suspend" callbacks for given device.
1347 * @dev: Device to handle.
1348 * @state: PM transition of the system being carried out.
1349 * @async: If true, the device is being suspended asynchronously.
1351 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1353 pm_callback_t callback = NULL;
1354 char *info = NULL;
1355 int error = 0;
1356 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1358 TRACE_DEVICE(dev);
1359 TRACE_SUSPEND(0);
1361 dpm_wait_for_children(dev, async);
1363 if (async_error)
1364 goto Complete;
1367 * If a device configured to wake up the system from sleep states
1368 * has been suspended at run time and there's a resume request pending
1369 * for it, this is equivalent to the device signaling wakeup, so the
1370 * system suspend operation should be aborted.
1372 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1373 pm_wakeup_event(dev, 0);
1375 if (pm_wakeup_pending()) {
1376 async_error = -EBUSY;
1377 goto Complete;
1380 if (dev->power.syscore)
1381 goto Complete;
1383 if (dev->power.direct_complete) {
1384 if (pm_runtime_status_suspended(dev)) {
1385 pm_runtime_disable(dev);
1386 if (pm_runtime_status_suspended(dev))
1387 goto Complete;
1389 pm_runtime_enable(dev);
1391 dev->power.direct_complete = false;
1394 dpm_watchdog_set(&wd, dev);
1395 device_lock(dev);
1397 if (dev->pm_domain) {
1398 info = "power domain ";
1399 callback = pm_op(&dev->pm_domain->ops, state);
1400 goto Run;
1403 if (dev->type && dev->type->pm) {
1404 info = "type ";
1405 callback = pm_op(dev->type->pm, state);
1406 goto Run;
1409 if (dev->class) {
1410 if (dev->class->pm) {
1411 info = "class ";
1412 callback = pm_op(dev->class->pm, state);
1413 goto Run;
1414 } else if (dev->class->suspend) {
1415 pm_dev_dbg(dev, state, "legacy class ");
1416 error = legacy_suspend(dev, state, dev->class->suspend,
1417 "legacy class ");
1418 goto End;
1422 if (dev->bus) {
1423 if (dev->bus->pm) {
1424 info = "bus ";
1425 callback = pm_op(dev->bus->pm, state);
1426 } else if (dev->bus->suspend) {
1427 pm_dev_dbg(dev, state, "legacy bus ");
1428 error = legacy_suspend(dev, state, dev->bus->suspend,
1429 "legacy bus ");
1430 goto End;
1434 Run:
1435 if (!callback && dev->driver && dev->driver->pm) {
1436 info = "driver ";
1437 callback = pm_op(dev->driver->pm, state);
1440 error = dpm_run_callback(callback, dev, state, info);
1442 End:
1443 if (!error) {
1444 struct device *parent = dev->parent;
1446 dev->power.is_suspended = true;
1447 if (parent) {
1448 spin_lock_irq(&parent->power.lock);
1450 dev->parent->power.direct_complete = false;
1451 if (dev->power.wakeup_path
1452 && !dev->parent->power.ignore_children)
1453 dev->parent->power.wakeup_path = true;
1455 spin_unlock_irq(&parent->power.lock);
1459 device_unlock(dev);
1460 dpm_watchdog_clear(&wd);
1462 Complete:
1463 complete_all(&dev->power.completion);
1464 if (error)
1465 async_error = error;
1467 TRACE_SUSPEND(error);
1468 return error;
1471 static void async_suspend(void *data, async_cookie_t cookie)
1473 struct device *dev = (struct device *)data;
1474 int error;
1476 error = __device_suspend(dev, pm_transition, true);
1477 if (error) {
1478 dpm_save_failed_dev(dev_name(dev));
1479 pm_dev_err(dev, pm_transition, " async", error);
1482 put_device(dev);
1485 static int device_suspend(struct device *dev)
1487 reinit_completion(&dev->power.completion);
1489 if (is_async(dev)) {
1490 get_device(dev);
1491 async_schedule(async_suspend, dev);
1492 return 0;
1495 return __device_suspend(dev, pm_transition, false);
1499 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1500 * @state: PM transition of the system being carried out.
1502 int dpm_suspend(pm_message_t state)
1504 ktime_t starttime = ktime_get();
1505 int error = 0;
1507 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1508 might_sleep();
1510 cpufreq_suspend();
1512 mutex_lock(&dpm_list_mtx);
1513 pm_transition = state;
1514 async_error = 0;
1515 while (!list_empty(&dpm_prepared_list)) {
1516 struct device *dev = to_device(dpm_prepared_list.prev);
1518 get_device(dev);
1519 mutex_unlock(&dpm_list_mtx);
1521 error = device_suspend(dev);
1523 mutex_lock(&dpm_list_mtx);
1524 if (error) {
1525 pm_dev_err(dev, state, "", error);
1526 dpm_save_failed_dev(dev_name(dev));
1527 put_device(dev);
1528 break;
1530 if (!list_empty(&dev->power.entry))
1531 list_move(&dev->power.entry, &dpm_suspended_list);
1532 put_device(dev);
1533 if (async_error)
1534 break;
1536 mutex_unlock(&dpm_list_mtx);
1537 async_synchronize_full();
1538 if (!error)
1539 error = async_error;
1540 if (error) {
1541 suspend_stats.failed_suspend++;
1542 dpm_save_failed_step(SUSPEND_SUSPEND);
1543 } else
1544 dpm_show_time(starttime, state, NULL);
1545 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1546 return error;
1550 * device_prepare - Prepare a device for system power transition.
1551 * @dev: Device to handle.
1552 * @state: PM transition of the system being carried out.
1554 * Execute the ->prepare() callback(s) for given device. No new children of the
1555 * device may be registered after this function has returned.
1557 static int device_prepare(struct device *dev, pm_message_t state)
1559 int (*callback)(struct device *) = NULL;
1560 char *info = NULL;
1561 int ret = 0;
1563 if (dev->power.syscore)
1564 return 0;
1567 * If a device's parent goes into runtime suspend at the wrong time,
1568 * it won't be possible to resume the device. To prevent this we
1569 * block runtime suspend here, during the prepare phase, and allow
1570 * it again during the complete phase.
1572 pm_runtime_get_noresume(dev);
1574 device_lock(dev);
1576 dev->power.wakeup_path = device_may_wakeup(dev);
1578 if (dev->power.no_pm_callbacks) {
1579 ret = 1; /* Let device go direct_complete */
1580 goto unlock;
1583 if (dev->pm_domain) {
1584 info = "preparing power domain ";
1585 callback = dev->pm_domain->ops.prepare;
1586 } else if (dev->type && dev->type->pm) {
1587 info = "preparing type ";
1588 callback = dev->type->pm->prepare;
1589 } else if (dev->class && dev->class->pm) {
1590 info = "preparing class ";
1591 callback = dev->class->pm->prepare;
1592 } else if (dev->bus && dev->bus->pm) {
1593 info = "preparing bus ";
1594 callback = dev->bus->pm->prepare;
1597 if (!callback && dev->driver && dev->driver->pm) {
1598 info = "preparing driver ";
1599 callback = dev->driver->pm->prepare;
1602 if (callback)
1603 ret = callback(dev);
1605 unlock:
1606 device_unlock(dev);
1608 if (ret < 0) {
1609 suspend_report_result(callback, ret);
1610 pm_runtime_put(dev);
1611 return ret;
1614 * A positive return value from ->prepare() means "this device appears
1615 * to be runtime-suspended and its state is fine, so if it really is
1616 * runtime-suspended, you can leave it in that state provided that you
1617 * will do the same thing with all of its descendants". This only
1618 * applies to suspend transitions, however.
1620 spin_lock_irq(&dev->power.lock);
1621 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1622 spin_unlock_irq(&dev->power.lock);
1623 return 0;
1627 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1628 * @state: PM transition of the system being carried out.
1630 * Execute the ->prepare() callback(s) for all devices.
1632 int dpm_prepare(pm_message_t state)
1634 int error = 0;
1636 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1637 might_sleep();
1640 * Give a chance for the known devices to complete their probes, before
1641 * disable probing of devices. This sync point is important at least
1642 * at boot time + hibernation restore.
1644 wait_for_device_probe();
1646 * It is unsafe if probing of devices will happen during suspend or
1647 * hibernation and system behavior will be unpredictable in this case.
1648 * So, let's prohibit device's probing here and defer their probes
1649 * instead. The normal behavior will be restored in dpm_complete().
1651 device_block_probing();
1653 mutex_lock(&dpm_list_mtx);
1654 while (!list_empty(&dpm_list)) {
1655 struct device *dev = to_device(dpm_list.next);
1657 get_device(dev);
1658 mutex_unlock(&dpm_list_mtx);
1660 trace_device_pm_callback_start(dev, "", state.event);
1661 error = device_prepare(dev, state);
1662 trace_device_pm_callback_end(dev, error);
1664 mutex_lock(&dpm_list_mtx);
1665 if (error) {
1666 if (error == -EAGAIN) {
1667 put_device(dev);
1668 error = 0;
1669 continue;
1671 printk(KERN_INFO "PM: Device %s not prepared "
1672 "for power transition: code %d\n",
1673 dev_name(dev), error);
1674 put_device(dev);
1675 break;
1677 dev->power.is_prepared = true;
1678 if (!list_empty(&dev->power.entry))
1679 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1680 put_device(dev);
1682 mutex_unlock(&dpm_list_mtx);
1683 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
1684 return error;
1688 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1689 * @state: PM transition of the system being carried out.
1691 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1692 * callbacks for them.
1694 int dpm_suspend_start(pm_message_t state)
1696 int error;
1698 error = dpm_prepare(state);
1699 if (error) {
1700 suspend_stats.failed_prepare++;
1701 dpm_save_failed_step(SUSPEND_PREPARE);
1702 } else
1703 error = dpm_suspend(state);
1704 return error;
1706 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1708 void __suspend_report_result(const char *function, void *fn, int ret)
1710 if (ret)
1711 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1713 EXPORT_SYMBOL_GPL(__suspend_report_result);
1716 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1717 * @dev: Device to wait for.
1718 * @subordinate: Device that needs to wait for @dev.
1720 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1722 dpm_wait(dev, subordinate->power.async_suspend);
1723 return async_error;
1725 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1728 * dpm_for_each_dev - device iterator.
1729 * @data: data for the callback.
1730 * @fn: function to be called for each device.
1732 * Iterate over devices in dpm_list, and call @fn for each device,
1733 * passing it @data.
1735 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1737 struct device *dev;
1739 if (!fn)
1740 return;
1742 device_pm_lock();
1743 list_for_each_entry(dev, &dpm_list, power.entry)
1744 fn(dev, data);
1745 device_pm_unlock();
1747 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
1749 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1751 if (!ops)
1752 return true;
1754 return !ops->prepare &&
1755 !ops->suspend &&
1756 !ops->suspend_late &&
1757 !ops->suspend_noirq &&
1758 !ops->resume_noirq &&
1759 !ops->resume_early &&
1760 !ops->resume &&
1761 !ops->complete;
1764 void device_pm_check_callbacks(struct device *dev)
1766 spin_lock_irq(&dev->power.lock);
1767 dev->power.no_pm_callbacks =
1768 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1769 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1770 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1771 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1772 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1773 spin_unlock_irq(&dev->power.lock);