blob: 0e3991a437c60fe64bcffa61b6d090daecbe09ca [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. Wysocki58aca232008-03-12 00:57:22 +010057/* 'true' if all devices have been suspended, protected by dpm_list_mtx */
58static bool all_sleeping;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010059
David Brownell075c1772007-04-26 00:12:06 -070060int (*platform_enable_wakeup)(struct device *dev, int is_on);
61
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010062/**
63 * device_pm_add - add a device to the list of active devices
64 * @dev: Device to be added to the list
65 */
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010066int device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010068 int error = 0;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 pr_debug("PM: Adding info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040071 dev->bus ? dev->bus->name : "No Bus",
72 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070073 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010074 if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
75 if (dev->parent->power.sleeping)
76 dev_warn(dev,
77 "parent %s is sleeping, will not add\n",
78 dev->parent->bus_id);
79 else
80 dev_warn(dev, "devices are sleeping, will not add\n");
81 WARN_ON(true);
82 error = -EBUSY;
83 } else {
84 list_add_tail(&dev->power.entry, &dpm_active);
85 }
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070086 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010087 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010090/**
91 * device_pm_remove - remove a device from the list of active devices
92 * @dev: Device to be removed from the list
93 *
94 * This function also removes the device's PM-related sysfs attributes.
95 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +020096void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -040099 dev->bus ? dev->bus->name : "No Bus",
100 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700101 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700104 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100105}
106
107/**
108 * device_pm_schedule_removal - schedule the removal of a suspended device
109 * @dev: Device to destroy
110 *
111 * Moves the device to the dpm_destroy list for further processing by
112 * unregister_dropped_devices().
113 */
114void device_pm_schedule_removal(struct device *dev)
115{
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);
122}
Rafael J. Wysocki9617c3e2008-01-25 01:30:25 +0100123EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100124
Alan Sterncd59abf2007-09-21 15:36:56 -0400125/*------------------------- Resume routines -------------------------*/
126
127/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100128 * resume_device_early - Power on one device (early resume).
Alan Sterncd59abf2007-09-21 15:36:56 -0400129 * @dev: Device.
130 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100131 * Must be called with interrupts disabled.
Alan Sterncd59abf2007-09-21 15:36:56 -0400132 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100133static int resume_device_early(struct device *dev)
Alan Sterncd59abf2007-09-21 15:36:56 -0400134{
135 int error = 0;
136
137 TRACE_DEVICE(dev);
138 TRACE_RESUME(0);
139
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100140 if (dev->bus && dev->bus->resume_early) {
141 dev_dbg(dev, "EARLY resume\n");
142 error = dev->bus->resume_early(dev);
143 }
144
145 TRACE_RESUME(error);
146 return error;
147}
148
149/**
150 * dpm_power_up - Power on all regular (non-sysdev) devices.
151 *
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.
156 *
157 * Must be called with interrupts disabled and only one CPU running.
158 */
159static void dpm_power_up(void)
160{
161
162 while (!list_empty(&dpm_off_irq)) {
163 struct list_head *entry = dpm_off_irq.next;
164 struct device *dev = to_device(entry);
165
166 list_move_tail(entry, &dpm_off);
167 resume_device_early(dev);
168 }
169}
170
171/**
172 * device_power_up - Turn on all devices that need special attention.
173 *
174 * Power on system devices, then devices that required we shut them down
175 * with interrupts disabled.
176 *
177 * Must be called with interrupts disabled.
178 */
179void device_power_up(void)
180{
181 sysdev_resume();
182 dpm_power_up();
183}
184EXPORT_SYMBOL_GPL(device_power_up);
185
186/**
187 * resume_device - Restore state for one device.
188 * @dev: Device.
189 *
190 */
191static int resume_device(struct device *dev)
192{
193 int error = 0;
194
195 TRACE_DEVICE(dev);
196 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400197
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100198 down(&dev->sem);
199
Alan Sterncd59abf2007-09-21 15:36:56 -0400200 if (dev->bus && dev->bus->resume) {
201 dev_dbg(dev,"resuming\n");
202 error = dev->bus->resume(dev);
203 }
204
205 if (!error && dev->type && dev->type->resume) {
206 dev_dbg(dev,"resuming\n");
207 error = dev->type->resume(dev);
208 }
209
210 if (!error && dev->class && dev->class->resume) {
211 dev_dbg(dev,"class resume\n");
212 error = dev->class->resume(dev);
213 }
214
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100215 up(&dev->sem);
216
Alan Sterncd59abf2007-09-21 15:36:56 -0400217 TRACE_RESUME(error);
218 return error;
219}
220
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100221/**
222 * dpm_resume - Resume every device.
223 *
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.
227 *
228 * Take devices from the dpm_off_list, resume them,
229 * and put them on the dpm_locked list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400230 */
231static void dpm_resume(void)
232{
233 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100234 all_sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400235 while(!list_empty(&dpm_off)) {
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100236 struct list_head *entry = dpm_off.next;
237 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400238
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100239 list_move_tail(entry, &dpm_active);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100240 dev->power.sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400241 mutex_unlock(&dpm_list_mtx);
242 resume_device(dev);
243 mutex_lock(&dpm_list_mtx);
Alan Sterncd59abf2007-09-21 15:36:56 -0400244 }
245 mutex_unlock(&dpm_list_mtx);
246}
247
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100248/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100249 * unregister_dropped_devices - Unregister devices scheduled for removal
250 *
251 * Unregister all devices on the dpm_destroy list.
252 */
253static void unregister_dropped_devices(void)
254{
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);
259
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100260 mutex_unlock(&dpm_list_mtx);
261 /* This also removes the device from the list */
262 device_unregister(dev);
263 mutex_lock(&dpm_list_mtx);
264 }
265 mutex_unlock(&dpm_list_mtx);
266}
Alan Sterncd59abf2007-09-21 15:36:56 -0400267
268/**
269 * device_resume - Restore state of each device in system.
270 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100271 * Resume all the devices, unlock them all, and allow new
272 * devices to be registered once again.
Alan Sterncd59abf2007-09-21 15:36:56 -0400273 */
Alan Sterncd59abf2007-09-21 15:36:56 -0400274void device_resume(void)
275{
276 might_sleep();
Alan Sterncd59abf2007-09-21 15:36:56 -0400277 dpm_resume();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100278 unregister_dropped_devices();
Alan Sterncd59abf2007-09-21 15:36:56 -0400279}
Alan Sterncd59abf2007-09-21 15:36:56 -0400280EXPORT_SYMBOL_GPL(device_resume);
281
282
Alan Sterncd59abf2007-09-21 15:36:56 -0400283/*------------------------- Suspend routines -------------------------*/
284
Alan Sterncd59abf2007-09-21 15:36:56 -0400285static inline char *suspend_verb(u32 event)
286{
287 switch (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)";
292 }
293}
294
Alan Sterncd59abf2007-09-21 15:36:56 -0400295static void
296suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
297{
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" : "");
301}
302
303/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100304 * suspend_device_late - Shut down one device (late suspend).
305 * @dev: Device.
306 * @state: Power state device is entering.
307 *
308 * This is called with interrupts off and only a single CPU running.
309 */
310static int suspend_device_late(struct device *dev, pm_message_t state)
311{
312 int error = 0;
313
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);
318 }
319 return error;
320}
321
322/**
323 * device_power_down - Shut down special devices.
324 * @state: Power state to enter.
325 *
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.
329 *
330 * Must be called with interrupts disabled and only one CPU running.
331 */
332int device_power_down(pm_message_t state)
333{
334 int error = 0;
335
336 while (!list_empty(&dpm_off)) {
337 struct list_head *entry = dpm_off.prev;
338 struct device *dev = to_device(entry);
339
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100340 error = suspend_device_late(dev, state);
341 if (error) {
342 printk(KERN_ERR "Could not power down device %s: "
343 "error %d\n",
344 kobject_name(&dev->kobj), error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100345 break;
346 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100347 if (!list_empty(&dev->power.entry))
348 list_move(&dev->power.entry, &dpm_off_irq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100349 }
350
351 if (!error)
352 error = sysdev_suspend(state);
353 if (error)
354 dpm_power_up();
355 return error;
356}
357EXPORT_SYMBOL_GPL(device_power_down);
358
359/**
Alan Sterncd59abf2007-09-21 15:36:56 -0400360 * suspend_device - Save state of one device.
361 * @dev: Device.
362 * @state: Power state device is entering.
363 */
Adrian Bunk19e20c92008-02-03 22:55:18 +0100364static int suspend_device(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400365{
366 int error = 0;
367
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100368 down(&dev->sem);
369
Alan Sterncd59abf2007-09-21 15:36:56 -0400370 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);
374 }
375
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);
380 }
381
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);
386 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100387
388 up(&dev->sem);
389
Alan Sterncd59abf2007-09-21 15:36:56 -0400390 return error;
391}
392
393/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100394 * dpm_suspend - Suspend every device.
395 * @state: Power state to put each device in.
Alan Sterncd59abf2007-09-21 15:36:56 -0400396 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100397 * Walk the dpm_locked list. Suspend each device and move it
398 * to the dpm_off list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400399 *
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).
Alan Sterncd59abf2007-09-21 15:36:56 -0400404 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100405static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400406{
407 int error = 0;
408
Alan Sterncd59abf2007-09-21 15:36:56 -0400409 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100410 while (!list_empty(&dpm_active)) {
411 struct list_head *entry = dpm_active.prev;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100412 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400413
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100414 WARN_ON(dev->parent && dev->parent->power.sleeping);
415
416 dev->power.sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100417 mutex_unlock(&dpm_list_mtx);
418 error = suspend_device(dev, state);
Alan Stern1b3cbec2008-02-29 11:50:22 -0500419 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100420 if (error) {
421 printk(KERN_ERR "Could not suspend device %s: "
422 "error %d%s\n",
423 kobject_name(&dev->kobj),
424 error,
425 (error == -EAGAIN ?
426 " (please convert to suspend_late)" :
427 ""));
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100428 dev->power.sleeping = false;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100429 break;
430 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100431 if (!list_empty(&dev->power.entry))
432 list_move(&dev->power.entry, &dpm_off);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100433 }
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100434 if (!error)
435 all_sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100436 mutex_unlock(&dpm_list_mtx);
437
438 return error;
439}
440
441/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100442 * device_suspend - Save state and stop all devices in system.
Randy Dunlap71996772008-02-18 13:09:03 -0800443 * @state: new power management state
Alan Sterncd59abf2007-09-21 15:36:56 -0400444 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100445 * Prevent new devices from being registered, then lock all devices
446 * and suspend them.
Alan Sterncd59abf2007-09-21 15:36:56 -0400447 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100448int device_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400449{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100450 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400451
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100452 might_sleep();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100453 error = dpm_suspend(state);
454 if (error)
455 device_resume();
Alan Sterncd59abf2007-09-21 15:36:56 -0400456 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400457}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100458EXPORT_SYMBOL_GPL(device_suspend);
Alan Sterncd59abf2007-09-21 15:36:56 -0400459
460void __suspend_report_result(const char *function, void *fn, int ret)
461{
462 if (ret) {
463 printk(KERN_ERR "%s(): ", function);
464 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
465 printk("%d\n", ret);
466 }
467}
468EXPORT_SYMBOL_GPL(__suspend_report_result);