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
);
54 static DEFINE_MUTEX(dpm_list_mtx
);
56 /* 'true' if all devices have been suspended, protected by dpm_list_mtx */
57 static bool all_sleeping
;
60 * device_pm_add - add a device to the list of active devices
61 * @dev: Device to be added to the list
63 int device_pm_add(struct device
*dev
)
67 pr_debug("PM: Adding info for %s:%s\n",
68 dev
->bus
? dev
->bus
->name
: "No Bus",
69 kobject_name(&dev
->kobj
));
70 mutex_lock(&dpm_list_mtx
);
71 if ((dev
->parent
&& dev
->parent
->power
.sleeping
) || all_sleeping
) {
72 if (dev
->parent
->power
.sleeping
)
73 dev_warn(dev
, "parent %s is sleeping\n",
76 dev_warn(dev
, "all devices are sleeping\n");
79 error
= dpm_sysfs_add(dev
);
81 list_add_tail(&dev
->power
.entry
, &dpm_active
);
82 mutex_unlock(&dpm_list_mtx
);
87 * device_pm_remove - remove a device from the list of active devices
88 * @dev: Device to be removed from the list
90 * This function also removes the device's PM-related sysfs attributes.
92 void device_pm_remove(struct device
*dev
)
94 pr_debug("PM: Removing info for %s:%s\n",
95 dev
->bus
? dev
->bus
->name
: "No Bus",
96 kobject_name(&dev
->kobj
));
97 mutex_lock(&dpm_list_mtx
);
98 dpm_sysfs_remove(dev
);
99 list_del_init(&dev
->power
.entry
);
100 mutex_unlock(&dpm_list_mtx
);
103 /*------------------------- Resume routines -------------------------*/
106 * resume_device_early - Power on one device (early resume).
109 * Must be called with interrupts disabled.
111 static int resume_device_early(struct device
*dev
)
118 if (dev
->bus
&& dev
->bus
->resume_early
) {
119 dev_dbg(dev
, "EARLY resume\n");
120 error
= dev
->bus
->resume_early(dev
);
128 * dpm_power_up - Power on all regular (non-sysdev) devices.
130 * Walk the dpm_off_irq list and power each device up. This
131 * is used for devices that required they be powered down with
132 * interrupts disabled. As devices are powered on, they are moved
133 * to the dpm_off list.
135 * Must be called with interrupts disabled and only one CPU running.
137 static void dpm_power_up(void)
140 while (!list_empty(&dpm_off_irq
)) {
141 struct list_head
*entry
= dpm_off_irq
.next
;
142 struct device
*dev
= to_device(entry
);
144 list_move_tail(entry
, &dpm_off
);
145 resume_device_early(dev
);
150 * device_power_up - Turn on all devices that need special attention.
152 * Power on system devices, then devices that required we shut them down
153 * with interrupts disabled.
155 * Must be called with interrupts disabled.
157 void device_power_up(void)
162 EXPORT_SYMBOL_GPL(device_power_up
);
165 * resume_device - Restore state for one device.
169 static int resume_device(struct device
*dev
)
178 if (dev
->bus
&& dev
->bus
->resume
) {
179 dev_dbg(dev
,"resuming\n");
180 error
= dev
->bus
->resume(dev
);
183 if (!error
&& dev
->type
&& dev
->type
->resume
) {
184 dev_dbg(dev
,"resuming\n");
185 error
= dev
->type
->resume(dev
);
188 if (!error
&& dev
->class && dev
->class->resume
) {
189 dev_dbg(dev
,"class resume\n");
190 error
= dev
->class->resume(dev
);
200 * dpm_resume - Resume every device.
202 * Resume the devices that have either not gone through
203 * the late suspend, or that did go through it but also
204 * went through the early resume.
206 * Take devices from the dpm_off_list, resume them,
207 * and put them on the dpm_locked list.
209 static void dpm_resume(void)
211 mutex_lock(&dpm_list_mtx
);
212 all_sleeping
= false;
213 while(!list_empty(&dpm_off
)) {
214 struct list_head
*entry
= dpm_off
.next
;
215 struct device
*dev
= to_device(entry
);
217 list_move_tail(entry
, &dpm_active
);
218 dev
->power
.sleeping
= false;
219 mutex_unlock(&dpm_list_mtx
);
221 mutex_lock(&dpm_list_mtx
);
223 mutex_unlock(&dpm_list_mtx
);
227 * device_resume - Restore state of each device in system.
229 * Resume all the devices, unlock them all, and allow new
230 * devices to be registered once again.
232 void device_resume(void)
237 EXPORT_SYMBOL_GPL(device_resume
);
240 /*------------------------- Suspend routines -------------------------*/
242 static inline char *suspend_verb(u32 event
)
245 case PM_EVENT_SUSPEND
: return "suspend";
246 case PM_EVENT_FREEZE
: return "freeze";
247 case PM_EVENT_PRETHAW
: return "prethaw";
248 default: return "(unknown suspend event)";
253 suspend_device_dbg(struct device
*dev
, pm_message_t state
, char *info
)
255 dev_dbg(dev
, "%s%s%s\n", info
, suspend_verb(state
.event
),
256 ((state
.event
== PM_EVENT_SUSPEND
) && device_may_wakeup(dev
)) ?
257 ", may wakeup" : "");
261 * suspend_device_late - Shut down one device (late suspend).
263 * @state: Power state device is entering.
265 * This is called with interrupts off and only a single CPU running.
267 static int suspend_device_late(struct device
*dev
, pm_message_t state
)
271 if (dev
->bus
&& dev
->bus
->suspend_late
) {
272 suspend_device_dbg(dev
, state
, "LATE ");
273 error
= dev
->bus
->suspend_late(dev
, state
);
274 suspend_report_result(dev
->bus
->suspend_late
, error
);
280 * device_power_down - Shut down special devices.
281 * @state: Power state to enter.
283 * Power down devices that require interrupts to be disabled
284 * and move them from the dpm_off list to the dpm_off_irq list.
285 * Then power down system devices.
287 * Must be called with interrupts disabled and only one CPU running.
289 int device_power_down(pm_message_t state
)
293 while (!list_empty(&dpm_off
)) {
294 struct list_head
*entry
= dpm_off
.prev
;
295 struct device
*dev
= to_device(entry
);
297 error
= suspend_device_late(dev
, state
);
299 printk(KERN_ERR
"Could not power down device %s: "
301 kobject_name(&dev
->kobj
), error
);
304 if (!list_empty(&dev
->power
.entry
))
305 list_move(&dev
->power
.entry
, &dpm_off_irq
);
309 error
= sysdev_suspend(state
);
314 EXPORT_SYMBOL_GPL(device_power_down
);
317 * suspend_device - Save state of one device.
319 * @state: Power state device is entering.
321 static int suspend_device(struct device
*dev
, pm_message_t state
)
327 if (dev
->class && dev
->class->suspend
) {
328 suspend_device_dbg(dev
, state
, "class ");
329 error
= dev
->class->suspend(dev
, state
);
330 suspend_report_result(dev
->class->suspend
, error
);
333 if (!error
&& dev
->type
&& dev
->type
->suspend
) {
334 suspend_device_dbg(dev
, state
, "type ");
335 error
= dev
->type
->suspend(dev
, state
);
336 suspend_report_result(dev
->type
->suspend
, error
);
339 if (!error
&& dev
->bus
&& dev
->bus
->suspend
) {
340 suspend_device_dbg(dev
, state
, "");
341 error
= dev
->bus
->suspend(dev
, state
);
342 suspend_report_result(dev
->bus
->suspend
, error
);
351 * dpm_suspend - Suspend every device.
352 * @state: Power state to put each device in.
354 * Walk the dpm_locked list. Suspend each device and move it
355 * to the dpm_off list.
357 * (For historical reasons, if it returns -EAGAIN, that used to mean
358 * that the device would be called again with interrupts disabled.
359 * These days, we use the "suspend_late()" callback for that, so we
360 * print a warning and consider it an error).
362 static int dpm_suspend(pm_message_t state
)
366 mutex_lock(&dpm_list_mtx
);
367 while (!list_empty(&dpm_active
)) {
368 struct list_head
*entry
= dpm_active
.prev
;
369 struct device
*dev
= to_device(entry
);
371 WARN_ON(dev
->parent
&& dev
->parent
->power
.sleeping
);
373 dev
->power
.sleeping
= true;
374 mutex_unlock(&dpm_list_mtx
);
375 error
= suspend_device(dev
, state
);
376 mutex_lock(&dpm_list_mtx
);
378 printk(KERN_ERR
"Could not suspend device %s: "
380 kobject_name(&dev
->kobj
),
383 " (please convert to suspend_late)" :
385 dev
->power
.sleeping
= false;
388 if (!list_empty(&dev
->power
.entry
))
389 list_move(&dev
->power
.entry
, &dpm_off
);
393 mutex_unlock(&dpm_list_mtx
);
399 * device_suspend - Save state and stop all devices in system.
400 * @state: new power management state
402 * Prevent new devices from being registered, then lock all devices
405 int device_suspend(pm_message_t state
)
410 error
= dpm_suspend(state
);
415 EXPORT_SYMBOL_GPL(device_suspend
);
417 void __suspend_report_result(const char *function
, void *fn
, int ret
)
420 printk(KERN_ERR
"%s(): ", function
);
421 print_fn_descriptor_symbol("%s returns ", fn
);
425 EXPORT_SYMBOL_GPL(__suspend_report_result
);