blob: b0c16f6fc1864654a53e6da1be60b19a7ea6a15d [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);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010053static LIST_HEAD(dpm_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Alan Sterncd59abf2007-09-21 15:36:56 -040055static DEFINE_MUTEX(dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010057static DECLARE_RWSEM(pm_sleep_rwsem);
58
David Brownell075c1772007-04-26 00:12:06 -070059int (*platform_enable_wakeup)(struct device *dev, int is_on);
60
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010061/**
62 * device_pm_add - add a device to the list of active devices
63 * @dev: Device to be added to the list
64 */
Daniel Drakedec13c12007-11-21 14:55:18 -080065void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 list_add_tail(&dev->power.entry, &dpm_active);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070072 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010075/**
76 * device_pm_remove - remove a device from the list of active devices
77 * @dev: Device to be removed from the list
78 *
79 * This function also removes the device's PM-related sysfs attributes.
80 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +020081void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040084 dev->bus ? dev->bus->name : "No Bus",
85 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070086 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070089 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010090}
91
92/**
93 * device_pm_schedule_removal - schedule the removal of a suspended device
94 * @dev: Device to destroy
95 *
96 * Moves the device to the dpm_destroy list for further processing by
97 * unregister_dropped_devices().
98 */
99void device_pm_schedule_removal(struct device *dev)
100{
101 pr_debug("PM: Preparing for removal: %s:%s\n",
102 dev->bus ? dev->bus->name : "No Bus",
103 kobject_name(&dev->kobj));
104 mutex_lock(&dpm_list_mtx);
105 list_move_tail(&dev->power.entry, &dpm_destroy);
106 mutex_unlock(&dpm_list_mtx);
107}
Rafael J. Wysocki9617c3e2008-01-25 01:30:25 +0100108EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100109
110/**
111 * pm_sleep_lock - mutual exclusion for registration and suspend
112 *
113 * Returns 0 if no suspend is underway and device registration
114 * may proceed, otherwise -EBUSY.
115 */
116int pm_sleep_lock(void)
117{
118 if (down_read_trylock(&pm_sleep_rwsem))
119 return 0;
120
121 return -EBUSY;
122}
123
124/**
125 * pm_sleep_unlock - mutual exclusion for registration and suspend
126 *
127 * This routine undoes the effect of device_pm_add_lock
128 * when a device's registration is complete.
129 */
130void pm_sleep_unlock(void)
131{
132 up_read(&pm_sleep_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135
Alan Sterncd59abf2007-09-21 15:36:56 -0400136/*------------------------- Resume routines -------------------------*/
137
138/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100139 * resume_device_early - Power on one device (early resume).
Alan Sterncd59abf2007-09-21 15:36:56 -0400140 * @dev: Device.
141 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100142 * Must be called with interrupts disabled.
Alan Sterncd59abf2007-09-21 15:36:56 -0400143 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100144static int resume_device_early(struct device *dev)
Alan Sterncd59abf2007-09-21 15:36:56 -0400145{
146 int error = 0;
147
148 TRACE_DEVICE(dev);
149 TRACE_RESUME(0);
150
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100151 if (dev->bus && dev->bus->resume_early) {
152 dev_dbg(dev, "EARLY resume\n");
153 error = dev->bus->resume_early(dev);
154 }
155
156 TRACE_RESUME(error);
157 return error;
158}
159
160/**
161 * dpm_power_up - Power on all regular (non-sysdev) devices.
162 *
163 * Walk the dpm_off_irq list and power each device up. This
164 * is used for devices that required they be powered down with
165 * interrupts disabled. As devices are powered on, they are moved
166 * to the dpm_off list.
167 *
168 * Must be called with interrupts disabled and only one CPU running.
169 */
170static void dpm_power_up(void)
171{
172
173 while (!list_empty(&dpm_off_irq)) {
174 struct list_head *entry = dpm_off_irq.next;
175 struct device *dev = to_device(entry);
176
177 list_move_tail(entry, &dpm_off);
178 resume_device_early(dev);
179 }
180}
181
182/**
183 * device_power_up - Turn on all devices that need special attention.
184 *
185 * Power on system devices, then devices that required we shut them down
186 * with interrupts disabled.
187 *
188 * Must be called with interrupts disabled.
189 */
190void device_power_up(void)
191{
192 sysdev_resume();
193 dpm_power_up();
194}
195EXPORT_SYMBOL_GPL(device_power_up);
196
197/**
198 * resume_device - Restore state for one device.
199 * @dev: Device.
200 *
201 */
202static int resume_device(struct device *dev)
203{
204 int error = 0;
205
206 TRACE_DEVICE(dev);
207 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400208
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100209 down(&dev->sem);
210
Alan Sterncd59abf2007-09-21 15:36:56 -0400211 if (dev->bus && dev->bus->resume) {
212 dev_dbg(dev,"resuming\n");
213 error = dev->bus->resume(dev);
214 }
215
216 if (!error && dev->type && dev->type->resume) {
217 dev_dbg(dev,"resuming\n");
218 error = dev->type->resume(dev);
219 }
220
221 if (!error && dev->class && dev->class->resume) {
222 dev_dbg(dev,"class resume\n");
223 error = dev->class->resume(dev);
224 }
225
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100226 up(&dev->sem);
227
Alan Sterncd59abf2007-09-21 15:36:56 -0400228 TRACE_RESUME(error);
229 return error;
230}
231
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100232/**
233 * dpm_resume - Resume every device.
234 *
235 * Resume the devices that have either not gone through
236 * the late suspend, or that did go through it but also
237 * went through the early resume.
238 *
239 * Take devices from the dpm_off_list, resume them,
240 * and put them on the dpm_locked list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400241 */
242static void dpm_resume(void)
243{
244 mutex_lock(&dpm_list_mtx);
245 while(!list_empty(&dpm_off)) {
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100246 struct list_head *entry = dpm_off.next;
247 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400248
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100249 list_move_tail(entry, &dpm_active);
Alan Sterncd59abf2007-09-21 15:36:56 -0400250 mutex_unlock(&dpm_list_mtx);
251 resume_device(dev);
252 mutex_lock(&dpm_list_mtx);
Alan Sterncd59abf2007-09-21 15:36:56 -0400253 }
254 mutex_unlock(&dpm_list_mtx);
255}
256
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100257/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100258 * unregister_dropped_devices - Unregister devices scheduled for removal
259 *
260 * Unregister all devices on the dpm_destroy list.
261 */
262static void unregister_dropped_devices(void)
263{
264 mutex_lock(&dpm_list_mtx);
265 while (!list_empty(&dpm_destroy)) {
266 struct list_head *entry = dpm_destroy.next;
267 struct device *dev = to_device(entry);
268
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100269 mutex_unlock(&dpm_list_mtx);
270 /* This also removes the device from the list */
271 device_unregister(dev);
272 mutex_lock(&dpm_list_mtx);
273 }
274 mutex_unlock(&dpm_list_mtx);
275}
Alan Sterncd59abf2007-09-21 15:36:56 -0400276
277/**
278 * device_resume - Restore state of each device in system.
279 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100280 * Resume all the devices, unlock them all, and allow new
281 * devices to be registered once again.
Alan Sterncd59abf2007-09-21 15:36:56 -0400282 */
Alan Sterncd59abf2007-09-21 15:36:56 -0400283void device_resume(void)
284{
285 might_sleep();
Alan Sterncd59abf2007-09-21 15:36:56 -0400286 dpm_resume();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100287 unregister_dropped_devices();
288 up_write(&pm_sleep_rwsem);
Alan Sterncd59abf2007-09-21 15:36:56 -0400289}
Alan Sterncd59abf2007-09-21 15:36:56 -0400290EXPORT_SYMBOL_GPL(device_resume);
291
292
Alan Sterncd59abf2007-09-21 15:36:56 -0400293/*------------------------- Suspend routines -------------------------*/
294
Alan Sterncd59abf2007-09-21 15:36:56 -0400295static inline char *suspend_verb(u32 event)
296{
297 switch (event) {
298 case PM_EVENT_SUSPEND: return "suspend";
299 case PM_EVENT_FREEZE: return "freeze";
300 case PM_EVENT_PRETHAW: return "prethaw";
301 default: return "(unknown suspend event)";
302 }
303}
304
Alan Sterncd59abf2007-09-21 15:36:56 -0400305static void
306suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
307{
308 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
309 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
310 ", may wakeup" : "");
311}
312
313/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100314 * suspend_device_late - Shut down one device (late suspend).
315 * @dev: Device.
316 * @state: Power state device is entering.
317 *
318 * This is called with interrupts off and only a single CPU running.
319 */
320static int suspend_device_late(struct device *dev, pm_message_t state)
321{
322 int error = 0;
323
324 if (dev->bus && dev->bus->suspend_late) {
325 suspend_device_dbg(dev, state, "LATE ");
326 error = dev->bus->suspend_late(dev, state);
327 suspend_report_result(dev->bus->suspend_late, error);
328 }
329 return error;
330}
331
332/**
333 * device_power_down - Shut down special devices.
334 * @state: Power state to enter.
335 *
336 * Power down devices that require interrupts to be disabled
337 * and move them from the dpm_off list to the dpm_off_irq list.
338 * Then power down system devices.
339 *
340 * Must be called with interrupts disabled and only one CPU running.
341 */
342int device_power_down(pm_message_t state)
343{
344 int error = 0;
345
346 while (!list_empty(&dpm_off)) {
347 struct list_head *entry = dpm_off.prev;
348 struct device *dev = to_device(entry);
349
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100350 error = suspend_device_late(dev, state);
351 if (error) {
352 printk(KERN_ERR "Could not power down device %s: "
353 "error %d\n",
354 kobject_name(&dev->kobj), error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100355 break;
356 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100357 if (!list_empty(&dev->power.entry))
358 list_move(&dev->power.entry, &dpm_off_irq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100359 }
360
361 if (!error)
362 error = sysdev_suspend(state);
363 if (error)
364 dpm_power_up();
365 return error;
366}
367EXPORT_SYMBOL_GPL(device_power_down);
368
369/**
Alan Sterncd59abf2007-09-21 15:36:56 -0400370 * suspend_device - Save state of one device.
371 * @dev: Device.
372 * @state: Power state device is entering.
373 */
Adrian Bunk19e20c92008-02-03 22:55:18 +0100374static int suspend_device(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400375{
376 int error = 0;
377
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100378 down(&dev->sem);
379
Alan Sterncd59abf2007-09-21 15:36:56 -0400380 if (dev->power.power_state.event) {
381 dev_dbg(dev, "PM: suspend %d-->%d\n",
382 dev->power.power_state.event, state.event);
383 }
384
385 if (dev->class && dev->class->suspend) {
386 suspend_device_dbg(dev, state, "class ");
387 error = dev->class->suspend(dev, state);
388 suspend_report_result(dev->class->suspend, error);
389 }
390
391 if (!error && dev->type && dev->type->suspend) {
392 suspend_device_dbg(dev, state, "type ");
393 error = dev->type->suspend(dev, state);
394 suspend_report_result(dev->type->suspend, error);
395 }
396
397 if (!error && dev->bus && dev->bus->suspend) {
398 suspend_device_dbg(dev, state, "");
399 error = dev->bus->suspend(dev, state);
400 suspend_report_result(dev->bus->suspend, error);
401 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100402
403 up(&dev->sem);
404
Alan Sterncd59abf2007-09-21 15:36:56 -0400405 return error;
406}
407
408/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100409 * dpm_suspend - Suspend every device.
410 * @state: Power state to put each device in.
Alan Sterncd59abf2007-09-21 15:36:56 -0400411 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100412 * Walk the dpm_locked list. Suspend each device and move it
413 * to the dpm_off list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400414 *
415 * (For historical reasons, if it returns -EAGAIN, that used to mean
416 * that the device would be called again with interrupts disabled.
417 * These days, we use the "suspend_late()" callback for that, so we
418 * print a warning and consider it an error).
Alan Sterncd59abf2007-09-21 15:36:56 -0400419 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100420static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400421{
422 int error = 0;
423
Alan Sterncd59abf2007-09-21 15:36:56 -0400424 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100425 while (!list_empty(&dpm_active)) {
426 struct list_head *entry = dpm_active.prev;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100427 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400428
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100429 mutex_unlock(&dpm_list_mtx);
430 error = suspend_device(dev, state);
431 if (error) {
432 printk(KERN_ERR "Could not suspend device %s: "
433 "error %d%s\n",
434 kobject_name(&dev->kobj),
435 error,
436 (error == -EAGAIN ?
437 " (please convert to suspend_late)" :
438 ""));
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100439 break;
440 }
441 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100442 if (!list_empty(&dev->power.entry))
443 list_move(&dev->power.entry, &dpm_off);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444 }
445 mutex_unlock(&dpm_list_mtx);
446
447 return error;
448}
449
450/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100451 * device_suspend - Save state and stop all devices in system.
Randy Dunlap71996772008-02-18 13:09:03 -0800452 * @state: new power management state
Alan Sterncd59abf2007-09-21 15:36:56 -0400453 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454 * Prevent new devices from being registered, then lock all devices
455 * and suspend them.
Alan Sterncd59abf2007-09-21 15:36:56 -0400456 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100457int device_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400458{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100459 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400460
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100461 might_sleep();
462 down_write(&pm_sleep_rwsem);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100463 error = dpm_suspend(state);
464 if (error)
465 device_resume();
Alan Sterncd59abf2007-09-21 15:36:56 -0400466 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400467}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100468EXPORT_SYMBOL_GPL(device_suspend);
Alan Sterncd59abf2007-09-21 15:36:56 -0400469
470void __suspend_report_result(const char *function, void *fn, int ret)
471{
472 if (ret) {
473 printk(KERN_ERR "%s(): ", function);
474 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
475 printk("%d\n", ret);
476 }
477}
478EXPORT_SYMBOL_GPL(__suspend_report_result);