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 void device_pm_add(struct device *dev)
72 pr_debug("PM: Adding info for %s:%s\n",
73 dev->bus ? dev->bus->name : "No Bus",
74 kobject_name(&dev->kobj));
75 mutex_lock(&dpm_list_mtx);
77 if (dev->parent->power.status >= DPM_SUSPENDING)
78 dev_warn(dev, "parent %s should not be sleeping\n",
79 dev_name(dev->parent));
80 } else if (transition_started) {
82 * We refuse to register parentless devices while a PM
83 * transition is in progress in order to avoid leaving them
84 * unhandled down the road
86 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
89 list_add_tail(&dev->power.entry, &dpm_list);
90 mutex_unlock(&dpm_list_mtx);
94 * device_pm_remove - remove a device from the list of active devices
95 * @dev: Device to be removed from the list
97 * This function also removes the device's PM-related sysfs attributes.
99 void device_pm_remove(struct device *dev)
101 pr_debug("PM: Removing info for %s:%s\n",
102 dev->bus ? dev->bus->name : "No Bus",
103 kobject_name(&dev->kobj));
104 mutex_lock(&dpm_list_mtx);
105 list_del_init(&dev->power.entry);
106 mutex_unlock(&dpm_list_mtx);
110 * pm_op - execute the PM operation appropiate for given PM event
112 * @ops: PM operations to choose from.
113 * @state: PM transition of the system being carried out.
115 static int pm_op(struct device *dev, struct dev_pm_ops *ops,
120 switch (state.event) {
121 #ifdef CONFIG_SUSPEND
122 case PM_EVENT_SUSPEND:
124 error = ops->suspend(dev);
125 suspend_report_result(ops->suspend, error);
128 case PM_EVENT_RESUME:
130 error = ops->resume(dev);
131 suspend_report_result(ops->resume, error);
134 #endif /* CONFIG_SUSPEND */
135 #ifdef CONFIG_HIBERNATION
136 case PM_EVENT_FREEZE:
137 case PM_EVENT_QUIESCE:
139 error = ops->freeze(dev);
140 suspend_report_result(ops->freeze, error);
143 case PM_EVENT_HIBERNATE:
145 error = ops->poweroff(dev);
146 suspend_report_result(ops->poweroff, error);
150 case PM_EVENT_RECOVER:
152 error = ops->thaw(dev);
153 suspend_report_result(ops->thaw, error);
156 case PM_EVENT_RESTORE:
158 error = ops->restore(dev);
159 suspend_report_result(ops->restore, error);
162 #endif /* CONFIG_HIBERNATION */
170 * pm_noirq_op - execute the PM operation appropiate for given PM event
172 * @ops: PM operations to choose from.
173 * @state: PM transition of the system being carried out.
175 * The operation is executed with interrupts disabled by the only remaining
176 * functional CPU in the system.
178 static int pm_noirq_op(struct device *dev, struct dev_pm_ops *ops,
183 switch (state.event) {
184 #ifdef CONFIG_SUSPEND
185 case PM_EVENT_SUSPEND:
186 if (ops->suspend_noirq) {
187 error = ops->suspend_noirq(dev);
188 suspend_report_result(ops->suspend_noirq, error);
191 case PM_EVENT_RESUME:
192 if (ops->resume_noirq) {
193 error = ops->resume_noirq(dev);
194 suspend_report_result(ops->resume_noirq, error);
197 #endif /* CONFIG_SUSPEND */
198 #ifdef CONFIG_HIBERNATION
199 case PM_EVENT_FREEZE:
200 case PM_EVENT_QUIESCE:
201 if (ops->freeze_noirq) {
202 error = ops->freeze_noirq(dev);
203 suspend_report_result(ops->freeze_noirq, error);
206 case PM_EVENT_HIBERNATE:
207 if (ops->poweroff_noirq) {
208 error = ops->poweroff_noirq(dev);
209 suspend_report_result(ops->poweroff_noirq, error);
213 case PM_EVENT_RECOVER:
214 if (ops->thaw_noirq) {
215 error = ops->thaw_noirq(dev);
216 suspend_report_result(ops->thaw_noirq, error);
219 case PM_EVENT_RESTORE:
220 if (ops->restore_noirq) {
221 error = ops->restore_noirq(dev);
222 suspend_report_result(ops->restore_noirq, error);
225 #endif /* CONFIG_HIBERNATION */
232 static char *pm_verb(int event)
235 case PM_EVENT_SUSPEND:
237 case PM_EVENT_RESUME:
239 case PM_EVENT_FREEZE:
241 case PM_EVENT_QUIESCE:
243 case PM_EVENT_HIBERNATE:
247 case PM_EVENT_RESTORE:
249 case PM_EVENT_RECOVER:
252 return "(unknown PM event)";
256 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
258 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
259 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
260 ", may wakeup" : "");
263 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
266 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
267 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
270 /*------------------------- Resume routines -------------------------*/
273 * resume_device_noirq - Power on one device (early resume).
275 * @state: PM transition of the system being carried out.
277 * Must be called with interrupts disabled.
279 static int resume_device_noirq(struct device *dev, pm_message_t state)
290 pm_dev_dbg(dev, state, "EARLY ");
291 error = pm_noirq_op(dev, dev->bus->pm, state);
292 } else if (dev->bus->resume_early) {
293 pm_dev_dbg(dev, state, "legacy EARLY ");
294 error = dev->bus->resume_early(dev);
302 * dpm_power_up - Power on all regular (non-sysdev) devices.
303 * @state: PM transition of the system being carried out.
305 * Execute the appropriate "noirq resume" callback for all devices marked
308 * Must be called with interrupts disabled and only one CPU running.
310 static void dpm_power_up(pm_message_t state)
314 list_for_each_entry(dev, &dpm_list, power.entry)
315 if (dev->power.status > DPM_OFF) {
318 dev->power.status = DPM_OFF;
319 error = resume_device_noirq(dev, state);
321 pm_dev_err(dev, state, " early", error);
326 * device_power_up - Turn on all devices that need special attention.
327 * @state: PM transition of the system being carried out.
329 * Power on system devices, then devices that required we shut them down
330 * with interrupts disabled.
332 * Must be called with interrupts disabled.
334 void device_power_up(pm_message_t state)
339 EXPORT_SYMBOL_GPL(device_power_up);
342 * resume_device - Restore state for one device.
344 * @state: PM transition of the system being carried out.
346 static int resume_device(struct device *dev, pm_message_t state)
357 pm_dev_dbg(dev, state, "");
358 error = pm_op(dev, dev->bus->pm, state);
359 } else if (dev->bus->resume) {
360 pm_dev_dbg(dev, state, "legacy ");
361 error = dev->bus->resume(dev);
369 pm_dev_dbg(dev, state, "type ");
370 error = pm_op(dev, dev->type->pm, state);
371 } else if (dev->type->resume) {
372 pm_dev_dbg(dev, state, "legacy type ");
373 error = dev->type->resume(dev);
380 if (dev->class->pm) {
381 pm_dev_dbg(dev, state, "class ");
382 error = pm_op(dev, dev->class->pm, state);
383 } else if (dev->class->resume) {
384 pm_dev_dbg(dev, state, "legacy class ");
385 error = dev->class->resume(dev);
396 * dpm_resume - Resume every device.
397 * @state: PM transition of the system being carried out.
399 * Execute the appropriate "resume" callback for all devices the status of
400 * which indicates that they are inactive.
402 static void dpm_resume(pm_message_t state)
404 struct list_head list;
406 INIT_LIST_HEAD(&list);
407 mutex_lock(&dpm_list_mtx);
408 transition_started = false;
409 while (!list_empty(&dpm_list)) {
410 struct device *dev = to_device(dpm_list.next);
413 if (dev->power.status >= DPM_OFF) {
416 dev->power.status = DPM_RESUMING;
417 mutex_unlock(&dpm_list_mtx);
419 error = resume_device(dev, state);
421 mutex_lock(&dpm_list_mtx);
423 pm_dev_err(dev, state, "", error);
424 } else if (dev->power.status == DPM_SUSPENDING) {
425 /* Allow new children of the device to be registered */
426 dev->power.status = DPM_RESUMING;
428 if (!list_empty(&dev->power.entry))
429 list_move_tail(&dev->power.entry, &list);
432 list_splice(&list, &dpm_list);
433 mutex_unlock(&dpm_list_mtx);
437 * complete_device - Complete a PM transition for given device
439 * @state: PM transition of the system being carried out.
441 static void complete_device(struct device *dev, pm_message_t state)
445 if (dev->class && dev->class->pm && dev->class->pm->complete) {
446 pm_dev_dbg(dev, state, "completing class ");
447 dev->class->pm->complete(dev);
450 if (dev->type && dev->type->pm && dev->type->pm->complete) {
451 pm_dev_dbg(dev, state, "completing type ");
452 dev->type->pm->complete(dev);
455 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
456 pm_dev_dbg(dev, state, "completing ");
457 dev->bus->pm->complete(dev);
464 * dpm_complete - Complete a PM transition for all devices.
465 * @state: PM transition of the system being carried out.
467 * Execute the ->complete() callbacks for all devices that are not marked
470 static void dpm_complete(pm_message_t state)
472 struct list_head list;
474 INIT_LIST_HEAD(&list);
475 mutex_lock(&dpm_list_mtx);
476 while (!list_empty(&dpm_list)) {
477 struct device *dev = to_device(dpm_list.prev);
480 if (dev->power.status > DPM_ON) {
481 dev->power.status = DPM_ON;
482 mutex_unlock(&dpm_list_mtx);
484 complete_device(dev, state);
486 mutex_lock(&dpm_list_mtx);
488 if (!list_empty(&dev->power.entry))
489 list_move(&dev->power.entry, &list);
492 list_splice(&list, &dpm_list);
493 mutex_unlock(&dpm_list_mtx);
497 * device_resume - Restore state of each device in system.
498 * @state: PM transition of the system being carried out.
500 * Resume all the devices, unlock them all, and allow new
501 * devices to be registered once again.
503 void device_resume(pm_message_t state)
509 EXPORT_SYMBOL_GPL(device_resume);
512 /*------------------------- Suspend routines -------------------------*/
515 * resume_event - return a PM message representing the resume event
516 * corresponding to given sleep state.
517 * @sleep_state: PM message representing a sleep state.
519 static pm_message_t resume_event(pm_message_t sleep_state)
521 switch (sleep_state.event) {
522 case PM_EVENT_SUSPEND:
524 case PM_EVENT_FREEZE:
525 case PM_EVENT_QUIESCE:
527 case PM_EVENT_HIBERNATE:
534 * suspend_device_noirq - Shut down one device (late suspend).
536 * @state: PM transition of the system being carried out.
538 * This is called with interrupts off and only a single CPU running.
540 static int suspend_device_noirq(struct device *dev, pm_message_t state)
548 pm_dev_dbg(dev, state, "LATE ");
549 error = pm_noirq_op(dev, dev->bus->pm, state);
550 } else if (dev->bus->suspend_late) {
551 pm_dev_dbg(dev, state, "legacy LATE ");
552 error = dev->bus->suspend_late(dev, state);
553 suspend_report_result(dev->bus->suspend_late, error);
559 * device_power_down - Shut down special devices.
560 * @state: PM transition of the system being carried out.
562 * Power down devices that require interrupts to be disabled.
563 * Then power down system devices.
565 * Must be called with interrupts disabled and only one CPU running.
567 int device_power_down(pm_message_t state)
572 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
573 error = suspend_device_noirq(dev, state);
575 pm_dev_err(dev, state, " late", error);
578 dev->power.status = DPM_OFF_IRQ;
581 error = sysdev_suspend(state);
583 dpm_power_up(resume_event(state));
586 EXPORT_SYMBOL_GPL(device_power_down);
589 * suspend_device - Save state of one device.
591 * @state: PM transition of the system being carried out.
593 static int suspend_device(struct device *dev, pm_message_t state)
600 if (dev->class->pm) {
601 pm_dev_dbg(dev, state, "class ");
602 error = pm_op(dev, dev->class->pm, state);
603 } else if (dev->class->suspend) {
604 pm_dev_dbg(dev, state, "legacy class ");
605 error = dev->class->suspend(dev, state);
606 suspend_report_result(dev->class->suspend, error);
614 pm_dev_dbg(dev, state, "type ");
615 error = pm_op(dev, dev->type->pm, state);
616 } else if (dev->type->suspend) {
617 pm_dev_dbg(dev, state, "legacy type ");
618 error = dev->type->suspend(dev, state);
619 suspend_report_result(dev->type->suspend, error);
627 pm_dev_dbg(dev, state, "");
628 error = pm_op(dev, dev->bus->pm, state);
629 } else if (dev->bus->suspend) {
630 pm_dev_dbg(dev, state, "legacy ");
631 error = dev->bus->suspend(dev, state);
632 suspend_report_result(dev->bus->suspend, error);
642 * dpm_suspend - Suspend every device.
643 * @state: PM transition of the system being carried out.
645 * Execute the appropriate "suspend" callbacks for all devices.
647 static int dpm_suspend(pm_message_t state)
649 struct list_head list;
652 INIT_LIST_HEAD(&list);
653 mutex_lock(&dpm_list_mtx);
654 while (!list_empty(&dpm_list)) {
655 struct device *dev = to_device(dpm_list.prev);
658 mutex_unlock(&dpm_list_mtx);
660 error = suspend_device(dev, state);
662 mutex_lock(&dpm_list_mtx);
664 pm_dev_err(dev, state, "", error);
668 dev->power.status = DPM_OFF;
669 if (!list_empty(&dev->power.entry))
670 list_move(&dev->power.entry, &list);
673 list_splice(&list, dpm_list.prev);
674 mutex_unlock(&dpm_list_mtx);
679 * prepare_device - Execute the ->prepare() callback(s) for given device.
681 * @state: PM transition of the system being carried out.
683 static int prepare_device(struct device *dev, pm_message_t state)
689 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
690 pm_dev_dbg(dev, state, "preparing ");
691 error = dev->bus->pm->prepare(dev);
692 suspend_report_result(dev->bus->pm->prepare, error);
697 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
698 pm_dev_dbg(dev, state, "preparing type ");
699 error = dev->type->pm->prepare(dev);
700 suspend_report_result(dev->type->pm->prepare, error);
705 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
706 pm_dev_dbg(dev, state, "preparing class ");
707 error = dev->class->pm->prepare(dev);
708 suspend_report_result(dev->class->pm->prepare, error);
717 * dpm_prepare - Prepare all devices for a PM transition.
718 * @state: PM transition of the system being carried out.
720 * Execute the ->prepare() callback for all devices.
722 static int dpm_prepare(pm_message_t state)
724 struct list_head list;
727 INIT_LIST_HEAD(&list);
728 mutex_lock(&dpm_list_mtx);
729 transition_started = true;
730 while (!list_empty(&dpm_list)) {
731 struct device *dev = to_device(dpm_list.next);
734 dev->power.status = DPM_PREPARING;
735 mutex_unlock(&dpm_list_mtx);
737 error = prepare_device(dev, state);
739 mutex_lock(&dpm_list_mtx);
741 dev->power.status = DPM_ON;
742 if (error == -EAGAIN) {
746 printk(KERN_ERR "PM: Failed to prepare device %s "
747 "for power transition: error %d\n",
748 kobject_name(&dev->kobj), error);
752 dev->power.status = DPM_SUSPENDING;
753 if (!list_empty(&dev->power.entry))
754 list_move_tail(&dev->power.entry, &list);
757 list_splice(&list, &dpm_list);
758 mutex_unlock(&dpm_list_mtx);
763 * device_suspend - Save state and stop all devices in system.
764 * @state: PM transition of the system being carried out.
766 * Prepare and suspend all devices.
768 int device_suspend(pm_message_t state)
773 error = dpm_prepare(state);
775 error = dpm_suspend(state);
778 EXPORT_SYMBOL_GPL(device_suspend);
780 void __suspend_report_result(const char *function, void *fn, int ret)
783 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
785 EXPORT_SYMBOL_GPL(__suspend_report_result);