blob: 93a146940b916975149b32be509151c45461b828 [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 {
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +010084 error = dpm_sysfs_add(dev);
85 if (!error)
86 list_add_tail(&dev->power.entry, &dpm_active);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010087 }
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070088 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +010089 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010092/**
93 * device_pm_remove - remove a device from the list of active devices
94 * @dev: Device to be removed from the list
95 *
96 * This function also removes the device's PM-related sysfs attributes.
97 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +020098void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 pr_debug("PM: Removing info for %s:%s\n",
Dmitry Torokhovc48ea602007-04-11 01:37:18 -0400101 dev->bus ? dev->bus->name : "No Bus",
102 kobject_name(&dev->kobj));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700103 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700106 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100107}
108
109/**
110 * device_pm_schedule_removal - schedule the removal of a suspended device
111 * @dev: Device to destroy
112 *
113 * Moves the device to the dpm_destroy list for further processing by
114 * unregister_dropped_devices().
115 */
116void device_pm_schedule_removal(struct device *dev)
117{
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);
124}
Rafael J. Wysocki9617c3e2008-01-25 01:30:25 +0100125EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100126
Alan Sterncd59abf2007-09-21 15:36:56 -0400127/*------------------------- Resume routines -------------------------*/
128
129/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100130 * resume_device_early - Power on one device (early resume).
Alan Sterncd59abf2007-09-21 15:36:56 -0400131 * @dev: Device.
132 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100133 * Must be called with interrupts disabled.
Alan Sterncd59abf2007-09-21 15:36:56 -0400134 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100135static int resume_device_early(struct device *dev)
Alan Sterncd59abf2007-09-21 15:36:56 -0400136{
137 int error = 0;
138
139 TRACE_DEVICE(dev);
140 TRACE_RESUME(0);
141
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100142 if (dev->bus && dev->bus->resume_early) {
143 dev_dbg(dev, "EARLY resume\n");
144 error = dev->bus->resume_early(dev);
145 }
146
147 TRACE_RESUME(error);
148 return error;
149}
150
151/**
152 * dpm_power_up - Power on all regular (non-sysdev) devices.
153 *
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.
158 *
159 * Must be called with interrupts disabled and only one CPU running.
160 */
161static void dpm_power_up(void)
162{
163
164 while (!list_empty(&dpm_off_irq)) {
165 struct list_head *entry = dpm_off_irq.next;
166 struct device *dev = to_device(entry);
167
168 list_move_tail(entry, &dpm_off);
169 resume_device_early(dev);
170 }
171}
172
173/**
174 * device_power_up - Turn on all devices that need special attention.
175 *
176 * Power on system devices, then devices that required we shut them down
177 * with interrupts disabled.
178 *
179 * Must be called with interrupts disabled.
180 */
181void device_power_up(void)
182{
183 sysdev_resume();
184 dpm_power_up();
185}
186EXPORT_SYMBOL_GPL(device_power_up);
187
188/**
189 * resume_device - Restore state for one device.
190 * @dev: Device.
191 *
192 */
193static int resume_device(struct device *dev)
194{
195 int error = 0;
196
197 TRACE_DEVICE(dev);
198 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400199
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100200 down(&dev->sem);
201
Alan Sterncd59abf2007-09-21 15:36:56 -0400202 if (dev->bus && dev->bus->resume) {
203 dev_dbg(dev,"resuming\n");
204 error = dev->bus->resume(dev);
205 }
206
207 if (!error && dev->type && dev->type->resume) {
208 dev_dbg(dev,"resuming\n");
209 error = dev->type->resume(dev);
210 }
211
212 if (!error && dev->class && dev->class->resume) {
213 dev_dbg(dev,"class resume\n");
214 error = dev->class->resume(dev);
215 }
216
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100217 up(&dev->sem);
218
Alan Sterncd59abf2007-09-21 15:36:56 -0400219 TRACE_RESUME(error);
220 return error;
221}
222
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100223/**
224 * dpm_resume - Resume every device.
225 *
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.
229 *
230 * Take devices from the dpm_off_list, resume them,
231 * and put them on the dpm_locked list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400232 */
233static void dpm_resume(void)
234{
235 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100236 all_sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400237 while(!list_empty(&dpm_off)) {
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100238 struct list_head *entry = dpm_off.next;
239 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400240
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100241 list_move_tail(entry, &dpm_active);
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100242 dev->power.sleeping = false;
Alan Sterncd59abf2007-09-21 15:36:56 -0400243 mutex_unlock(&dpm_list_mtx);
244 resume_device(dev);
245 mutex_lock(&dpm_list_mtx);
Alan Sterncd59abf2007-09-21 15:36:56 -0400246 }
247 mutex_unlock(&dpm_list_mtx);
248}
249
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100250/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100251 * unregister_dropped_devices - Unregister devices scheduled for removal
252 *
253 * Unregister all devices on the dpm_destroy list.
254 */
255static void unregister_dropped_devices(void)
256{
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);
261
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100262 mutex_unlock(&dpm_list_mtx);
263 /* This also removes the device from the list */
264 device_unregister(dev);
265 mutex_lock(&dpm_list_mtx);
266 }
267 mutex_unlock(&dpm_list_mtx);
268}
Alan Sterncd59abf2007-09-21 15:36:56 -0400269
270/**
271 * device_resume - Restore state of each device in system.
272 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100273 * Resume all the devices, unlock them all, and allow new
274 * devices to be registered once again.
Alan Sterncd59abf2007-09-21 15:36:56 -0400275 */
Alan Sterncd59abf2007-09-21 15:36:56 -0400276void device_resume(void)
277{
278 might_sleep();
Alan Sterncd59abf2007-09-21 15:36:56 -0400279 dpm_resume();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100280 unregister_dropped_devices();
Alan Sterncd59abf2007-09-21 15:36:56 -0400281}
Alan Sterncd59abf2007-09-21 15:36:56 -0400282EXPORT_SYMBOL_GPL(device_resume);
283
284
Alan Sterncd59abf2007-09-21 15:36:56 -0400285/*------------------------- Suspend routines -------------------------*/
286
Alan Sterncd59abf2007-09-21 15:36:56 -0400287static inline char *suspend_verb(u32 event)
288{
289 switch (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)";
294 }
295}
296
Alan Sterncd59abf2007-09-21 15:36:56 -0400297static void
298suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
299{
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" : "");
303}
304
305/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100306 * suspend_device_late - Shut down one device (late suspend).
307 * @dev: Device.
308 * @state: Power state device is entering.
309 *
310 * This is called with interrupts off and only a single CPU running.
311 */
312static int suspend_device_late(struct device *dev, pm_message_t state)
313{
314 int error = 0;
315
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);
320 }
321 return error;
322}
323
324/**
325 * device_power_down - Shut down special devices.
326 * @state: Power state to enter.
327 *
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.
331 *
332 * Must be called with interrupts disabled and only one CPU running.
333 */
334int device_power_down(pm_message_t state)
335{
336 int error = 0;
337
338 while (!list_empty(&dpm_off)) {
339 struct list_head *entry = dpm_off.prev;
340 struct device *dev = to_device(entry);
341
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100342 error = suspend_device_late(dev, state);
343 if (error) {
344 printk(KERN_ERR "Could not power down device %s: "
345 "error %d\n",
346 kobject_name(&dev->kobj), error);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100347 break;
348 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100349 if (!list_empty(&dev->power.entry))
350 list_move(&dev->power.entry, &dpm_off_irq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100351 }
352
353 if (!error)
354 error = sysdev_suspend(state);
355 if (error)
356 dpm_power_up();
357 return error;
358}
359EXPORT_SYMBOL_GPL(device_power_down);
360
361/**
Alan Sterncd59abf2007-09-21 15:36:56 -0400362 * suspend_device - Save state of one device.
363 * @dev: Device.
364 * @state: Power state device is entering.
365 */
Adrian Bunk19e20c92008-02-03 22:55:18 +0100366static int suspend_device(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400367{
368 int error = 0;
369
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100370 down(&dev->sem);
371
Alan Sterncd59abf2007-09-21 15:36:56 -0400372 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);
376 }
377
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);
382 }
383
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);
388 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100389
390 up(&dev->sem);
391
Alan Sterncd59abf2007-09-21 15:36:56 -0400392 return error;
393}
394
395/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100396 * dpm_suspend - Suspend every device.
397 * @state: Power state to put each device in.
Alan Sterncd59abf2007-09-21 15:36:56 -0400398 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100399 * Walk the dpm_locked list. Suspend each device and move it
400 * to the dpm_off list.
Alan Sterncd59abf2007-09-21 15:36:56 -0400401 *
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).
Alan Sterncd59abf2007-09-21 15:36:56 -0400406 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100407static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400408{
409 int error = 0;
410
Alan Sterncd59abf2007-09-21 15:36:56 -0400411 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100412 while (!list_empty(&dpm_active)) {
413 struct list_head *entry = dpm_active.prev;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100414 struct device *dev = to_device(entry);
Alan Sterncd59abf2007-09-21 15:36:56 -0400415
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100416 WARN_ON(dev->parent && dev->parent->power.sleeping);
417
418 dev->power.sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100419 mutex_unlock(&dpm_list_mtx);
420 error = suspend_device(dev, state);
Alan Stern1b3cbec2008-02-29 11:50:22 -0500421 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100422 if (error) {
423 printk(KERN_ERR "Could not suspend device %s: "
424 "error %d%s\n",
425 kobject_name(&dev->kobj),
426 error,
427 (error == -EAGAIN ?
428 " (please convert to suspend_late)" :
429 ""));
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100430 dev->power.sleeping = false;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100431 break;
432 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100433 if (!list_empty(&dev->power.entry))
434 list_move(&dev->power.entry, &dpm_off);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100435 }
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100436 if (!error)
437 all_sleeping = true;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100438 mutex_unlock(&dpm_list_mtx);
439
440 return error;
441}
442
443/**
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444 * device_suspend - Save state and stop all devices in system.
Randy Dunlap71996772008-02-18 13:09:03 -0800445 * @state: new power management state
Alan Sterncd59abf2007-09-21 15:36:56 -0400446 *
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100447 * Prevent new devices from being registered, then lock all devices
448 * and suspend them.
Alan Sterncd59abf2007-09-21 15:36:56 -0400449 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100450int device_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400451{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100452 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400453
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454 might_sleep();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100455 error = dpm_suspend(state);
456 if (error)
457 device_resume();
Alan Sterncd59abf2007-09-21 15:36:56 -0400458 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -0400459}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100460EXPORT_SYMBOL_GPL(device_suspend);
Alan Sterncd59abf2007-09-21 15:36:56 -0400461
462void __suspend_report_result(const char *function, void *fn, int ret)
463{
464 if (ret) {
465 printk(KERN_ERR "%s(): ", function);
466 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
467 printk("%d\n", ret);
468 }
469}
470EXPORT_SYMBOL_GPL(__suspend_report_result);