blob: c4568b82875b9fb6379f3c650175143c2ebc6e81 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/power/main.c - Where the driver meets power management.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
9 *
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.
14 *
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.
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040023#include <linux/kallsyms.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070024#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040025#include <linux/pm.h>
26#include <linux/resume-trace.h>
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010027#include <linux/rwsem.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070028
Alan Sterncd59abf2007-09-21 15:36:56 -040029#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "power.h"
31
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010032/*
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.
36 *
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
43 * child.
44 *
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
47 * dpm_list_mutex.
48 */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050LIST_HEAD(dpm_active);
Alan Sterncd59abf2007-09-21 15:36:56 -040051static LIST_HEAD(dpm_off);
52static LIST_HEAD(dpm_off_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Alan Sterncd59abf2007-09-21 15:36:56 -040054static DEFINE_MUTEX(dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010056/* 'true' if all devices have been suspended, protected by dpm_list_mtx */
57static bool all_sleeping;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010058
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010059/**
60 * device_pm_add - add a device to the list of active devices
61 * @dev: Device to be added to the list
62 */
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010063int device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010065 int error = 0;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040068 dev->bus ? dev->bus->name : "No Bus",
69 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070070 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010071 if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
72 if (dev->parent->power.sleeping)
73 dev_warn(dev,
74 "parent %s is sleeping, will not add\n",
75 dev->parent->bus_id);
76 else
77 dev_warn(dev, "devices are sleeping, will not add\n");
78 WARN_ON(true);
79 error = -EBUSY;
80 } else {
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +010081 error = dpm_sysfs_add(dev);
82 if (!error)
83 list_add_tail(&dev->power.entry, &dpm_active);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010084 }
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070085 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010089/**
90 * device_pm_remove - remove a device from the list of active devices
91 * @dev: Device to be removed from the list
92 *
93 * This function also removes the device's PM-related sysfs attributes.
94 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +020095void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040098 dev->bus ? dev->bus->name : "No Bus",
99 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700100 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700103 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100104}
105
Alan Sterncd59abf2007-09-21 15:36:56 -0400106/*------------------------- Resume routines -------------------------*/
107
108/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100109 * resume_device_early - Power on one device (early resume).
Alan Sterncd59abf2007-09-21 15:36:56 -0400110 * @dev: Device.
111 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100112 * Must be called with interrupts disabled.
Alan Sterncd59abf2007-09-21 15:36:56 -0400113 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100114static int resume_device_early(struct device *dev)
Alan Sterncd59abf2007-09-21 15:36:56 -0400115{
116 int error = 0;
117
118 TRACE_DEVICE(dev);
119 TRACE_RESUME(0);
120
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100121 if (dev->bus && dev->bus->resume_early) {
122 dev_dbg(dev, "EARLY resume\n");
123 error = dev->bus->resume_early(dev);
124 }
125
126 TRACE_RESUME(error);
127 return error;
128}
129
130/**
131 * dpm_power_up - Power on all regular (non-sysdev) devices.
132 *
133 * Walk the dpm_off_irq list and power each device up. This
134 * is used for devices that required they be powered down with
135 * interrupts disabled. As devices are powered on, they are moved
136 * to the dpm_off list.
137 *
138 * Must be called with interrupts disabled and only one CPU running.
139 */
140static void dpm_power_up(void)
141{
142
143 while (!list_empty(&dpm_off_irq)) {
144 struct list_head *entry = dpm_off_irq.next;
145 struct device *dev = to_device(entry);
146
147 list_move_tail(entry, &dpm_off);
148 resume_device_early(dev);
149 }
150}
151
152/**
153 * device_power_up - Turn on all devices that need special attention.
154 *
155 * Power on system devices, then devices that required we shut them down
156 * with interrupts disabled.
157 *
158 * Must be called with interrupts disabled.
159 */
160void device_power_up(void)
161{
162 sysdev_resume();
163 dpm_power_up();
164}
165EXPORT_SYMBOL_GPL(device_power_up);
166
167/**
168 * resume_device - Restore state for one device.
169 * @dev: Device.
170 *
171 */
172static int resume_device(struct device *dev)
173{
174 int error = 0;
175
176 TRACE_DEVICE(dev);
177 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400178
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100179 down(&dev->sem);
180
Alan Sterncd59abf2007-09-21 15:36:56 -0400181 if (dev->bus && dev->bus->resume) {
182 dev_dbg(dev,"resuming\n");
183 error = dev->bus->resume(dev);
184 }
185
186 if (!error && dev->type && dev->type->resume) {
187 dev_dbg(dev,"resuming\n");
188 error = dev->type->resume(dev);
189 }
190
191 if (!error && dev->class && dev->class->resume) {
192 dev_dbg(dev,"class resume\n");
193 error = dev->class->resume(dev);
194 }
195
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100196 up(&dev->sem);
197
Alan Sterncd59abf2007-09-21 15:36:56 -0400198 TRACE_RESUME(error);
199 return error;
200}
201
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100202/**
203 * dpm_resume - Resume every device.
204 *
205 * Resume the devices that have either not gone through
206 * the late suspend, or that did go through it but also
207 * went through the early resume.
208 *
209 * Take devices from the dpm_off_list, resume them,
210 * and put them on the dpm_locked list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400211 */
212static void dpm_resume(void)
213{
214 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100215 all_sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400216 while(!list_empty(&dpm_off)) {
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100217 struct list_head *entry = dpm_off.next;
218 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400219
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100220 list_move_tail(entry, &dpm_active);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100221 dev->power.sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400222 mutex_unlock(&dpm_list_mtx);
223 resume_device(dev);
224 mutex_lock(&dpm_list_mtx);
Alan Sterncd59abf2007-09-21 15:36:56 -0400225 }
226 mutex_unlock(&dpm_list_mtx);
227}
228
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100229/**
Alan Sterncd59abf2007-09-21 15:36:56 -0400230 * device_resume - Restore state of each device in system.
231 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100232 * Resume all the devices, unlock them all, and allow new
233 * devices to be registered once again.
Alan Sterncd59abf2007-09-21 15:36:56 -0400234 */
Alan Sterncd59abf2007-09-21 15:36:56 -0400235void device_resume(void)
236{
237 might_sleep();
Alan Sterncd59abf2007-09-21 15:36:56 -0400238 dpm_resume();
Alan Sterncd59abf2007-09-21 15:36:56 -0400239}
Alan Sterncd59abf2007-09-21 15:36:56 -0400240EXPORT_SYMBOL_GPL(device_resume);
241
242
Alan Sterncd59abf2007-09-21 15:36:56 -0400243/*------------------------- Suspend routines -------------------------*/
244
Alan Sterncd59abf2007-09-21 15:36:56 -0400245static inline char *suspend_verb(u32 event)
246{
247 switch (event) {
248 case PM_EVENT_SUSPEND: return "suspend";
249 case PM_EVENT_FREEZE: return "freeze";
250 case PM_EVENT_PRETHAW: return "prethaw";
251 default: return "(unknown suspend event)";
252 }
253}
254
Alan Sterncd59abf2007-09-21 15:36:56 -0400255static void
256suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
257{
258 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
259 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
260 ", may wakeup" : "");
261}
262
263/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100264 * suspend_device_late - Shut down one device (late suspend).
265 * @dev: Device.
266 * @state: Power state device is entering.
267 *
268 * This is called with interrupts off and only a single CPU running.
269 */
270static int suspend_device_late(struct device *dev, pm_message_t state)
271{
272 int error = 0;
273
274 if (dev->bus && dev->bus->suspend_late) {
275 suspend_device_dbg(dev, state, "LATE ");
276 error = dev->bus->suspend_late(dev, state);
277 suspend_report_result(dev->bus->suspend_late, error);
278 }
279 return error;
280}
281
282/**
283 * device_power_down - Shut down special devices.
284 * @state: Power state to enter.
285 *
286 * Power down devices that require interrupts to be disabled
287 * and move them from the dpm_off list to the dpm_off_irq list.
288 * Then power down system devices.
289 *
290 * Must be called with interrupts disabled and only one CPU running.
291 */
292int device_power_down(pm_message_t state)
293{
294 int error = 0;
295
296 while (!list_empty(&dpm_off)) {
297 struct list_head *entry = dpm_off.prev;
298 struct device *dev = to_device(entry);
299
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100300 error = suspend_device_late(dev, state);
301 if (error) {
302 printk(KERN_ERR "Could not power down device %s: "
303 "error %d\n",
304 kobject_name(&dev->kobj), error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100305 break;
306 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100307 if (!list_empty(&dev->power.entry))
308 list_move(&dev->power.entry, &dpm_off_irq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100309 }
310
311 if (!error)
312 error = sysdev_suspend(state);
313 if (error)
314 dpm_power_up();
315 return error;
316}
317EXPORT_SYMBOL_GPL(device_power_down);
318
319/**
Alan Sterncd59abf2007-09-21 15:36:56 -0400320 * suspend_device - Save state of one device.
321 * @dev: Device.
322 * @state: Power state device is entering.
323 */
Adrian Bunk19e20c92008-02-03 22:55:18 +0100324static int suspend_device(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400325{
326 int error = 0;
327
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100328 down(&dev->sem);
329
Alan Sterncd59abf2007-09-21 15:36:56 -0400330 if (dev->class && dev->class->suspend) {
331 suspend_device_dbg(dev, state, "class ");
332 error = dev->class->suspend(dev, state);
333 suspend_report_result(dev->class->suspend, error);
334 }
335
336 if (!error && dev->type && dev->type->suspend) {
337 suspend_device_dbg(dev, state, "type ");
338 error = dev->type->suspend(dev, state);
339 suspend_report_result(dev->type->suspend, error);
340 }
341
342 if (!error && dev->bus && dev->bus->suspend) {
343 suspend_device_dbg(dev, state, "");
344 error = dev->bus->suspend(dev, state);
345 suspend_report_result(dev->bus->suspend, error);
346 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100347
348 up(&dev->sem);
349
Alan Sterncd59abf2007-09-21 15:36:56 -0400350 return error;
351}
352
353/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100354 * dpm_suspend - Suspend every device.
355 * @state: Power state to put each device in.
Alan Sterncd59abf2007-09-21 15:36:56 -0400356 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100357 * Walk the dpm_locked list. Suspend each device and move it
358 * to the dpm_off list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400359 *
360 * (For historical reasons, if it returns -EAGAIN, that used to mean
361 * that the device would be called again with interrupts disabled.
362 * These days, we use the "suspend_late()" callback for that, so we
363 * print a warning and consider it an error).
Alan Sterncd59abf2007-09-21 15:36:56 -0400364 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100365static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400366{
367 int error = 0;
368
Alan Sterncd59abf2007-09-21 15:36:56 -0400369 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100370 while (!list_empty(&dpm_active)) {
371 struct list_head *entry = dpm_active.prev;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100372 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400373
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100374 WARN_ON(dev->parent && dev->parent->power.sleeping);
375
376 dev->power.sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100377 mutex_unlock(&dpm_list_mtx);
378 error = suspend_device(dev, state);
Alan Stern1b3cbec2008-02-29 11:50:22 -0500379 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100380 if (error) {
381 printk(KERN_ERR "Could not suspend device %s: "
382 "error %d%s\n",
383 kobject_name(&dev->kobj),
384 error,
385 (error == -EAGAIN ?
386 " (please convert to suspend_late)" :
387 ""));
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100388 dev->power.sleeping = false;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100389 break;
390 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100391 if (!list_empty(&dev->power.entry))
392 list_move(&dev->power.entry, &dpm_off);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100393 }
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100394 if (!error)
395 all_sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100396 mutex_unlock(&dpm_list_mtx);
397
398 return error;
399}
400
401/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100402 * device_suspend - Save state and stop all devices in system.
Randy Dunlap71996772008-02-18 13:09:03 -0800403 * @state: new power management state
Alan Sterncd59abf2007-09-21 15:36:56 -0400404 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100405 * Prevent new devices from being registered, then lock all devices
406 * and suspend them.
Alan Sterncd59abf2007-09-21 15:36:56 -0400407 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100408int device_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400409{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100410 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400411
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100412 might_sleep();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100413 error = dpm_suspend(state);
414 if (error)
415 device_resume();
Alan Sterncd59abf2007-09-21 15:36:56 -0400416 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400417}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100418EXPORT_SYMBOL_GPL(device_suspend);
Alan Sterncd59abf2007-09-21 15:36:56 -0400419
420void __suspend_report_result(const char *function, void *fn, int ret)
421{
422 if (ret) {
423 printk(KERN_ERR "%s(): ", function);
424 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
425 printk("%d\n", ret);
426 }
427}
428EXPORT_SYMBOL_GPL(__suspend_report_result);