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>
26 #include <linux/interrupt.h>
32 * The entries in the dpm_list list are in a depth first order, simply
33 * because children are guaranteed to be discovered after parents, and
34 * are inserted at the back of the list on discovery.
36 * Since device_pm_add() may be called with a device semaphore held,
37 * we must never try to acquire a device semaphore while holding
43 static DEFINE_MUTEX(dpm_list_mtx
);
46 * Set once the preparation of devices for a PM transition has started, reset
47 * before starting to resume devices. Protected by dpm_list_mtx.
49 static bool transition_started
;
52 * device_pm_lock - lock the list of active devices used by the PM core
54 void device_pm_lock(void)
56 mutex_lock(&dpm_list_mtx
);
60 * device_pm_unlock - unlock the list of active devices used by the PM core
62 void device_pm_unlock(void)
64 mutex_unlock(&dpm_list_mtx
);
68 * device_pm_add - add a device to the list of active devices
69 * @dev: Device to be added to the list
71 void device_pm_add(struct device
*dev
)
73 pr_debug("PM: Adding info for %s:%s\n",
74 dev
->bus
? dev
->bus
->name
: "No Bus",
75 kobject_name(&dev
->kobj
));
76 mutex_lock(&dpm_list_mtx
);
78 if (dev
->parent
->power
.status
>= DPM_SUSPENDING
)
79 dev_warn(dev
, "parent %s should not be sleeping\n",
80 dev_name(dev
->parent
));
81 } else if (transition_started
) {
83 * We refuse to register parentless devices while a PM
84 * transition is in progress in order to avoid leaving them
85 * unhandled down the road
87 dev_WARN(dev
, "Parentless device registered during a PM transaction\n");
90 list_add_tail(&dev
->power
.entry
, &dpm_list
);
91 mutex_unlock(&dpm_list_mtx
);
95 * device_pm_remove - remove a device from the list of active devices
96 * @dev: Device to be removed from the list
98 * This function also removes the device's PM-related sysfs attributes.
100 void device_pm_remove(struct device
*dev
)
102 pr_debug("PM: Removing info for %s:%s\n",
103 dev
->bus
? dev
->bus
->name
: "No Bus",
104 kobject_name(&dev
->kobj
));
105 mutex_lock(&dpm_list_mtx
);
106 list_del_init(&dev
->power
.entry
);
107 mutex_unlock(&dpm_list_mtx
);
111 * device_pm_move_before - move device in dpm_list
112 * @deva: Device to move in dpm_list
113 * @devb: Device @deva should come before
115 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
117 pr_debug("PM: Moving %s:%s before %s:%s\n",
118 deva
->bus
? deva
->bus
->name
: "No Bus",
119 kobject_name(&deva
->kobj
),
120 devb
->bus
? devb
->bus
->name
: "No Bus",
121 kobject_name(&devb
->kobj
));
122 /* Delete deva from dpm_list and reinsert before devb. */
123 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
127 * device_pm_move_after - move device in dpm_list
128 * @deva: Device to move in dpm_list
129 * @devb: Device @deva should come after
131 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
133 pr_debug("PM: Moving %s:%s after %s:%s\n",
134 deva
->bus
? deva
->bus
->name
: "No Bus",
135 kobject_name(&deva
->kobj
),
136 devb
->bus
? devb
->bus
->name
: "No Bus",
137 kobject_name(&devb
->kobj
));
138 /* Delete deva from dpm_list and reinsert after devb. */
139 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
143 * device_pm_move_last - move device to end of dpm_list
144 * @dev: Device to move in dpm_list
146 void device_pm_move_last(struct device
*dev
)
148 pr_debug("PM: Moving %s:%s to end of list\n",
149 dev
->bus
? dev
->bus
->name
: "No Bus",
150 kobject_name(&dev
->kobj
));
151 list_move_tail(&dev
->power
.entry
, &dpm_list
);
155 * pm_op - execute the PM operation appropiate for given PM event
157 * @ops: PM operations to choose from.
158 * @state: PM transition of the system being carried out.
160 static int pm_op(struct device
*dev
, struct dev_pm_ops
*ops
,
165 switch (state
.event
) {
166 #ifdef CONFIG_SUSPEND
167 case PM_EVENT_SUSPEND
:
169 error
= ops
->suspend(dev
);
170 suspend_report_result(ops
->suspend
, error
);
173 case PM_EVENT_RESUME
:
175 error
= ops
->resume(dev
);
176 suspend_report_result(ops
->resume
, error
);
179 #endif /* CONFIG_SUSPEND */
180 #ifdef CONFIG_HIBERNATION
181 case PM_EVENT_FREEZE
:
182 case PM_EVENT_QUIESCE
:
184 error
= ops
->freeze(dev
);
185 suspend_report_result(ops
->freeze
, error
);
188 case PM_EVENT_HIBERNATE
:
190 error
= ops
->poweroff(dev
);
191 suspend_report_result(ops
->poweroff
, error
);
195 case PM_EVENT_RECOVER
:
197 error
= ops
->thaw(dev
);
198 suspend_report_result(ops
->thaw
, error
);
201 case PM_EVENT_RESTORE
:
203 error
= ops
->restore(dev
);
204 suspend_report_result(ops
->restore
, error
);
207 #endif /* CONFIG_HIBERNATION */
215 * pm_noirq_op - execute the PM operation appropiate for given PM event
217 * @ops: PM operations to choose from.
218 * @state: PM transition of the system being carried out.
220 * The operation is executed with interrupts disabled by the only remaining
221 * functional CPU in the system.
223 static int pm_noirq_op(struct device
*dev
, struct dev_pm_ops
*ops
,
228 switch (state
.event
) {
229 #ifdef CONFIG_SUSPEND
230 case PM_EVENT_SUSPEND
:
231 if (ops
->suspend_noirq
) {
232 error
= ops
->suspend_noirq(dev
);
233 suspend_report_result(ops
->suspend_noirq
, error
);
236 case PM_EVENT_RESUME
:
237 if (ops
->resume_noirq
) {
238 error
= ops
->resume_noirq(dev
);
239 suspend_report_result(ops
->resume_noirq
, error
);
242 #endif /* CONFIG_SUSPEND */
243 #ifdef CONFIG_HIBERNATION
244 case PM_EVENT_FREEZE
:
245 case PM_EVENT_QUIESCE
:
246 if (ops
->freeze_noirq
) {
247 error
= ops
->freeze_noirq(dev
);
248 suspend_report_result(ops
->freeze_noirq
, error
);
251 case PM_EVENT_HIBERNATE
:
252 if (ops
->poweroff_noirq
) {
253 error
= ops
->poweroff_noirq(dev
);
254 suspend_report_result(ops
->poweroff_noirq
, error
);
258 case PM_EVENT_RECOVER
:
259 if (ops
->thaw_noirq
) {
260 error
= ops
->thaw_noirq(dev
);
261 suspend_report_result(ops
->thaw_noirq
, error
);
264 case PM_EVENT_RESTORE
:
265 if (ops
->restore_noirq
) {
266 error
= ops
->restore_noirq(dev
);
267 suspend_report_result(ops
->restore_noirq
, error
);
270 #endif /* CONFIG_HIBERNATION */
277 static char *pm_verb(int event
)
280 case PM_EVENT_SUSPEND
:
282 case PM_EVENT_RESUME
:
284 case PM_EVENT_FREEZE
:
286 case PM_EVENT_QUIESCE
:
288 case PM_EVENT_HIBERNATE
:
292 case PM_EVENT_RESTORE
:
294 case PM_EVENT_RECOVER
:
297 return "(unknown PM event)";
301 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
303 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
304 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
305 ", may wakeup" : "");
308 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
311 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
312 kobject_name(&dev
->kobj
), pm_verb(state
.event
), info
, error
);
315 /*------------------------- Resume routines -------------------------*/
318 * resume_device_noirq - Power on one device (early resume).
320 * @state: PM transition of the system being carried out.
322 * Must be called with interrupts disabled.
324 static int resume_device_noirq(struct device
*dev
, pm_message_t state
)
335 pm_dev_dbg(dev
, state
, "EARLY ");
336 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
337 } else if (dev
->bus
->resume_early
) {
338 pm_dev_dbg(dev
, state
, "legacy EARLY ");
339 error
= dev
->bus
->resume_early(dev
);
347 * dpm_power_up - Power on all regular (non-sysdev) devices.
348 * @state: PM transition of the system being carried out.
350 * Execute the appropriate "noirq resume" callback for all devices marked
353 * Must be called under dpm_list_mtx. Device drivers should not receive
354 * interrupts while it's being executed.
356 static void dpm_power_up(pm_message_t state
)
360 mutex_lock(&dpm_list_mtx
);
361 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
362 if (dev
->power
.status
> DPM_OFF
) {
365 dev
->power
.status
= DPM_OFF
;
366 error
= resume_device_noirq(dev
, state
);
368 pm_dev_err(dev
, state
, " early", error
);
370 mutex_unlock(&dpm_list_mtx
);
374 * device_power_up - Turn on all devices that need special attention.
375 * @state: PM transition of the system being carried out.
377 * Call the "early" resume handlers and enable device drivers to receive
380 void device_power_up(pm_message_t state
)
383 resume_device_irqs();
385 EXPORT_SYMBOL_GPL(device_power_up
);
388 * resume_device - Restore state for one device.
390 * @state: PM transition of the system being carried out.
392 static int resume_device(struct device
*dev
, pm_message_t state
)
403 pm_dev_dbg(dev
, state
, "");
404 error
= pm_op(dev
, dev
->bus
->pm
, state
);
405 } else if (dev
->bus
->resume
) {
406 pm_dev_dbg(dev
, state
, "legacy ");
407 error
= dev
->bus
->resume(dev
);
415 pm_dev_dbg(dev
, state
, "type ");
416 error
= pm_op(dev
, dev
->type
->pm
, state
);
417 } else if (dev
->type
->resume
) {
418 pm_dev_dbg(dev
, state
, "legacy type ");
419 error
= dev
->type
->resume(dev
);
426 if (dev
->class->pm
) {
427 pm_dev_dbg(dev
, state
, "class ");
428 error
= pm_op(dev
, dev
->class->pm
, state
);
429 } else if (dev
->class->resume
) {
430 pm_dev_dbg(dev
, state
, "legacy class ");
431 error
= dev
->class->resume(dev
);
442 * dpm_resume - Resume every device.
443 * @state: PM transition of the system being carried out.
445 * Execute the appropriate "resume" callback for all devices the status of
446 * which indicates that they are inactive.
448 static void dpm_resume(pm_message_t state
)
450 struct list_head list
;
452 INIT_LIST_HEAD(&list
);
453 mutex_lock(&dpm_list_mtx
);
454 transition_started
= false;
455 while (!list_empty(&dpm_list
)) {
456 struct device
*dev
= to_device(dpm_list
.next
);
459 if (dev
->power
.status
>= DPM_OFF
) {
462 dev
->power
.status
= DPM_RESUMING
;
463 mutex_unlock(&dpm_list_mtx
);
465 error
= resume_device(dev
, state
);
467 mutex_lock(&dpm_list_mtx
);
469 pm_dev_err(dev
, state
, "", error
);
470 } else if (dev
->power
.status
== DPM_SUSPENDING
) {
471 /* Allow new children of the device to be registered */
472 dev
->power
.status
= DPM_RESUMING
;
474 if (!list_empty(&dev
->power
.entry
))
475 list_move_tail(&dev
->power
.entry
, &list
);
478 list_splice(&list
, &dpm_list
);
479 mutex_unlock(&dpm_list_mtx
);
483 * complete_device - Complete a PM transition for given device
485 * @state: PM transition of the system being carried out.
487 static void complete_device(struct device
*dev
, pm_message_t state
)
491 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->complete
) {
492 pm_dev_dbg(dev
, state
, "completing class ");
493 dev
->class->pm
->complete(dev
);
496 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->complete
) {
497 pm_dev_dbg(dev
, state
, "completing type ");
498 dev
->type
->pm
->complete(dev
);
501 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->complete
) {
502 pm_dev_dbg(dev
, state
, "completing ");
503 dev
->bus
->pm
->complete(dev
);
510 * dpm_complete - Complete a PM transition for all devices.
511 * @state: PM transition of the system being carried out.
513 * Execute the ->complete() callbacks for all devices that are not marked
516 static void dpm_complete(pm_message_t state
)
518 struct list_head list
;
520 INIT_LIST_HEAD(&list
);
521 mutex_lock(&dpm_list_mtx
);
522 while (!list_empty(&dpm_list
)) {
523 struct device
*dev
= to_device(dpm_list
.prev
);
526 if (dev
->power
.status
> DPM_ON
) {
527 dev
->power
.status
= DPM_ON
;
528 mutex_unlock(&dpm_list_mtx
);
530 complete_device(dev
, state
);
532 mutex_lock(&dpm_list_mtx
);
534 if (!list_empty(&dev
->power
.entry
))
535 list_move(&dev
->power
.entry
, &list
);
538 list_splice(&list
, &dpm_list
);
539 mutex_unlock(&dpm_list_mtx
);
543 * device_resume - Restore state of each device in system.
544 * @state: PM transition of the system being carried out.
546 * Resume all the devices, unlock them all, and allow new
547 * devices to be registered once again.
549 void device_resume(pm_message_t state
)
555 EXPORT_SYMBOL_GPL(device_resume
);
558 /*------------------------- Suspend routines -------------------------*/
561 * resume_event - return a PM message representing the resume event
562 * corresponding to given sleep state.
563 * @sleep_state: PM message representing a sleep state.
565 static pm_message_t
resume_event(pm_message_t sleep_state
)
567 switch (sleep_state
.event
) {
568 case PM_EVENT_SUSPEND
:
570 case PM_EVENT_FREEZE
:
571 case PM_EVENT_QUIESCE
:
573 case PM_EVENT_HIBERNATE
:
580 * suspend_device_noirq - Shut down one device (late suspend).
582 * @state: PM transition of the system being carried out.
584 * This is called with interrupts off and only a single CPU running.
586 static int suspend_device_noirq(struct device
*dev
, pm_message_t state
)
594 pm_dev_dbg(dev
, state
, "LATE ");
595 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
596 } else if (dev
->bus
->suspend_late
) {
597 pm_dev_dbg(dev
, state
, "legacy LATE ");
598 error
= dev
->bus
->suspend_late(dev
, state
);
599 suspend_report_result(dev
->bus
->suspend_late
, error
);
605 * device_power_down - Shut down special devices.
606 * @state: PM transition of the system being carried out.
608 * Prevent device drivers from receiving interrupts and call the "late"
611 * Must be called under dpm_list_mtx.
613 int device_power_down(pm_message_t state
)
618 suspend_device_irqs();
619 mutex_lock(&dpm_list_mtx
);
620 list_for_each_entry_reverse(dev
, &dpm_list
, power
.entry
) {
621 error
= suspend_device_noirq(dev
, state
);
623 pm_dev_err(dev
, state
, " late", error
);
626 dev
->power
.status
= DPM_OFF_IRQ
;
628 mutex_unlock(&dpm_list_mtx
);
630 device_power_up(resume_event(state
));
633 EXPORT_SYMBOL_GPL(device_power_down
);
636 * suspend_device - Save state of one device.
638 * @state: PM transition of the system being carried out.
640 static int suspend_device(struct device
*dev
, pm_message_t state
)
647 if (dev
->class->pm
) {
648 pm_dev_dbg(dev
, state
, "class ");
649 error
= pm_op(dev
, dev
->class->pm
, state
);
650 } else if (dev
->class->suspend
) {
651 pm_dev_dbg(dev
, state
, "legacy class ");
652 error
= dev
->class->suspend(dev
, state
);
653 suspend_report_result(dev
->class->suspend
, error
);
661 pm_dev_dbg(dev
, state
, "type ");
662 error
= pm_op(dev
, dev
->type
->pm
, state
);
663 } else if (dev
->type
->suspend
) {
664 pm_dev_dbg(dev
, state
, "legacy type ");
665 error
= dev
->type
->suspend(dev
, state
);
666 suspend_report_result(dev
->type
->suspend
, error
);
674 pm_dev_dbg(dev
, state
, "");
675 error
= pm_op(dev
, dev
->bus
->pm
, state
);
676 } else if (dev
->bus
->suspend
) {
677 pm_dev_dbg(dev
, state
, "legacy ");
678 error
= dev
->bus
->suspend(dev
, state
);
679 suspend_report_result(dev
->bus
->suspend
, error
);
689 * dpm_suspend - Suspend every device.
690 * @state: PM transition of the system being carried out.
692 * Execute the appropriate "suspend" callbacks for all devices.
694 static int dpm_suspend(pm_message_t state
)
696 struct list_head list
;
699 INIT_LIST_HEAD(&list
);
700 mutex_lock(&dpm_list_mtx
);
701 while (!list_empty(&dpm_list
)) {
702 struct device
*dev
= to_device(dpm_list
.prev
);
705 mutex_unlock(&dpm_list_mtx
);
707 error
= suspend_device(dev
, state
);
709 mutex_lock(&dpm_list_mtx
);
711 pm_dev_err(dev
, state
, "", error
);
715 dev
->power
.status
= DPM_OFF
;
716 if (!list_empty(&dev
->power
.entry
))
717 list_move(&dev
->power
.entry
, &list
);
720 list_splice(&list
, dpm_list
.prev
);
721 mutex_unlock(&dpm_list_mtx
);
726 * prepare_device - Execute the ->prepare() callback(s) for given device.
728 * @state: PM transition of the system being carried out.
730 static int prepare_device(struct device
*dev
, pm_message_t state
)
736 if (dev
->bus
&& dev
->bus
->pm
&& dev
->bus
->pm
->prepare
) {
737 pm_dev_dbg(dev
, state
, "preparing ");
738 error
= dev
->bus
->pm
->prepare(dev
);
739 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
744 if (dev
->type
&& dev
->type
->pm
&& dev
->type
->pm
->prepare
) {
745 pm_dev_dbg(dev
, state
, "preparing type ");
746 error
= dev
->type
->pm
->prepare(dev
);
747 suspend_report_result(dev
->type
->pm
->prepare
, error
);
752 if (dev
->class && dev
->class->pm
&& dev
->class->pm
->prepare
) {
753 pm_dev_dbg(dev
, state
, "preparing class ");
754 error
= dev
->class->pm
->prepare(dev
);
755 suspend_report_result(dev
->class->pm
->prepare
, error
);
764 * dpm_prepare - Prepare all devices for a PM transition.
765 * @state: PM transition of the system being carried out.
767 * Execute the ->prepare() callback for all devices.
769 static int dpm_prepare(pm_message_t state
)
771 struct list_head list
;
774 INIT_LIST_HEAD(&list
);
775 mutex_lock(&dpm_list_mtx
);
776 transition_started
= true;
777 while (!list_empty(&dpm_list
)) {
778 struct device
*dev
= to_device(dpm_list
.next
);
781 dev
->power
.status
= DPM_PREPARING
;
782 mutex_unlock(&dpm_list_mtx
);
784 error
= prepare_device(dev
, state
);
786 mutex_lock(&dpm_list_mtx
);
788 dev
->power
.status
= DPM_ON
;
789 if (error
== -EAGAIN
) {
793 printk(KERN_ERR
"PM: Failed to prepare device %s "
794 "for power transition: error %d\n",
795 kobject_name(&dev
->kobj
), error
);
799 dev
->power
.status
= DPM_SUSPENDING
;
800 if (!list_empty(&dev
->power
.entry
))
801 list_move_tail(&dev
->power
.entry
, &list
);
804 list_splice(&list
, &dpm_list
);
805 mutex_unlock(&dpm_list_mtx
);
810 * device_suspend - Save state and stop all devices in system.
811 * @state: PM transition of the system being carried out.
813 * Prepare and suspend all devices.
815 int device_suspend(pm_message_t state
)
820 error
= dpm_prepare(state
);
822 error
= dpm_suspend(state
);
825 EXPORT_SYMBOL_GPL(device_suspend
);
827 void __suspend_report_result(const char *function
, void *fn
, int ret
)
830 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
832 EXPORT_SYMBOL_GPL(__suspend_report_result
);
This page took 0.074507 seconds and 5 git commands to generate.