PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / base / power / main.c
blob9335b326d1f50870052bc72406d822e32ce975df
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/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/cpuidle.h>
33 #include <linux/timer.h>
35 #include "../base.h"
36 #include "power.h"
38 typedef int (*pm_callback_t)(struct device *);
41 * The entries in the dpm_list list are in a depth first order, simply
42 * because children are guaranteed to be discovered after parents, and
43 * are inserted at the back of the list on discovery.
45 * Since device_pm_add() may be called with a device lock held,
46 * we must never try to acquire a device lock while holding
47 * dpm_list_mutex.
50 LIST_HEAD(dpm_list);
51 static LIST_HEAD(dpm_prepared_list);
52 static LIST_HEAD(dpm_suspended_list);
53 static LIST_HEAD(dpm_late_early_list);
54 static LIST_HEAD(dpm_noirq_list);
56 struct suspend_stats suspend_stats;
57 static DEFINE_MUTEX(dpm_list_mtx);
58 static pm_message_t pm_transition;
60 static int async_error;
62 static char *pm_verb(int event)
64 switch (event) {
65 case PM_EVENT_SUSPEND:
66 return "suspend";
67 case PM_EVENT_RESUME:
68 return "resume";
69 case PM_EVENT_FREEZE:
70 return "freeze";
71 case PM_EVENT_QUIESCE:
72 return "quiesce";
73 case PM_EVENT_HIBERNATE:
74 return "hibernate";
75 case PM_EVENT_THAW:
76 return "thaw";
77 case PM_EVENT_RESTORE:
78 return "restore";
79 case PM_EVENT_RECOVER:
80 return "recover";
81 default:
82 return "(unknown PM event)";
86 /**
87 * device_pm_sleep_init - Initialize system suspend-related device fields.
88 * @dev: Device object being initialized.
90 void device_pm_sleep_init(struct device *dev)
92 dev->power.is_prepared = false;
93 dev->power.is_suspended = false;
94 dev->power.is_noirq_suspended = false;
95 dev->power.is_late_suspended = false;
96 init_completion(&dev->power.completion);
97 complete_all(&dev->power.completion);
98 dev->power.wakeup = NULL;
99 INIT_LIST_HEAD(&dev->power.entry);
103 * device_pm_lock - Lock the list of active devices used by the PM core.
105 void device_pm_lock(void)
107 mutex_lock(&dpm_list_mtx);
111 * device_pm_unlock - Unlock the list of active devices used by the PM core.
113 void device_pm_unlock(void)
115 mutex_unlock(&dpm_list_mtx);
119 * device_pm_add - Add a device to the PM core's list of active devices.
120 * @dev: Device to add to the list.
122 void device_pm_add(struct device *dev)
124 pr_debug("PM: Adding info for %s:%s\n",
125 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
126 mutex_lock(&dpm_list_mtx);
127 if (dev->parent && dev->parent->power.is_prepared)
128 dev_warn(dev, "parent %s should not be sleeping\n",
129 dev_name(dev->parent));
130 list_add_tail(&dev->power.entry, &dpm_list);
131 mutex_unlock(&dpm_list_mtx);
135 * device_pm_remove - Remove a device from the PM core's list of active devices.
136 * @dev: Device to be removed from the list.
138 void device_pm_remove(struct device *dev)
140 pr_debug("PM: Removing info for %s:%s\n",
141 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
142 complete_all(&dev->power.completion);
143 mutex_lock(&dpm_list_mtx);
144 list_del_init(&dev->power.entry);
145 mutex_unlock(&dpm_list_mtx);
146 device_wakeup_disable(dev);
147 pm_runtime_remove(dev);
151 * device_pm_move_before - Move device in the PM core's list of active devices.
152 * @deva: Device to move in dpm_list.
153 * @devb: Device @deva should come before.
155 void device_pm_move_before(struct device *deva, struct device *devb)
157 pr_debug("PM: Moving %s:%s before %s:%s\n",
158 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
159 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
160 /* Delete deva from dpm_list and reinsert before devb. */
161 list_move_tail(&deva->power.entry, &devb->power.entry);
165 * device_pm_move_after - Move device in the PM core's list of active devices.
166 * @deva: Device to move in dpm_list.
167 * @devb: Device @deva should come after.
169 void device_pm_move_after(struct device *deva, struct device *devb)
171 pr_debug("PM: Moving %s:%s after %s:%s\n",
172 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
173 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
174 /* Delete deva from dpm_list and reinsert after devb. */
175 list_move(&deva->power.entry, &devb->power.entry);
179 * device_pm_move_last - Move device to end of the PM core's list of devices.
180 * @dev: Device to move in dpm_list.
182 void device_pm_move_last(struct device *dev)
184 pr_debug("PM: Moving %s:%s to end of list\n",
185 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
186 list_move_tail(&dev->power.entry, &dpm_list);
189 static ktime_t initcall_debug_start(struct device *dev)
191 ktime_t calltime = ktime_set(0, 0);
193 if (pm_print_times_enabled) {
194 pr_info("calling %s+ @ %i, parent: %s\n",
195 dev_name(dev), task_pid_nr(current),
196 dev->parent ? dev_name(dev->parent) : "none");
197 calltime = ktime_get();
200 return calltime;
203 static void initcall_debug_report(struct device *dev, ktime_t calltime,
204 int error, pm_message_t state, char *info)
206 ktime_t rettime;
207 s64 nsecs;
209 rettime = ktime_get();
210 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
212 if (pm_print_times_enabled) {
213 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
214 error, (unsigned long long)nsecs >> 10);
217 trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event),
218 error);
222 * dpm_wait - Wait for a PM operation to complete.
223 * @dev: Device to wait for.
224 * @async: If unset, wait only if the device's power.async_suspend flag is set.
226 static void dpm_wait(struct device *dev, bool async)
228 if (!dev)
229 return;
231 if (async || (pm_async_enabled && dev->power.async_suspend))
232 wait_for_completion(&dev->power.completion);
235 static int dpm_wait_fn(struct device *dev, void *async_ptr)
237 dpm_wait(dev, *((bool *)async_ptr));
238 return 0;
241 static void dpm_wait_for_children(struct device *dev, bool async)
243 device_for_each_child(dev, &async, dpm_wait_fn);
247 * pm_op - Return the PM operation appropriate for given PM event.
248 * @ops: PM operations to choose from.
249 * @state: PM transition of the system being carried out.
251 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
253 switch (state.event) {
254 #ifdef CONFIG_SUSPEND
255 case PM_EVENT_SUSPEND:
256 return ops->suspend;
257 case PM_EVENT_RESUME:
258 return ops->resume;
259 #endif /* CONFIG_SUSPEND */
260 #ifdef CONFIG_HIBERNATE_CALLBACKS
261 case PM_EVENT_FREEZE:
262 case PM_EVENT_QUIESCE:
263 return ops->freeze;
264 case PM_EVENT_HIBERNATE:
265 return ops->poweroff;
266 case PM_EVENT_THAW:
267 case PM_EVENT_RECOVER:
268 return ops->thaw;
269 break;
270 case PM_EVENT_RESTORE:
271 return ops->restore;
272 #endif /* CONFIG_HIBERNATE_CALLBACKS */
275 return NULL;
279 * pm_late_early_op - Return the PM operation appropriate for given PM event.
280 * @ops: PM operations to choose from.
281 * @state: PM transition of the system being carried out.
283 * Runtime PM is disabled for @dev while this function is being executed.
285 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
286 pm_message_t state)
288 switch (state.event) {
289 #ifdef CONFIG_SUSPEND
290 case PM_EVENT_SUSPEND:
291 return ops->suspend_late;
292 case PM_EVENT_RESUME:
293 return ops->resume_early;
294 #endif /* CONFIG_SUSPEND */
295 #ifdef CONFIG_HIBERNATE_CALLBACKS
296 case PM_EVENT_FREEZE:
297 case PM_EVENT_QUIESCE:
298 return ops->freeze_late;
299 case PM_EVENT_HIBERNATE:
300 return ops->poweroff_late;
301 case PM_EVENT_THAW:
302 case PM_EVENT_RECOVER:
303 return ops->thaw_early;
304 case PM_EVENT_RESTORE:
305 return ops->restore_early;
306 #endif /* CONFIG_HIBERNATE_CALLBACKS */
309 return NULL;
313 * pm_noirq_op - Return the PM operation appropriate for given PM event.
314 * @ops: PM operations to choose from.
315 * @state: PM transition of the system being carried out.
317 * The driver of @dev will not receive interrupts while this function is being
318 * executed.
320 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
322 switch (state.event) {
323 #ifdef CONFIG_SUSPEND
324 case PM_EVENT_SUSPEND:
325 return ops->suspend_noirq;
326 case PM_EVENT_RESUME:
327 return ops->resume_noirq;
328 #endif /* CONFIG_SUSPEND */
329 #ifdef CONFIG_HIBERNATE_CALLBACKS
330 case PM_EVENT_FREEZE:
331 case PM_EVENT_QUIESCE:
332 return ops->freeze_noirq;
333 case PM_EVENT_HIBERNATE:
334 return ops->poweroff_noirq;
335 case PM_EVENT_THAW:
336 case PM_EVENT_RECOVER:
337 return ops->thaw_noirq;
338 case PM_EVENT_RESTORE:
339 return ops->restore_noirq;
340 #endif /* CONFIG_HIBERNATE_CALLBACKS */
343 return NULL;
346 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
348 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
349 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
350 ", may wakeup" : "");
353 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
354 int error)
356 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
357 dev_name(dev), pm_verb(state.event), info, error);
360 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
362 ktime_t calltime;
363 u64 usecs64;
364 int usecs;
366 calltime = ktime_get();
367 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
368 do_div(usecs64, NSEC_PER_USEC);
369 usecs = usecs64;
370 if (usecs == 0)
371 usecs = 1;
372 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
373 info ?: "", info ? " " : "", pm_verb(state.event),
374 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
377 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
378 pm_message_t state, char *info)
380 ktime_t calltime;
381 int error;
383 if (!cb)
384 return 0;
386 calltime = initcall_debug_start(dev);
388 pm_dev_dbg(dev, state, info);
389 error = cb(dev);
390 suspend_report_result(cb, error);
392 initcall_debug_report(dev, calltime, error, state, info);
394 return error;
397 #ifdef CONFIG_DPM_WATCHDOG
398 struct dpm_watchdog {
399 struct device *dev;
400 struct task_struct *tsk;
401 struct timer_list timer;
404 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
405 struct dpm_watchdog wd
408 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
409 * @data: Watchdog object address.
411 * Called when a driver has timed out suspending or resuming.
412 * There's not much we can do here to recover so panic() to
413 * capture a crash-dump in pstore.
415 static void dpm_watchdog_handler(unsigned long data)
417 struct dpm_watchdog *wd = (void *)data;
419 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
420 show_stack(wd->tsk, NULL);
421 panic("%s %s: unrecoverable failure\n",
422 dev_driver_string(wd->dev), dev_name(wd->dev));
426 * dpm_watchdog_set - Enable pm watchdog for given device.
427 * @wd: Watchdog. Must be allocated on the stack.
428 * @dev: Device to handle.
430 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
432 struct timer_list *timer = &wd->timer;
434 wd->dev = dev;
435 wd->tsk = current;
437 init_timer_on_stack(timer);
438 /* use same timeout value for both suspend and resume */
439 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
440 timer->function = dpm_watchdog_handler;
441 timer->data = (unsigned long)wd;
442 add_timer(timer);
446 * dpm_watchdog_clear - Disable suspend/resume watchdog.
447 * @wd: Watchdog to disable.
449 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
451 struct timer_list *timer = &wd->timer;
453 del_timer_sync(timer);
454 destroy_timer_on_stack(timer);
456 #else
457 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
458 #define dpm_watchdog_set(x, y)
459 #define dpm_watchdog_clear(x)
460 #endif
462 /*------------------------- Resume routines -------------------------*/
465 * device_resume_noirq - Execute an "early resume" callback for given device.
466 * @dev: Device to handle.
467 * @state: PM transition of the system being carried out.
469 * The driver of @dev will not receive interrupts while this function is being
470 * executed.
472 static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
474 pm_callback_t callback = NULL;
475 char *info = NULL;
476 int error = 0;
478 TRACE_DEVICE(dev);
479 TRACE_RESUME(0);
481 if (dev->power.syscore)
482 goto Out;
484 if (!dev->power.is_noirq_suspended)
485 goto Out;
487 dpm_wait(dev->parent, async);
489 if (dev->pm_domain) {
490 info = "noirq power domain ";
491 callback = pm_noirq_op(&dev->pm_domain->ops, state);
492 } else if (dev->type && dev->type->pm) {
493 info = "noirq type ";
494 callback = pm_noirq_op(dev->type->pm, state);
495 } else if (dev->class && dev->class->pm) {
496 info = "noirq class ";
497 callback = pm_noirq_op(dev->class->pm, state);
498 } else if (dev->bus && dev->bus->pm) {
499 info = "noirq bus ";
500 callback = pm_noirq_op(dev->bus->pm, state);
503 if (!callback && dev->driver && dev->driver->pm) {
504 info = "noirq driver ";
505 callback = pm_noirq_op(dev->driver->pm, state);
508 error = dpm_run_callback(callback, dev, state, info);
509 dev->power.is_noirq_suspended = false;
511 Out:
512 complete_all(&dev->power.completion);
513 TRACE_RESUME(error);
514 return error;
517 static bool is_async(struct device *dev)
519 return dev->power.async_suspend && pm_async_enabled
520 && !pm_trace_is_enabled();
523 static void async_resume_noirq(void *data, async_cookie_t cookie)
525 struct device *dev = (struct device *)data;
526 int error;
528 error = device_resume_noirq(dev, pm_transition, true);
529 if (error)
530 pm_dev_err(dev, pm_transition, " async", error);
532 put_device(dev);
536 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
537 * @state: PM transition of the system being carried out.
539 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
540 * enable device drivers to receive interrupts.
542 static void dpm_resume_noirq(pm_message_t state)
544 struct device *dev;
545 ktime_t starttime = ktime_get();
547 mutex_lock(&dpm_list_mtx);
548 pm_transition = state;
551 * Advanced the async threads upfront,
552 * in case the starting of async threads is
553 * delayed by non-async resuming devices.
555 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
556 reinit_completion(&dev->power.completion);
557 if (is_async(dev)) {
558 get_device(dev);
559 async_schedule(async_resume_noirq, dev);
563 while (!list_empty(&dpm_noirq_list)) {
564 dev = to_device(dpm_noirq_list.next);
565 get_device(dev);
566 list_move_tail(&dev->power.entry, &dpm_late_early_list);
567 mutex_unlock(&dpm_list_mtx);
569 if (!is_async(dev)) {
570 int error;
572 error = device_resume_noirq(dev, state, false);
573 if (error) {
574 suspend_stats.failed_resume_noirq++;
575 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
576 dpm_save_failed_dev(dev_name(dev));
577 pm_dev_err(dev, state, " noirq", error);
581 mutex_lock(&dpm_list_mtx);
582 put_device(dev);
584 mutex_unlock(&dpm_list_mtx);
585 async_synchronize_full();
586 dpm_show_time(starttime, state, "noirq");
587 resume_device_irqs();
588 cpuidle_resume();
592 * device_resume_early - Execute an "early resume" callback for given device.
593 * @dev: Device to handle.
594 * @state: PM transition of the system being carried out.
596 * Runtime PM is disabled for @dev while this function is being executed.
598 static int device_resume_early(struct device *dev, pm_message_t state, bool async)
600 pm_callback_t callback = NULL;
601 char *info = NULL;
602 int error = 0;
604 TRACE_DEVICE(dev);
605 TRACE_RESUME(0);
607 if (dev->power.syscore)
608 goto Out;
610 if (!dev->power.is_late_suspended)
611 goto Out;
613 dpm_wait(dev->parent, async);
615 if (dev->pm_domain) {
616 info = "early power domain ";
617 callback = pm_late_early_op(&dev->pm_domain->ops, state);
618 } else if (dev->type && dev->type->pm) {
619 info = "early type ";
620 callback = pm_late_early_op(dev->type->pm, state);
621 } else if (dev->class && dev->class->pm) {
622 info = "early class ";
623 callback = pm_late_early_op(dev->class->pm, state);
624 } else if (dev->bus && dev->bus->pm) {
625 info = "early bus ";
626 callback = pm_late_early_op(dev->bus->pm, state);
629 if (!callback && dev->driver && dev->driver->pm) {
630 info = "early driver ";
631 callback = pm_late_early_op(dev->driver->pm, state);
634 error = dpm_run_callback(callback, dev, state, info);
635 dev->power.is_late_suspended = false;
637 Out:
638 TRACE_RESUME(error);
640 pm_runtime_enable(dev);
641 complete_all(&dev->power.completion);
642 return error;
645 static void async_resume_early(void *data, async_cookie_t cookie)
647 struct device *dev = (struct device *)data;
648 int error;
650 error = device_resume_early(dev, pm_transition, true);
651 if (error)
652 pm_dev_err(dev, pm_transition, " async", error);
654 put_device(dev);
658 * dpm_resume_early - Execute "early resume" callbacks for all devices.
659 * @state: PM transition of the system being carried out.
661 static void dpm_resume_early(pm_message_t state)
663 struct device *dev;
664 ktime_t starttime = ktime_get();
666 mutex_lock(&dpm_list_mtx);
667 pm_transition = state;
670 * Advanced the async threads upfront,
671 * in case the starting of async threads is
672 * delayed by non-async resuming devices.
674 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
675 reinit_completion(&dev->power.completion);
676 if (is_async(dev)) {
677 get_device(dev);
678 async_schedule(async_resume_early, dev);
682 while (!list_empty(&dpm_late_early_list)) {
683 dev = to_device(dpm_late_early_list.next);
684 get_device(dev);
685 list_move_tail(&dev->power.entry, &dpm_suspended_list);
686 mutex_unlock(&dpm_list_mtx);
688 if (!is_async(dev)) {
689 int error;
691 error = device_resume_early(dev, state, false);
692 if (error) {
693 suspend_stats.failed_resume_early++;
694 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
695 dpm_save_failed_dev(dev_name(dev));
696 pm_dev_err(dev, state, " early", error);
699 mutex_lock(&dpm_list_mtx);
700 put_device(dev);
702 mutex_unlock(&dpm_list_mtx);
703 async_synchronize_full();
704 dpm_show_time(starttime, state, "early");
708 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
709 * @state: PM transition of the system being carried out.
711 void dpm_resume_start(pm_message_t state)
713 dpm_resume_noirq(state);
714 dpm_resume_early(state);
716 EXPORT_SYMBOL_GPL(dpm_resume_start);
719 * device_resume - Execute "resume" callbacks for given device.
720 * @dev: Device to handle.
721 * @state: PM transition of the system being carried out.
722 * @async: If true, the device is being resumed asynchronously.
724 static int device_resume(struct device *dev, pm_message_t state, bool async)
726 pm_callback_t callback = NULL;
727 char *info = NULL;
728 int error = 0;
729 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
731 TRACE_DEVICE(dev);
732 TRACE_RESUME(0);
734 if (dev->power.syscore)
735 goto Complete;
737 dpm_wait(dev->parent, async);
738 dpm_watchdog_set(&wd, dev);
739 device_lock(dev);
742 * This is a fib. But we'll allow new children to be added below
743 * a resumed device, even if the device hasn't been completed yet.
745 dev->power.is_prepared = false;
747 if (!dev->power.is_suspended)
748 goto Unlock;
750 if (dev->pm_domain) {
751 info = "power domain ";
752 callback = pm_op(&dev->pm_domain->ops, state);
753 goto Driver;
756 if (dev->type && dev->type->pm) {
757 info = "type ";
758 callback = pm_op(dev->type->pm, state);
759 goto Driver;
762 if (dev->class) {
763 if (dev->class->pm) {
764 info = "class ";
765 callback = pm_op(dev->class->pm, state);
766 goto Driver;
767 } else if (dev->class->resume) {
768 info = "legacy class ";
769 callback = dev->class->resume;
770 goto End;
774 if (dev->bus) {
775 if (dev->bus->pm) {
776 info = "bus ";
777 callback = pm_op(dev->bus->pm, state);
778 } else if (dev->bus->resume) {
779 info = "legacy bus ";
780 callback = dev->bus->resume;
781 goto End;
785 Driver:
786 if (!callback && dev->driver && dev->driver->pm) {
787 info = "driver ";
788 callback = pm_op(dev->driver->pm, state);
791 End:
792 error = dpm_run_callback(callback, dev, state, info);
793 dev->power.is_suspended = false;
795 Unlock:
796 device_unlock(dev);
797 dpm_watchdog_clear(&wd);
799 Complete:
800 complete_all(&dev->power.completion);
802 TRACE_RESUME(error);
804 return error;
807 static void async_resume(void *data, async_cookie_t cookie)
809 struct device *dev = (struct device *)data;
810 int error;
812 error = device_resume(dev, pm_transition, true);
813 if (error)
814 pm_dev_err(dev, pm_transition, " async", error);
815 put_device(dev);
819 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
820 * @state: PM transition of the system being carried out.
822 * Execute the appropriate "resume" callback for all devices whose status
823 * indicates that they are suspended.
825 void dpm_resume(pm_message_t state)
827 struct device *dev;
828 ktime_t starttime = ktime_get();
830 might_sleep();
832 mutex_lock(&dpm_list_mtx);
833 pm_transition = state;
834 async_error = 0;
836 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
837 reinit_completion(&dev->power.completion);
838 if (is_async(dev)) {
839 get_device(dev);
840 async_schedule(async_resume, dev);
844 while (!list_empty(&dpm_suspended_list)) {
845 dev = to_device(dpm_suspended_list.next);
846 get_device(dev);
847 if (!is_async(dev)) {
848 int error;
850 mutex_unlock(&dpm_list_mtx);
852 error = device_resume(dev, state, false);
853 if (error) {
854 suspend_stats.failed_resume++;
855 dpm_save_failed_step(SUSPEND_RESUME);
856 dpm_save_failed_dev(dev_name(dev));
857 pm_dev_err(dev, state, "", error);
860 mutex_lock(&dpm_list_mtx);
862 if (!list_empty(&dev->power.entry))
863 list_move_tail(&dev->power.entry, &dpm_prepared_list);
864 put_device(dev);
866 mutex_unlock(&dpm_list_mtx);
867 async_synchronize_full();
868 dpm_show_time(starttime, state, NULL);
872 * device_complete - Complete a PM transition for given device.
873 * @dev: Device to handle.
874 * @state: PM transition of the system being carried out.
876 static void device_complete(struct device *dev, pm_message_t state)
878 void (*callback)(struct device *) = NULL;
879 char *info = NULL;
881 if (dev->power.syscore)
882 return;
884 device_lock(dev);
886 if (dev->pm_domain) {
887 info = "completing power domain ";
888 callback = dev->pm_domain->ops.complete;
889 } else if (dev->type && dev->type->pm) {
890 info = "completing type ";
891 callback = dev->type->pm->complete;
892 } else if (dev->class && dev->class->pm) {
893 info = "completing class ";
894 callback = dev->class->pm->complete;
895 } else if (dev->bus && dev->bus->pm) {
896 info = "completing bus ";
897 callback = dev->bus->pm->complete;
900 if (!callback && dev->driver && dev->driver->pm) {
901 info = "completing driver ";
902 callback = dev->driver->pm->complete;
905 if (callback) {
906 pm_dev_dbg(dev, state, info);
907 callback(dev);
910 device_unlock(dev);
912 pm_runtime_put(dev);
916 * dpm_complete - Complete a PM transition for all non-sysdev devices.
917 * @state: PM transition of the system being carried out.
919 * Execute the ->complete() callbacks for all devices whose PM status is not
920 * DPM_ON (this allows new devices to be registered).
922 void dpm_complete(pm_message_t state)
924 struct list_head list;
926 might_sleep();
928 INIT_LIST_HEAD(&list);
929 mutex_lock(&dpm_list_mtx);
930 while (!list_empty(&dpm_prepared_list)) {
931 struct device *dev = to_device(dpm_prepared_list.prev);
933 get_device(dev);
934 dev->power.is_prepared = false;
935 list_move(&dev->power.entry, &list);
936 mutex_unlock(&dpm_list_mtx);
938 device_complete(dev, state);
940 mutex_lock(&dpm_list_mtx);
941 put_device(dev);
943 list_splice(&list, &dpm_list);
944 mutex_unlock(&dpm_list_mtx);
948 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
949 * @state: PM transition of the system being carried out.
951 * Execute "resume" callbacks for all devices and complete the PM transition of
952 * the system.
954 void dpm_resume_end(pm_message_t state)
956 dpm_resume(state);
957 dpm_complete(state);
959 EXPORT_SYMBOL_GPL(dpm_resume_end);
962 /*------------------------- Suspend routines -------------------------*/
965 * resume_event - Return a "resume" message for given "suspend" sleep state.
966 * @sleep_state: PM message representing a sleep state.
968 * Return a PM message representing the resume event corresponding to given
969 * sleep state.
971 static pm_message_t resume_event(pm_message_t sleep_state)
973 switch (sleep_state.event) {
974 case PM_EVENT_SUSPEND:
975 return PMSG_RESUME;
976 case PM_EVENT_FREEZE:
977 case PM_EVENT_QUIESCE:
978 return PMSG_RECOVER;
979 case PM_EVENT_HIBERNATE:
980 return PMSG_RESTORE;
982 return PMSG_ON;
986 * device_suspend_noirq - Execute a "late suspend" callback for given device.
987 * @dev: Device to handle.
988 * @state: PM transition of the system being carried out.
990 * The driver of @dev will not receive interrupts while this function is being
991 * executed.
993 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
995 pm_callback_t callback = NULL;
996 char *info = NULL;
997 int error = 0;
999 if (async_error)
1000 goto Complete;
1002 if (pm_wakeup_pending()) {
1003 async_error = -EBUSY;
1004 goto Complete;
1007 if (dev->power.syscore)
1008 goto Complete;
1010 dpm_wait_for_children(dev, async);
1012 if (dev->pm_domain) {
1013 info = "noirq power domain ";
1014 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1015 } else if (dev->type && dev->type->pm) {
1016 info = "noirq type ";
1017 callback = pm_noirq_op(dev->type->pm, state);
1018 } else if (dev->class && dev->class->pm) {
1019 info = "noirq class ";
1020 callback = pm_noirq_op(dev->class->pm, state);
1021 } else if (dev->bus && dev->bus->pm) {
1022 info = "noirq bus ";
1023 callback = pm_noirq_op(dev->bus->pm, state);
1026 if (!callback && dev->driver && dev->driver->pm) {
1027 info = "noirq driver ";
1028 callback = pm_noirq_op(dev->driver->pm, state);
1031 error = dpm_run_callback(callback, dev, state, info);
1032 if (!error)
1033 dev->power.is_noirq_suspended = true;
1034 else
1035 async_error = error;
1037 Complete:
1038 complete_all(&dev->power.completion);
1039 return error;
1042 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1044 struct device *dev = (struct device *)data;
1045 int error;
1047 error = __device_suspend_noirq(dev, pm_transition, true);
1048 if (error) {
1049 dpm_save_failed_dev(dev_name(dev));
1050 pm_dev_err(dev, pm_transition, " async", error);
1053 put_device(dev);
1056 static int device_suspend_noirq(struct device *dev)
1058 reinit_completion(&dev->power.completion);
1060 if (pm_async_enabled && dev->power.async_suspend) {
1061 get_device(dev);
1062 async_schedule(async_suspend_noirq, dev);
1063 return 0;
1065 return __device_suspend_noirq(dev, pm_transition, false);
1069 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1070 * @state: PM transition of the system being carried out.
1072 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1073 * handlers for all non-sysdev devices.
1075 static int dpm_suspend_noirq(pm_message_t state)
1077 ktime_t starttime = ktime_get();
1078 int error = 0;
1080 cpuidle_pause();
1081 suspend_device_irqs();
1082 mutex_lock(&dpm_list_mtx);
1083 pm_transition = state;
1084 async_error = 0;
1086 while (!list_empty(&dpm_late_early_list)) {
1087 struct device *dev = to_device(dpm_late_early_list.prev);
1089 get_device(dev);
1090 mutex_unlock(&dpm_list_mtx);
1092 error = device_suspend_noirq(dev);
1094 mutex_lock(&dpm_list_mtx);
1095 if (error) {
1096 pm_dev_err(dev, state, " noirq", error);
1097 dpm_save_failed_dev(dev_name(dev));
1098 put_device(dev);
1099 break;
1101 if (!list_empty(&dev->power.entry))
1102 list_move(&dev->power.entry, &dpm_noirq_list);
1103 put_device(dev);
1105 if (async_error)
1106 break;
1108 mutex_unlock(&dpm_list_mtx);
1109 async_synchronize_full();
1110 if (!error)
1111 error = async_error;
1113 if (error) {
1114 suspend_stats.failed_suspend_noirq++;
1115 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1116 dpm_resume_noirq(resume_event(state));
1117 } else {
1118 dpm_show_time(starttime, state, "noirq");
1120 return error;
1124 * device_suspend_late - Execute a "late suspend" callback for given device.
1125 * @dev: Device to handle.
1126 * @state: PM transition of the system being carried out.
1128 * Runtime PM is disabled for @dev while this function is being executed.
1130 static int device_suspend_late(struct device *dev, pm_message_t state)
1132 pm_callback_t callback = NULL;
1133 char *info = NULL;
1134 int error;
1136 __pm_runtime_disable(dev, false);
1138 if (dev->power.syscore)
1139 return 0;
1141 if (dev->pm_domain) {
1142 info = "late power domain ";
1143 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1144 } else if (dev->type && dev->type->pm) {
1145 info = "late type ";
1146 callback = pm_late_early_op(dev->type->pm, state);
1147 } else if (dev->class && dev->class->pm) {
1148 info = "late class ";
1149 callback = pm_late_early_op(dev->class->pm, state);
1150 } else if (dev->bus && dev->bus->pm) {
1151 info = "late bus ";
1152 callback = pm_late_early_op(dev->bus->pm, state);
1155 if (!callback && dev->driver && dev->driver->pm) {
1156 info = "late driver ";
1157 callback = pm_late_early_op(dev->driver->pm, state);
1160 error = dpm_run_callback(callback, dev, state, info);
1161 if (!error)
1162 dev->power.is_late_suspended = true;
1164 return error;
1168 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1169 * @state: PM transition of the system being carried out.
1171 static int dpm_suspend_late(pm_message_t state)
1173 ktime_t starttime = ktime_get();
1174 int error = 0;
1176 mutex_lock(&dpm_list_mtx);
1177 while (!list_empty(&dpm_suspended_list)) {
1178 struct device *dev = to_device(dpm_suspended_list.prev);
1180 get_device(dev);
1181 mutex_unlock(&dpm_list_mtx);
1183 error = device_suspend_late(dev, state);
1185 mutex_lock(&dpm_list_mtx);
1186 if (error) {
1187 pm_dev_err(dev, state, " late", error);
1188 suspend_stats.failed_suspend_late++;
1189 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1190 dpm_save_failed_dev(dev_name(dev));
1191 put_device(dev);
1192 break;
1194 if (!list_empty(&dev->power.entry))
1195 list_move(&dev->power.entry, &dpm_late_early_list);
1196 put_device(dev);
1198 if (pm_wakeup_pending()) {
1199 error = -EBUSY;
1200 break;
1203 mutex_unlock(&dpm_list_mtx);
1204 if (error)
1205 dpm_resume_early(resume_event(state));
1206 else
1207 dpm_show_time(starttime, state, "late");
1209 return error;
1213 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1214 * @state: PM transition of the system being carried out.
1216 int dpm_suspend_end(pm_message_t state)
1218 int error = dpm_suspend_late(state);
1219 if (error)
1220 return error;
1222 error = dpm_suspend_noirq(state);
1223 if (error) {
1224 dpm_resume_early(resume_event(state));
1225 return error;
1228 return 0;
1230 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1233 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1234 * @dev: Device to suspend.
1235 * @state: PM transition of the system being carried out.
1236 * @cb: Suspend callback to execute.
1238 static int legacy_suspend(struct device *dev, pm_message_t state,
1239 int (*cb)(struct device *dev, pm_message_t state),
1240 char *info)
1242 int error;
1243 ktime_t calltime;
1245 calltime = initcall_debug_start(dev);
1247 error = cb(dev, state);
1248 suspend_report_result(cb, error);
1250 initcall_debug_report(dev, calltime, error, state, info);
1252 return error;
1256 * device_suspend - Execute "suspend" callbacks for given device.
1257 * @dev: Device to handle.
1258 * @state: PM transition of the system being carried out.
1259 * @async: If true, the device is being suspended asynchronously.
1261 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1263 pm_callback_t callback = NULL;
1264 char *info = NULL;
1265 int error = 0;
1266 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1268 dpm_wait_for_children(dev, async);
1270 if (async_error)
1271 goto Complete;
1274 * If a device configured to wake up the system from sleep states
1275 * has been suspended at run time and there's a resume request pending
1276 * for it, this is equivalent to the device signaling wakeup, so the
1277 * system suspend operation should be aborted.
1279 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1280 pm_wakeup_event(dev, 0);
1282 if (pm_wakeup_pending()) {
1283 async_error = -EBUSY;
1284 goto Complete;
1287 if (dev->power.syscore)
1288 goto Complete;
1290 dpm_watchdog_set(&wd, dev);
1291 device_lock(dev);
1293 if (dev->pm_domain) {
1294 info = "power domain ";
1295 callback = pm_op(&dev->pm_domain->ops, state);
1296 goto Run;
1299 if (dev->type && dev->type->pm) {
1300 info = "type ";
1301 callback = pm_op(dev->type->pm, state);
1302 goto Run;
1305 if (dev->class) {
1306 if (dev->class->pm) {
1307 info = "class ";
1308 callback = pm_op(dev->class->pm, state);
1309 goto Run;
1310 } else if (dev->class->suspend) {
1311 pm_dev_dbg(dev, state, "legacy class ");
1312 error = legacy_suspend(dev, state, dev->class->suspend,
1313 "legacy class ");
1314 goto End;
1318 if (dev->bus) {
1319 if (dev->bus->pm) {
1320 info = "bus ";
1321 callback = pm_op(dev->bus->pm, state);
1322 } else if (dev->bus->suspend) {
1323 pm_dev_dbg(dev, state, "legacy bus ");
1324 error = legacy_suspend(dev, state, dev->bus->suspend,
1325 "legacy bus ");
1326 goto End;
1330 Run:
1331 if (!callback && dev->driver && dev->driver->pm) {
1332 info = "driver ";
1333 callback = pm_op(dev->driver->pm, state);
1336 error = dpm_run_callback(callback, dev, state, info);
1338 End:
1339 if (!error) {
1340 dev->power.is_suspended = true;
1341 if (dev->power.wakeup_path
1342 && dev->parent && !dev->parent->power.ignore_children)
1343 dev->parent->power.wakeup_path = true;
1346 device_unlock(dev);
1347 dpm_watchdog_clear(&wd);
1349 Complete:
1350 complete_all(&dev->power.completion);
1351 if (error)
1352 async_error = error;
1354 return error;
1357 static void async_suspend(void *data, async_cookie_t cookie)
1359 struct device *dev = (struct device *)data;
1360 int error;
1362 error = __device_suspend(dev, pm_transition, true);
1363 if (error) {
1364 dpm_save_failed_dev(dev_name(dev));
1365 pm_dev_err(dev, pm_transition, " async", error);
1368 put_device(dev);
1371 static int device_suspend(struct device *dev)
1373 reinit_completion(&dev->power.completion);
1375 if (pm_async_enabled && dev->power.async_suspend) {
1376 get_device(dev);
1377 async_schedule(async_suspend, dev);
1378 return 0;
1381 return __device_suspend(dev, pm_transition, false);
1385 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1386 * @state: PM transition of the system being carried out.
1388 int dpm_suspend(pm_message_t state)
1390 ktime_t starttime = ktime_get();
1391 int error = 0;
1393 might_sleep();
1395 mutex_lock(&dpm_list_mtx);
1396 pm_transition = state;
1397 async_error = 0;
1398 while (!list_empty(&dpm_prepared_list)) {
1399 struct device *dev = to_device(dpm_prepared_list.prev);
1401 get_device(dev);
1402 mutex_unlock(&dpm_list_mtx);
1404 error = device_suspend(dev);
1406 mutex_lock(&dpm_list_mtx);
1407 if (error) {
1408 pm_dev_err(dev, state, "", error);
1409 dpm_save_failed_dev(dev_name(dev));
1410 put_device(dev);
1411 break;
1413 if (!list_empty(&dev->power.entry))
1414 list_move(&dev->power.entry, &dpm_suspended_list);
1415 put_device(dev);
1416 if (async_error)
1417 break;
1419 mutex_unlock(&dpm_list_mtx);
1420 async_synchronize_full();
1421 if (!error)
1422 error = async_error;
1423 if (error) {
1424 suspend_stats.failed_suspend++;
1425 dpm_save_failed_step(SUSPEND_SUSPEND);
1426 } else
1427 dpm_show_time(starttime, state, NULL);
1428 return error;
1432 * device_prepare - Prepare a device for system power transition.
1433 * @dev: Device to handle.
1434 * @state: PM transition of the system being carried out.
1436 * Execute the ->prepare() callback(s) for given device. No new children of the
1437 * device may be registered after this function has returned.
1439 static int device_prepare(struct device *dev, pm_message_t state)
1441 int (*callback)(struct device *) = NULL;
1442 char *info = NULL;
1443 int error = 0;
1445 if (dev->power.syscore)
1446 return 0;
1449 * If a device's parent goes into runtime suspend at the wrong time,
1450 * it won't be possible to resume the device. To prevent this we
1451 * block runtime suspend here, during the prepare phase, and allow
1452 * it again during the complete phase.
1454 pm_runtime_get_noresume(dev);
1456 device_lock(dev);
1458 dev->power.wakeup_path = device_may_wakeup(dev);
1460 if (dev->pm_domain) {
1461 info = "preparing power domain ";
1462 callback = dev->pm_domain->ops.prepare;
1463 } else if (dev->type && dev->type->pm) {
1464 info = "preparing type ";
1465 callback = dev->type->pm->prepare;
1466 } else if (dev->class && dev->class->pm) {
1467 info = "preparing class ";
1468 callback = dev->class->pm->prepare;
1469 } else if (dev->bus && dev->bus->pm) {
1470 info = "preparing bus ";
1471 callback = dev->bus->pm->prepare;
1474 if (!callback && dev->driver && dev->driver->pm) {
1475 info = "preparing driver ";
1476 callback = dev->driver->pm->prepare;
1479 if (callback) {
1480 error = callback(dev);
1481 suspend_report_result(callback, error);
1484 device_unlock(dev);
1486 if (error)
1487 pm_runtime_put(dev);
1489 return error;
1493 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1494 * @state: PM transition of the system being carried out.
1496 * Execute the ->prepare() callback(s) for all devices.
1498 int dpm_prepare(pm_message_t state)
1500 int error = 0;
1502 might_sleep();
1504 mutex_lock(&dpm_list_mtx);
1505 while (!list_empty(&dpm_list)) {
1506 struct device *dev = to_device(dpm_list.next);
1508 get_device(dev);
1509 mutex_unlock(&dpm_list_mtx);
1511 error = device_prepare(dev, state);
1513 mutex_lock(&dpm_list_mtx);
1514 if (error) {
1515 if (error == -EAGAIN) {
1516 put_device(dev);
1517 error = 0;
1518 continue;
1520 printk(KERN_INFO "PM: Device %s not prepared "
1521 "for power transition: code %d\n",
1522 dev_name(dev), error);
1523 put_device(dev);
1524 break;
1526 dev->power.is_prepared = true;
1527 if (!list_empty(&dev->power.entry))
1528 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1529 put_device(dev);
1531 mutex_unlock(&dpm_list_mtx);
1532 return error;
1536 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1537 * @state: PM transition of the system being carried out.
1539 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1540 * callbacks for them.
1542 int dpm_suspend_start(pm_message_t state)
1544 int error;
1546 error = dpm_prepare(state);
1547 if (error) {
1548 suspend_stats.failed_prepare++;
1549 dpm_save_failed_step(SUSPEND_PREPARE);
1550 } else
1551 error = dpm_suspend(state);
1552 return error;
1554 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1556 void __suspend_report_result(const char *function, void *fn, int ret)
1558 if (ret)
1559 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1561 EXPORT_SYMBOL_GPL(__suspend_report_result);
1564 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1565 * @dev: Device to wait for.
1566 * @subordinate: Device that needs to wait for @dev.
1568 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1570 dpm_wait(dev, subordinate->power.async_suspend);
1571 return async_error;
1573 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1576 * dpm_for_each_dev - device iterator.
1577 * @data: data for the callback.
1578 * @fn: function to be called for each device.
1580 * Iterate over devices in dpm_list, and call @fn for each device,
1581 * passing it @data.
1583 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1585 struct device *dev;
1587 if (!fn)
1588 return;
1590 device_pm_lock();
1591 list_for_each_entry(dev, &dpm_list, power.entry)
1592 fn(dev, data);
1593 device_pm_unlock();
1595 EXPORT_SYMBOL_GPL(dpm_for_each_dev);