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;
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 int device_pm_add(struct device *dev)
70 pr_debug("PM: Adding info for %s:%s\n",
71 dev->bus ? dev->bus->name : "No Bus",
72 kobject_name(&dev->kobj));
73 mutex_lock(&dpm_list_mtx);
74 if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
75 if (dev->parent->power.sleeping)
77 "parent %s is sleeping, will not add\n",
80 dev_warn(dev, "devices are sleeping, will not add\n");
84 error = dpm_sysfs_add(dev);
86 list_add_tail(&dev->power.entry, &dpm_active);
88 mutex_unlock(&dpm_list_mtx);
93 * device_pm_remove - remove a device from the list of active devices
94 * @dev: Device to be removed from the list
96 * This function also removes the device's PM-related sysfs attributes.
98 void device_pm_remove(struct device *dev)
100 pr_debug("PM: Removing info for %s:%s\n",
101 dev->bus ? dev->bus->name : "No Bus",
102 kobject_name(&dev->kobj));
103 mutex_lock(&dpm_list_mtx);
104 dpm_sysfs_remove(dev);
105 list_del_init(&dev->power.entry);
106 mutex_unlock(&dpm_list_mtx);
110 * device_pm_schedule_removal - schedule the removal of a suspended device
111 * @dev: Device to destroy
113 * Moves the device to the dpm_destroy list for further processing by
114 * unregister_dropped_devices().
116 void device_pm_schedule_removal(struct device *dev)
118 pr_debug("PM: Preparing for removal: %s:%s\n",
119 dev->bus ? dev->bus->name : "No Bus",
120 kobject_name(&dev->kobj));
121 mutex_lock(&dpm_list_mtx);
122 list_move_tail(&dev->power.entry, &dpm_destroy);
123 mutex_unlock(&dpm_list_mtx);
125 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
127 /*------------------------- Resume routines -------------------------*/
130 * resume_device_early - Power on one device (early resume).
133 * Must be called with interrupts disabled.
135 static int resume_device_early(struct device *dev)
142 if (dev->bus && dev->bus->resume_early) {
143 dev_dbg(dev, "EARLY resume\n");
144 error = dev->bus->resume_early(dev);
152 * dpm_power_up - Power on all regular (non-sysdev) devices.
154 * Walk the dpm_off_irq list and power each device up. This
155 * is used for devices that required they be powered down with
156 * interrupts disabled. As devices are powered on, they are moved
157 * to the dpm_off list.
159 * Must be called with interrupts disabled and only one CPU running.
161 static void dpm_power_up(void)
164 while (!list_empty(&dpm_off_irq)) {
165 struct list_head *entry = dpm_off_irq.next;
166 struct device *dev = to_device(entry);
168 list_move_tail(entry, &dpm_off);
169 resume_device_early(dev);
174 * device_power_up - Turn on all devices that need special attention.
176 * Power on system devices, then devices that required we shut them down
177 * with interrupts disabled.
179 * Must be called with interrupts disabled.
181 void device_power_up(void)
186 EXPORT_SYMBOL_GPL(device_power_up);
189 * resume_device - Restore state for one device.
193 static int resume_device(struct device *dev)
202 if (dev->bus && dev->bus->resume) {
203 dev_dbg(dev,"resuming\n");
204 error = dev->bus->resume(dev);
207 if (!error && dev->type && dev->type->resume) {
208 dev_dbg(dev,"resuming\n");
209 error = dev->type->resume(dev);
212 if (!error && dev->class && dev->class->resume) {
213 dev_dbg(dev,"class resume\n");
214 error = dev->class->resume(dev);
224 * dpm_resume - Resume every device.
226 * Resume the devices that have either not gone through
227 * the late suspend, or that did go through it but also
228 * went through the early resume.
230 * Take devices from the dpm_off_list, resume them,
231 * and put them on the dpm_locked list.
233 static void dpm_resume(void)
235 mutex_lock(&dpm_list_mtx);
236 all_sleeping = false;
237 while(!list_empty(&dpm_off)) {
238 struct list_head *entry = dpm_off.next;
239 struct device *dev = to_device(entry);
241 list_move_tail(entry, &dpm_active);
242 dev->power.sleeping = false;
243 mutex_unlock(&dpm_list_mtx);
245 mutex_lock(&dpm_list_mtx);
247 mutex_unlock(&dpm_list_mtx);
251 * unregister_dropped_devices - Unregister devices scheduled for removal
253 * Unregister all devices on the dpm_destroy list.
255 static void unregister_dropped_devices(void)
257 mutex_lock(&dpm_list_mtx);
258 while (!list_empty(&dpm_destroy)) {
259 struct list_head *entry = dpm_destroy.next;
260 struct device *dev = to_device(entry);
262 mutex_unlock(&dpm_list_mtx);
263 /* This also removes the device from the list */
264 device_unregister(dev);
265 mutex_lock(&dpm_list_mtx);
267 mutex_unlock(&dpm_list_mtx);
271 * device_resume - Restore state of each device in system.
273 * Resume all the devices, unlock them all, and allow new
274 * devices to be registered once again.
276 void device_resume(void)
280 unregister_dropped_devices();
282 EXPORT_SYMBOL_GPL(device_resume);
285 /*------------------------- Suspend routines -------------------------*/
287 static inline char *suspend_verb(u32 event)
290 case PM_EVENT_SUSPEND: return "suspend";
291 case PM_EVENT_FREEZE: return "freeze";
292 case PM_EVENT_PRETHAW: return "prethaw";
293 default: return "(unknown suspend event)";
298 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
300 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
301 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
302 ", may wakeup" : "");
306 * suspend_device_late - Shut down one device (late suspend).
308 * @state: Power state device is entering.
310 * This is called with interrupts off and only a single CPU running.
312 static int suspend_device_late(struct device *dev, pm_message_t state)
316 if (dev->bus && dev->bus->suspend_late) {
317 suspend_device_dbg(dev, state, "LATE ");
318 error = dev->bus->suspend_late(dev, state);
319 suspend_report_result(dev->bus->suspend_late, error);
325 * device_power_down - Shut down special devices.
326 * @state: Power state to enter.
328 * Power down devices that require interrupts to be disabled
329 * and move them from the dpm_off list to the dpm_off_irq list.
330 * Then power down system devices.
332 * Must be called with interrupts disabled and only one CPU running.
334 int device_power_down(pm_message_t state)
338 while (!list_empty(&dpm_off)) {
339 struct list_head *entry = dpm_off.prev;
340 struct device *dev = to_device(entry);
342 error = suspend_device_late(dev, state);
344 printk(KERN_ERR "Could not power down device %s: "
346 kobject_name(&dev->kobj), error);
349 if (!list_empty(&dev->power.entry))
350 list_move(&dev->power.entry, &dpm_off_irq);
354 error = sysdev_suspend(state);
359 EXPORT_SYMBOL_GPL(device_power_down);
362 * suspend_device - Save state of one device.
364 * @state: Power state device is entering.
366 static int suspend_device(struct device *dev, pm_message_t state)
372 if (dev->class && dev->class->suspend) {
373 suspend_device_dbg(dev, state, "class ");
374 error = dev->class->suspend(dev, state);
375 suspend_report_result(dev->class->suspend, error);
378 if (!error && dev->type && dev->type->suspend) {
379 suspend_device_dbg(dev, state, "type ");
380 error = dev->type->suspend(dev, state);
381 suspend_report_result(dev->type->suspend, error);
384 if (!error && dev->bus && dev->bus->suspend) {
385 suspend_device_dbg(dev, state, "");
386 error = dev->bus->suspend(dev, state);
387 suspend_report_result(dev->bus->suspend, error);
396 * dpm_suspend - Suspend every device.
397 * @state: Power state to put each device in.
399 * Walk the dpm_locked list. Suspend each device and move it
400 * to the dpm_off list.
402 * (For historical reasons, if it returns -EAGAIN, that used to mean
403 * that the device would be called again with interrupts disabled.
404 * These days, we use the "suspend_late()" callback for that, so we
405 * print a warning and consider it an error).
407 static int dpm_suspend(pm_message_t state)
411 mutex_lock(&dpm_list_mtx);
412 while (!list_empty(&dpm_active)) {
413 struct list_head *entry = dpm_active.prev;
414 struct device *dev = to_device(entry);
416 WARN_ON(dev->parent && dev->parent->power.sleeping);
418 dev->power.sleeping = true;
419 mutex_unlock(&dpm_list_mtx);
420 error = suspend_device(dev, state);
421 mutex_lock(&dpm_list_mtx);
423 printk(KERN_ERR "Could not suspend device %s: "
425 kobject_name(&dev->kobj),
428 " (please convert to suspend_late)" :
430 dev->power.sleeping = false;
433 if (!list_empty(&dev->power.entry))
434 list_move(&dev->power.entry, &dpm_off);
438 mutex_unlock(&dpm_list_mtx);
444 * device_suspend - Save state and stop all devices in system.
445 * @state: new power management state
447 * Prevent new devices from being registered, then lock all devices
450 int device_suspend(pm_message_t state)
455 error = dpm_suspend(state);
460 EXPORT_SYMBOL_GPL(device_suspend);
462 void __suspend_report_result(const char *function, void *fn, int ret)
465 printk(KERN_ERR "%s(): ", function);
466 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
470 EXPORT_SYMBOL_GPL(__suspend_report_result);