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_off);
52 static LIST_HEAD(dpm_off_irq);
53 static LIST_HEAD(dpm_destroy);
55 static DEFINE_MUTEX(dpm_list_mtx);
57 /* 'true' if all devices have been suspended, protected by dpm_list_mtx */
58 static bool all_sleeping;
61 * device_pm_add - add a device to the list of active devices
62 * @dev: Device to be added to the list
64 int 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 if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
73 if (dev->parent->power.sleeping)
75 "parent %s is sleeping, will not add\n",
78 dev_warn(dev, "devices are sleeping, will not add\n");
82 error = dpm_sysfs_add(dev);
84 list_add_tail(&dev->power.entry, &dpm_active);
86 mutex_unlock(&dpm_list_mtx);
91 * device_pm_remove - remove a device from the list of active devices
92 * @dev: Device to be removed from the list
94 * This function also removes the device's PM-related sysfs attributes.
96 void device_pm_remove(struct device *dev)
98 pr_debug("PM: Removing info for %s:%s\n",
99 dev->bus ? dev->bus->name : "No Bus",
100 kobject_name(&dev->kobj));
101 mutex_lock(&dpm_list_mtx);
102 dpm_sysfs_remove(dev);
103 list_del_init(&dev->power.entry);
104 mutex_unlock(&dpm_list_mtx);
108 * device_pm_schedule_removal - schedule the removal of a suspended device
109 * @dev: Device to destroy
111 * Moves the device to the dpm_destroy list for further processing by
112 * unregister_dropped_devices().
114 void device_pm_schedule_removal(struct device *dev)
116 pr_debug("PM: Preparing for removal: %s:%s\n",
117 dev->bus ? dev->bus->name : "No Bus",
118 kobject_name(&dev->kobj));
119 mutex_lock(&dpm_list_mtx);
120 list_move_tail(&dev->power.entry, &dpm_destroy);
121 mutex_unlock(&dpm_list_mtx);
123 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
125 /*------------------------- Resume routines -------------------------*/
128 * resume_device_early - Power on one device (early resume).
131 * Must be called with interrupts disabled.
133 static int resume_device_early(struct device *dev)
140 if (dev->bus && dev->bus->resume_early) {
141 dev_dbg(dev, "EARLY resume\n");
142 error = dev->bus->resume_early(dev);
150 * dpm_power_up - Power on all regular (non-sysdev) devices.
152 * Walk the dpm_off_irq list and power each device up. This
153 * is used for devices that required they be powered down with
154 * interrupts disabled. As devices are powered on, they are moved
155 * to the dpm_off list.
157 * Must be called with interrupts disabled and only one CPU running.
159 static void dpm_power_up(void)
162 while (!list_empty(&dpm_off_irq)) {
163 struct list_head *entry = dpm_off_irq.next;
164 struct device *dev = to_device(entry);
166 list_move_tail(entry, &dpm_off);
167 resume_device_early(dev);
172 * device_power_up - Turn on all devices that need special attention.
174 * Power on system devices, then devices that required we shut them down
175 * with interrupts disabled.
177 * Must be called with interrupts disabled.
179 void device_power_up(void)
184 EXPORT_SYMBOL_GPL(device_power_up);
187 * resume_device - Restore state for one device.
191 static int resume_device(struct device *dev)
200 if (dev->bus && dev->bus->resume) {
201 dev_dbg(dev,"resuming\n");
202 error = dev->bus->resume(dev);
205 if (!error && dev->type && dev->type->resume) {
206 dev_dbg(dev,"resuming\n");
207 error = dev->type->resume(dev);
210 if (!error && dev->class && dev->class->resume) {
211 dev_dbg(dev,"class resume\n");
212 error = dev->class->resume(dev);
222 * dpm_resume - Resume every device.
224 * Resume the devices that have either not gone through
225 * the late suspend, or that did go through it but also
226 * went through the early resume.
228 * Take devices from the dpm_off_list, resume them,
229 * and put them on the dpm_locked list.
231 static void dpm_resume(void)
233 mutex_lock(&dpm_list_mtx);
234 all_sleeping = false;
235 while(!list_empty(&dpm_off)) {
236 struct list_head *entry = dpm_off.next;
237 struct device *dev = to_device(entry);
239 list_move_tail(entry, &dpm_active);
240 dev->power.sleeping = false;
241 mutex_unlock(&dpm_list_mtx);
243 mutex_lock(&dpm_list_mtx);
245 mutex_unlock(&dpm_list_mtx);
249 * unregister_dropped_devices - Unregister devices scheduled for removal
251 * Unregister all devices on the dpm_destroy list.
253 static void unregister_dropped_devices(void)
255 mutex_lock(&dpm_list_mtx);
256 while (!list_empty(&dpm_destroy)) {
257 struct list_head *entry = dpm_destroy.next;
258 struct device *dev = to_device(entry);
260 mutex_unlock(&dpm_list_mtx);
261 /* This also removes the device from the list */
262 device_unregister(dev);
263 mutex_lock(&dpm_list_mtx);
265 mutex_unlock(&dpm_list_mtx);
269 * device_resume - Restore state of each device in system.
271 * Resume all the devices, unlock them all, and allow new
272 * devices to be registered once again.
274 void device_resume(void)
278 unregister_dropped_devices();
280 EXPORT_SYMBOL_GPL(device_resume);
283 /*------------------------- Suspend routines -------------------------*/
285 static inline char *suspend_verb(u32 event)
288 case PM_EVENT_SUSPEND: return "suspend";
289 case PM_EVENT_FREEZE: return "freeze";
290 case PM_EVENT_PRETHAW: return "prethaw";
291 default: return "(unknown suspend event)";
296 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
298 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
299 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
300 ", may wakeup" : "");
304 * suspend_device_late - Shut down one device (late suspend).
306 * @state: Power state device is entering.
308 * This is called with interrupts off and only a single CPU running.
310 static int suspend_device_late(struct device *dev, pm_message_t state)
314 if (dev->bus && dev->bus->suspend_late) {
315 suspend_device_dbg(dev, state, "LATE ");
316 error = dev->bus->suspend_late(dev, state);
317 suspend_report_result(dev->bus->suspend_late, error);
323 * device_power_down - Shut down special devices.
324 * @state: Power state to enter.
326 * Power down devices that require interrupts to be disabled
327 * and move them from the dpm_off list to the dpm_off_irq list.
328 * Then power down system devices.
330 * Must be called with interrupts disabled and only one CPU running.
332 int device_power_down(pm_message_t state)
336 while (!list_empty(&dpm_off)) {
337 struct list_head *entry = dpm_off.prev;
338 struct device *dev = to_device(entry);
340 error = suspend_device_late(dev, state);
342 printk(KERN_ERR "Could not power down device %s: "
344 kobject_name(&dev->kobj), error);
347 if (!list_empty(&dev->power.entry))
348 list_move(&dev->power.entry, &dpm_off_irq);
352 error = sysdev_suspend(state);
357 EXPORT_SYMBOL_GPL(device_power_down);
360 * suspend_device - Save state of one device.
362 * @state: Power state device is entering.
364 static int suspend_device(struct device *dev, pm_message_t state)
370 if (dev->class && dev->class->suspend) {
371 suspend_device_dbg(dev, state, "class ");
372 error = dev->class->suspend(dev, state);
373 suspend_report_result(dev->class->suspend, error);
376 if (!error && dev->type && dev->type->suspend) {
377 suspend_device_dbg(dev, state, "type ");
378 error = dev->type->suspend(dev, state);
379 suspend_report_result(dev->type->suspend, error);
382 if (!error && dev->bus && dev->bus->suspend) {
383 suspend_device_dbg(dev, state, "");
384 error = dev->bus->suspend(dev, state);
385 suspend_report_result(dev->bus->suspend, error);
394 * dpm_suspend - Suspend every device.
395 * @state: Power state to put each device in.
397 * Walk the dpm_locked list. Suspend each device and move it
398 * to the dpm_off list.
400 * (For historical reasons, if it returns -EAGAIN, that used to mean
401 * that the device would be called again with interrupts disabled.
402 * These days, we use the "suspend_late()" callback for that, so we
403 * print a warning and consider it an error).
405 static int dpm_suspend(pm_message_t state)
409 mutex_lock(&dpm_list_mtx);
410 while (!list_empty(&dpm_active)) {
411 struct list_head *entry = dpm_active.prev;
412 struct device *dev = to_device(entry);
414 WARN_ON(dev->parent && dev->parent->power.sleeping);
416 dev->power.sleeping = true;
417 mutex_unlock(&dpm_list_mtx);
418 error = suspend_device(dev, state);
419 mutex_lock(&dpm_list_mtx);
421 printk(KERN_ERR "Could not suspend device %s: "
423 kobject_name(&dev->kobj),
426 " (please convert to suspend_late)" :
428 dev->power.sleeping = false;
431 if (!list_empty(&dev->power.entry))
432 list_move(&dev->power.entry, &dpm_off);
436 mutex_unlock(&dpm_list_mtx);
442 * device_suspend - Save state and stop all devices in system.
443 * @state: new power management state
445 * Prevent new devices from being registered, then lock all devices
448 int device_suspend(pm_message_t state)
453 error = dpm_suspend(state);
458 EXPORT_SYMBOL_GPL(device_suspend);
460 void __suspend_report_result(const char *function, void *fn, int ret)
463 printk(KERN_ERR "%s(): ", function);
464 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
468 EXPORT_SYMBOL_GPL(__suspend_report_result);