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 intialize 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/mutex.h>
24 #include <linux/resume-trace.h>
25 #include <linux/rwsem.h>
31 * The entries in the dpm_list list are in a depth first order, simply
32 * because children are guaranteed to be discovered after parents, and
33 * are inserted at the back of the list on discovery.
35 * Since device_pm_add() may be called with a device semaphore held,
36 * we must never try to acquire a device semaphore while holding
42 static DEFINE_MUTEX(dpm_list_mtx);
45 * Set once the preparation of devices for a PM transition has started, reset
46 * before starting to resume devices. Protected by dpm_list_mtx.
48 static bool transition_started;
51 * device_pm_lock - lock the list of active devices used by the PM core
53 void device_pm_lock(void)
55 mutex_lock(&dpm_list_mtx);
59 * device_pm_unlock - unlock the list of active devices used by the PM core
61 void device_pm_unlock(void)
63 mutex_unlock(&dpm_list_mtx);
67 * device_pm_add - add a device to the list of active devices
68 * @dev: Device to be added to the list
70 int device_pm_add(struct device *dev)
74 pr_debug("PM: Adding info for %s:%s\n",
75 dev->bus ? dev->bus->name : "No Bus",
76 kobject_name(&dev->kobj));
77 mutex_lock(&dpm_list_mtx);
79 if (dev->parent->power.status >= DPM_SUSPENDING) {
80 dev_warn(dev, "parent %s is sleeping, will not add\n",
84 } else if (transition_started) {
86 * We refuse to register parentless devices while a PM
87 * transition is in progress in order to avoid leaving them
88 * unhandled down the road
92 error = dpm_sysfs_add(dev);
94 dev->power.status = DPM_ON;
95 list_add_tail(&dev->power.entry, &dpm_list);
97 mutex_unlock(&dpm_list_mtx);
102 * device_pm_remove - remove a device from the list of active devices
103 * @dev: Device to be removed from the list
105 * This function also removes the device's PM-related sysfs attributes.
107 void device_pm_remove(struct device *dev)
109 pr_debug("PM: Removing info for %s:%s\n",
110 dev->bus ? dev->bus->name : "No Bus",
111 kobject_name(&dev->kobj));
112 mutex_lock(&dpm_list_mtx);
113 dpm_sysfs_remove(dev);
114 list_del_init(&dev->power.entry);
115 mutex_unlock(&dpm_list_mtx);
119 * pm_op - execute the PM operation appropiate for given PM event
121 * @ops: PM operations to choose from.
122 * @state: PM transition of the system being carried out.
124 static int pm_op(struct device *dev, struct pm_ops *ops, pm_message_t state)
128 switch (state.event) {
129 #ifdef CONFIG_SUSPEND
130 case PM_EVENT_SUSPEND:
132 error = ops->suspend(dev);
133 suspend_report_result(ops->suspend, error);
136 case PM_EVENT_RESUME:
138 error = ops->resume(dev);
139 suspend_report_result(ops->resume, error);
142 #endif /* CONFIG_SUSPEND */
143 #ifdef CONFIG_HIBERNATION
144 case PM_EVENT_FREEZE:
145 case PM_EVENT_QUIESCE:
147 error = ops->freeze(dev);
148 suspend_report_result(ops->freeze, error);
151 case PM_EVENT_HIBERNATE:
153 error = ops->poweroff(dev);
154 suspend_report_result(ops->poweroff, error);
158 case PM_EVENT_RECOVER:
160 error = ops->thaw(dev);
161 suspend_report_result(ops->thaw, error);
164 case PM_EVENT_RESTORE:
166 error = ops->restore(dev);
167 suspend_report_result(ops->restore, error);
170 #endif /* CONFIG_HIBERNATION */
178 * pm_noirq_op - execute the PM operation appropiate for given PM event
180 * @ops: PM operations to choose from.
181 * @state: PM transition of the system being carried out.
183 * The operation is executed with interrupts disabled by the only remaining
184 * functional CPU in the system.
186 static int pm_noirq_op(struct device *dev, struct pm_ext_ops *ops,
191 switch (state.event) {
192 #ifdef CONFIG_SUSPEND
193 case PM_EVENT_SUSPEND:
194 if (ops->suspend_noirq) {
195 error = ops->suspend_noirq(dev);
196 suspend_report_result(ops->suspend_noirq, error);
199 case PM_EVENT_RESUME:
200 if (ops->resume_noirq) {
201 error = ops->resume_noirq(dev);
202 suspend_report_result(ops->resume_noirq, error);
205 #endif /* CONFIG_SUSPEND */
206 #ifdef CONFIG_HIBERNATION
207 case PM_EVENT_FREEZE:
208 case PM_EVENT_QUIESCE:
209 if (ops->freeze_noirq) {
210 error = ops->freeze_noirq(dev);
211 suspend_report_result(ops->freeze_noirq, error);
214 case PM_EVENT_HIBERNATE:
215 if (ops->poweroff_noirq) {
216 error = ops->poweroff_noirq(dev);
217 suspend_report_result(ops->poweroff_noirq, error);
221 case PM_EVENT_RECOVER:
222 if (ops->thaw_noirq) {
223 error = ops->thaw_noirq(dev);
224 suspend_report_result(ops->thaw_noirq, error);
227 case PM_EVENT_RESTORE:
228 if (ops->restore_noirq) {
229 error = ops->restore_noirq(dev);
230 suspend_report_result(ops->restore_noirq, error);
233 #endif /* CONFIG_HIBERNATION */
240 static char *pm_verb(int event)
243 case PM_EVENT_SUSPEND:
245 case PM_EVENT_RESUME:
247 case PM_EVENT_FREEZE:
249 case PM_EVENT_QUIESCE:
251 case PM_EVENT_HIBERNATE:
255 case PM_EVENT_RESTORE:
257 case PM_EVENT_RECOVER:
260 return "(unknown PM event)";
264 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
266 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
267 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
268 ", may wakeup" : "");
271 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
274 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
275 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
278 /*------------------------- Resume routines -------------------------*/
281 * resume_device_noirq - Power on one device (early resume).
283 * @state: PM transition of the system being carried out.
285 * Must be called with interrupts disabled.
287 static int resume_device_noirq(struct device *dev, pm_message_t state)
298 pm_dev_dbg(dev, state, "EARLY ");
299 error = pm_noirq_op(dev, dev->bus->pm, state);
300 } else if (dev->bus->resume_early) {
301 pm_dev_dbg(dev, state, "legacy EARLY ");
302 error = dev->bus->resume_early(dev);
310 * dpm_power_up - Power on all regular (non-sysdev) devices.
311 * @state: PM transition of the system being carried out.
313 * Execute the appropriate "noirq resume" callback for all devices marked
316 * Must be called with interrupts disabled and only one CPU running.
318 static void dpm_power_up(pm_message_t state)
322 list_for_each_entry(dev, &dpm_list, power.entry)
323 if (dev->power.status > DPM_OFF) {
326 dev->power.status = DPM_OFF;
327 error = resume_device_noirq(dev, state);
329 pm_dev_err(dev, state, " early", error);
334 * device_power_up - Turn on all devices that need special attention.
335 * @state: PM transition of the system being carried out.
337 * Power on system devices, then devices that required we shut them down
338 * with interrupts disabled.
340 * Must be called with interrupts disabled.
342 void device_power_up(pm_message_t state)
347 EXPORT_SYMBOL_GPL(device_power_up);
350 * resume_device - Restore state for one device.
352 * @state: PM transition of the system being carried out.
354 static int resume_device(struct device *dev, pm_message_t state)
365 pm_dev_dbg(dev, state, "");
366 error = pm_op(dev, &dev->bus->pm->base, state);
367 } else if (dev->bus->resume) {
368 pm_dev_dbg(dev, state, "legacy ");
369 error = dev->bus->resume(dev);
377 pm_dev_dbg(dev, state, "type ");
378 error = pm_op(dev, dev->type->pm, state);
379 } else if (dev->type->resume) {
380 pm_dev_dbg(dev, state, "legacy type ");
381 error = dev->type->resume(dev);
388 if (dev->class->pm) {
389 pm_dev_dbg(dev, state, "class ");
390 error = pm_op(dev, dev->class->pm, state);
391 } else if (dev->class->resume) {
392 pm_dev_dbg(dev, state, "legacy class ");
393 error = dev->class->resume(dev);
404 * dpm_resume - Resume every device.
405 * @state: PM transition of the system being carried out.
407 * Execute the appropriate "resume" callback for all devices the status of
408 * which indicates that they are inactive.
410 static void dpm_resume(pm_message_t state)
412 struct list_head list;
414 INIT_LIST_HEAD(&list);
415 mutex_lock(&dpm_list_mtx);
416 transition_started = false;
417 while (!list_empty(&dpm_list)) {
418 struct device *dev = to_device(dpm_list.next);
421 if (dev->power.status >= DPM_OFF) {
424 dev->power.status = DPM_RESUMING;
425 mutex_unlock(&dpm_list_mtx);
427 error = resume_device(dev, state);
429 mutex_lock(&dpm_list_mtx);
431 pm_dev_err(dev, state, "", error);
432 } else if (dev->power.status == DPM_SUSPENDING) {
433 /* Allow new children of the device to be registered */
434 dev->power.status = DPM_RESUMING;
436 if (!list_empty(&dev->power.entry))
437 list_move_tail(&dev->power.entry, &list);
440 list_splice(&list, &dpm_list);
441 mutex_unlock(&dpm_list_mtx);
445 * complete_device - Complete a PM transition for given device
447 * @state: PM transition of the system being carried out.
449 static void complete_device(struct device *dev, pm_message_t state)
453 if (dev->class && dev->class->pm && dev->class->pm->complete) {
454 pm_dev_dbg(dev, state, "completing class ");
455 dev->class->pm->complete(dev);
458 if (dev->type && dev->type->pm && dev->type->pm->complete) {
459 pm_dev_dbg(dev, state, "completing type ");
460 dev->type->pm->complete(dev);
463 if (dev->bus && dev->bus->pm && dev->bus->pm->base.complete) {
464 pm_dev_dbg(dev, state, "completing ");
465 dev->bus->pm->base.complete(dev);
472 * dpm_complete - Complete a PM transition for all devices.
473 * @state: PM transition of the system being carried out.
475 * Execute the ->complete() callbacks for all devices that are not marked
478 static void dpm_complete(pm_message_t state)
480 struct list_head list;
482 INIT_LIST_HEAD(&list);
483 mutex_lock(&dpm_list_mtx);
484 while (!list_empty(&dpm_list)) {
485 struct device *dev = to_device(dpm_list.prev);
488 if (dev->power.status > DPM_ON) {
489 dev->power.status = DPM_ON;
490 mutex_unlock(&dpm_list_mtx);
492 complete_device(dev, state);
494 mutex_lock(&dpm_list_mtx);
496 if (!list_empty(&dev->power.entry))
497 list_move(&dev->power.entry, &list);
500 list_splice(&list, &dpm_list);
501 mutex_unlock(&dpm_list_mtx);
505 * device_resume - Restore state of each device in system.
506 * @state: PM transition of the system being carried out.
508 * Resume all the devices, unlock them all, and allow new
509 * devices to be registered once again.
511 void device_resume(pm_message_t state)
517 EXPORT_SYMBOL_GPL(device_resume);
520 /*------------------------- Suspend routines -------------------------*/
523 * resume_event - return a PM message representing the resume event
524 * corresponding to given sleep state.
525 * @sleep_state: PM message representing a sleep state.
527 static pm_message_t resume_event(pm_message_t sleep_state)
529 switch (sleep_state.event) {
530 case PM_EVENT_SUSPEND:
532 case PM_EVENT_FREEZE:
533 case PM_EVENT_QUIESCE:
535 case PM_EVENT_HIBERNATE:
542 * suspend_device_noirq - Shut down one device (late suspend).
544 * @state: PM transition of the system being carried out.
546 * This is called with interrupts off and only a single CPU running.
548 static int suspend_device_noirq(struct device *dev, pm_message_t state)
556 pm_dev_dbg(dev, state, "LATE ");
557 error = pm_noirq_op(dev, dev->bus->pm, state);
558 } else if (dev->bus->suspend_late) {
559 pm_dev_dbg(dev, state, "legacy LATE ");
560 error = dev->bus->suspend_late(dev, state);
561 suspend_report_result(dev->bus->suspend_late, error);
567 * device_power_down - Shut down special devices.
568 * @state: PM transition of the system being carried out.
570 * Power down devices that require interrupts to be disabled.
571 * Then power down system devices.
573 * Must be called with interrupts disabled and only one CPU running.
575 int device_power_down(pm_message_t state)
580 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
581 error = suspend_device_noirq(dev, state);
583 pm_dev_err(dev, state, " late", error);
586 dev->power.status = DPM_OFF_IRQ;
589 error = sysdev_suspend(state);
591 dpm_power_up(resume_event(state));
594 EXPORT_SYMBOL_GPL(device_power_down);
597 * suspend_device - Save state of one device.
599 * @state: PM transition of the system being carried out.
601 static int suspend_device(struct device *dev, pm_message_t state)
608 if (dev->class->pm) {
609 pm_dev_dbg(dev, state, "class ");
610 error = pm_op(dev, dev->class->pm, state);
611 } else if (dev->class->suspend) {
612 pm_dev_dbg(dev, state, "legacy class ");
613 error = dev->class->suspend(dev, state);
614 suspend_report_result(dev->class->suspend, error);
622 pm_dev_dbg(dev, state, "type ");
623 error = pm_op(dev, dev->type->pm, state);
624 } else if (dev->type->suspend) {
625 pm_dev_dbg(dev, state, "legacy type ");
626 error = dev->type->suspend(dev, state);
627 suspend_report_result(dev->type->suspend, error);
635 pm_dev_dbg(dev, state, "");
636 error = pm_op(dev, &dev->bus->pm->base, state);
637 } else if (dev->bus->suspend) {
638 pm_dev_dbg(dev, state, "legacy ");
639 error = dev->bus->suspend(dev, state);
640 suspend_report_result(dev->bus->suspend, error);
650 * dpm_suspend - Suspend every device.
651 * @state: PM transition of the system being carried out.
653 * Execute the appropriate "suspend" callbacks for all devices.
655 static int dpm_suspend(pm_message_t state)
657 struct list_head list;
660 INIT_LIST_HEAD(&list);
661 mutex_lock(&dpm_list_mtx);
662 while (!list_empty(&dpm_list)) {
663 struct device *dev = to_device(dpm_list.prev);
666 mutex_unlock(&dpm_list_mtx);
668 error = suspend_device(dev, state);
670 mutex_lock(&dpm_list_mtx);
672 pm_dev_err(dev, state, "", error);
676 dev->power.status = DPM_OFF;
677 if (!list_empty(&dev->power.entry))
678 list_move(&dev->power.entry, &list);
681 list_splice(&list, dpm_list.prev);
682 mutex_unlock(&dpm_list_mtx);
687 * prepare_device - Execute the ->prepare() callback(s) for given device.
689 * @state: PM transition of the system being carried out.
691 static int prepare_device(struct device *dev, pm_message_t state)
697 if (dev->bus && dev->bus->pm && dev->bus->pm->base.prepare) {
698 pm_dev_dbg(dev, state, "preparing ");
699 error = dev->bus->pm->base.prepare(dev);
700 suspend_report_result(dev->bus->pm->base.prepare, error);
705 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
706 pm_dev_dbg(dev, state, "preparing type ");
707 error = dev->type->pm->prepare(dev);
708 suspend_report_result(dev->type->pm->prepare, error);
713 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
714 pm_dev_dbg(dev, state, "preparing class ");
715 error = dev->class->pm->prepare(dev);
716 suspend_report_result(dev->class->pm->prepare, error);
725 * dpm_prepare - Prepare all devices for a PM transition.
726 * @state: PM transition of the system being carried out.
728 * Execute the ->prepare() callback for all devices.
730 static int dpm_prepare(pm_message_t state)
732 struct list_head list;
735 INIT_LIST_HEAD(&list);
736 mutex_lock(&dpm_list_mtx);
737 transition_started = true;
738 while (!list_empty(&dpm_list)) {
739 struct device *dev = to_device(dpm_list.next);
742 dev->power.status = DPM_PREPARING;
743 mutex_unlock(&dpm_list_mtx);
745 error = prepare_device(dev, state);
747 mutex_lock(&dpm_list_mtx);
749 dev->power.status = DPM_ON;
750 if (error == -EAGAIN) {
754 printk(KERN_ERR "PM: Failed to prepare device %s "
755 "for power transition: error %d\n",
756 kobject_name(&dev->kobj), error);
760 dev->power.status = DPM_SUSPENDING;
761 if (!list_empty(&dev->power.entry))
762 list_move_tail(&dev->power.entry, &list);
765 list_splice(&list, &dpm_list);
766 mutex_unlock(&dpm_list_mtx);
771 * device_suspend - Save state and stop all devices in system.
772 * @state: PM transition of the system being carried out.
774 * Prepare and suspend all devices.
776 int device_suspend(pm_message_t state)
781 error = dpm_prepare(state);
783 error = dpm_suspend(state);
786 EXPORT_SYMBOL_GPL(device_suspend);
788 void __suspend_report_result(const char *function, void *fn, int ret)
791 printk(KERN_ERR "%s(): ", function);
792 print_fn_descriptor_symbol("%s returns ", fn);
796 EXPORT_SYMBOL_GPL(__suspend_report_result);