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/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>
36 #include <linux/wakeup_reason.h>
41 typedef int (*pm_callback_t)(struct device *);
44 * The entries in the dpm_list list are in a depth first order, simply
45 * because children are guaranteed to be discovered after parents, and
46 * are inserted at the back of the list on discovery.
48 * Since device_pm_add() may be called with a device lock held,
49 * we must never try to acquire a device lock while holding
54 static LIST_HEAD(dpm_prepared_list);
55 static LIST_HEAD(dpm_suspended_list);
56 static LIST_HEAD(dpm_late_early_list);
57 static LIST_HEAD(dpm_noirq_list);
59 struct suspend_stats suspend_stats;
60 static DEFINE_MUTEX(dpm_list_mtx);
61 static pm_message_t pm_transition;
63 static void dpm_drv_timeout(unsigned long data);
64 struct dpm_drv_wd_data {
66 struct task_struct *tsk;
69 static int async_error;
71 static char *pm_verb(int event)
74 case PM_EVENT_SUSPEND:
80 case PM_EVENT_QUIESCE:
82 case PM_EVENT_HIBERNATE:
86 case PM_EVENT_RESTORE:
88 case PM_EVENT_RECOVER:
91 return "(unknown PM event)";
96 * device_pm_sleep_init - Initialize system suspend-related device fields.
97 * @dev: Device object being initialized.
99 void device_pm_sleep_init(struct device *dev)
101 dev->power.is_prepared = false;
102 dev->power.is_suspended = false;
103 dev->power.is_noirq_suspended = false;
104 dev->power.is_late_suspended = false;
105 init_completion(&dev->power.completion);
106 complete_all(&dev->power.completion);
107 dev->power.wakeup = NULL;
108 INIT_LIST_HEAD(&dev->power.entry);
112 * device_pm_lock - Lock the list of active devices used by the PM core.
114 void device_pm_lock(void)
116 mutex_lock(&dpm_list_mtx);
120 * device_pm_unlock - Unlock the list of active devices used by the PM core.
122 void device_pm_unlock(void)
124 mutex_unlock(&dpm_list_mtx);
128 * device_pm_add - Add a device to the PM core's list of active devices.
129 * @dev: Device to add to the list.
131 void device_pm_add(struct device *dev)
133 pr_debug("PM: Adding info for %s:%s\n",
134 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
135 mutex_lock(&dpm_list_mtx);
136 if (dev->parent && dev->parent->power.is_prepared)
137 dev_warn(dev, "parent %s should not be sleeping\n",
138 dev_name(dev->parent));
139 list_add_tail(&dev->power.entry, &dpm_list);
140 mutex_unlock(&dpm_list_mtx);
144 * device_pm_remove - Remove a device from the PM core's list of active devices.
145 * @dev: Device to be removed from the list.
147 void device_pm_remove(struct device *dev)
149 pr_debug("PM: Removing info for %s:%s\n",
150 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
151 complete_all(&dev->power.completion);
152 mutex_lock(&dpm_list_mtx);
153 list_del_init(&dev->power.entry);
154 mutex_unlock(&dpm_list_mtx);
155 device_wakeup_disable(dev);
156 pm_runtime_remove(dev);
160 * device_pm_move_before - Move device in the PM core's list of active devices.
161 * @deva: Device to move in dpm_list.
162 * @devb: Device @deva should come before.
164 void device_pm_move_before(struct device *deva, struct device *devb)
166 pr_debug("PM: Moving %s:%s before %s:%s\n",
167 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
168 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
169 /* Delete deva from dpm_list and reinsert before devb. */
170 list_move_tail(&deva->power.entry, &devb->power.entry);
174 * device_pm_move_after - Move device in the PM core's list of active devices.
175 * @deva: Device to move in dpm_list.
176 * @devb: Device @deva should come after.
178 void device_pm_move_after(struct device *deva, struct device *devb)
180 pr_debug("PM: Moving %s:%s after %s:%s\n",
181 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
182 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
183 /* Delete deva from dpm_list and reinsert after devb. */
184 list_move(&deva->power.entry, &devb->power.entry);
188 * device_pm_move_last - Move device to end of the PM core's list of devices.
189 * @dev: Device to move in dpm_list.
191 void device_pm_move_last(struct device *dev)
193 pr_debug("PM: Moving %s:%s to end of list\n",
194 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
195 list_move_tail(&dev->power.entry, &dpm_list);
198 static ktime_t initcall_debug_start(struct device *dev)
200 ktime_t calltime = ktime_set(0, 0);
202 if (pm_print_times_enabled) {
203 pr_info("calling %s+ @ %i, parent: %s\n",
204 dev_name(dev), task_pid_nr(current),
205 dev->parent ? dev_name(dev->parent) : "none");
206 calltime = ktime_get();
212 static void initcall_debug_report(struct device *dev, ktime_t calltime,
213 int error, pm_message_t state, char *info)
218 rettime = ktime_get();
219 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
221 if (pm_print_times_enabled) {
222 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
223 error, (unsigned long long)nsecs >> 10);
228 * dpm_wait - Wait for a PM operation to complete.
229 * @dev: Device to wait for.
230 * @async: If unset, wait only if the device's power.async_suspend flag is set.
232 static void dpm_wait(struct device *dev, bool async)
237 if (async || (pm_async_enabled && dev->power.async_suspend))
238 wait_for_completion(&dev->power.completion);
241 static int dpm_wait_fn(struct device *dev, void *async_ptr)
243 dpm_wait(dev, *((bool *)async_ptr));
247 static void dpm_wait_for_children(struct device *dev, bool async)
249 device_for_each_child(dev, &async, dpm_wait_fn);
253 * pm_op - Return the PM operation appropriate for given PM event.
254 * @ops: PM operations to choose from.
255 * @state: PM transition of the system being carried out.
257 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
259 switch (state.event) {
260 #ifdef CONFIG_SUSPEND
261 case PM_EVENT_SUSPEND:
263 case PM_EVENT_RESUME:
265 #endif /* CONFIG_SUSPEND */
266 #ifdef CONFIG_HIBERNATE_CALLBACKS
267 case PM_EVENT_FREEZE:
268 case PM_EVENT_QUIESCE:
270 case PM_EVENT_HIBERNATE:
271 return ops->poweroff;
273 case PM_EVENT_RECOVER:
276 case PM_EVENT_RESTORE:
278 #endif /* CONFIG_HIBERNATE_CALLBACKS */
285 * pm_late_early_op - Return the PM operation appropriate for given PM event.
286 * @ops: PM operations to choose from.
287 * @state: PM transition of the system being carried out.
289 * Runtime PM is disabled for @dev while this function is being executed.
291 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
294 switch (state.event) {
295 #ifdef CONFIG_SUSPEND
296 case PM_EVENT_SUSPEND:
297 return ops->suspend_late;
298 case PM_EVENT_RESUME:
299 return ops->resume_early;
300 #endif /* CONFIG_SUSPEND */
301 #ifdef CONFIG_HIBERNATE_CALLBACKS
302 case PM_EVENT_FREEZE:
303 case PM_EVENT_QUIESCE:
304 return ops->freeze_late;
305 case PM_EVENT_HIBERNATE:
306 return ops->poweroff_late;
308 case PM_EVENT_RECOVER:
309 return ops->thaw_early;
310 case PM_EVENT_RESTORE:
311 return ops->restore_early;
312 #endif /* CONFIG_HIBERNATE_CALLBACKS */
319 * pm_noirq_op - Return the PM operation appropriate for given PM event.
320 * @ops: PM operations to choose from.
321 * @state: PM transition of the system being carried out.
323 * The driver of @dev will not receive interrupts while this function is being
326 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
328 switch (state.event) {
329 #ifdef CONFIG_SUSPEND
330 case PM_EVENT_SUSPEND:
331 return ops->suspend_noirq;
332 case PM_EVENT_RESUME:
333 return ops->resume_noirq;
334 #endif /* CONFIG_SUSPEND */
335 #ifdef CONFIG_HIBERNATE_CALLBACKS
336 case PM_EVENT_FREEZE:
337 case PM_EVENT_QUIESCE:
338 return ops->freeze_noirq;
339 case PM_EVENT_HIBERNATE:
340 return ops->poweroff_noirq;
342 case PM_EVENT_RECOVER:
343 return ops->thaw_noirq;
344 case PM_EVENT_RESTORE:
345 return ops->restore_noirq;
346 #endif /* CONFIG_HIBERNATE_CALLBACKS */
352 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
354 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
355 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
356 ", may wakeup" : "");
359 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
362 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
363 dev_name(dev), pm_verb(state.event), info, error);
366 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
372 calltime = ktime_get();
373 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
374 do_div(usecs64, NSEC_PER_USEC);
378 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
379 info ?: "", info ? " " : "", pm_verb(state.event),
380 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
383 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
384 pm_message_t state, char *info)
392 calltime = initcall_debug_start(dev);
394 pm_dev_dbg(dev, state, info);
395 trace_device_pm_callback_start(dev, info, state.event);
397 trace_device_pm_callback_end(dev, error);
398 suspend_report_result(cb, error);
400 initcall_debug_report(dev, calltime, error, state, info);
405 #ifdef CONFIG_DPM_WATCHDOG
406 struct dpm_watchdog {
408 struct task_struct *tsk;
409 struct timer_list timer;
412 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
413 struct dpm_watchdog wd
416 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
417 * @data: Watchdog object address.
419 * Called when a driver has timed out suspending or resuming.
420 * There's not much we can do here to recover so panic() to
421 * capture a crash-dump in pstore.
423 static void dpm_watchdog_handler(unsigned long data)
425 struct dpm_watchdog *wd = (void *)data;
427 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
428 show_stack(wd->tsk, NULL);
429 panic("%s %s: unrecoverable failure\n",
430 dev_driver_string(wd->dev), dev_name(wd->dev));
434 * dpm_watchdog_set - Enable pm watchdog for given device.
435 * @wd: Watchdog. Must be allocated on the stack.
436 * @dev: Device to handle.
438 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
440 struct timer_list *timer = &wd->timer;
445 init_timer_on_stack(timer);
446 /* use same timeout value for both suspend and resume */
447 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
448 timer->function = dpm_watchdog_handler;
449 timer->data = (unsigned long)wd;
454 * dpm_watchdog_clear - Disable suspend/resume watchdog.
455 * @wd: Watchdog to disable.
457 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
459 struct timer_list *timer = &wd->timer;
461 del_timer_sync(timer);
462 destroy_timer_on_stack(timer);
465 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
466 #define dpm_watchdog_set(x, y)
467 #define dpm_watchdog_clear(x)
470 /*------------------------- Resume routines -------------------------*/
473 * device_resume_noirq - Execute an "early resume" callback for given device.
474 * @dev: Device to handle.
475 * @state: PM transition of the system being carried out.
476 * @async: If true, the device is being resumed asynchronously.
478 * The driver of @dev will not receive interrupts while this function is being
481 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
483 pm_callback_t callback = NULL;
490 if (dev->power.syscore || dev->power.direct_complete)
493 if (!dev->power.is_noirq_suspended)
496 dpm_wait(dev->parent, async);
498 if (dev->pm_domain) {
499 info = "noirq power domain ";
500 callback = pm_noirq_op(&dev->pm_domain->ops, state);
501 } else if (dev->type && dev->type->pm) {
502 info = "noirq type ";
503 callback = pm_noirq_op(dev->type->pm, state);
504 } else if (dev->class && dev->class->pm) {
505 info = "noirq class ";
506 callback = pm_noirq_op(dev->class->pm, state);
507 } else if (dev->bus && dev->bus->pm) {
509 callback = pm_noirq_op(dev->bus->pm, state);
512 if (!callback && dev->driver && dev->driver->pm) {
513 info = "noirq driver ";
514 callback = pm_noirq_op(dev->driver->pm, state);
517 error = dpm_run_callback(callback, dev, state, info);
518 dev->power.is_noirq_suspended = false;
521 complete_all(&dev->power.completion);
526 static bool is_async(struct device *dev)
528 return dev->power.async_suspend && pm_async_enabled
529 && !pm_trace_is_enabled();
532 static void async_resume_noirq(void *data, async_cookie_t cookie)
534 struct device *dev = (struct device *)data;
537 error = device_resume_noirq(dev, pm_transition, true);
539 pm_dev_err(dev, pm_transition, " async", error);
545 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
546 * @state: PM transition of the system being carried out.
548 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
549 * enable device drivers to receive interrupts.
551 void dpm_resume_noirq(pm_message_t state)
554 ktime_t starttime = ktime_get();
556 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
557 mutex_lock(&dpm_list_mtx);
558 pm_transition = state;
561 * Advanced the async threads upfront,
562 * in case the starting of async threads is
563 * delayed by non-async resuming devices.
565 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
566 reinit_completion(&dev->power.completion);
569 async_schedule(async_resume_noirq, dev);
573 while (!list_empty(&dpm_noirq_list)) {
574 dev = to_device(dpm_noirq_list.next);
576 list_move_tail(&dev->power.entry, &dpm_late_early_list);
577 mutex_unlock(&dpm_list_mtx);
579 if (!is_async(dev)) {
582 error = device_resume_noirq(dev, state, false);
584 suspend_stats.failed_resume_noirq++;
585 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
586 dpm_save_failed_dev(dev_name(dev));
587 pm_dev_err(dev, state, " noirq", error);
591 mutex_lock(&dpm_list_mtx);
594 mutex_unlock(&dpm_list_mtx);
595 async_synchronize_full();
596 dpm_show_time(starttime, state, "noirq");
597 resume_device_irqs();
598 device_wakeup_disarm_wake_irqs();
600 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
604 * device_resume_early - Execute an "early resume" callback for given device.
605 * @dev: Device to handle.
606 * @state: PM transition of the system being carried out.
607 * @async: If true, the device is being resumed asynchronously.
609 * Runtime PM is disabled for @dev while this function is being executed.
611 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
613 pm_callback_t callback = NULL;
620 if (dev->power.syscore || dev->power.direct_complete)
623 if (!dev->power.is_late_suspended)
626 dpm_wait(dev->parent, async);
628 if (dev->pm_domain) {
629 info = "early power domain ";
630 callback = pm_late_early_op(&dev->pm_domain->ops, state);
631 } else if (dev->type && dev->type->pm) {
632 info = "early type ";
633 callback = pm_late_early_op(dev->type->pm, state);
634 } else if (dev->class && dev->class->pm) {
635 info = "early class ";
636 callback = pm_late_early_op(dev->class->pm, state);
637 } else if (dev->bus && dev->bus->pm) {
639 callback = pm_late_early_op(dev->bus->pm, state);
642 if (!callback && dev->driver && dev->driver->pm) {
643 info = "early driver ";
644 callback = pm_late_early_op(dev->driver->pm, state);
647 error = dpm_run_callback(callback, dev, state, info);
648 dev->power.is_late_suspended = false;
653 pm_runtime_enable(dev);
654 complete_all(&dev->power.completion);
658 static void async_resume_early(void *data, async_cookie_t cookie)
660 struct device *dev = (struct device *)data;
663 error = device_resume_early(dev, pm_transition, true);
665 pm_dev_err(dev, pm_transition, " async", error);
671 * dpm_resume_early - Execute "early resume" callbacks for all devices.
672 * @state: PM transition of the system being carried out.
674 void dpm_resume_early(pm_message_t state)
677 ktime_t starttime = ktime_get();
679 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
680 mutex_lock(&dpm_list_mtx);
681 pm_transition = state;
684 * Advanced the async threads upfront,
685 * in case the starting of async threads is
686 * delayed by non-async resuming devices.
688 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
689 reinit_completion(&dev->power.completion);
692 async_schedule(async_resume_early, dev);
696 while (!list_empty(&dpm_late_early_list)) {
697 dev = to_device(dpm_late_early_list.next);
699 list_move_tail(&dev->power.entry, &dpm_suspended_list);
700 mutex_unlock(&dpm_list_mtx);
702 if (!is_async(dev)) {
705 error = device_resume_early(dev, state, false);
707 suspend_stats.failed_resume_early++;
708 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
709 dpm_save_failed_dev(dev_name(dev));
710 pm_dev_err(dev, state, " early", error);
713 mutex_lock(&dpm_list_mtx);
716 mutex_unlock(&dpm_list_mtx);
717 async_synchronize_full();
718 dpm_show_time(starttime, state, "early");
719 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
723 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
724 * @state: PM transition of the system being carried out.
726 void dpm_resume_start(pm_message_t state)
728 dpm_resume_noirq(state);
729 dpm_resume_early(state);
731 EXPORT_SYMBOL_GPL(dpm_resume_start);
734 * device_resume - Execute "resume" callbacks for given device.
735 * @dev: Device to handle.
736 * @state: PM transition of the system being carried out.
737 * @async: If true, the device is being resumed asynchronously.
739 static int device_resume(struct device *dev, pm_message_t state, bool async)
741 pm_callback_t callback = NULL;
744 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
749 if (dev->power.syscore)
752 if (dev->power.direct_complete) {
753 /* Match the pm_runtime_disable() in __device_suspend(). */
754 pm_runtime_enable(dev);
758 dpm_wait(dev->parent, async);
759 dpm_watchdog_set(&wd, dev);
763 * This is a fib. But we'll allow new children to be added below
764 * a resumed device, even if the device hasn't been completed yet.
766 dev->power.is_prepared = false;
768 if (!dev->power.is_suspended)
771 if (dev->pm_domain) {
772 info = "power domain ";
773 callback = pm_op(&dev->pm_domain->ops, state);
777 if (dev->type && dev->type->pm) {
779 callback = pm_op(dev->type->pm, state);
784 if (dev->class->pm) {
786 callback = pm_op(dev->class->pm, state);
788 } else if (dev->class->resume) {
789 info = "legacy class ";
790 callback = dev->class->resume;
798 callback = pm_op(dev->bus->pm, state);
799 } else if (dev->bus->resume) {
800 info = "legacy bus ";
801 callback = dev->bus->resume;
807 if (!callback && dev->driver && dev->driver->pm) {
809 callback = pm_op(dev->driver->pm, state);
813 error = dpm_run_callback(callback, dev, state, info);
814 dev->power.is_suspended = false;
818 dpm_watchdog_clear(&wd);
821 complete_all(&dev->power.completion);
828 static void async_resume(void *data, async_cookie_t cookie)
830 struct device *dev = (struct device *)data;
833 error = device_resume(dev, pm_transition, true);
835 pm_dev_err(dev, pm_transition, " async", error);
840 * dpm_drv_timeout - Driver suspend / resume watchdog handler
841 * @data: struct device which timed out
843 * Called when a driver has timed out suspending or resuming.
844 * There's not much we can do here to recover so
845 * BUG() out for a crash-dump
848 static void dpm_drv_timeout(unsigned long data)
850 struct dpm_drv_wd_data *wd_data = (void *)data;
851 struct device *dev = wd_data->dev;
852 struct task_struct *tsk = wd_data->tsk;
854 printk(KERN_EMERG "**** DPM device timeout: %s (%s)\n", dev_name(dev),
855 (dev->driver ? dev->driver->name : "no driver"));
857 printk(KERN_EMERG "dpm suspend stack:\n");
858 show_stack(tsk, NULL);
864 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
865 * @state: PM transition of the system being carried out.
867 * Execute the appropriate "resume" callback for all devices whose status
868 * indicates that they are suspended.
870 void dpm_resume(pm_message_t state)
873 ktime_t starttime = ktime_get();
875 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
878 mutex_lock(&dpm_list_mtx);
879 pm_transition = state;
882 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
883 reinit_completion(&dev->power.completion);
886 async_schedule(async_resume, dev);
890 while (!list_empty(&dpm_suspended_list)) {
891 dev = to_device(dpm_suspended_list.next);
893 if (!is_async(dev)) {
896 mutex_unlock(&dpm_list_mtx);
898 error = device_resume(dev, state, false);
900 suspend_stats.failed_resume++;
901 dpm_save_failed_step(SUSPEND_RESUME);
902 dpm_save_failed_dev(dev_name(dev));
903 pm_dev_err(dev, state, "", error);
906 mutex_lock(&dpm_list_mtx);
908 if (!list_empty(&dev->power.entry))
909 list_move_tail(&dev->power.entry, &dpm_prepared_list);
912 mutex_unlock(&dpm_list_mtx);
913 async_synchronize_full();
914 dpm_show_time(starttime, state, NULL);
917 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
921 * device_complete - Complete a PM transition for given device.
922 * @dev: Device to handle.
923 * @state: PM transition of the system being carried out.
925 static void device_complete(struct device *dev, pm_message_t state)
927 void (*callback)(struct device *) = NULL;
930 if (dev->power.syscore)
935 if (dev->pm_domain) {
936 info = "completing power domain ";
937 callback = dev->pm_domain->ops.complete;
938 } else if (dev->type && dev->type->pm) {
939 info = "completing type ";
940 callback = dev->type->pm->complete;
941 } else if (dev->class && dev->class->pm) {
942 info = "completing class ";
943 callback = dev->class->pm->complete;
944 } else if (dev->bus && dev->bus->pm) {
945 info = "completing bus ";
946 callback = dev->bus->pm->complete;
949 if (!callback && dev->driver && dev->driver->pm) {
950 info = "completing driver ";
951 callback = dev->driver->pm->complete;
955 pm_dev_dbg(dev, state, info);
965 * dpm_complete - Complete a PM transition for all non-sysdev devices.
966 * @state: PM transition of the system being carried out.
968 * Execute the ->complete() callbacks for all devices whose PM status is not
969 * DPM_ON (this allows new devices to be registered).
971 void dpm_complete(pm_message_t state)
973 struct list_head list;
975 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
978 INIT_LIST_HEAD(&list);
979 mutex_lock(&dpm_list_mtx);
980 while (!list_empty(&dpm_prepared_list)) {
981 struct device *dev = to_device(dpm_prepared_list.prev);
984 dev->power.is_prepared = false;
985 list_move(&dev->power.entry, &list);
986 mutex_unlock(&dpm_list_mtx);
988 trace_device_pm_callback_start(dev, "", state.event);
989 device_complete(dev, state);
990 trace_device_pm_callback_end(dev, 0);
992 mutex_lock(&dpm_list_mtx);
995 list_splice(&list, &dpm_list);
996 mutex_unlock(&dpm_list_mtx);
997 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1001 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1002 * @state: PM transition of the system being carried out.
1004 * Execute "resume" callbacks for all devices and complete the PM transition of
1007 void dpm_resume_end(pm_message_t state)
1010 dpm_complete(state);
1012 EXPORT_SYMBOL_GPL(dpm_resume_end);
1015 /*------------------------- Suspend routines -------------------------*/
1018 * resume_event - Return a "resume" message for given "suspend" sleep state.
1019 * @sleep_state: PM message representing a sleep state.
1021 * Return a PM message representing the resume event corresponding to given
1024 static pm_message_t resume_event(pm_message_t sleep_state)
1026 switch (sleep_state.event) {
1027 case PM_EVENT_SUSPEND:
1029 case PM_EVENT_FREEZE:
1030 case PM_EVENT_QUIESCE:
1031 return PMSG_RECOVER;
1032 case PM_EVENT_HIBERNATE:
1033 return PMSG_RESTORE;
1039 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1040 * @dev: Device to handle.
1041 * @state: PM transition of the system being carried out.
1042 * @async: If true, the device is being suspended asynchronously.
1044 * The driver of @dev will not receive interrupts while this function is being
1047 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1049 pm_callback_t callback = NULL;
1059 if (pm_wakeup_pending()) {
1060 async_error = -EBUSY;
1064 if (dev->power.syscore || dev->power.direct_complete)
1067 dpm_wait_for_children(dev, async);
1069 if (dev->pm_domain) {
1070 info = "noirq power domain ";
1071 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1072 } else if (dev->type && dev->type->pm) {
1073 info = "noirq type ";
1074 callback = pm_noirq_op(dev->type->pm, state);
1075 } else if (dev->class && dev->class->pm) {
1076 info = "noirq class ";
1077 callback = pm_noirq_op(dev->class->pm, state);
1078 } else if (dev->bus && dev->bus->pm) {
1079 info = "noirq bus ";
1080 callback = pm_noirq_op(dev->bus->pm, state);
1083 if (!callback && dev->driver && dev->driver->pm) {
1084 info = "noirq driver ";
1085 callback = pm_noirq_op(dev->driver->pm, state);
1088 error = dpm_run_callback(callback, dev, state, info);
1090 dev->power.is_noirq_suspended = true;
1092 async_error = error;
1095 complete_all(&dev->power.completion);
1096 TRACE_SUSPEND(error);
1100 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1102 struct device *dev = (struct device *)data;
1105 error = __device_suspend_noirq(dev, pm_transition, true);
1107 dpm_save_failed_dev(dev_name(dev));
1108 pm_dev_err(dev, pm_transition, " async", error);
1114 static int device_suspend_noirq(struct device *dev)
1116 reinit_completion(&dev->power.completion);
1118 if (is_async(dev)) {
1120 async_schedule(async_suspend_noirq, dev);
1123 return __device_suspend_noirq(dev, pm_transition, false);
1127 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1128 * @state: PM transition of the system being carried out.
1130 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1131 * handlers for all non-sysdev devices.
1133 int dpm_suspend_noirq(pm_message_t state)
1135 ktime_t starttime = ktime_get();
1138 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1140 device_wakeup_arm_wake_irqs();
1141 suspend_device_irqs();
1142 mutex_lock(&dpm_list_mtx);
1143 pm_transition = state;
1146 while (!list_empty(&dpm_late_early_list)) {
1147 struct device *dev = to_device(dpm_late_early_list.prev);
1150 mutex_unlock(&dpm_list_mtx);
1152 error = device_suspend_noirq(dev);
1154 mutex_lock(&dpm_list_mtx);
1156 pm_dev_err(dev, state, " noirq", error);
1157 dpm_save_failed_dev(dev_name(dev));
1161 if (!list_empty(&dev->power.entry))
1162 list_move(&dev->power.entry, &dpm_noirq_list);
1168 mutex_unlock(&dpm_list_mtx);
1169 async_synchronize_full();
1171 error = async_error;
1174 suspend_stats.failed_suspend_noirq++;
1175 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1176 dpm_resume_noirq(resume_event(state));
1178 dpm_show_time(starttime, state, "noirq");
1180 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1185 * device_suspend_late - Execute a "late suspend" callback for given device.
1186 * @dev: Device to handle.
1187 * @state: PM transition of the system being carried out.
1188 * @async: If true, the device is being suspended asynchronously.
1190 * Runtime PM is disabled for @dev while this function is being executed.
1192 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1194 pm_callback_t callback = NULL;
1201 __pm_runtime_disable(dev, false);
1206 if (pm_wakeup_pending()) {
1207 async_error = -EBUSY;
1211 if (dev->power.syscore || dev->power.direct_complete)
1214 dpm_wait_for_children(dev, async);
1216 if (dev->pm_domain) {
1217 info = "late power domain ";
1218 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1219 } else if (dev->type && dev->type->pm) {
1220 info = "late type ";
1221 callback = pm_late_early_op(dev->type->pm, state);
1222 } else if (dev->class && dev->class->pm) {
1223 info = "late class ";
1224 callback = pm_late_early_op(dev->class->pm, state);
1225 } else if (dev->bus && dev->bus->pm) {
1227 callback = pm_late_early_op(dev->bus->pm, state);
1230 if (!callback && dev->driver && dev->driver->pm) {
1231 info = "late driver ";
1232 callback = pm_late_early_op(dev->driver->pm, state);
1235 error = dpm_run_callback(callback, dev, state, info);
1237 dev->power.is_late_suspended = true;
1239 async_error = error;
1242 TRACE_SUSPEND(error);
1243 complete_all(&dev->power.completion);
1247 static void async_suspend_late(void *data, async_cookie_t cookie)
1249 struct device *dev = (struct device *)data;
1252 error = __device_suspend_late(dev, pm_transition, true);
1254 dpm_save_failed_dev(dev_name(dev));
1255 pm_dev_err(dev, pm_transition, " async", error);
1260 static int device_suspend_late(struct device *dev)
1262 reinit_completion(&dev->power.completion);
1264 if (is_async(dev)) {
1266 async_schedule(async_suspend_late, dev);
1270 return __device_suspend_late(dev, pm_transition, false);
1274 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1275 * @state: PM transition of the system being carried out.
1277 int dpm_suspend_late(pm_message_t state)
1279 ktime_t starttime = ktime_get();
1282 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1283 mutex_lock(&dpm_list_mtx);
1284 pm_transition = state;
1287 while (!list_empty(&dpm_suspended_list)) {
1288 struct device *dev = to_device(dpm_suspended_list.prev);
1291 mutex_unlock(&dpm_list_mtx);
1293 error = device_suspend_late(dev);
1295 mutex_lock(&dpm_list_mtx);
1297 pm_dev_err(dev, state, " late", error);
1298 dpm_save_failed_dev(dev_name(dev));
1302 if (!list_empty(&dev->power.entry))
1303 list_move(&dev->power.entry, &dpm_late_early_list);
1309 mutex_unlock(&dpm_list_mtx);
1310 async_synchronize_full();
1312 error = async_error;
1314 suspend_stats.failed_suspend_late++;
1315 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1316 dpm_resume_early(resume_event(state));
1318 dpm_show_time(starttime, state, "late");
1320 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1325 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1326 * @state: PM transition of the system being carried out.
1328 int dpm_suspend_end(pm_message_t state)
1330 int error = dpm_suspend_late(state);
1334 error = dpm_suspend_noirq(state);
1336 dpm_resume_early(resume_event(state));
1342 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1345 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1346 * @dev: Device to suspend.
1347 * @state: PM transition of the system being carried out.
1348 * @cb: Suspend callback to execute.
1349 * @info: string description of caller.
1351 static int legacy_suspend(struct device *dev, pm_message_t state,
1352 int (*cb)(struct device *dev, pm_message_t state),
1358 calltime = initcall_debug_start(dev);
1360 trace_device_pm_callback_start(dev, info, state.event);
1361 error = cb(dev, state);
1362 trace_device_pm_callback_end(dev, error);
1363 suspend_report_result(cb, error);
1365 initcall_debug_report(dev, calltime, error, state, info);
1371 * device_suspend - Execute "suspend" callbacks for given device.
1372 * @dev: Device to handle.
1373 * @state: PM transition of the system being carried out.
1374 * @async: If true, the device is being suspended asynchronously.
1376 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1378 pm_callback_t callback = NULL;
1381 struct timer_list timer;
1382 struct dpm_drv_wd_data data;
1383 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
1384 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1389 dpm_wait_for_children(dev, async);
1395 * If a device configured to wake up the system from sleep states
1396 * has been suspended at run time and there's a resume request pending
1397 * for it, this is equivalent to the device signaling wakeup, so the
1398 * system suspend operation should be aborted.
1400 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1401 pm_wakeup_event(dev, 0);
1403 if (pm_wakeup_pending()) {
1404 pm_get_active_wakeup_sources(suspend_abort,
1405 MAX_SUSPEND_ABORT_LEN);
1406 log_suspend_abort_reason(suspend_abort);
1407 async_error = -EBUSY;
1411 if (dev->power.syscore)
1415 data.tsk = get_current();
1416 init_timer_on_stack(&timer);
1417 timer.expires = jiffies + HZ * 12;
1418 timer.function = dpm_drv_timeout;
1419 timer.data = (unsigned long)&data;
1422 if (dev->power.direct_complete) {
1423 if (pm_runtime_status_suspended(dev)) {
1424 pm_runtime_disable(dev);
1425 if (pm_runtime_status_suspended(dev))
1428 pm_runtime_enable(dev);
1430 dev->power.direct_complete = false;
1433 dpm_watchdog_set(&wd, dev);
1436 if (dev->pm_domain) {
1437 info = "power domain ";
1438 callback = pm_op(&dev->pm_domain->ops, state);
1442 if (dev->type && dev->type->pm) {
1444 callback = pm_op(dev->type->pm, state);
1449 if (dev->class->pm) {
1451 callback = pm_op(dev->class->pm, state);
1453 } else if (dev->class->suspend) {
1454 pm_dev_dbg(dev, state, "legacy class ");
1455 error = legacy_suspend(dev, state, dev->class->suspend,
1464 callback = pm_op(dev->bus->pm, state);
1465 } else if (dev->bus->suspend) {
1466 pm_dev_dbg(dev, state, "legacy bus ");
1467 error = legacy_suspend(dev, state, dev->bus->suspend,
1474 if (!callback && dev->driver && dev->driver->pm) {
1476 callback = pm_op(dev->driver->pm, state);
1479 error = dpm_run_callback(callback, dev, state, info);
1483 struct device *parent = dev->parent;
1485 dev->power.is_suspended = true;
1487 spin_lock_irq(&parent->power.lock);
1489 dev->parent->power.direct_complete = false;
1490 if (dev->power.wakeup_path
1491 && !dev->parent->power.ignore_children)
1492 dev->parent->power.wakeup_path = true;
1494 spin_unlock_irq(&parent->power.lock);
1499 dpm_watchdog_clear(&wd);
1501 del_timer_sync(&timer);
1502 destroy_timer_on_stack(&timer);
1505 complete_all(&dev->power.completion);
1507 async_error = error;
1509 TRACE_SUSPEND(error);
1513 static void async_suspend(void *data, async_cookie_t cookie)
1515 struct device *dev = (struct device *)data;
1518 error = __device_suspend(dev, pm_transition, true);
1520 dpm_save_failed_dev(dev_name(dev));
1521 pm_dev_err(dev, pm_transition, " async", error);
1527 static int device_suspend(struct device *dev)
1529 reinit_completion(&dev->power.completion);
1531 if (is_async(dev)) {
1533 async_schedule(async_suspend, dev);
1537 return __device_suspend(dev, pm_transition, false);
1541 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1542 * @state: PM transition of the system being carried out.
1544 int dpm_suspend(pm_message_t state)
1546 ktime_t starttime = ktime_get();
1549 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1554 mutex_lock(&dpm_list_mtx);
1555 pm_transition = state;
1557 while (!list_empty(&dpm_prepared_list)) {
1558 struct device *dev = to_device(dpm_prepared_list.prev);
1561 mutex_unlock(&dpm_list_mtx);
1563 error = device_suspend(dev);
1565 mutex_lock(&dpm_list_mtx);
1567 pm_dev_err(dev, state, "", error);
1568 dpm_save_failed_dev(dev_name(dev));
1572 if (!list_empty(&dev->power.entry))
1573 list_move(&dev->power.entry, &dpm_suspended_list);
1578 mutex_unlock(&dpm_list_mtx);
1579 async_synchronize_full();
1581 error = async_error;
1583 suspend_stats.failed_suspend++;
1584 dpm_save_failed_step(SUSPEND_SUSPEND);
1586 dpm_show_time(starttime, state, NULL);
1587 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1592 * device_prepare - Prepare a device for system power transition.
1593 * @dev: Device to handle.
1594 * @state: PM transition of the system being carried out.
1596 * Execute the ->prepare() callback(s) for given device. No new children of the
1597 * device may be registered after this function has returned.
1599 static int device_prepare(struct device *dev, pm_message_t state)
1601 int (*callback)(struct device *) = NULL;
1605 if (dev->power.syscore)
1609 * If a device's parent goes into runtime suspend at the wrong time,
1610 * it won't be possible to resume the device. To prevent this we
1611 * block runtime suspend here, during the prepare phase, and allow
1612 * it again during the complete phase.
1614 pm_runtime_get_noresume(dev);
1618 dev->power.wakeup_path = device_may_wakeup(dev);
1620 if (dev->pm_domain) {
1621 info = "preparing power domain ";
1622 callback = dev->pm_domain->ops.prepare;
1623 } else if (dev->type && dev->type->pm) {
1624 info = "preparing type ";
1625 callback = dev->type->pm->prepare;
1626 } else if (dev->class && dev->class->pm) {
1627 info = "preparing class ";
1628 callback = dev->class->pm->prepare;
1629 } else if (dev->bus && dev->bus->pm) {
1630 info = "preparing bus ";
1631 callback = dev->bus->pm->prepare;
1634 if (!callback && dev->driver && dev->driver->pm) {
1635 info = "preparing driver ";
1636 callback = dev->driver->pm->prepare;
1640 ret = callback(dev);
1645 suspend_report_result(callback, ret);
1646 pm_runtime_put(dev);
1650 * A positive return value from ->prepare() means "this device appears
1651 * to be runtime-suspended and its state is fine, so if it really is
1652 * runtime-suspended, you can leave it in that state provided that you
1653 * will do the same thing with all of its descendants". This only
1654 * applies to suspend transitions, however.
1656 spin_lock_irq(&dev->power.lock);
1657 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1658 spin_unlock_irq(&dev->power.lock);
1663 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1664 * @state: PM transition of the system being carried out.
1666 * Execute the ->prepare() callback(s) for all devices.
1668 int dpm_prepare(pm_message_t state)
1672 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1675 mutex_lock(&dpm_list_mtx);
1676 while (!list_empty(&dpm_list)) {
1677 struct device *dev = to_device(dpm_list.next);
1680 mutex_unlock(&dpm_list_mtx);
1682 trace_device_pm_callback_start(dev, "", state.event);
1683 error = device_prepare(dev, state);
1684 trace_device_pm_callback_end(dev, error);
1686 mutex_lock(&dpm_list_mtx);
1688 if (error == -EAGAIN) {
1693 printk(KERN_INFO "PM: Device %s not prepared "
1694 "for power transition: code %d\n",
1695 dev_name(dev), error);
1699 dev->power.is_prepared = true;
1700 if (!list_empty(&dev->power.entry))
1701 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1704 mutex_unlock(&dpm_list_mtx);
1705 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
1710 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1711 * @state: PM transition of the system being carried out.
1713 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1714 * callbacks for them.
1716 int dpm_suspend_start(pm_message_t state)
1720 error = dpm_prepare(state);
1722 suspend_stats.failed_prepare++;
1723 dpm_save_failed_step(SUSPEND_PREPARE);
1725 error = dpm_suspend(state);
1728 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1730 void __suspend_report_result(const char *function, void *fn, int ret)
1733 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1735 EXPORT_SYMBOL_GPL(__suspend_report_result);
1738 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1739 * @dev: Device to wait for.
1740 * @subordinate: Device that needs to wait for @dev.
1742 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1744 dpm_wait(dev, subordinate->power.async_suspend);
1747 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1750 * dpm_for_each_dev - device iterator.
1751 * @data: data for the callback.
1752 * @fn: function to be called for each device.
1754 * Iterate over devices in dpm_list, and call @fn for each device,
1757 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1765 list_for_each_entry(dev, &dpm_list, power.entry)
1769 EXPORT_SYMBOL_GPL(dpm_for_each_dev);