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);
132 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
135 * pm_sleep_lock - mutual exclusion for registration and suspend
137 * Returns 0 if no suspend is underway and device registration
138 * may proceed, otherwise -EBUSY.
140 int pm_sleep_lock(void)
142 if (down_read_trylock(&pm_sleep_rwsem))
149 * pm_sleep_unlock - mutual exclusion for registration and suspend
151 * This routine undoes the effect of device_pm_add_lock
152 * when a device's registration is complete.
154 void pm_sleep_unlock(void)
156 up_read(&pm_sleep_rwsem);
160 /*------------------------- Resume routines -------------------------*/
163 * resume_device_early - Power on one device (early resume).
166 * Must be called with interrupts disabled.
168 static int resume_device_early(struct device *dev)
175 if (dev->bus && dev->bus->resume_early) {
176 dev_dbg(dev, "EARLY resume\n");
177 error = dev->bus->resume_early(dev);
185 * dpm_power_up - Power on all regular (non-sysdev) devices.
187 * Walk the dpm_off_irq list and power each device up. This
188 * is used for devices that required they be powered down with
189 * interrupts disabled. As devices are powered on, they are moved
190 * to the dpm_off list.
192 * Must be called with interrupts disabled and only one CPU running.
194 static void dpm_power_up(void)
197 while (!list_empty(&dpm_off_irq)) {
198 struct list_head *entry = dpm_off_irq.next;
199 struct device *dev = to_device(entry);
201 list_move_tail(entry, &dpm_off);
202 resume_device_early(dev);
207 * device_power_up - Turn on all devices that need special attention.
209 * Power on system devices, then devices that required we shut them down
210 * with interrupts disabled.
212 * Must be called with interrupts disabled.
214 void device_power_up(void)
219 EXPORT_SYMBOL_GPL(device_power_up);
222 * resume_device - Restore state for one device.
226 static int resume_device(struct device *dev)
233 if (dev->bus && dev->bus->resume) {
234 dev_dbg(dev,"resuming\n");
235 error = dev->bus->resume(dev);
238 if (!error && dev->type && dev->type->resume) {
239 dev_dbg(dev,"resuming\n");
240 error = dev->type->resume(dev);
243 if (!error && dev->class && dev->class->resume) {
244 dev_dbg(dev,"class resume\n");
245 error = dev->class->resume(dev);
253 * dpm_resume - Resume every device.
255 * Resume the devices that have either not gone through
256 * the late suspend, or that did go through it but also
257 * went through the early resume.
259 * Take devices from the dpm_off_list, resume them,
260 * and put them on the dpm_locked list.
262 static void dpm_resume(void)
264 mutex_lock(&dpm_list_mtx);
265 while(!list_empty(&dpm_off)) {
266 struct list_head *entry = dpm_off.next;
267 struct device *dev = to_device(entry);
269 list_move_tail(entry, &dpm_locked);
270 mutex_unlock(&dpm_list_mtx);
272 mutex_lock(&dpm_list_mtx);
274 mutex_unlock(&dpm_list_mtx);
278 * unlock_all_devices - Release each device's semaphore
280 * Go through the dpm_off list. Put each device on the dpm_active
281 * list and unlock it.
283 static void unlock_all_devices(void)
285 mutex_lock(&dpm_list_mtx);
286 while (!list_empty(&dpm_locked)) {
287 struct list_head *entry = dpm_locked.prev;
288 struct device *dev = to_device(entry);
290 list_move(entry, &dpm_active);
293 mutex_unlock(&dpm_list_mtx);
297 * unregister_dropped_devices - Unregister devices scheduled for removal
299 * Unregister all devices on the dpm_destroy list.
301 static void unregister_dropped_devices(void)
303 mutex_lock(&dpm_list_mtx);
304 while (!list_empty(&dpm_destroy)) {
305 struct list_head *entry = dpm_destroy.next;
306 struct device *dev = to_device(entry);
309 mutex_unlock(&dpm_list_mtx);
310 /* This also removes the device from the list */
311 device_unregister(dev);
312 mutex_lock(&dpm_list_mtx);
314 mutex_unlock(&dpm_list_mtx);
318 * device_resume - Restore state of each device in system.
320 * Resume all the devices, unlock them all, and allow new
321 * devices to be registered once again.
323 void device_resume(void)
327 unlock_all_devices();
328 unregister_dropped_devices();
329 up_write(&pm_sleep_rwsem);
331 EXPORT_SYMBOL_GPL(device_resume);
334 /*------------------------- Suspend routines -------------------------*/
336 static inline char *suspend_verb(u32 event)
339 case PM_EVENT_SUSPEND: return "suspend";
340 case PM_EVENT_FREEZE: return "freeze";
341 case PM_EVENT_PRETHAW: return "prethaw";
342 default: return "(unknown suspend event)";
347 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
349 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
350 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
351 ", may wakeup" : "");
355 * suspend_device_late - Shut down one device (late suspend).
357 * @state: Power state device is entering.
359 * This is called with interrupts off and only a single CPU running.
361 static int suspend_device_late(struct device *dev, pm_message_t state)
365 if (dev->bus && dev->bus->suspend_late) {
366 suspend_device_dbg(dev, state, "LATE ");
367 error = dev->bus->suspend_late(dev, state);
368 suspend_report_result(dev->bus->suspend_late, error);
374 * device_power_down - Shut down special devices.
375 * @state: Power state to enter.
377 * Power down devices that require interrupts to be disabled
378 * and move them from the dpm_off list to the dpm_off_irq list.
379 * Then power down system devices.
381 * Must be called with interrupts disabled and only one CPU running.
383 int device_power_down(pm_message_t state)
387 while (!list_empty(&dpm_off)) {
388 struct list_head *entry = dpm_off.prev;
389 struct device *dev = to_device(entry);
391 list_del_init(&dev->power.entry);
392 error = suspend_device_late(dev, state);
394 printk(KERN_ERR "Could not power down device %s: "
396 kobject_name(&dev->kobj), error);
397 if (list_empty(&dev->power.entry))
398 list_add(&dev->power.entry, &dpm_off);
401 if (list_empty(&dev->power.entry))
402 list_add(&dev->power.entry, &dpm_off_irq);
406 error = sysdev_suspend(state);
411 EXPORT_SYMBOL_GPL(device_power_down);
414 * suspend_device - Save state of one device.
416 * @state: Power state device is entering.
418 int suspend_device(struct device *dev, pm_message_t state)
422 if (dev->power.power_state.event) {
423 dev_dbg(dev, "PM: suspend %d-->%d\n",
424 dev->power.power_state.event, state.event);
427 if (dev->class && dev->class->suspend) {
428 suspend_device_dbg(dev, state, "class ");
429 error = dev->class->suspend(dev, state);
430 suspend_report_result(dev->class->suspend, error);
433 if (!error && dev->type && dev->type->suspend) {
434 suspend_device_dbg(dev, state, "type ");
435 error = dev->type->suspend(dev, state);
436 suspend_report_result(dev->type->suspend, error);
439 if (!error && dev->bus && dev->bus->suspend) {
440 suspend_device_dbg(dev, state, "");
441 error = dev->bus->suspend(dev, state);
442 suspend_report_result(dev->bus->suspend, error);
448 * dpm_suspend - Suspend every device.
449 * @state: Power state to put each device in.
451 * Walk the dpm_locked list. Suspend each device and move it
452 * to the dpm_off list.
454 * (For historical reasons, if it returns -EAGAIN, that used to mean
455 * that the device would be called again with interrupts disabled.
456 * These days, we use the "suspend_late()" callback for that, so we
457 * print a warning and consider it an error).
459 static int dpm_suspend(pm_message_t state)
463 mutex_lock(&dpm_list_mtx);
464 while (!list_empty(&dpm_locked)) {
465 struct list_head *entry = dpm_locked.prev;
466 struct device *dev = to_device(entry);
468 list_del_init(&dev->power.entry);
469 mutex_unlock(&dpm_list_mtx);
470 error = suspend_device(dev, state);
472 printk(KERN_ERR "Could not suspend device %s: "
474 kobject_name(&dev->kobj),
477 " (please convert to suspend_late)" :
479 mutex_lock(&dpm_list_mtx);
480 if (list_empty(&dev->power.entry))
481 list_add(&dev->power.entry, &dpm_locked);
482 mutex_unlock(&dpm_list_mtx);
485 mutex_lock(&dpm_list_mtx);
486 if (list_empty(&dev->power.entry))
487 list_add(&dev->power.entry, &dpm_off);
489 mutex_unlock(&dpm_list_mtx);
495 * lock_all_devices - Acquire every device's semaphore
497 * Go through the dpm_active list. Carefully lock each device's
498 * semaphore and put it in on the dpm_locked list.
500 static void lock_all_devices(void)
502 mutex_lock(&dpm_list_mtx);
503 while (!list_empty(&dpm_active)) {
504 struct list_head *entry = dpm_active.next;
505 struct device *dev = to_device(entry);
507 /* Required locking order is dev->sem first,
508 * then dpm_list_mutex. Hence this awkward code.
511 mutex_unlock(&dpm_list_mtx);
513 mutex_lock(&dpm_list_mtx);
515 if (list_empty(entry))
516 up(&dev->sem); /* Device was removed */
518 list_move_tail(entry, &dpm_locked);
521 mutex_unlock(&dpm_list_mtx);
525 * device_suspend - Save state and stop all devices in system.
527 * Prevent new devices from being registered, then lock all devices
530 int device_suspend(pm_message_t state)
535 down_write(&pm_sleep_rwsem);
537 error = dpm_suspend(state);
542 EXPORT_SYMBOL_GPL(device_suspend);
544 void __suspend_report_result(const char *function, void *fn, int ret)
547 printk(KERN_ERR "%s(): ", function);
548 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
552 EXPORT_SYMBOL_GPL(__suspend_report_result);