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 static LIST_HEAD(dpm_locked
);
52 static LIST_HEAD(dpm_off
);
53 static LIST_HEAD(dpm_off_irq
);
54 static LIST_HEAD(dpm_destroy
);
56 static DEFINE_MUTEX(dpm_list_mtx
);
58 static DECLARE_RWSEM(pm_sleep_rwsem
);
60 int (*platform_enable_wakeup
)(struct device
*dev
, int is_on
);
63 * device_pm_add - add a device to the list of active devices
64 * @dev: Device to be added to the list
66 void device_pm_add(struct device
*dev
)
68 pr_debug("PM: Adding info for %s:%s\n",
69 dev
->bus
? dev
->bus
->name
: "No Bus",
70 kobject_name(&dev
->kobj
));
71 mutex_lock(&dpm_list_mtx
);
72 list_add_tail(&dev
->power
.entry
, &dpm_active
);
73 mutex_unlock(&dpm_list_mtx
);
77 * device_pm_remove - remove a device from the list of active devices
78 * @dev: Device to be removed from the list
80 * This function also removes the device's PM-related sysfs attributes.
82 void device_pm_remove(struct device
*dev
)
85 * If this function is called during a suspend, it will be blocked,
86 * because we're holding the device's semaphore at that time, which may
87 * lead to a deadlock. In that case we want to print a warning.
88 * However, it may also be called by unregister_dropped_devices() with
89 * the device's semaphore released, in which case the warning should
92 if (down_trylock(&dev
->sem
)) {
93 if (down_read_trylock(&pm_sleep_rwsem
)) {
94 /* No suspend in progress, wait on dev->sem */
96 up_read(&pm_sleep_rwsem
);
98 /* Suspend in progress, we may deadlock */
99 dev_warn(dev
, "Suspicious %s during suspend\n",
102 /* The user has been warned ... */
106 pr_debug("PM: Removing info for %s:%s\n",
107 dev
->bus
? dev
->bus
->name
: "No Bus",
108 kobject_name(&dev
->kobj
));
109 mutex_lock(&dpm_list_mtx
);
110 dpm_sysfs_remove(dev
);
111 list_del_init(&dev
->power
.entry
);
112 mutex_unlock(&dpm_list_mtx
);
117 * device_pm_schedule_removal - schedule the removal of a suspended device
118 * @dev: Device to destroy
120 * Moves the device to the dpm_destroy list for further processing by
121 * unregister_dropped_devices().
123 void device_pm_schedule_removal(struct device
*dev
)
125 pr_debug("PM: Preparing for removal: %s:%s\n",
126 dev
->bus
? dev
->bus
->name
: "No Bus",
127 kobject_name(&dev
->kobj
));
128 mutex_lock(&dpm_list_mtx
);
129 list_move_tail(&dev
->power
.entry
, &dpm_destroy
);
130 mutex_unlock(&dpm_list_mtx
);
134 * pm_sleep_lock - mutual exclusion for registration and suspend
136 * Returns 0 if no suspend is underway and device registration
137 * may proceed, otherwise -EBUSY.
139 int pm_sleep_lock(void)
141 if (down_read_trylock(&pm_sleep_rwsem
))
148 * pm_sleep_unlock - mutual exclusion for registration and suspend
150 * This routine undoes the effect of device_pm_add_lock
151 * when a device's registration is complete.
153 void pm_sleep_unlock(void)
155 up_read(&pm_sleep_rwsem
);
159 /*------------------------- Resume routines -------------------------*/
162 * resume_device_early - Power on one device (early resume).
165 * Must be called with interrupts disabled.
167 static int resume_device_early(struct device
*dev
)
174 if (dev
->bus
&& dev
->bus
->resume_early
) {
175 dev_dbg(dev
, "EARLY resume\n");
176 error
= dev
->bus
->resume_early(dev
);
184 * dpm_power_up - Power on all regular (non-sysdev) devices.
186 * Walk the dpm_off_irq list and power each device up. This
187 * is used for devices that required they be powered down with
188 * interrupts disabled. As devices are powered on, they are moved
189 * to the dpm_off list.
191 * Must be called with interrupts disabled and only one CPU running.
193 static void dpm_power_up(void)
196 while (!list_empty(&dpm_off_irq
)) {
197 struct list_head
*entry
= dpm_off_irq
.next
;
198 struct device
*dev
= to_device(entry
);
200 list_move_tail(entry
, &dpm_off
);
201 resume_device_early(dev
);
206 * device_power_up - Turn on all devices that need special attention.
208 * Power on system devices, then devices that required we shut them down
209 * with interrupts disabled.
211 * Must be called with interrupts disabled.
213 void device_power_up(void)
218 EXPORT_SYMBOL_GPL(device_power_up
);
221 * resume_device - Restore state for one device.
225 static int resume_device(struct device
*dev
)
232 if (dev
->bus
&& dev
->bus
->resume
) {
233 dev_dbg(dev
,"resuming\n");
234 error
= dev
->bus
->resume(dev
);
237 if (!error
&& dev
->type
&& dev
->type
->resume
) {
238 dev_dbg(dev
,"resuming\n");
239 error
= dev
->type
->resume(dev
);
242 if (!error
&& dev
->class && dev
->class->resume
) {
243 dev_dbg(dev
,"class resume\n");
244 error
= dev
->class->resume(dev
);
252 * dpm_resume - Resume every device.
254 * Resume the devices that have either not gone through
255 * the late suspend, or that did go through it but also
256 * went through the early resume.
258 * Take devices from the dpm_off_list, resume them,
259 * and put them on the dpm_locked list.
261 static void dpm_resume(void)
263 mutex_lock(&dpm_list_mtx
);
264 while(!list_empty(&dpm_off
)) {
265 struct list_head
*entry
= dpm_off
.next
;
266 struct device
*dev
= to_device(entry
);
268 list_move_tail(entry
, &dpm_locked
);
269 mutex_unlock(&dpm_list_mtx
);
271 mutex_lock(&dpm_list_mtx
);
273 mutex_unlock(&dpm_list_mtx
);
277 * unlock_all_devices - Release each device's semaphore
279 * Go through the dpm_off list. Put each device on the dpm_active
280 * list and unlock it.
282 static void unlock_all_devices(void)
284 mutex_lock(&dpm_list_mtx
);
285 while (!list_empty(&dpm_locked
)) {
286 struct list_head
*entry
= dpm_locked
.prev
;
287 struct device
*dev
= to_device(entry
);
289 list_move(entry
, &dpm_active
);
292 mutex_unlock(&dpm_list_mtx
);
296 * unregister_dropped_devices - Unregister devices scheduled for removal
298 * Unregister all devices on the dpm_destroy list.
300 static void unregister_dropped_devices(void)
302 mutex_lock(&dpm_list_mtx
);
303 while (!list_empty(&dpm_destroy
)) {
304 struct list_head
*entry
= dpm_destroy
.next
;
305 struct device
*dev
= to_device(entry
);
308 mutex_unlock(&dpm_list_mtx
);
309 /* This also removes the device from the list */
310 device_unregister(dev
);
311 mutex_lock(&dpm_list_mtx
);
313 mutex_unlock(&dpm_list_mtx
);
317 * device_resume - Restore state of each device in system.
319 * Resume all the devices, unlock them all, and allow new
320 * devices to be registered once again.
322 void device_resume(void)
326 unlock_all_devices();
327 unregister_dropped_devices();
328 up_write(&pm_sleep_rwsem
);
330 EXPORT_SYMBOL_GPL(device_resume
);
333 /*------------------------- Suspend routines -------------------------*/
335 static inline char *suspend_verb(u32 event
)
338 case PM_EVENT_SUSPEND
: return "suspend";
339 case PM_EVENT_FREEZE
: return "freeze";
340 case PM_EVENT_PRETHAW
: return "prethaw";
341 default: return "(unknown suspend event)";
346 suspend_device_dbg(struct device
*dev
, pm_message_t state
, char *info
)
348 dev_dbg(dev
, "%s%s%s\n", info
, suspend_verb(state
.event
),
349 ((state
.event
== PM_EVENT_SUSPEND
) && device_may_wakeup(dev
)) ?
350 ", may wakeup" : "");
354 * suspend_device_late - Shut down one device (late suspend).
356 * @state: Power state device is entering.
358 * This is called with interrupts off and only a single CPU running.
360 static int suspend_device_late(struct device
*dev
, pm_message_t state
)
364 if (dev
->bus
&& dev
->bus
->suspend_late
) {
365 suspend_device_dbg(dev
, state
, "LATE ");
366 error
= dev
->bus
->suspend_late(dev
, state
);
367 suspend_report_result(dev
->bus
->suspend_late
, error
);
373 * device_power_down - Shut down special devices.
374 * @state: Power state to enter.
376 * Power down devices that require interrupts to be disabled
377 * and move them from the dpm_off list to the dpm_off_irq list.
378 * Then power down system devices.
380 * Must be called with interrupts disabled and only one CPU running.
382 int device_power_down(pm_message_t state
)
386 while (!list_empty(&dpm_off
)) {
387 struct list_head
*entry
= dpm_off
.prev
;
388 struct device
*dev
= to_device(entry
);
390 list_del_init(&dev
->power
.entry
);
391 error
= suspend_device_late(dev
, state
);
393 printk(KERN_ERR
"Could not power down device %s: "
395 kobject_name(&dev
->kobj
), error
);
396 if (list_empty(&dev
->power
.entry
))
397 list_add(&dev
->power
.entry
, &dpm_off
);
400 if (list_empty(&dev
->power
.entry
))
401 list_add(&dev
->power
.entry
, &dpm_off_irq
);
405 error
= sysdev_suspend(state
);
410 EXPORT_SYMBOL_GPL(device_power_down
);
413 * suspend_device - Save state of one device.
415 * @state: Power state device is entering.
417 int suspend_device(struct device
*dev
, pm_message_t state
)
421 if (dev
->power
.power_state
.event
) {
422 dev_dbg(dev
, "PM: suspend %d-->%d\n",
423 dev
->power
.power_state
.event
, state
.event
);
426 if (dev
->class && dev
->class->suspend
) {
427 suspend_device_dbg(dev
, state
, "class ");
428 error
= dev
->class->suspend(dev
, state
);
429 suspend_report_result(dev
->class->suspend
, error
);
432 if (!error
&& dev
->type
&& dev
->type
->suspend
) {
433 suspend_device_dbg(dev
, state
, "type ");
434 error
= dev
->type
->suspend(dev
, state
);
435 suspend_report_result(dev
->type
->suspend
, error
);
438 if (!error
&& dev
->bus
&& dev
->bus
->suspend
) {
439 suspend_device_dbg(dev
, state
, "");
440 error
= dev
->bus
->suspend(dev
, state
);
441 suspend_report_result(dev
->bus
->suspend
, error
);
447 * dpm_suspend - Suspend every device.
448 * @state: Power state to put each device in.
450 * Walk the dpm_locked list. Suspend each device and move it
451 * to the dpm_off list.
453 * (For historical reasons, if it returns -EAGAIN, that used to mean
454 * that the device would be called again with interrupts disabled.
455 * These days, we use the "suspend_late()" callback for that, so we
456 * print a warning and consider it an error).
458 static int dpm_suspend(pm_message_t state
)
462 mutex_lock(&dpm_list_mtx
);
463 while (!list_empty(&dpm_locked
)) {
464 struct list_head
*entry
= dpm_locked
.prev
;
465 struct device
*dev
= to_device(entry
);
467 list_del_init(&dev
->power
.entry
);
468 mutex_unlock(&dpm_list_mtx
);
469 error
= suspend_device(dev
, state
);
471 printk(KERN_ERR
"Could not suspend device %s: "
473 kobject_name(&dev
->kobj
),
476 " (please convert to suspend_late)" :
478 mutex_lock(&dpm_list_mtx
);
479 if (list_empty(&dev
->power
.entry
))
480 list_add(&dev
->power
.entry
, &dpm_locked
);
481 mutex_unlock(&dpm_list_mtx
);
484 mutex_lock(&dpm_list_mtx
);
485 if (list_empty(&dev
->power
.entry
))
486 list_add(&dev
->power
.entry
, &dpm_off
);
488 mutex_unlock(&dpm_list_mtx
);
494 * lock_all_devices - Acquire every device's semaphore
496 * Go through the dpm_active list. Carefully lock each device's
497 * semaphore and put it in on the dpm_locked list.
499 static void lock_all_devices(void)
501 mutex_lock(&dpm_list_mtx
);
502 while (!list_empty(&dpm_active
)) {
503 struct list_head
*entry
= dpm_active
.next
;
504 struct device
*dev
= to_device(entry
);
506 /* Required locking order is dev->sem first,
507 * then dpm_list_mutex. Hence this awkward code.
510 mutex_unlock(&dpm_list_mtx
);
512 mutex_lock(&dpm_list_mtx
);
514 if (list_empty(entry
))
515 up(&dev
->sem
); /* Device was removed */
517 list_move_tail(entry
, &dpm_locked
);
520 mutex_unlock(&dpm_list_mtx
);
524 * device_suspend - Save state and stop all devices in system.
526 * Prevent new devices from being registered, then lock all devices
529 int device_suspend(pm_message_t state
)
534 down_write(&pm_sleep_rwsem
);
536 error
= dpm_suspend(state
);
541 EXPORT_SYMBOL_GPL(device_suspend
);
543 void __suspend_report_result(const char *function
, void *fn
, int ret
)
546 printk(KERN_ERR
"%s(): ", function
);
547 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn
);
551 EXPORT_SYMBOL_GPL(__suspend_report_result
);