Revert "PM QoS: Implement per-device PM QoS constraints"
[linux-2.6/next.git] / drivers / base / power / main.c
blob30f2a853a52ca30f0122258e0cbf5ca4c3b6af17
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>
32 #include "../base.h"
33 #include "power.h"
36 * The entries in the dpm_list list are in a depth first order, simply
37 * because children are guaranteed to be discovered after parents, and
38 * are inserted at the back of the list on discovery.
40 * Since device_pm_add() may be called with a device lock held,
41 * we must never try to acquire a device lock while holding
42 * dpm_list_mutex.
45 LIST_HEAD(dpm_list);
46 LIST_HEAD(dpm_prepared_list);
47 LIST_HEAD(dpm_suspended_list);
48 LIST_HEAD(dpm_noirq_list);
50 struct suspend_stats suspend_stats;
51 static DEFINE_MUTEX(dpm_list_mtx);
52 static pm_message_t pm_transition;
54 static int async_error;
56 /**
57 * device_pm_init - Initialize the PM-related part of a device object.
58 * @dev: Device object being initialized.
60 void device_pm_init(struct device *dev)
62 dev->power.is_prepared = false;
63 dev->power.is_suspended = false;
64 init_completion(&dev->power.completion);
65 complete_all(&dev->power.completion);
66 dev->power.wakeup = NULL;
67 spin_lock_init(&dev->power.lock);
68 pm_runtime_init(dev);
69 INIT_LIST_HEAD(&dev->power.entry);
72 /**
73 * device_pm_lock - Lock the list of active devices used by the PM core.
75 void device_pm_lock(void)
77 mutex_lock(&dpm_list_mtx);
80 /**
81 * device_pm_unlock - Unlock the list of active devices used by the PM core.
83 void device_pm_unlock(void)
85 mutex_unlock(&dpm_list_mtx);
88 /**
89 * device_pm_add - Add a device to the PM core's list of active devices.
90 * @dev: Device to add to the list.
92 void device_pm_add(struct device *dev)
94 pr_debug("PM: Adding info for %s:%s\n",
95 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
96 mutex_lock(&dpm_list_mtx);
97 if (dev->parent && dev->parent->power.is_prepared)
98 dev_warn(dev, "parent %s should not be sleeping\n",
99 dev_name(dev->parent));
100 list_add_tail(&dev->power.entry, &dpm_list);
101 mutex_unlock(&dpm_list_mtx);
105 * device_pm_remove - Remove a device from the PM core's list of active devices.
106 * @dev: Device to be removed from the list.
108 void device_pm_remove(struct device *dev)
110 pr_debug("PM: Removing info for %s:%s\n",
111 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
112 complete_all(&dev->power.completion);
113 mutex_lock(&dpm_list_mtx);
114 list_del_init(&dev->power.entry);
115 mutex_unlock(&dpm_list_mtx);
116 device_wakeup_disable(dev);
117 pm_runtime_remove(dev);
121 * device_pm_move_before - Move device in the PM core's list of active devices.
122 * @deva: Device to move in dpm_list.
123 * @devb: Device @deva should come before.
125 void device_pm_move_before(struct device *deva, struct device *devb)
127 pr_debug("PM: Moving %s:%s before %s:%s\n",
128 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
129 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
130 /* Delete deva from dpm_list and reinsert before devb. */
131 list_move_tail(&deva->power.entry, &devb->power.entry);
135 * device_pm_move_after - Move device in the PM core's list of active devices.
136 * @deva: Device to move in dpm_list.
137 * @devb: Device @deva should come after.
139 void device_pm_move_after(struct device *deva, struct device *devb)
141 pr_debug("PM: Moving %s:%s after %s:%s\n",
142 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
143 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
144 /* Delete deva from dpm_list and reinsert after devb. */
145 list_move(&deva->power.entry, &devb->power.entry);
149 * device_pm_move_last - Move device to end of the PM core's list of devices.
150 * @dev: Device to move in dpm_list.
152 void device_pm_move_last(struct device *dev)
154 pr_debug("PM: Moving %s:%s to end of list\n",
155 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
156 list_move_tail(&dev->power.entry, &dpm_list);
159 static ktime_t initcall_debug_start(struct device *dev)
161 ktime_t calltime = ktime_set(0, 0);
163 if (initcall_debug) {
164 pr_info("calling %s+ @ %i\n",
165 dev_name(dev), task_pid_nr(current));
166 calltime = ktime_get();
169 return calltime;
172 static void initcall_debug_report(struct device *dev, ktime_t calltime,
173 int error)
175 ktime_t delta, rettime;
177 if (initcall_debug) {
178 rettime = ktime_get();
179 delta = ktime_sub(rettime, calltime);
180 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
181 error, (unsigned long long)ktime_to_ns(delta) >> 10);
186 * dpm_wait - Wait for a PM operation to complete.
187 * @dev: Device to wait for.
188 * @async: If unset, wait only if the device's power.async_suspend flag is set.
190 static void dpm_wait(struct device *dev, bool async)
192 if (!dev)
193 return;
195 if (async || (pm_async_enabled && dev->power.async_suspend))
196 wait_for_completion(&dev->power.completion);
199 static int dpm_wait_fn(struct device *dev, void *async_ptr)
201 dpm_wait(dev, *((bool *)async_ptr));
202 return 0;
205 static void dpm_wait_for_children(struct device *dev, bool async)
207 device_for_each_child(dev, &async, dpm_wait_fn);
211 * pm_op - Execute the PM operation appropriate for given PM event.
212 * @dev: Device to handle.
213 * @ops: PM operations to choose from.
214 * @state: PM transition of the system being carried out.
216 static int pm_op(struct device *dev,
217 const struct dev_pm_ops *ops,
218 pm_message_t state)
220 int error = 0;
221 ktime_t calltime;
223 calltime = initcall_debug_start(dev);
225 switch (state.event) {
226 #ifdef CONFIG_SUSPEND
227 case PM_EVENT_SUSPEND:
228 if (ops->suspend) {
229 error = ops->suspend(dev);
230 suspend_report_result(ops->suspend, error);
232 break;
233 case PM_EVENT_RESUME:
234 if (ops->resume) {
235 error = ops->resume(dev);
236 suspend_report_result(ops->resume, error);
238 break;
239 #endif /* CONFIG_SUSPEND */
240 #ifdef CONFIG_HIBERNATE_CALLBACKS
241 case PM_EVENT_FREEZE:
242 case PM_EVENT_QUIESCE:
243 if (ops->freeze) {
244 error = ops->freeze(dev);
245 suspend_report_result(ops->freeze, error);
247 break;
248 case PM_EVENT_HIBERNATE:
249 if (ops->poweroff) {
250 error = ops->poweroff(dev);
251 suspend_report_result(ops->poweroff, error);
253 break;
254 case PM_EVENT_THAW:
255 case PM_EVENT_RECOVER:
256 if (ops->thaw) {
257 error = ops->thaw(dev);
258 suspend_report_result(ops->thaw, error);
260 break;
261 case PM_EVENT_RESTORE:
262 if (ops->restore) {
263 error = ops->restore(dev);
264 suspend_report_result(ops->restore, error);
266 break;
267 #endif /* CONFIG_HIBERNATE_CALLBACKS */
268 default:
269 error = -EINVAL;
272 initcall_debug_report(dev, calltime, error);
274 return error;
278 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
279 * @dev: Device to handle.
280 * @ops: PM operations to choose from.
281 * @state: PM transition of the system being carried out.
283 * The driver of @dev will not receive interrupts while this function is being
284 * executed.
286 static int pm_noirq_op(struct device *dev,
287 const struct dev_pm_ops *ops,
288 pm_message_t state)
290 int error = 0;
291 ktime_t calltime = ktime_set(0, 0), delta, rettime;
293 if (initcall_debug) {
294 pr_info("calling %s+ @ %i, parent: %s\n",
295 dev_name(dev), task_pid_nr(current),
296 dev->parent ? dev_name(dev->parent) : "none");
297 calltime = ktime_get();
300 switch (state.event) {
301 #ifdef CONFIG_SUSPEND
302 case PM_EVENT_SUSPEND:
303 if (ops->suspend_noirq) {
304 error = ops->suspend_noirq(dev);
305 suspend_report_result(ops->suspend_noirq, error);
307 break;
308 case PM_EVENT_RESUME:
309 if (ops->resume_noirq) {
310 error = ops->resume_noirq(dev);
311 suspend_report_result(ops->resume_noirq, error);
313 break;
314 #endif /* CONFIG_SUSPEND */
315 #ifdef CONFIG_HIBERNATE_CALLBACKS
316 case PM_EVENT_FREEZE:
317 case PM_EVENT_QUIESCE:
318 if (ops->freeze_noirq) {
319 error = ops->freeze_noirq(dev);
320 suspend_report_result(ops->freeze_noirq, error);
322 break;
323 case PM_EVENT_HIBERNATE:
324 if (ops->poweroff_noirq) {
325 error = ops->poweroff_noirq(dev);
326 suspend_report_result(ops->poweroff_noirq, error);
328 break;
329 case PM_EVENT_THAW:
330 case PM_EVENT_RECOVER:
331 if (ops->thaw_noirq) {
332 error = ops->thaw_noirq(dev);
333 suspend_report_result(ops->thaw_noirq, error);
335 break;
336 case PM_EVENT_RESTORE:
337 if (ops->restore_noirq) {
338 error = ops->restore_noirq(dev);
339 suspend_report_result(ops->restore_noirq, error);
341 break;
342 #endif /* CONFIG_HIBERNATE_CALLBACKS */
343 default:
344 error = -EINVAL;
347 if (initcall_debug) {
348 rettime = ktime_get();
349 delta = ktime_sub(rettime, calltime);
350 printk("initcall %s_i+ returned %d after %Ld usecs\n",
351 dev_name(dev), error,
352 (unsigned long long)ktime_to_ns(delta) >> 10);
355 return error;
358 static char *pm_verb(int event)
360 switch (event) {
361 case PM_EVENT_SUSPEND:
362 return "suspend";
363 case PM_EVENT_RESUME:
364 return "resume";
365 case PM_EVENT_FREEZE:
366 return "freeze";
367 case PM_EVENT_QUIESCE:
368 return "quiesce";
369 case PM_EVENT_HIBERNATE:
370 return "hibernate";
371 case PM_EVENT_THAW:
372 return "thaw";
373 case PM_EVENT_RESTORE:
374 return "restore";
375 case PM_EVENT_RECOVER:
376 return "recover";
377 default:
378 return "(unknown PM event)";
382 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
384 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
385 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
386 ", may wakeup" : "");
389 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
390 int error)
392 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
393 dev_name(dev), pm_verb(state.event), info, error);
396 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
398 ktime_t calltime;
399 u64 usecs64;
400 int usecs;
402 calltime = ktime_get();
403 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
404 do_div(usecs64, NSEC_PER_USEC);
405 usecs = usecs64;
406 if (usecs == 0)
407 usecs = 1;
408 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
409 info ?: "", info ? " " : "", pm_verb(state.event),
410 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
413 /*------------------------- Resume routines -------------------------*/
416 * device_resume_noirq - Execute an "early resume" callback for given device.
417 * @dev: Device to handle.
418 * @state: PM transition of the system being carried out.
420 * The driver of @dev will not receive interrupts while this function is being
421 * executed.
423 static int device_resume_noirq(struct device *dev, pm_message_t state)
425 int error = 0;
427 TRACE_DEVICE(dev);
428 TRACE_RESUME(0);
430 if (dev->pm_domain) {
431 pm_dev_dbg(dev, state, "EARLY power domain ");
432 error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
433 } else if (dev->type && dev->type->pm) {
434 pm_dev_dbg(dev, state, "EARLY type ");
435 error = pm_noirq_op(dev, dev->type->pm, state);
436 } else if (dev->class && dev->class->pm) {
437 pm_dev_dbg(dev, state, "EARLY class ");
438 error = pm_noirq_op(dev, dev->class->pm, state);
439 } else if (dev->bus && dev->bus->pm) {
440 pm_dev_dbg(dev, state, "EARLY ");
441 error = pm_noirq_op(dev, dev->bus->pm, state);
444 TRACE_RESUME(error);
445 return error;
449 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
450 * @state: PM transition of the system being carried out.
452 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
453 * enable device drivers to receive interrupts.
455 void dpm_resume_noirq(pm_message_t state)
457 ktime_t starttime = ktime_get();
459 mutex_lock(&dpm_list_mtx);
460 while (!list_empty(&dpm_noirq_list)) {
461 struct device *dev = to_device(dpm_noirq_list.next);
462 int error;
464 get_device(dev);
465 list_move_tail(&dev->power.entry, &dpm_suspended_list);
466 mutex_unlock(&dpm_list_mtx);
468 error = device_resume_noirq(dev, state);
469 if (error) {
470 suspend_stats.failed_resume_noirq++;
471 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
472 dpm_save_failed_dev(dev_name(dev));
473 pm_dev_err(dev, state, " early", error);
476 mutex_lock(&dpm_list_mtx);
477 put_device(dev);
479 mutex_unlock(&dpm_list_mtx);
480 dpm_show_time(starttime, state, "early");
481 resume_device_irqs();
483 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
486 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
487 * @dev: Device to resume.
488 * @cb: Resume callback to execute.
490 static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
492 int error;
493 ktime_t calltime;
495 calltime = initcall_debug_start(dev);
497 error = cb(dev);
498 suspend_report_result(cb, error);
500 initcall_debug_report(dev, calltime, error);
502 return error;
506 * device_resume - Execute "resume" callbacks for given device.
507 * @dev: Device to handle.
508 * @state: PM transition of the system being carried out.
509 * @async: If true, the device is being resumed asynchronously.
511 static int device_resume(struct device *dev, pm_message_t state, bool async)
513 int error = 0;
514 bool put = false;
516 TRACE_DEVICE(dev);
517 TRACE_RESUME(0);
519 dpm_wait(dev->parent, async);
520 device_lock(dev);
523 * This is a fib. But we'll allow new children to be added below
524 * a resumed device, even if the device hasn't been completed yet.
526 dev->power.is_prepared = false;
528 if (!dev->power.is_suspended)
529 goto Unlock;
531 pm_runtime_enable(dev);
532 put = true;
534 if (dev->pm_domain) {
535 pm_dev_dbg(dev, state, "power domain ");
536 error = pm_op(dev, &dev->pm_domain->ops, state);
537 goto End;
540 if (dev->type && dev->type->pm) {
541 pm_dev_dbg(dev, state, "type ");
542 error = pm_op(dev, dev->type->pm, state);
543 goto End;
546 if (dev->class) {
547 if (dev->class->pm) {
548 pm_dev_dbg(dev, state, "class ");
549 error = pm_op(dev, dev->class->pm, state);
550 goto End;
551 } else if (dev->class->resume) {
552 pm_dev_dbg(dev, state, "legacy class ");
553 error = legacy_resume(dev, dev->class->resume);
554 goto End;
558 if (dev->bus) {
559 if (dev->bus->pm) {
560 pm_dev_dbg(dev, state, "");
561 error = pm_op(dev, dev->bus->pm, state);
562 } else if (dev->bus->resume) {
563 pm_dev_dbg(dev, state, "legacy ");
564 error = legacy_resume(dev, dev->bus->resume);
568 End:
569 dev->power.is_suspended = false;
571 Unlock:
572 device_unlock(dev);
573 complete_all(&dev->power.completion);
575 TRACE_RESUME(error);
577 if (put)
578 pm_runtime_put_sync(dev);
580 return error;
583 static void async_resume(void *data, async_cookie_t cookie)
585 struct device *dev = (struct device *)data;
586 int error;
588 error = device_resume(dev, pm_transition, true);
589 if (error)
590 pm_dev_err(dev, pm_transition, " async", error);
591 put_device(dev);
594 static bool is_async(struct device *dev)
596 return dev->power.async_suspend && pm_async_enabled
597 && !pm_trace_is_enabled();
601 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
602 * @state: PM transition of the system being carried out.
604 * Execute the appropriate "resume" callback for all devices whose status
605 * indicates that they are suspended.
607 void dpm_resume(pm_message_t state)
609 struct device *dev;
610 ktime_t starttime = ktime_get();
612 might_sleep();
614 mutex_lock(&dpm_list_mtx);
615 pm_transition = state;
616 async_error = 0;
618 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
619 INIT_COMPLETION(dev->power.completion);
620 if (is_async(dev)) {
621 get_device(dev);
622 async_schedule(async_resume, dev);
626 while (!list_empty(&dpm_suspended_list)) {
627 dev = to_device(dpm_suspended_list.next);
628 get_device(dev);
629 if (!is_async(dev)) {
630 int error;
632 mutex_unlock(&dpm_list_mtx);
634 error = device_resume(dev, state, false);
635 if (error) {
636 suspend_stats.failed_resume++;
637 dpm_save_failed_step(SUSPEND_RESUME);
638 dpm_save_failed_dev(dev_name(dev));
639 pm_dev_err(dev, state, "", error);
642 mutex_lock(&dpm_list_mtx);
644 if (!list_empty(&dev->power.entry))
645 list_move_tail(&dev->power.entry, &dpm_prepared_list);
646 put_device(dev);
648 mutex_unlock(&dpm_list_mtx);
649 async_synchronize_full();
650 dpm_show_time(starttime, state, NULL);
654 * device_complete - Complete a PM transition for given device.
655 * @dev: Device to handle.
656 * @state: PM transition of the system being carried out.
658 static void device_complete(struct device *dev, pm_message_t state)
660 device_lock(dev);
662 if (dev->pm_domain) {
663 pm_dev_dbg(dev, state, "completing power domain ");
664 if (dev->pm_domain->ops.complete)
665 dev->pm_domain->ops.complete(dev);
666 } else if (dev->type && dev->type->pm) {
667 pm_dev_dbg(dev, state, "completing type ");
668 if (dev->type->pm->complete)
669 dev->type->pm->complete(dev);
670 } else if (dev->class && dev->class->pm) {
671 pm_dev_dbg(dev, state, "completing class ");
672 if (dev->class->pm->complete)
673 dev->class->pm->complete(dev);
674 } else if (dev->bus && dev->bus->pm) {
675 pm_dev_dbg(dev, state, "completing ");
676 if (dev->bus->pm->complete)
677 dev->bus->pm->complete(dev);
680 device_unlock(dev);
684 * dpm_complete - Complete a PM transition for all non-sysdev devices.
685 * @state: PM transition of the system being carried out.
687 * Execute the ->complete() callbacks for all devices whose PM status is not
688 * DPM_ON (this allows new devices to be registered).
690 void dpm_complete(pm_message_t state)
692 struct list_head list;
694 might_sleep();
696 INIT_LIST_HEAD(&list);
697 mutex_lock(&dpm_list_mtx);
698 while (!list_empty(&dpm_prepared_list)) {
699 struct device *dev = to_device(dpm_prepared_list.prev);
701 get_device(dev);
702 dev->power.is_prepared = false;
703 list_move(&dev->power.entry, &list);
704 mutex_unlock(&dpm_list_mtx);
706 device_complete(dev, state);
708 mutex_lock(&dpm_list_mtx);
709 put_device(dev);
711 list_splice(&list, &dpm_list);
712 mutex_unlock(&dpm_list_mtx);
716 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
717 * @state: PM transition of the system being carried out.
719 * Execute "resume" callbacks for all devices and complete the PM transition of
720 * the system.
722 void dpm_resume_end(pm_message_t state)
724 dpm_resume(state);
725 dpm_complete(state);
727 EXPORT_SYMBOL_GPL(dpm_resume_end);
730 /*------------------------- Suspend routines -------------------------*/
733 * resume_event - Return a "resume" message for given "suspend" sleep state.
734 * @sleep_state: PM message representing a sleep state.
736 * Return a PM message representing the resume event corresponding to given
737 * sleep state.
739 static pm_message_t resume_event(pm_message_t sleep_state)
741 switch (sleep_state.event) {
742 case PM_EVENT_SUSPEND:
743 return PMSG_RESUME;
744 case PM_EVENT_FREEZE:
745 case PM_EVENT_QUIESCE:
746 return PMSG_RECOVER;
747 case PM_EVENT_HIBERNATE:
748 return PMSG_RESTORE;
750 return PMSG_ON;
754 * device_suspend_noirq - Execute a "late suspend" callback for given device.
755 * @dev: Device to handle.
756 * @state: PM transition of the system being carried out.
758 * The driver of @dev will not receive interrupts while this function is being
759 * executed.
761 static int device_suspend_noirq(struct device *dev, pm_message_t state)
763 int error;
765 if (dev->pm_domain) {
766 pm_dev_dbg(dev, state, "LATE power domain ");
767 error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
768 if (error)
769 return error;
770 } else if (dev->type && dev->type->pm) {
771 pm_dev_dbg(dev, state, "LATE type ");
772 error = pm_noirq_op(dev, dev->type->pm, state);
773 if (error)
774 return error;
775 } else if (dev->class && dev->class->pm) {
776 pm_dev_dbg(dev, state, "LATE class ");
777 error = pm_noirq_op(dev, dev->class->pm, state);
778 if (error)
779 return error;
780 } else if (dev->bus && dev->bus->pm) {
781 pm_dev_dbg(dev, state, "LATE ");
782 error = pm_noirq_op(dev, dev->bus->pm, state);
783 if (error)
784 return error;
787 return 0;
791 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
792 * @state: PM transition of the system being carried out.
794 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
795 * handlers for all non-sysdev devices.
797 int dpm_suspend_noirq(pm_message_t state)
799 ktime_t starttime = ktime_get();
800 int error = 0;
802 suspend_device_irqs();
803 mutex_lock(&dpm_list_mtx);
804 while (!list_empty(&dpm_suspended_list)) {
805 struct device *dev = to_device(dpm_suspended_list.prev);
807 get_device(dev);
808 mutex_unlock(&dpm_list_mtx);
810 error = device_suspend_noirq(dev, state);
812 mutex_lock(&dpm_list_mtx);
813 if (error) {
814 pm_dev_err(dev, state, " late", error);
815 suspend_stats.failed_suspend_noirq++;
816 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
817 dpm_save_failed_dev(dev_name(dev));
818 put_device(dev);
819 break;
821 if (!list_empty(&dev->power.entry))
822 list_move(&dev->power.entry, &dpm_noirq_list);
823 put_device(dev);
825 mutex_unlock(&dpm_list_mtx);
826 if (error)
827 dpm_resume_noirq(resume_event(state));
828 else
829 dpm_show_time(starttime, state, "late");
830 return error;
832 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
835 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
836 * @dev: Device to suspend.
837 * @state: PM transition of the system being carried out.
838 * @cb: Suspend callback to execute.
840 static int legacy_suspend(struct device *dev, pm_message_t state,
841 int (*cb)(struct device *dev, pm_message_t state))
843 int error;
844 ktime_t calltime;
846 calltime = initcall_debug_start(dev);
848 error = cb(dev, state);
849 suspend_report_result(cb, error);
851 initcall_debug_report(dev, calltime, error);
853 return error;
857 * device_suspend - Execute "suspend" callbacks for given device.
858 * @dev: Device to handle.
859 * @state: PM transition of the system being carried out.
860 * @async: If true, the device is being suspended asynchronously.
862 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
864 int error = 0;
866 dpm_wait_for_children(dev, async);
868 if (async_error)
869 return 0;
871 pm_runtime_get_noresume(dev);
872 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
873 pm_wakeup_event(dev, 0);
875 if (pm_wakeup_pending()) {
876 pm_runtime_put_sync(dev);
877 async_error = -EBUSY;
878 return 0;
881 device_lock(dev);
883 if (dev->pm_domain) {
884 pm_dev_dbg(dev, state, "power domain ");
885 error = pm_op(dev, &dev->pm_domain->ops, state);
886 goto End;
889 if (dev->type && dev->type->pm) {
890 pm_dev_dbg(dev, state, "type ");
891 error = pm_op(dev, dev->type->pm, state);
892 goto End;
895 if (dev->class) {
896 if (dev->class->pm) {
897 pm_dev_dbg(dev, state, "class ");
898 error = pm_op(dev, dev->class->pm, state);
899 goto End;
900 } else if (dev->class->suspend) {
901 pm_dev_dbg(dev, state, "legacy class ");
902 error = legacy_suspend(dev, state, dev->class->suspend);
903 goto End;
907 if (dev->bus) {
908 if (dev->bus->pm) {
909 pm_dev_dbg(dev, state, "");
910 error = pm_op(dev, dev->bus->pm, state);
911 } else if (dev->bus->suspend) {
912 pm_dev_dbg(dev, state, "legacy ");
913 error = legacy_suspend(dev, state, dev->bus->suspend);
917 End:
918 dev->power.is_suspended = !error;
920 device_unlock(dev);
921 complete_all(&dev->power.completion);
923 if (error) {
924 pm_runtime_put_sync(dev);
925 async_error = error;
926 } else if (dev->power.is_suspended) {
927 __pm_runtime_disable(dev, false);
930 return error;
933 static void async_suspend(void *data, async_cookie_t cookie)
935 struct device *dev = (struct device *)data;
936 int error;
938 error = __device_suspend(dev, pm_transition, true);
939 if (error) {
940 dpm_save_failed_dev(dev_name(dev));
941 pm_dev_err(dev, pm_transition, " async", error);
944 put_device(dev);
947 static int device_suspend(struct device *dev)
949 INIT_COMPLETION(dev->power.completion);
951 if (pm_async_enabled && dev->power.async_suspend) {
952 get_device(dev);
953 async_schedule(async_suspend, dev);
954 return 0;
957 return __device_suspend(dev, pm_transition, false);
961 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
962 * @state: PM transition of the system being carried out.
964 int dpm_suspend(pm_message_t state)
966 ktime_t starttime = ktime_get();
967 int error = 0;
969 might_sleep();
971 mutex_lock(&dpm_list_mtx);
972 pm_transition = state;
973 async_error = 0;
974 while (!list_empty(&dpm_prepared_list)) {
975 struct device *dev = to_device(dpm_prepared_list.prev);
977 get_device(dev);
978 mutex_unlock(&dpm_list_mtx);
980 error = device_suspend(dev);
982 mutex_lock(&dpm_list_mtx);
983 if (error) {
984 pm_dev_err(dev, state, "", error);
985 dpm_save_failed_dev(dev_name(dev));
986 put_device(dev);
987 break;
989 if (!list_empty(&dev->power.entry))
990 list_move(&dev->power.entry, &dpm_suspended_list);
991 put_device(dev);
992 if (async_error)
993 break;
995 mutex_unlock(&dpm_list_mtx);
996 async_synchronize_full();
997 if (!error)
998 error = async_error;
999 if (error) {
1000 suspend_stats.failed_suspend++;
1001 dpm_save_failed_step(SUSPEND_SUSPEND);
1002 } else
1003 dpm_show_time(starttime, state, NULL);
1004 return error;
1008 * device_prepare - Prepare a device for system power transition.
1009 * @dev: Device to handle.
1010 * @state: PM transition of the system being carried out.
1012 * Execute the ->prepare() callback(s) for given device. No new children of the
1013 * device may be registered after this function has returned.
1015 static int device_prepare(struct device *dev, pm_message_t state)
1017 int error = 0;
1019 device_lock(dev);
1021 if (dev->pm_domain) {
1022 pm_dev_dbg(dev, state, "preparing power domain ");
1023 if (dev->pm_domain->ops.prepare)
1024 error = dev->pm_domain->ops.prepare(dev);
1025 suspend_report_result(dev->pm_domain->ops.prepare, error);
1026 if (error)
1027 goto End;
1028 } else if (dev->type && dev->type->pm) {
1029 pm_dev_dbg(dev, state, "preparing type ");
1030 if (dev->type->pm->prepare)
1031 error = dev->type->pm->prepare(dev);
1032 suspend_report_result(dev->type->pm->prepare, error);
1033 if (error)
1034 goto End;
1035 } else if (dev->class && dev->class->pm) {
1036 pm_dev_dbg(dev, state, "preparing class ");
1037 if (dev->class->pm->prepare)
1038 error = dev->class->pm->prepare(dev);
1039 suspend_report_result(dev->class->pm->prepare, error);
1040 if (error)
1041 goto End;
1042 } else if (dev->bus && dev->bus->pm) {
1043 pm_dev_dbg(dev, state, "preparing ");
1044 if (dev->bus->pm->prepare)
1045 error = dev->bus->pm->prepare(dev);
1046 suspend_report_result(dev->bus->pm->prepare, error);
1049 End:
1050 device_unlock(dev);
1052 return error;
1056 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1057 * @state: PM transition of the system being carried out.
1059 * Execute the ->prepare() callback(s) for all devices.
1061 int dpm_prepare(pm_message_t state)
1063 int error = 0;
1065 might_sleep();
1067 mutex_lock(&dpm_list_mtx);
1068 while (!list_empty(&dpm_list)) {
1069 struct device *dev = to_device(dpm_list.next);
1071 get_device(dev);
1072 mutex_unlock(&dpm_list_mtx);
1074 error = device_prepare(dev, state);
1076 mutex_lock(&dpm_list_mtx);
1077 if (error) {
1078 if (error == -EAGAIN) {
1079 put_device(dev);
1080 error = 0;
1081 continue;
1083 printk(KERN_INFO "PM: Device %s not prepared "
1084 "for power transition: code %d\n",
1085 dev_name(dev), error);
1086 put_device(dev);
1087 break;
1089 dev->power.is_prepared = true;
1090 if (!list_empty(&dev->power.entry))
1091 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1092 put_device(dev);
1094 mutex_unlock(&dpm_list_mtx);
1095 return error;
1099 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1100 * @state: PM transition of the system being carried out.
1102 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1103 * callbacks for them.
1105 int dpm_suspend_start(pm_message_t state)
1107 int error;
1109 error = dpm_prepare(state);
1110 if (error) {
1111 suspend_stats.failed_prepare++;
1112 dpm_save_failed_step(SUSPEND_PREPARE);
1113 } else
1114 error = dpm_suspend(state);
1115 return error;
1117 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1119 void __suspend_report_result(const char *function, void *fn, int ret)
1121 if (ret)
1122 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1124 EXPORT_SYMBOL_GPL(__suspend_report_result);
1127 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1128 * @dev: Device to wait for.
1129 * @subordinate: Device that needs to wait for @dev.
1131 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1133 dpm_wait(dev, subordinate->power.async_suspend);
1134 return async_error;
1136 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);