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 * device_pm_move_before - move device in dpm_list
111 * @deva: Device to move in dpm_list
112 * @devb: Device @deva should come before
114 void device_pm_move_before(struct device *deva, struct device *devb)
116 pr_debug("PM: Moving %s:%s before %s:%s\n",
117 deva->bus ? deva->bus->name : "No Bus",
118 kobject_name(&deva->kobj),
119 devb->bus ? devb->bus->name : "No Bus",
120 kobject_name(&devb->kobj));
121 /* Delete deva from dpm_list and reinsert before devb. */
122 list_move_tail(&deva->power.entry, &devb->power.entry);
126 * device_pm_move_after - move device in dpm_list
127 * @deva: Device to move in dpm_list
128 * @devb: Device @deva should come after
130 void device_pm_move_after(struct device *deva, struct device *devb)
132 pr_debug("PM: Moving %s:%s after %s:%s\n",
133 deva->bus ? deva->bus->name : "No Bus",
134 kobject_name(&deva->kobj),
135 devb->bus ? devb->bus->name : "No Bus",
136 kobject_name(&devb->kobj));
137 /* Delete deva from dpm_list and reinsert after devb. */
138 list_move(&deva->power.entry, &devb->power.entry);
142 * device_pm_move_last - move device to end of dpm_list
143 * @dev: Device to move in dpm_list
145 void device_pm_move_last(struct device *dev)
147 pr_debug("PM: Moving %s:%s to end of list\n",
148 dev->bus ? dev->bus->name : "No Bus",
149 kobject_name(&dev->kobj));
150 list_move_tail(&dev->power.entry, &dpm_list);
154 * pm_op - execute the PM operation appropiate for given PM event
156 * @ops: PM operations to choose from.
157 * @state: PM transition of the system being carried out.
159 static int pm_op(struct device *dev, struct dev_pm_ops *ops,
164 switch (state.event) {
165 #ifdef CONFIG_SUSPEND
166 case PM_EVENT_SUSPEND:
168 error = ops->suspend(dev);
169 suspend_report_result(ops->suspend, error);
172 case PM_EVENT_RESUME:
174 error = ops->resume(dev);
175 suspend_report_result(ops->resume, error);
178 #endif /* CONFIG_SUSPEND */
179 #ifdef CONFIG_HIBERNATION
180 case PM_EVENT_FREEZE:
181 case PM_EVENT_QUIESCE:
183 error = ops->freeze(dev);
184 suspend_report_result(ops->freeze, error);
187 case PM_EVENT_HIBERNATE:
189 error = ops->poweroff(dev);
190 suspend_report_result(ops->poweroff, error);
194 case PM_EVENT_RECOVER:
196 error = ops->thaw(dev);
197 suspend_report_result(ops->thaw, error);
200 case PM_EVENT_RESTORE:
202 error = ops->restore(dev);
203 suspend_report_result(ops->restore, error);
206 #endif /* CONFIG_HIBERNATION */
214 * pm_noirq_op - execute the PM operation appropiate for given PM event
216 * @ops: PM operations to choose from.
217 * @state: PM transition of the system being carried out.
219 * The operation is executed with interrupts disabled by the only remaining
220 * functional CPU in the system.
222 static int pm_noirq_op(struct device *dev, struct dev_pm_ops *ops,
227 switch (state.event) {
228 #ifdef CONFIG_SUSPEND
229 case PM_EVENT_SUSPEND:
230 if (ops->suspend_noirq) {
231 error = ops->suspend_noirq(dev);
232 suspend_report_result(ops->suspend_noirq, error);
235 case PM_EVENT_RESUME:
236 if (ops->resume_noirq) {
237 error = ops->resume_noirq(dev);
238 suspend_report_result(ops->resume_noirq, error);
241 #endif /* CONFIG_SUSPEND */
242 #ifdef CONFIG_HIBERNATION
243 case PM_EVENT_FREEZE:
244 case PM_EVENT_QUIESCE:
245 if (ops->freeze_noirq) {
246 error = ops->freeze_noirq(dev);
247 suspend_report_result(ops->freeze_noirq, error);
250 case PM_EVENT_HIBERNATE:
251 if (ops->poweroff_noirq) {
252 error = ops->poweroff_noirq(dev);
253 suspend_report_result(ops->poweroff_noirq, error);
257 case PM_EVENT_RECOVER:
258 if (ops->thaw_noirq) {
259 error = ops->thaw_noirq(dev);
260 suspend_report_result(ops->thaw_noirq, error);
263 case PM_EVENT_RESTORE:
264 if (ops->restore_noirq) {
265 error = ops->restore_noirq(dev);
266 suspend_report_result(ops->restore_noirq, error);
269 #endif /* CONFIG_HIBERNATION */
276 static char *pm_verb(int event)
279 case PM_EVENT_SUSPEND:
281 case PM_EVENT_RESUME:
283 case PM_EVENT_FREEZE:
285 case PM_EVENT_QUIESCE:
287 case PM_EVENT_HIBERNATE:
291 case PM_EVENT_RESTORE:
293 case PM_EVENT_RECOVER:
296 return "(unknown PM event)";
300 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
302 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
303 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
304 ", may wakeup" : "");
307 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
310 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
311 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
314 /*------------------------- Resume routines -------------------------*/
317 * resume_device_noirq - Power on one device (early resume).
319 * @state: PM transition of the system being carried out.
321 * Must be called with interrupts disabled.
323 static int resume_device_noirq(struct device *dev, pm_message_t state)
334 pm_dev_dbg(dev, state, "EARLY ");
335 error = pm_noirq_op(dev, dev->bus->pm, state);
336 } else if (dev->bus->resume_early) {
337 pm_dev_dbg(dev, state, "legacy EARLY ");
338 error = dev->bus->resume_early(dev);
346 * dpm_power_up - Power on all regular (non-sysdev) devices.
347 * @state: PM transition of the system being carried out.
349 * Execute the appropriate "noirq resume" callback for all devices marked
352 * Must be called with interrupts disabled and only one CPU running.
354 static void dpm_power_up(pm_message_t state)
358 list_for_each_entry(dev, &dpm_list, power.entry)
359 if (dev->power.status > DPM_OFF) {
362 dev->power.status = DPM_OFF;
363 error = resume_device_noirq(dev, state);
365 pm_dev_err(dev, state, " early", error);
370 * device_power_up - Turn on all devices that need special attention.
371 * @state: PM transition of the system being carried out.
373 * Power on system devices, then devices that required we shut them down
374 * with interrupts disabled.
376 * Must be called with interrupts disabled.
378 void device_power_up(pm_message_t state)
382 EXPORT_SYMBOL_GPL(device_power_up);
385 * resume_device - Restore state for one device.
387 * @state: PM transition of the system being carried out.
389 static int resume_device(struct device *dev, pm_message_t state)
400 pm_dev_dbg(dev, state, "");
401 error = pm_op(dev, dev->bus->pm, state);
402 } else if (dev->bus->resume) {
403 pm_dev_dbg(dev, state, "legacy ");
404 error = dev->bus->resume(dev);
412 pm_dev_dbg(dev, state, "type ");
413 error = pm_op(dev, dev->type->pm, state);
414 } else if (dev->type->resume) {
415 pm_dev_dbg(dev, state, "legacy type ");
416 error = dev->type->resume(dev);
423 if (dev->class->pm) {
424 pm_dev_dbg(dev, state, "class ");
425 error = pm_op(dev, dev->class->pm, state);
426 } else if (dev->class->resume) {
427 pm_dev_dbg(dev, state, "legacy class ");
428 error = dev->class->resume(dev);
439 * dpm_resume - Resume every device.
440 * @state: PM transition of the system being carried out.
442 * Execute the appropriate "resume" callback for all devices the status of
443 * which indicates that they are inactive.
445 static void dpm_resume(pm_message_t state)
447 struct list_head list;
449 INIT_LIST_HEAD(&list);
450 mutex_lock(&dpm_list_mtx);
451 transition_started = false;
452 while (!list_empty(&dpm_list)) {
453 struct device *dev = to_device(dpm_list.next);
456 if (dev->power.status >= DPM_OFF) {
459 dev->power.status = DPM_RESUMING;
460 mutex_unlock(&dpm_list_mtx);
462 error = resume_device(dev, state);
464 mutex_lock(&dpm_list_mtx);
466 pm_dev_err(dev, state, "", error);
467 } else if (dev->power.status == DPM_SUSPENDING) {
468 /* Allow new children of the device to be registered */
469 dev->power.status = DPM_RESUMING;
471 if (!list_empty(&dev->power.entry))
472 list_move_tail(&dev->power.entry, &list);
475 list_splice(&list, &dpm_list);
476 mutex_unlock(&dpm_list_mtx);
480 * complete_device - Complete a PM transition for given device
482 * @state: PM transition of the system being carried out.
484 static void complete_device(struct device *dev, pm_message_t state)
488 if (dev->class && dev->class->pm && dev->class->pm->complete) {
489 pm_dev_dbg(dev, state, "completing class ");
490 dev->class->pm->complete(dev);
493 if (dev->type && dev->type->pm && dev->type->pm->complete) {
494 pm_dev_dbg(dev, state, "completing type ");
495 dev->type->pm->complete(dev);
498 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
499 pm_dev_dbg(dev, state, "completing ");
500 dev->bus->pm->complete(dev);
507 * dpm_complete - Complete a PM transition for all devices.
508 * @state: PM transition of the system being carried out.
510 * Execute the ->complete() callbacks for all devices that are not marked
513 static void dpm_complete(pm_message_t state)
515 struct list_head list;
517 INIT_LIST_HEAD(&list);
518 mutex_lock(&dpm_list_mtx);
519 while (!list_empty(&dpm_list)) {
520 struct device *dev = to_device(dpm_list.prev);
523 if (dev->power.status > DPM_ON) {
524 dev->power.status = DPM_ON;
525 mutex_unlock(&dpm_list_mtx);
527 complete_device(dev, state);
529 mutex_lock(&dpm_list_mtx);
531 if (!list_empty(&dev->power.entry))
532 list_move(&dev->power.entry, &list);
535 list_splice(&list, &dpm_list);
536 mutex_unlock(&dpm_list_mtx);
540 * device_resume - Restore state of each device in system.
541 * @state: PM transition of the system being carried out.
543 * Resume all the devices, unlock them all, and allow new
544 * devices to be registered once again.
546 void device_resume(pm_message_t state)
552 EXPORT_SYMBOL_GPL(device_resume);
555 /*------------------------- Suspend routines -------------------------*/
558 * resume_event - return a PM message representing the resume event
559 * corresponding to given sleep state.
560 * @sleep_state: PM message representing a sleep state.
562 static pm_message_t resume_event(pm_message_t sleep_state)
564 switch (sleep_state.event) {
565 case PM_EVENT_SUSPEND:
567 case PM_EVENT_FREEZE:
568 case PM_EVENT_QUIESCE:
570 case PM_EVENT_HIBERNATE:
577 * suspend_device_noirq - Shut down one device (late suspend).
579 * @state: PM transition of the system being carried out.
581 * This is called with interrupts off and only a single CPU running.
583 static int suspend_device_noirq(struct device *dev, pm_message_t state)
591 pm_dev_dbg(dev, state, "LATE ");
592 error = pm_noirq_op(dev, dev->bus->pm, state);
593 } else if (dev->bus->suspend_late) {
594 pm_dev_dbg(dev, state, "legacy LATE ");
595 error = dev->bus->suspend_late(dev, state);
596 suspend_report_result(dev->bus->suspend_late, error);
602 * device_power_down - Shut down special devices.
603 * @state: PM transition of the system being carried out.
605 * Power down devices that require interrupts to be disabled.
606 * Then power down system devices.
608 * Must be called with interrupts disabled and only one CPU running.
610 int device_power_down(pm_message_t state)
615 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
616 error = suspend_device_noirq(dev, state);
618 pm_dev_err(dev, state, " late", error);
621 dev->power.status = DPM_OFF_IRQ;
624 dpm_power_up(resume_event(state));
627 EXPORT_SYMBOL_GPL(device_power_down);
630 * suspend_device - Save state of one device.
632 * @state: PM transition of the system being carried out.
634 static int suspend_device(struct device *dev, pm_message_t state)
641 if (dev->class->pm) {
642 pm_dev_dbg(dev, state, "class ");
643 error = pm_op(dev, dev->class->pm, state);
644 } else if (dev->class->suspend) {
645 pm_dev_dbg(dev, state, "legacy class ");
646 error = dev->class->suspend(dev, state);
647 suspend_report_result(dev->class->suspend, error);
655 pm_dev_dbg(dev, state, "type ");
656 error = pm_op(dev, dev->type->pm, state);
657 } else if (dev->type->suspend) {
658 pm_dev_dbg(dev, state, "legacy type ");
659 error = dev->type->suspend(dev, state);
660 suspend_report_result(dev->type->suspend, error);
668 pm_dev_dbg(dev, state, "");
669 error = pm_op(dev, dev->bus->pm, state);
670 } else if (dev->bus->suspend) {
671 pm_dev_dbg(dev, state, "legacy ");
672 error = dev->bus->suspend(dev, state);
673 suspend_report_result(dev->bus->suspend, error);
683 * dpm_suspend - Suspend every device.
684 * @state: PM transition of the system being carried out.
686 * Execute the appropriate "suspend" callbacks for all devices.
688 static int dpm_suspend(pm_message_t state)
690 struct list_head list;
693 INIT_LIST_HEAD(&list);
694 mutex_lock(&dpm_list_mtx);
695 while (!list_empty(&dpm_list)) {
696 struct device *dev = to_device(dpm_list.prev);
699 mutex_unlock(&dpm_list_mtx);
701 error = suspend_device(dev, state);
703 mutex_lock(&dpm_list_mtx);
705 pm_dev_err(dev, state, "", error);
709 dev->power.status = DPM_OFF;
710 if (!list_empty(&dev->power.entry))
711 list_move(&dev->power.entry, &list);
714 list_splice(&list, dpm_list.prev);
715 mutex_unlock(&dpm_list_mtx);
720 * prepare_device - Execute the ->prepare() callback(s) for given device.
722 * @state: PM transition of the system being carried out.
724 static int prepare_device(struct device *dev, pm_message_t state)
730 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
731 pm_dev_dbg(dev, state, "preparing ");
732 error = dev->bus->pm->prepare(dev);
733 suspend_report_result(dev->bus->pm->prepare, error);
738 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
739 pm_dev_dbg(dev, state, "preparing type ");
740 error = dev->type->pm->prepare(dev);
741 suspend_report_result(dev->type->pm->prepare, error);
746 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
747 pm_dev_dbg(dev, state, "preparing class ");
748 error = dev->class->pm->prepare(dev);
749 suspend_report_result(dev->class->pm->prepare, error);
758 * dpm_prepare - Prepare all devices for a PM transition.
759 * @state: PM transition of the system being carried out.
761 * Execute the ->prepare() callback for all devices.
763 static int dpm_prepare(pm_message_t state)
765 struct list_head list;
768 INIT_LIST_HEAD(&list);
769 mutex_lock(&dpm_list_mtx);
770 transition_started = true;
771 while (!list_empty(&dpm_list)) {
772 struct device *dev = to_device(dpm_list.next);
775 dev->power.status = DPM_PREPARING;
776 mutex_unlock(&dpm_list_mtx);
778 error = prepare_device(dev, state);
780 mutex_lock(&dpm_list_mtx);
782 dev->power.status = DPM_ON;
783 if (error == -EAGAIN) {
787 printk(KERN_ERR "PM: Failed to prepare device %s "
788 "for power transition: error %d\n",
789 kobject_name(&dev->kobj), error);
793 dev->power.status = DPM_SUSPENDING;
794 if (!list_empty(&dev->power.entry))
795 list_move_tail(&dev->power.entry, &list);
798 list_splice(&list, &dpm_list);
799 mutex_unlock(&dpm_list_mtx);
804 * device_suspend - Save state and stop all devices in system.
805 * @state: PM transition of the system being carried out.
807 * Prepare and suspend all devices.
809 int device_suspend(pm_message_t state)
814 error = dpm_prepare(state);
816 error = dpm_suspend(state);
819 EXPORT_SYMBOL_GPL(device_suspend);
821 void __suspend_report_result(const char *function, void *fn, int ret)
824 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
826 EXPORT_SYMBOL_GPL(__suspend_report_result);