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>
34 #include <linux/timer.h>
39 typedef int (*pm_callback_t)(struct device *);
42 * The entries in the dpm_list list are in a depth first order, simply
43 * because children are guaranteed to be discovered after parents, and
44 * are inserted at the back of the list on discovery.
46 * Since device_pm_add() may be called with a device lock held,
47 * we must never try to acquire a device lock while holding
52 static LIST_HEAD(dpm_prepared_list);
53 static LIST_HEAD(dpm_suspended_list);
54 static LIST_HEAD(dpm_late_early_list);
55 static LIST_HEAD(dpm_noirq_list);
57 struct suspend_stats suspend_stats;
58 static DEFINE_MUTEX(dpm_list_mtx);
59 static pm_message_t pm_transition;
61 static int async_error;
63 static char *pm_verb(int event)
66 case PM_EVENT_SUSPEND:
72 case PM_EVENT_QUIESCE:
74 case PM_EVENT_HIBERNATE:
78 case PM_EVENT_RESTORE:
80 case PM_EVENT_RECOVER:
83 return "(unknown PM event)";
88 * device_pm_sleep_init - Initialize system suspend-related device fields.
89 * @dev: Device object being initialized.
91 void device_pm_sleep_init(struct device *dev)
93 dev->power.is_prepared = false;
94 dev->power.is_suspended = false;
95 init_completion(&dev->power.completion);
96 complete_all(&dev->power.completion);
97 dev->power.wakeup = NULL;
98 INIT_LIST_HEAD(&dev->power.entry);
102 * device_pm_lock - Lock the list of active devices used by the PM core.
104 void device_pm_lock(void)
106 mutex_lock(&dpm_list_mtx);
110 * device_pm_unlock - Unlock the list of active devices used by the PM core.
112 void device_pm_unlock(void)
114 mutex_unlock(&dpm_list_mtx);
118 * device_pm_add - Add a device to the PM core's list of active devices.
119 * @dev: Device to add to the list.
121 void device_pm_add(struct device *dev)
123 pr_debug("PM: Adding info for %s:%s\n",
124 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
125 mutex_lock(&dpm_list_mtx);
126 if (dev->parent && dev->parent->power.is_prepared)
127 dev_warn(dev, "parent %s should not be sleeping\n",
128 dev_name(dev->parent));
129 list_add_tail(&dev->power.entry, &dpm_list);
130 mutex_unlock(&dpm_list_mtx);
134 * device_pm_remove - Remove a device from the PM core's list of active devices.
135 * @dev: Device to be removed from the list.
137 void device_pm_remove(struct device *dev)
139 pr_debug("PM: Removing info for %s:%s\n",
140 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
141 complete_all(&dev->power.completion);
142 mutex_lock(&dpm_list_mtx);
143 list_del_init(&dev->power.entry);
144 mutex_unlock(&dpm_list_mtx);
145 device_wakeup_disable(dev);
146 pm_runtime_remove(dev);
150 * device_pm_move_before - Move device in the PM core's list of active devices.
151 * @deva: Device to move in dpm_list.
152 * @devb: Device @deva should come before.
154 void device_pm_move_before(struct device *deva, struct device *devb)
156 pr_debug("PM: Moving %s:%s before %s:%s\n",
157 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
158 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
159 /* Delete deva from dpm_list and reinsert before devb. */
160 list_move_tail(&deva->power.entry, &devb->power.entry);
164 * device_pm_move_after - Move device in the PM core's list of active devices.
165 * @deva: Device to move in dpm_list.
166 * @devb: Device @deva should come after.
168 void device_pm_move_after(struct device *deva, struct device *devb)
170 pr_debug("PM: Moving %s:%s after %s:%s\n",
171 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
172 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
173 /* Delete deva from dpm_list and reinsert after devb. */
174 list_move(&deva->power.entry, &devb->power.entry);
178 * device_pm_move_last - Move device to end of the PM core's list of devices.
179 * @dev: Device to move in dpm_list.
181 void device_pm_move_last(struct device *dev)
183 pr_debug("PM: Moving %s:%s to end of list\n",
184 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
185 list_move_tail(&dev->power.entry, &dpm_list);
188 static ktime_t initcall_debug_start(struct device *dev)
190 ktime_t calltime = ktime_set(0, 0);
192 if (pm_print_times_enabled) {
193 pr_info("calling %s+ @ %i, parent: %s\n",
194 dev_name(dev), task_pid_nr(current),
195 dev->parent ? dev_name(dev->parent) : "none");
196 calltime = ktime_get();
202 static void initcall_debug_report(struct device *dev, ktime_t calltime,
203 int error, pm_message_t state, char *info)
208 rettime = ktime_get();
209 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
211 if (pm_print_times_enabled) {
212 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
213 error, (unsigned long long)nsecs >> 10);
216 trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event),
221 * dpm_wait - Wait for a PM operation to complete.
222 * @dev: Device to wait for.
223 * @async: If unset, wait only if the device's power.async_suspend flag is set.
225 static void dpm_wait(struct device *dev, bool async)
230 if (async || (pm_async_enabled && dev->power.async_suspend))
231 wait_for_completion(&dev->power.completion);
234 static int dpm_wait_fn(struct device *dev, void *async_ptr)
236 dpm_wait(dev, *((bool *)async_ptr));
240 static void dpm_wait_for_children(struct device *dev, bool async)
242 device_for_each_child(dev, &async, dpm_wait_fn);
246 * pm_op - Return the PM operation appropriate for given PM event.
247 * @ops: PM operations to choose from.
248 * @state: PM transition of the system being carried out.
250 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
252 switch (state.event) {
253 #ifdef CONFIG_SUSPEND
254 case PM_EVENT_SUSPEND:
256 case PM_EVENT_RESUME:
258 #endif /* CONFIG_SUSPEND */
259 #ifdef CONFIG_HIBERNATE_CALLBACKS
260 case PM_EVENT_FREEZE:
261 case PM_EVENT_QUIESCE:
263 case PM_EVENT_HIBERNATE:
264 return ops->poweroff;
266 case PM_EVENT_RECOVER:
269 case PM_EVENT_RESTORE:
271 #endif /* CONFIG_HIBERNATE_CALLBACKS */
278 * pm_late_early_op - Return the PM operation appropriate for given PM event.
279 * @ops: PM operations to choose from.
280 * @state: PM transition of the system being carried out.
282 * Runtime PM is disabled for @dev while this function is being executed.
284 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
287 switch (state.event) {
288 #ifdef CONFIG_SUSPEND
289 case PM_EVENT_SUSPEND:
290 return ops->suspend_late;
291 case PM_EVENT_RESUME:
292 return ops->resume_early;
293 #endif /* CONFIG_SUSPEND */
294 #ifdef CONFIG_HIBERNATE_CALLBACKS
295 case PM_EVENT_FREEZE:
296 case PM_EVENT_QUIESCE:
297 return ops->freeze_late;
298 case PM_EVENT_HIBERNATE:
299 return ops->poweroff_late;
301 case PM_EVENT_RECOVER:
302 return ops->thaw_early;
303 case PM_EVENT_RESTORE:
304 return ops->restore_early;
305 #endif /* CONFIG_HIBERNATE_CALLBACKS */
312 * pm_noirq_op - Return the PM operation appropriate for given PM event.
313 * @ops: PM operations to choose from.
314 * @state: PM transition of the system being carried out.
316 * The driver of @dev will not receive interrupts while this function is being
319 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
321 switch (state.event) {
322 #ifdef CONFIG_SUSPEND
323 case PM_EVENT_SUSPEND:
324 return ops->suspend_noirq;
325 case PM_EVENT_RESUME:
326 return ops->resume_noirq;
327 #endif /* CONFIG_SUSPEND */
328 #ifdef CONFIG_HIBERNATE_CALLBACKS
329 case PM_EVENT_FREEZE:
330 case PM_EVENT_QUIESCE:
331 return ops->freeze_noirq;
332 case PM_EVENT_HIBERNATE:
333 return ops->poweroff_noirq;
335 case PM_EVENT_RECOVER:
336 return ops->thaw_noirq;
337 case PM_EVENT_RESTORE:
338 return ops->restore_noirq;
339 #endif /* CONFIG_HIBERNATE_CALLBACKS */
345 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
347 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
348 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
349 ", may wakeup" : "");
352 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
355 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
356 dev_name(dev), pm_verb(state.event), info, error);
359 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
365 calltime = ktime_get();
366 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
367 do_div(usecs64, NSEC_PER_USEC);
371 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
372 info ?: "", info ? " " : "", pm_verb(state.event),
373 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
376 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
377 pm_message_t state, char *info)
385 calltime = initcall_debug_start(dev);
387 pm_dev_dbg(dev, state, info);
389 suspend_report_result(cb, error);
391 initcall_debug_report(dev, calltime, error, state, info);
396 #ifdef CONFIG_DPM_WATCHDOG
397 struct dpm_watchdog {
399 struct task_struct *tsk;
400 struct timer_list timer;
403 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
404 struct dpm_watchdog wd
407 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
408 * @data: Watchdog object address.
410 * Called when a driver has timed out suspending or resuming.
411 * There's not much we can do here to recover so panic() to
412 * capture a crash-dump in pstore.
414 static void dpm_watchdog_handler(unsigned long data)
416 struct dpm_watchdog *wd = (void *)data;
418 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
419 show_stack(wd->tsk, NULL);
420 panic("%s %s: unrecoverable failure\n",
421 dev_driver_string(wd->dev), dev_name(wd->dev));
425 * dpm_watchdog_set - Enable pm watchdog for given device.
426 * @wd: Watchdog. Must be allocated on the stack.
427 * @dev: Device to handle.
429 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
431 struct timer_list *timer = &wd->timer;
436 init_timer_on_stack(timer);
437 /* use same timeout value for both suspend and resume */
438 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
439 timer->function = dpm_watchdog_handler;
440 timer->data = (unsigned long)wd;
445 * dpm_watchdog_clear - Disable suspend/resume watchdog.
446 * @wd: Watchdog to disable.
448 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
450 struct timer_list *timer = &wd->timer;
452 del_timer_sync(timer);
453 destroy_timer_on_stack(timer);
456 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
457 #define dpm_watchdog_set(x, y)
458 #define dpm_watchdog_clear(x)
461 /*------------------------- Resume routines -------------------------*/
464 * device_resume_noirq - Execute an "early resume" callback for given device.
465 * @dev: Device to handle.
466 * @state: PM transition of the system being carried out.
468 * The driver of @dev will not receive interrupts while this function is being
471 static int device_resume_noirq(struct device *dev, pm_message_t state)
473 pm_callback_t callback = NULL;
480 if (dev->power.syscore)
483 if (dev->pm_domain) {
484 info = "noirq power domain ";
485 callback = pm_noirq_op(&dev->pm_domain->ops, state);
486 } else if (dev->type && dev->type->pm) {
487 info = "noirq type ";
488 callback = pm_noirq_op(dev->type->pm, state);
489 } else if (dev->class && dev->class->pm) {
490 info = "noirq class ";
491 callback = pm_noirq_op(dev->class->pm, state);
492 } else if (dev->bus && dev->bus->pm) {
494 callback = pm_noirq_op(dev->bus->pm, state);
497 if (!callback && dev->driver && dev->driver->pm) {
498 info = "noirq driver ";
499 callback = pm_noirq_op(dev->driver->pm, state);
502 error = dpm_run_callback(callback, dev, state, info);
510 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
511 * @state: PM transition of the system being carried out.
513 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
514 * enable device drivers to receive interrupts.
516 static void dpm_resume_noirq(pm_message_t state)
518 ktime_t starttime = ktime_get();
520 mutex_lock(&dpm_list_mtx);
521 while (!list_empty(&dpm_noirq_list)) {
522 struct device *dev = to_device(dpm_noirq_list.next);
526 list_move_tail(&dev->power.entry, &dpm_late_early_list);
527 mutex_unlock(&dpm_list_mtx);
529 error = device_resume_noirq(dev, state);
531 suspend_stats.failed_resume_noirq++;
532 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
533 dpm_save_failed_dev(dev_name(dev));
534 pm_dev_err(dev, state, " noirq", error);
537 mutex_lock(&dpm_list_mtx);
540 mutex_unlock(&dpm_list_mtx);
541 dpm_show_time(starttime, state, "noirq");
542 resume_device_irqs();
548 * device_resume_early - Execute an "early resume" callback for given device.
549 * @dev: Device to handle.
550 * @state: PM transition of the system being carried out.
552 * Runtime PM is disabled for @dev while this function is being executed.
554 static int device_resume_early(struct device *dev, pm_message_t state)
556 pm_callback_t callback = NULL;
563 if (dev->power.syscore)
566 if (dev->pm_domain) {
567 info = "early power domain ";
568 callback = pm_late_early_op(&dev->pm_domain->ops, state);
569 } else if (dev->type && dev->type->pm) {
570 info = "early type ";
571 callback = pm_late_early_op(dev->type->pm, state);
572 } else if (dev->class && dev->class->pm) {
573 info = "early class ";
574 callback = pm_late_early_op(dev->class->pm, state);
575 } else if (dev->bus && dev->bus->pm) {
577 callback = pm_late_early_op(dev->bus->pm, state);
580 if (!callback && dev->driver && dev->driver->pm) {
581 info = "early driver ";
582 callback = pm_late_early_op(dev->driver->pm, state);
585 error = dpm_run_callback(callback, dev, state, info);
590 pm_runtime_enable(dev);
595 * dpm_resume_early - Execute "early resume" callbacks for all devices.
596 * @state: PM transition of the system being carried out.
598 static void dpm_resume_early(pm_message_t state)
600 ktime_t starttime = ktime_get();
602 mutex_lock(&dpm_list_mtx);
603 while (!list_empty(&dpm_late_early_list)) {
604 struct device *dev = to_device(dpm_late_early_list.next);
608 list_move_tail(&dev->power.entry, &dpm_suspended_list);
609 mutex_unlock(&dpm_list_mtx);
611 error = device_resume_early(dev, state);
613 suspend_stats.failed_resume_early++;
614 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
615 dpm_save_failed_dev(dev_name(dev));
616 pm_dev_err(dev, state, " early", error);
619 mutex_lock(&dpm_list_mtx);
622 mutex_unlock(&dpm_list_mtx);
623 dpm_show_time(starttime, state, "early");
627 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
628 * @state: PM transition of the system being carried out.
630 void dpm_resume_start(pm_message_t state)
632 dpm_resume_noirq(state);
633 dpm_resume_early(state);
635 EXPORT_SYMBOL_GPL(dpm_resume_start);
638 * device_resume - Execute "resume" callbacks for given device.
639 * @dev: Device to handle.
640 * @state: PM transition of the system being carried out.
641 * @async: If true, the device is being resumed asynchronously.
643 static int device_resume(struct device *dev, pm_message_t state, bool async)
645 pm_callback_t callback = NULL;
648 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
653 if (dev->power.syscore)
656 dpm_wait(dev->parent, async);
657 dpm_watchdog_set(&wd, dev);
661 * This is a fib. But we'll allow new children to be added below
662 * a resumed device, even if the device hasn't been completed yet.
664 dev->power.is_prepared = false;
666 if (!dev->power.is_suspended)
669 if (dev->pm_domain) {
670 info = "power domain ";
671 callback = pm_op(&dev->pm_domain->ops, state);
675 if (dev->type && dev->type->pm) {
677 callback = pm_op(dev->type->pm, state);
682 if (dev->class->pm) {
684 callback = pm_op(dev->class->pm, state);
686 } else if (dev->class->resume) {
687 info = "legacy class ";
688 callback = dev->class->resume;
696 callback = pm_op(dev->bus->pm, state);
697 } else if (dev->bus->resume) {
698 info = "legacy bus ";
699 callback = dev->bus->resume;
705 if (!callback && dev->driver && dev->driver->pm) {
707 callback = pm_op(dev->driver->pm, state);
711 error = dpm_run_callback(callback, dev, state, info);
712 dev->power.is_suspended = false;
716 dpm_watchdog_clear(&wd);
719 complete_all(&dev->power.completion);
726 static void async_resume(void *data, async_cookie_t cookie)
728 struct device *dev = (struct device *)data;
731 error = device_resume(dev, pm_transition, true);
733 pm_dev_err(dev, pm_transition, " async", error);
737 static bool is_async(struct device *dev)
739 return dev->power.async_suspend && pm_async_enabled
740 && !pm_trace_is_enabled();
744 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
745 * @state: PM transition of the system being carried out.
747 * Execute the appropriate "resume" callback for all devices whose status
748 * indicates that they are suspended.
750 void dpm_resume(pm_message_t state)
753 ktime_t starttime = ktime_get();
757 mutex_lock(&dpm_list_mtx);
758 pm_transition = state;
761 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
762 reinit_completion(&dev->power.completion);
765 async_schedule(async_resume, dev);
769 while (!list_empty(&dpm_suspended_list)) {
770 dev = to_device(dpm_suspended_list.next);
772 if (!is_async(dev)) {
775 mutex_unlock(&dpm_list_mtx);
777 error = device_resume(dev, state, false);
779 suspend_stats.failed_resume++;
780 dpm_save_failed_step(SUSPEND_RESUME);
781 dpm_save_failed_dev(dev_name(dev));
782 pm_dev_err(dev, state, "", error);
785 mutex_lock(&dpm_list_mtx);
787 if (!list_empty(&dev->power.entry))
788 list_move_tail(&dev->power.entry, &dpm_prepared_list);
791 mutex_unlock(&dpm_list_mtx);
792 async_synchronize_full();
793 dpm_show_time(starttime, state, NULL);
797 * device_complete - Complete a PM transition for given device.
798 * @dev: Device to handle.
799 * @state: PM transition of the system being carried out.
801 static void device_complete(struct device *dev, pm_message_t state)
803 void (*callback)(struct device *) = NULL;
806 if (dev->power.syscore)
811 if (dev->pm_domain) {
812 info = "completing power domain ";
813 callback = dev->pm_domain->ops.complete;
814 } else if (dev->type && dev->type->pm) {
815 info = "completing type ";
816 callback = dev->type->pm->complete;
817 } else if (dev->class && dev->class->pm) {
818 info = "completing class ";
819 callback = dev->class->pm->complete;
820 } else if (dev->bus && dev->bus->pm) {
821 info = "completing bus ";
822 callback = dev->bus->pm->complete;
825 if (!callback && dev->driver && dev->driver->pm) {
826 info = "completing driver ";
827 callback = dev->driver->pm->complete;
831 pm_dev_dbg(dev, state, info);
841 * dpm_complete - Complete a PM transition for all non-sysdev devices.
842 * @state: PM transition of the system being carried out.
844 * Execute the ->complete() callbacks for all devices whose PM status is not
845 * DPM_ON (this allows new devices to be registered).
847 void dpm_complete(pm_message_t state)
849 struct list_head list;
853 INIT_LIST_HEAD(&list);
854 mutex_lock(&dpm_list_mtx);
855 while (!list_empty(&dpm_prepared_list)) {
856 struct device *dev = to_device(dpm_prepared_list.prev);
859 dev->power.is_prepared = false;
860 list_move(&dev->power.entry, &list);
861 mutex_unlock(&dpm_list_mtx);
863 device_complete(dev, state);
865 mutex_lock(&dpm_list_mtx);
868 list_splice(&list, &dpm_list);
869 mutex_unlock(&dpm_list_mtx);
873 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
874 * @state: PM transition of the system being carried out.
876 * Execute "resume" callbacks for all devices and complete the PM transition of
879 void dpm_resume_end(pm_message_t state)
884 EXPORT_SYMBOL_GPL(dpm_resume_end);
887 /*------------------------- Suspend routines -------------------------*/
890 * resume_event - Return a "resume" message for given "suspend" sleep state.
891 * @sleep_state: PM message representing a sleep state.
893 * Return a PM message representing the resume event corresponding to given
896 static pm_message_t resume_event(pm_message_t sleep_state)
898 switch (sleep_state.event) {
899 case PM_EVENT_SUSPEND:
901 case PM_EVENT_FREEZE:
902 case PM_EVENT_QUIESCE:
904 case PM_EVENT_HIBERNATE:
911 * device_suspend_noirq - Execute a "late suspend" callback for given device.
912 * @dev: Device to handle.
913 * @state: PM transition of the system being carried out.
915 * The driver of @dev will not receive interrupts while this function is being
918 static int device_suspend_noirq(struct device *dev, pm_message_t state)
920 pm_callback_t callback = NULL;
923 if (dev->power.syscore)
926 if (dev->pm_domain) {
927 info = "noirq power domain ";
928 callback = pm_noirq_op(&dev->pm_domain->ops, state);
929 } else if (dev->type && dev->type->pm) {
930 info = "noirq type ";
931 callback = pm_noirq_op(dev->type->pm, state);
932 } else if (dev->class && dev->class->pm) {
933 info = "noirq class ";
934 callback = pm_noirq_op(dev->class->pm, state);
935 } else if (dev->bus && dev->bus->pm) {
937 callback = pm_noirq_op(dev->bus->pm, state);
940 if (!callback && dev->driver && dev->driver->pm) {
941 info = "noirq driver ";
942 callback = pm_noirq_op(dev->driver->pm, state);
945 return dpm_run_callback(callback, dev, state, info);
949 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
950 * @state: PM transition of the system being carried out.
952 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
953 * handlers for all non-sysdev devices.
955 static int dpm_suspend_noirq(pm_message_t state)
957 ktime_t starttime = ktime_get();
962 suspend_device_irqs();
963 mutex_lock(&dpm_list_mtx);
964 while (!list_empty(&dpm_late_early_list)) {
965 struct device *dev = to_device(dpm_late_early_list.prev);
968 mutex_unlock(&dpm_list_mtx);
970 error = device_suspend_noirq(dev, state);
972 mutex_lock(&dpm_list_mtx);
974 pm_dev_err(dev, state, " noirq", error);
975 suspend_stats.failed_suspend_noirq++;
976 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
977 dpm_save_failed_dev(dev_name(dev));
981 if (!list_empty(&dev->power.entry))
982 list_move(&dev->power.entry, &dpm_noirq_list);
985 if (pm_wakeup_pending()) {
990 mutex_unlock(&dpm_list_mtx);
992 dpm_resume_noirq(resume_event(state));
994 dpm_show_time(starttime, state, "noirq");
999 * device_suspend_late - Execute a "late suspend" callback for given device.
1000 * @dev: Device to handle.
1001 * @state: PM transition of the system being carried out.
1003 * Runtime PM is disabled for @dev while this function is being executed.
1005 static int device_suspend_late(struct device *dev, pm_message_t state)
1007 pm_callback_t callback = NULL;
1010 __pm_runtime_disable(dev, false);
1012 if (dev->power.syscore)
1015 if (dev->pm_domain) {
1016 info = "late power domain ";
1017 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1018 } else if (dev->type && dev->type->pm) {
1019 info = "late type ";
1020 callback = pm_late_early_op(dev->type->pm, state);
1021 } else if (dev->class && dev->class->pm) {
1022 info = "late class ";
1023 callback = pm_late_early_op(dev->class->pm, state);
1024 } else if (dev->bus && dev->bus->pm) {
1026 callback = pm_late_early_op(dev->bus->pm, state);
1029 if (!callback && dev->driver && dev->driver->pm) {
1030 info = "late driver ";
1031 callback = pm_late_early_op(dev->driver->pm, state);
1034 return dpm_run_callback(callback, dev, state, info);
1038 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1039 * @state: PM transition of the system being carried out.
1041 static int dpm_suspend_late(pm_message_t state)
1043 ktime_t starttime = ktime_get();
1046 mutex_lock(&dpm_list_mtx);
1047 while (!list_empty(&dpm_suspended_list)) {
1048 struct device *dev = to_device(dpm_suspended_list.prev);
1051 mutex_unlock(&dpm_list_mtx);
1053 error = device_suspend_late(dev, state);
1055 mutex_lock(&dpm_list_mtx);
1057 pm_dev_err(dev, state, " late", error);
1058 suspend_stats.failed_suspend_late++;
1059 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1060 dpm_save_failed_dev(dev_name(dev));
1064 if (!list_empty(&dev->power.entry))
1065 list_move(&dev->power.entry, &dpm_late_early_list);
1068 if (pm_wakeup_pending()) {
1073 mutex_unlock(&dpm_list_mtx);
1075 dpm_resume_early(resume_event(state));
1077 dpm_show_time(starttime, state, "late");
1083 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1084 * @state: PM transition of the system being carried out.
1086 int dpm_suspend_end(pm_message_t state)
1088 int error = dpm_suspend_late(state);
1092 error = dpm_suspend_noirq(state);
1094 dpm_resume_early(resume_event(state));
1100 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1103 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1104 * @dev: Device to suspend.
1105 * @state: PM transition of the system being carried out.
1106 * @cb: Suspend callback to execute.
1108 static int legacy_suspend(struct device *dev, pm_message_t state,
1109 int (*cb)(struct device *dev, pm_message_t state),
1115 calltime = initcall_debug_start(dev);
1117 error = cb(dev, state);
1118 suspend_report_result(cb, error);
1120 initcall_debug_report(dev, calltime, error, state, info);
1126 * device_suspend - Execute "suspend" callbacks for given device.
1127 * @dev: Device to handle.
1128 * @state: PM transition of the system being carried out.
1129 * @async: If true, the device is being suspended asynchronously.
1131 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1133 pm_callback_t callback = NULL;
1136 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1138 dpm_wait_for_children(dev, async);
1144 * If a device configured to wake up the system from sleep states
1145 * has been suspended at run time and there's a resume request pending
1146 * for it, this is equivalent to the device signaling wakeup, so the
1147 * system suspend operation should be aborted.
1149 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1150 pm_wakeup_event(dev, 0);
1152 if (pm_wakeup_pending()) {
1153 async_error = -EBUSY;
1157 if (dev->power.syscore)
1160 dpm_watchdog_set(&wd, dev);
1163 if (dev->pm_domain) {
1164 info = "power domain ";
1165 callback = pm_op(&dev->pm_domain->ops, state);
1169 if (dev->type && dev->type->pm) {
1171 callback = pm_op(dev->type->pm, state);
1176 if (dev->class->pm) {
1178 callback = pm_op(dev->class->pm, state);
1180 } else if (dev->class->suspend) {
1181 pm_dev_dbg(dev, state, "legacy class ");
1182 error = legacy_suspend(dev, state, dev->class->suspend,
1191 callback = pm_op(dev->bus->pm, state);
1192 } else if (dev->bus->suspend) {
1193 pm_dev_dbg(dev, state, "legacy bus ");
1194 error = legacy_suspend(dev, state, dev->bus->suspend,
1201 if (!callback && dev->driver && dev->driver->pm) {
1203 callback = pm_op(dev->driver->pm, state);
1206 error = dpm_run_callback(callback, dev, state, info);
1210 dev->power.is_suspended = true;
1211 if (dev->power.wakeup_path
1212 && dev->parent && !dev->parent->power.ignore_children)
1213 dev->parent->power.wakeup_path = true;
1217 dpm_watchdog_clear(&wd);
1220 complete_all(&dev->power.completion);
1222 async_error = error;
1227 static void async_suspend(void *data, async_cookie_t cookie)
1229 struct device *dev = (struct device *)data;
1232 error = __device_suspend(dev, pm_transition, true);
1234 dpm_save_failed_dev(dev_name(dev));
1235 pm_dev_err(dev, pm_transition, " async", error);
1241 static int device_suspend(struct device *dev)
1243 reinit_completion(&dev->power.completion);
1245 if (pm_async_enabled && dev->power.async_suspend) {
1247 async_schedule(async_suspend, dev);
1251 return __device_suspend(dev, pm_transition, false);
1255 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1256 * @state: PM transition of the system being carried out.
1258 int dpm_suspend(pm_message_t state)
1260 ktime_t starttime = ktime_get();
1265 mutex_lock(&dpm_list_mtx);
1266 pm_transition = state;
1268 while (!list_empty(&dpm_prepared_list)) {
1269 struct device *dev = to_device(dpm_prepared_list.prev);
1272 mutex_unlock(&dpm_list_mtx);
1274 error = device_suspend(dev);
1276 mutex_lock(&dpm_list_mtx);
1278 pm_dev_err(dev, state, "", error);
1279 dpm_save_failed_dev(dev_name(dev));
1283 if (!list_empty(&dev->power.entry))
1284 list_move(&dev->power.entry, &dpm_suspended_list);
1289 mutex_unlock(&dpm_list_mtx);
1290 async_synchronize_full();
1292 error = async_error;
1294 suspend_stats.failed_suspend++;
1295 dpm_save_failed_step(SUSPEND_SUSPEND);
1297 dpm_show_time(starttime, state, NULL);
1302 * device_prepare - Prepare a device for system power transition.
1303 * @dev: Device to handle.
1304 * @state: PM transition of the system being carried out.
1306 * Execute the ->prepare() callback(s) for given device. No new children of the
1307 * device may be registered after this function has returned.
1309 static int device_prepare(struct device *dev, pm_message_t state)
1311 int (*callback)(struct device *) = NULL;
1315 if (dev->power.syscore)
1319 * If a device's parent goes into runtime suspend at the wrong time,
1320 * it won't be possible to resume the device. To prevent this we
1321 * block runtime suspend here, during the prepare phase, and allow
1322 * it again during the complete phase.
1324 pm_runtime_get_noresume(dev);
1328 dev->power.wakeup_path = device_may_wakeup(dev);
1330 if (dev->pm_domain) {
1331 info = "preparing power domain ";
1332 callback = dev->pm_domain->ops.prepare;
1333 } else if (dev->type && dev->type->pm) {
1334 info = "preparing type ";
1335 callback = dev->type->pm->prepare;
1336 } else if (dev->class && dev->class->pm) {
1337 info = "preparing class ";
1338 callback = dev->class->pm->prepare;
1339 } else if (dev->bus && dev->bus->pm) {
1340 info = "preparing bus ";
1341 callback = dev->bus->pm->prepare;
1344 if (!callback && dev->driver && dev->driver->pm) {
1345 info = "preparing driver ";
1346 callback = dev->driver->pm->prepare;
1350 error = callback(dev);
1351 suspend_report_result(callback, error);
1357 pm_runtime_put(dev);
1363 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1364 * @state: PM transition of the system being carried out.
1366 * Execute the ->prepare() callback(s) for all devices.
1368 int dpm_prepare(pm_message_t state)
1374 mutex_lock(&dpm_list_mtx);
1375 while (!list_empty(&dpm_list)) {
1376 struct device *dev = to_device(dpm_list.next);
1379 mutex_unlock(&dpm_list_mtx);
1381 error = device_prepare(dev, state);
1383 mutex_lock(&dpm_list_mtx);
1385 if (error == -EAGAIN) {
1390 printk(KERN_INFO "PM: Device %s not prepared "
1391 "for power transition: code %d\n",
1392 dev_name(dev), error);
1396 dev->power.is_prepared = true;
1397 if (!list_empty(&dev->power.entry))
1398 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1401 mutex_unlock(&dpm_list_mtx);
1406 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1407 * @state: PM transition of the system being carried out.
1409 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1410 * callbacks for them.
1412 int dpm_suspend_start(pm_message_t state)
1416 error = dpm_prepare(state);
1418 suspend_stats.failed_prepare++;
1419 dpm_save_failed_step(SUSPEND_PREPARE);
1421 error = dpm_suspend(state);
1424 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1426 void __suspend_report_result(const char *function, void *fn, int ret)
1429 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1431 EXPORT_SYMBOL_GPL(__suspend_report_result);
1434 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1435 * @dev: Device to wait for.
1436 * @subordinate: Device that needs to wait for @dev.
1438 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1440 dpm_wait(dev, subordinate->power.async_suspend);
1443 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1446 * dpm_for_each_dev - device iterator.
1447 * @data: data for the callback.
1448 * @fn: function to be called for each device.
1450 * Iterate over devices in dpm_list, and call @fn for each device,
1453 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1461 list_for_each_entry(dev, &dpm_list, power.entry)
1465 EXPORT_SYMBOL_GPL(dpm_for_each_dev);