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 different set of lists than the global subsystem list are used to
16 * keep track of power info because we use different lists to hold
17 * devices based on what stage of the power management process they
18 * are in. The power domain dependencies may also differ from the
19 * ancestral dependencies that the subsystem list maintains.
22 #include <linux/device.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mutex.h>
26 #include <linux/resume-trace.h>
27 #include <linux/rwsem.h>
33 * The entries in the dpm_active list are in a depth first order, simply
34 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery.
37 * All the other lists are kept in the same order, for consistency.
38 * However the lists aren't always traversed in the same order.
39 * Semaphores must be acquired from the top (i.e., front) down
40 * and released in the opposite order. Devices must be suspended
41 * from the bottom (i.e., end) up and resumed in the opposite order.
42 * That way no parent will be suspended while it still has an active
45 * Since device_pm_add() may be called with a device semaphore held,
46 * we must never try to acquire a device semaphore while holding
50 LIST_HEAD(dpm_active
);
51 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
52 static LIST_HEAD(dpm_locked
);
54 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
55 static LIST_HEAD(dpm_off
);
56 static LIST_HEAD(dpm_off_irq
);
57 static LIST_HEAD(dpm_destroy
);
59 static DEFINE_MUTEX(dpm_list_mtx
);
61 static DECLARE_RWSEM(pm_sleep_rwsem
);
63 int (*platform_enable_wakeup
)(struct device
*dev
, int is_on
);
66 * device_pm_add - add a device to the list of active devices
67 * @dev: Device to be added to the list
69 void device_pm_add(struct device
*dev
)
71 pr_debug("PM: Adding info for %s:%s\n",
72 dev
->bus
? dev
->bus
->name
: "No Bus",
73 kobject_name(&dev
->kobj
));
74 mutex_lock(&dpm_list_mtx
);
75 list_add_tail(&dev
->power
.entry
, &dpm_active
);
76 mutex_unlock(&dpm_list_mtx
);
80 * device_pm_remove - remove a device from the list of active devices
81 * @dev: Device to be removed from the list
83 * This function also removes the device's PM-related sysfs attributes.
85 void device_pm_remove(struct device
*dev
)
87 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
89 * If this function is called during a suspend, it will be blocked,
90 * because we're holding the device's semaphore at that time, which may
91 * lead to a deadlock. In that case we want to print a warning.
92 * However, it may also be called by unregister_dropped_devices() with
93 * the device's semaphore released, in which case the warning should
96 if (down_trylock(&dev
->sem
)) {
97 if (down_read_trylock(&pm_sleep_rwsem
)) {
98 /* No suspend in progress, wait on dev->sem */
100 up_read(&pm_sleep_rwsem
);
102 /* Suspend in progress, we may deadlock */
103 dev_warn(dev
, "Suspicious %s during suspend\n",
106 /* The user has been warned ... */
111 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
112 pr_debug("PM: Removing info for %s:%s\n",
113 dev
->bus
? dev
->bus
->name
: "No Bus",
114 kobject_name(&dev
->kobj
));
115 mutex_lock(&dpm_list_mtx
);
116 dpm_sysfs_remove(dev
);
117 list_del_init(&dev
->power
.entry
);
118 mutex_unlock(&dpm_list_mtx
);
119 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
122 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
126 * device_pm_schedule_removal - schedule the removal of a suspended device
127 * @dev: Device to destroy
129 * Moves the device to the dpm_destroy list for further processing by
130 * unregister_dropped_devices().
132 void device_pm_schedule_removal(struct device
*dev
)
134 pr_debug("PM: Preparing for removal: %s:%s\n",
135 dev
->bus
? dev
->bus
->name
: "No Bus",
136 kobject_name(&dev
->kobj
));
137 mutex_lock(&dpm_list_mtx
);
138 list_move_tail(&dev
->power
.entry
, &dpm_destroy
);
139 mutex_unlock(&dpm_list_mtx
);
141 EXPORT_SYMBOL_GPL(device_pm_schedule_removal
);
144 * pm_sleep_lock - mutual exclusion for registration and suspend
146 * Returns 0 if no suspend is underway and device registration
147 * may proceed, otherwise -EBUSY.
149 int pm_sleep_lock(void)
151 if (down_read_trylock(&pm_sleep_rwsem
))
158 * pm_sleep_unlock - mutual exclusion for registration and suspend
160 * This routine undoes the effect of device_pm_add_lock
161 * when a device's registration is complete.
163 void pm_sleep_unlock(void)
165 up_read(&pm_sleep_rwsem
);
169 /*------------------------- Resume routines -------------------------*/
172 * resume_device_early - Power on one device (early resume).
175 * Must be called with interrupts disabled.
177 static int resume_device_early(struct device
*dev
)
184 if (dev
->bus
&& dev
->bus
->resume_early
) {
185 dev_dbg(dev
, "EARLY resume\n");
186 error
= dev
->bus
->resume_early(dev
);
194 * dpm_power_up - Power on all regular (non-sysdev) devices.
196 * Walk the dpm_off_irq list and power each device up. This
197 * is used for devices that required they be powered down with
198 * interrupts disabled. As devices are powered on, they are moved
199 * to the dpm_off list.
201 * Must be called with interrupts disabled and only one CPU running.
203 static void dpm_power_up(void)
206 while (!list_empty(&dpm_off_irq
)) {
207 struct list_head
*entry
= dpm_off_irq
.next
;
208 struct device
*dev
= to_device(entry
);
210 list_move_tail(entry
, &dpm_off
);
211 resume_device_early(dev
);
216 * device_power_up - Turn on all devices that need special attention.
218 * Power on system devices, then devices that required we shut them down
219 * with interrupts disabled.
221 * Must be called with interrupts disabled.
223 void device_power_up(void)
228 EXPORT_SYMBOL_GPL(device_power_up
);
231 * resume_device - Restore state for one device.
235 static int resume_device(struct device
*dev
)
242 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
246 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
247 if (dev
->bus
&& dev
->bus
->resume
) {
248 dev_dbg(dev
,"resuming\n");
249 error
= dev
->bus
->resume(dev
);
252 if (!error
&& dev
->type
&& dev
->type
->resume
) {
253 dev_dbg(dev
,"resuming\n");
254 error
= dev
->type
->resume(dev
);
257 if (!error
&& dev
->class && dev
->class->resume
) {
258 dev_dbg(dev
,"class resume\n");
259 error
= dev
->class->resume(dev
);
262 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
266 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
272 * dpm_resume - Resume every device.
274 * Resume the devices that have either not gone through
275 * the late suspend, or that did go through it but also
276 * went through the early resume.
278 * Take devices from the dpm_off_list, resume them,
279 * and put them on the dpm_locked list.
281 static void dpm_resume(void)
283 mutex_lock(&dpm_list_mtx
);
284 while(!list_empty(&dpm_off
)) {
285 struct list_head
*entry
= dpm_off
.next
;
286 struct device
*dev
= to_device(entry
);
288 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
289 list_move_tail(entry
, &dpm_locked
);
291 list_move_tail(entry
, &dpm_active
);
292 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
293 mutex_unlock(&dpm_list_mtx
);
295 mutex_lock(&dpm_list_mtx
);
297 mutex_unlock(&dpm_list_mtx
);
301 <<<<<<< HEAD:drivers/base/power/main.c
302 * unlock_all_devices - Release each device's semaphore
304 * Go through the dpm_off list. Put each device on the dpm_active
305 * list and unlock it.
307 static void unlock_all_devices(void)
309 mutex_lock(&dpm_list_mtx
);
310 while (!list_empty(&dpm_locked
)) {
311 struct list_head
*entry
= dpm_locked
.prev
;
312 struct device
*dev
= to_device(entry
);
314 list_move(entry
, &dpm_active
);
317 mutex_unlock(&dpm_list_mtx
);
322 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/base/power/main.c
323 * unregister_dropped_devices - Unregister devices scheduled for removal
325 * Unregister all devices on the dpm_destroy list.
327 static void unregister_dropped_devices(void)
329 mutex_lock(&dpm_list_mtx
);
330 while (!list_empty(&dpm_destroy
)) {
331 struct list_head
*entry
= dpm_destroy
.next
;
332 struct device
*dev
= to_device(entry
);
334 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
337 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
338 mutex_unlock(&dpm_list_mtx
);
339 /* This also removes the device from the list */
340 device_unregister(dev
);
341 mutex_lock(&dpm_list_mtx
);
343 mutex_unlock(&dpm_list_mtx
);
347 * device_resume - Restore state of each device in system.
349 * Resume all the devices, unlock them all, and allow new
350 * devices to be registered once again.
352 void device_resume(void)
356 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
357 unlock_all_devices();
359 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
360 unregister_dropped_devices();
361 up_write(&pm_sleep_rwsem
);
363 EXPORT_SYMBOL_GPL(device_resume
);
366 /*------------------------- Suspend routines -------------------------*/
368 static inline char *suspend_verb(u32 event
)
371 case PM_EVENT_SUSPEND
: return "suspend";
372 case PM_EVENT_FREEZE
: return "freeze";
373 case PM_EVENT_PRETHAW
: return "prethaw";
374 default: return "(unknown suspend event)";
379 suspend_device_dbg(struct device
*dev
, pm_message_t state
, char *info
)
381 dev_dbg(dev
, "%s%s%s\n", info
, suspend_verb(state
.event
),
382 ((state
.event
== PM_EVENT_SUSPEND
) && device_may_wakeup(dev
)) ?
383 ", may wakeup" : "");
387 * suspend_device_late - Shut down one device (late suspend).
389 * @state: Power state device is entering.
391 * This is called with interrupts off and only a single CPU running.
393 static int suspend_device_late(struct device
*dev
, pm_message_t state
)
397 if (dev
->bus
&& dev
->bus
->suspend_late
) {
398 suspend_device_dbg(dev
, state
, "LATE ");
399 error
= dev
->bus
->suspend_late(dev
, state
);
400 suspend_report_result(dev
->bus
->suspend_late
, error
);
406 * device_power_down - Shut down special devices.
407 * @state: Power state to enter.
409 * Power down devices that require interrupts to be disabled
410 * and move them from the dpm_off list to the dpm_off_irq list.
411 * Then power down system devices.
413 * Must be called with interrupts disabled and only one CPU running.
415 int device_power_down(pm_message_t state
)
419 while (!list_empty(&dpm_off
)) {
420 struct list_head
*entry
= dpm_off
.prev
;
421 struct device
*dev
= to_device(entry
);
423 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
424 list_del_init(&dev
->power
.entry
);
426 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
427 error
= suspend_device_late(dev
, state
);
429 printk(KERN_ERR
"Could not power down device %s: "
431 kobject_name(&dev
->kobj
), error
);
432 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
433 if (list_empty(&dev
->power
.entry
))
434 list_add(&dev
->power
.entry
, &dpm_off
);
436 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
439 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
440 if (list_empty(&dev
->power
.entry
))
441 list_add(&dev
->power
.entry
, &dpm_off_irq
);
443 if (!list_empty(&dev
->power
.entry
))
444 list_move(&dev
->power
.entry
, &dpm_off_irq
);
445 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
449 error
= sysdev_suspend(state
);
454 EXPORT_SYMBOL_GPL(device_power_down
);
457 * suspend_device - Save state of one device.
459 * @state: Power state device is entering.
461 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
462 int suspend_device(struct device
*dev
, pm_message_t state
)
464 static int suspend_device(struct device
*dev
, pm_message_t state
)
465 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
469 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
473 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
474 if (dev
->power
.power_state
.event
) {
475 dev_dbg(dev
, "PM: suspend %d-->%d\n",
476 dev
->power
.power_state
.event
, state
.event
);
479 if (dev
->class && dev
->class->suspend
) {
480 suspend_device_dbg(dev
, state
, "class ");
481 error
= dev
->class->suspend(dev
, state
);
482 suspend_report_result(dev
->class->suspend
, error
);
485 if (!error
&& dev
->type
&& dev
->type
->suspend
) {
486 suspend_device_dbg(dev
, state
, "type ");
487 error
= dev
->type
->suspend(dev
, state
);
488 suspend_report_result(dev
->type
->suspend
, error
);
491 if (!error
&& dev
->bus
&& dev
->bus
->suspend
) {
492 suspend_device_dbg(dev
, state
, "");
493 error
= dev
->bus
->suspend(dev
, state
);
494 suspend_report_result(dev
->bus
->suspend
, error
);
496 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
501 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
506 * dpm_suspend - Suspend every device.
507 * @state: Power state to put each device in.
509 * Walk the dpm_locked list. Suspend each device and move it
510 * to the dpm_off list.
512 * (For historical reasons, if it returns -EAGAIN, that used to mean
513 * that the device would be called again with interrupts disabled.
514 * These days, we use the "suspend_late()" callback for that, so we
515 * print a warning and consider it an error).
517 static int dpm_suspend(pm_message_t state
)
521 mutex_lock(&dpm_list_mtx
);
522 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
523 while (!list_empty(&dpm_locked
)) {
524 struct list_head
*entry
= dpm_locked
.prev
;
526 while (!list_empty(&dpm_active
)) {
527 struct list_head
*entry
= dpm_active
.prev
;
528 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
529 struct device
*dev
= to_device(entry
);
531 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
532 list_del_init(&dev
->power
.entry
);
534 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
535 mutex_unlock(&dpm_list_mtx
);
536 error
= suspend_device(dev
, state
);
537 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
539 mutex_lock(&dpm_list_mtx
);
540 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
542 printk(KERN_ERR
"Could not suspend device %s: "
544 kobject_name(&dev
->kobj
),
547 " (please convert to suspend_late)" :
549 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
550 mutex_lock(&dpm_list_mtx
);
551 if (list_empty(&dev
->power
.entry
))
552 list_add(&dev
->power
.entry
, &dpm_locked
);
553 mutex_unlock(&dpm_list_mtx
);
555 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
558 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
559 mutex_lock(&dpm_list_mtx
);
560 if (list_empty(&dev
->power
.entry
))
561 list_add(&dev
->power
.entry
, &dpm_off
);
563 if (!list_empty(&dev
->power
.entry
))
564 list_move(&dev
->power
.entry
, &dpm_off
);
565 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
567 mutex_unlock(&dpm_list_mtx
);
573 <<<<<<< HEAD:drivers/base/power/main.c
574 * lock_all_devices - Acquire every device's semaphore
576 * Go through the dpm_active list. Carefully lock each device's
577 * semaphore and put it in on the dpm_locked list.
579 static void lock_all_devices(void)
581 mutex_lock(&dpm_list_mtx
);
582 while (!list_empty(&dpm_active
)) {
583 struct list_head
*entry
= dpm_active
.next
;
584 struct device
*dev
= to_device(entry
);
586 /* Required locking order is dev->sem first,
587 * then dpm_list_mutex. Hence this awkward code.
590 mutex_unlock(&dpm_list_mtx
);
592 mutex_lock(&dpm_list_mtx
);
594 if (list_empty(entry
))
595 up(&dev
->sem
); /* Device was removed */
597 list_move_tail(entry
, &dpm_locked
);
600 mutex_unlock(&dpm_list_mtx
);
605 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/base/power/main.c
606 * device_suspend - Save state and stop all devices in system.
607 <<<<<<< HEAD:drivers/base/power/main.c
609 * @state: new power management state
610 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/base/power/main.c
612 * Prevent new devices from being registered, then lock all devices
615 int device_suspend(pm_message_t state
)
620 down_write(&pm_sleep_rwsem
);
621 <<<<<<< HEAD
:drivers
/base
/power
/main
.c
624 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/base
/power
/main
.c
625 error
= dpm_suspend(state
);
630 EXPORT_SYMBOL_GPL(device_suspend
);
632 void __suspend_report_result(const char *function
, void *fn
, int ret
)
635 printk(KERN_ERR
"%s(): ", function
);
636 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn
);
640 EXPORT_SYMBOL_GPL(__suspend_report_result
);