blob: 05b989139b5434e7fe3e95340b59938d3fecda51 [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.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011 * This will initialize the embedded device_pm_info object in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
14 *
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020015 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/device.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040021#include <linux/kallsyms.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070022#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040023#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020024#include <linux/pm_runtime.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040025#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +010026#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010027#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010028#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010029#include <linux/suspend.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070030
Alan Sterncd59abf2007-09-21 15:36:56 -040031#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "power.h"
33
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010034/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020035 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010036 * because children are guaranteed to be discovered after parents, and
37 * are inserted at the back of the list on discovery.
38 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080039 * Since device_pm_add() may be called with a device lock held,
40 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010041 * dpm_list_mutex.
42 */
43
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020044LIST_HEAD(dpm_list);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +010045LIST_HEAD(dpm_prepared_list);
46LIST_HEAD(dpm_suspended_list);
47LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alan Sterncd59abf2007-09-21 15:36:56 -040049static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010050static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020052static int async_error;
53
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020054/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020055 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020056 * @dev: Device object being initialized.
57 */
58void device_pm_init(struct device *dev)
59{
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +010060 dev->power.in_suspend = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010061 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020062 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020063 dev->power.wakeup = NULL;
64 spin_lock_init(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020065 pm_runtime_init(dev);
66}
67
68/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020069 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020070 */
71void device_pm_lock(void)
72{
73 mutex_lock(&dpm_list_mtx);
74}
75
76/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020077 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020078 */
79void device_pm_unlock(void)
80{
81 mutex_unlock(&dpm_list_mtx);
82}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010083
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010084/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020085 * device_pm_add - Add a device to the PM core's list of active devices.
86 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010087 */
Alan Stern3b98aea2008-08-07 13:06:12 -040088void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +010091 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070092 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockib64959e2010-12-16 17:11:45 +010093 if (dev->parent && dev->parent->power.in_suspend)
94 dev_warn(dev, "parent %s should not be sleeping\n",
95 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -040096 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070097 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100100/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200101 * device_pm_remove - Remove a device from the PM core's list of active devices.
102 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100103 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200104void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100107 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100108 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700109 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700111 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200112 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200113 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100114}
115
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200116/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200117 * device_pm_move_before - Move device in the PM core's list of active devices.
118 * @deva: Device to move in dpm_list.
119 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100120 */
121void device_pm_move_before(struct device *deva, struct device *devb)
122{
123 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100124 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
125 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100126 /* Delete deva from dpm_list and reinsert before devb. */
127 list_move_tail(&deva->power.entry, &devb->power.entry);
128}
129
130/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200131 * device_pm_move_after - Move device in the PM core's list of active devices.
132 * @deva: Device to move in dpm_list.
133 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100134 */
135void device_pm_move_after(struct device *deva, struct device *devb)
136{
137 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100138 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
139 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100140 /* Delete deva from dpm_list and reinsert after devb. */
141 list_move(&deva->power.entry, &devb->power.entry);
142}
143
144/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200145 * device_pm_move_last - Move device to end of the PM core's list of devices.
146 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100147 */
148void device_pm_move_last(struct device *dev)
149{
150 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100151 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100152 list_move_tail(&dev->power.entry, &dpm_list);
153}
154
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100155static ktime_t initcall_debug_start(struct device *dev)
156{
157 ktime_t calltime = ktime_set(0, 0);
158
159 if (initcall_debug) {
160 pr_info("calling %s+ @ %i\n",
161 dev_name(dev), task_pid_nr(current));
162 calltime = ktime_get();
163 }
164
165 return calltime;
166}
167
168static void initcall_debug_report(struct device *dev, ktime_t calltime,
169 int error)
170{
171 ktime_t delta, rettime;
172
173 if (initcall_debug) {
174 rettime = ktime_get();
175 delta = ktime_sub(rettime, calltime);
176 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
177 error, (unsigned long long)ktime_to_ns(delta) >> 10);
178 }
179}
180
Cornelia Huckffa6a702009-03-04 12:44:00 +0100181/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100182 * dpm_wait - Wait for a PM operation to complete.
183 * @dev: Device to wait for.
184 * @async: If unset, wait only if the device's power.async_suspend flag is set.
185 */
186static void dpm_wait(struct device *dev, bool async)
187{
188 if (!dev)
189 return;
190
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100191 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100192 wait_for_completion(&dev->power.completion);
193}
194
195static int dpm_wait_fn(struct device *dev, void *async_ptr)
196{
197 dpm_wait(dev, *((bool *)async_ptr));
198 return 0;
199}
200
201static void dpm_wait_for_children(struct device *dev, bool async)
202{
203 device_for_each_child(dev, &async, dpm_wait_fn);
204}
205
206/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200207 * pm_op - Execute the PM operation appropriate for given PM event.
208 * @dev: Device to handle.
209 * @ops: PM operations to choose from.
210 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200211 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200212static int pm_op(struct device *dev,
213 const struct dev_pm_ops *ops,
214 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200215{
216 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100217 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100218
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100219 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200220
221 switch (state.event) {
222#ifdef CONFIG_SUSPEND
223 case PM_EVENT_SUSPEND:
224 if (ops->suspend) {
225 error = ops->suspend(dev);
226 suspend_report_result(ops->suspend, error);
227 }
228 break;
229 case PM_EVENT_RESUME:
230 if (ops->resume) {
231 error = ops->resume(dev);
232 suspend_report_result(ops->resume, error);
233 }
234 break;
235#endif /* CONFIG_SUSPEND */
236#ifdef CONFIG_HIBERNATION
237 case PM_EVENT_FREEZE:
238 case PM_EVENT_QUIESCE:
239 if (ops->freeze) {
240 error = ops->freeze(dev);
241 suspend_report_result(ops->freeze, error);
242 }
243 break;
244 case PM_EVENT_HIBERNATE:
245 if (ops->poweroff) {
246 error = ops->poweroff(dev);
247 suspend_report_result(ops->poweroff, error);
248 }
249 break;
250 case PM_EVENT_THAW:
251 case PM_EVENT_RECOVER:
252 if (ops->thaw) {
253 error = ops->thaw(dev);
254 suspend_report_result(ops->thaw, error);
255 }
256 break;
257 case PM_EVENT_RESTORE:
258 if (ops->restore) {
259 error = ops->restore(dev);
260 suspend_report_result(ops->restore, error);
261 }
262 break;
263#endif /* CONFIG_HIBERNATION */
264 default:
265 error = -EINVAL;
266 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100267
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100268 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100269
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200270 return error;
271}
272
273/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200274 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
275 * @dev: Device to handle.
276 * @ops: PM operations to choose from.
277 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200278 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200279 * The driver of @dev will not receive interrupts while this function is being
280 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200281 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200282static int pm_noirq_op(struct device *dev,
283 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200284 pm_message_t state)
285{
286 int error = 0;
Rafael J. Wysocki2cbb3ce2010-12-15 00:17:29 +0100287 ktime_t calltime = ktime_set(0, 0), delta, rettime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100288
289 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100290 pr_info("calling %s+ @ %i, parent: %s\n",
291 dev_name(dev), task_pid_nr(current),
292 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100293 calltime = ktime_get();
294 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200295
296 switch (state.event) {
297#ifdef CONFIG_SUSPEND
298 case PM_EVENT_SUSPEND:
299 if (ops->suspend_noirq) {
300 error = ops->suspend_noirq(dev);
301 suspend_report_result(ops->suspend_noirq, error);
302 }
303 break;
304 case PM_EVENT_RESUME:
305 if (ops->resume_noirq) {
306 error = ops->resume_noirq(dev);
307 suspend_report_result(ops->resume_noirq, error);
308 }
309 break;
310#endif /* CONFIG_SUSPEND */
311#ifdef CONFIG_HIBERNATION
312 case PM_EVENT_FREEZE:
313 case PM_EVENT_QUIESCE:
314 if (ops->freeze_noirq) {
315 error = ops->freeze_noirq(dev);
316 suspend_report_result(ops->freeze_noirq, error);
317 }
318 break;
319 case PM_EVENT_HIBERNATE:
320 if (ops->poweroff_noirq) {
321 error = ops->poweroff_noirq(dev);
322 suspend_report_result(ops->poweroff_noirq, error);
323 }
324 break;
325 case PM_EVENT_THAW:
326 case PM_EVENT_RECOVER:
327 if (ops->thaw_noirq) {
328 error = ops->thaw_noirq(dev);
329 suspend_report_result(ops->thaw_noirq, error);
330 }
331 break;
332 case PM_EVENT_RESTORE:
333 if (ops->restore_noirq) {
334 error = ops->restore_noirq(dev);
335 suspend_report_result(ops->restore_noirq, error);
336 }
337 break;
338#endif /* CONFIG_HIBERNATION */
339 default:
340 error = -EINVAL;
341 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100342
343 if (initcall_debug) {
344 rettime = ktime_get();
345 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100346 printk("initcall %s_i+ returned %d after %Ld usecs\n",
347 dev_name(dev), error,
348 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100349 }
350
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200351 return error;
352}
353
354static char *pm_verb(int event)
355{
356 switch (event) {
357 case PM_EVENT_SUSPEND:
358 return "suspend";
359 case PM_EVENT_RESUME:
360 return "resume";
361 case PM_EVENT_FREEZE:
362 return "freeze";
363 case PM_EVENT_QUIESCE:
364 return "quiesce";
365 case PM_EVENT_HIBERNATE:
366 return "hibernate";
367 case PM_EVENT_THAW:
368 return "thaw";
369 case PM_EVENT_RESTORE:
370 return "restore";
371 case PM_EVENT_RECOVER:
372 return "recover";
373 default:
374 return "(unknown PM event)";
375 }
376}
377
378static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
379{
380 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
381 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
382 ", may wakeup" : "");
383}
384
385static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
386 int error)
387{
388 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100389 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200390}
391
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100392static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
393{
394 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200395 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100396 int usecs;
397
398 calltime = ktime_get();
399 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
400 do_div(usecs64, NSEC_PER_USEC);
401 usecs = usecs64;
402 if (usecs == 0)
403 usecs = 1;
404 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
405 info ?: "", info ? " " : "", pm_verb(state.event),
406 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
407}
408
Alan Sterncd59abf2007-09-21 15:36:56 -0400409/*------------------------- Resume routines -------------------------*/
410
411/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200412 * device_resume_noirq - Execute an "early resume" callback for given device.
413 * @dev: Device to handle.
414 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400415 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200416 * The driver of @dev will not receive interrupts while this function is being
417 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400418 */
Alan Sternd1616302009-05-24 22:05:42 +0200419static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400420{
421 int error = 0;
422
423 TRACE_DEVICE(dev);
424 TRACE_RESUME(0);
425
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100426 if (dev->pwr_domain) {
427 pm_dev_dbg(dev, state, "EARLY power domain ");
428 pm_noirq_op(dev, &dev->pwr_domain->ops, state);
429 }
430
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100431 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200432 pm_dev_dbg(dev, state, "EARLY ");
433 error = pm_noirq_op(dev, dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100434 if (error)
435 goto End;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100436 }
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100437
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100438 if (dev->type && dev->type->pm) {
439 pm_dev_dbg(dev, state, "EARLY type ");
440 error = pm_noirq_op(dev, dev->type->pm, state);
441 if (error)
442 goto End;
443 }
444
445 if (dev->class && dev->class->pm) {
446 pm_dev_dbg(dev, state, "EARLY class ");
447 error = pm_noirq_op(dev, dev->class->pm, state);
448 }
449
450End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100451 TRACE_RESUME(error);
452 return error;
453}
454
455/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200456 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
457 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100458 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200459 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
460 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100461 */
Alan Sternd1616302009-05-24 22:05:42 +0200462void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100463{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100464 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100465
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200466 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100467 while (!list_empty(&dpm_noirq_list)) {
468 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100469 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100470
471 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100472 list_move_tail(&dev->power.entry, &dpm_suspended_list);
473 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100474
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100475 error = device_resume_noirq(dev, state);
476 if (error)
477 pm_dev_err(dev, state, " early", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100478
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100479 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100480 put_device(dev);
481 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200482 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100483 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100484 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100485}
Alan Sternd1616302009-05-24 22:05:42 +0200486EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100487
488/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100489 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800490 * @dev: Device to resume.
491 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100492 */
493static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
494{
495 int error;
496 ktime_t calltime;
497
498 calltime = initcall_debug_start(dev);
499
500 error = cb(dev);
501 suspend_report_result(cb, error);
502
503 initcall_debug_report(dev, calltime, error);
504
505 return error;
506}
507
508/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100509 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200510 * @dev: Device to handle.
511 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100512 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100513 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100514static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100515{
516 int error = 0;
517
518 TRACE_DEVICE(dev);
519 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400520
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100521 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800522 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100523
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100524 dev->power.in_suspend = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100525
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100526 if (dev->pwr_domain) {
527 pm_dev_dbg(dev, state, "power domain ");
528 pm_op(dev, &dev->pwr_domain->ops, state);
529 }
530
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200531 if (dev->bus) {
532 if (dev->bus->pm) {
533 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200534 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200535 } else if (dev->bus->resume) {
536 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100537 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200538 }
539 if (error)
540 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400541 }
542
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200543 if (dev->type) {
544 if (dev->type->pm) {
545 pm_dev_dbg(dev, state, "type ");
546 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200547 }
548 if (error)
549 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400550 }
551
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200552 if (dev->class) {
553 if (dev->class->pm) {
554 pm_dev_dbg(dev, state, "class ");
555 error = pm_op(dev, dev->class->pm, state);
556 } else if (dev->class->resume) {
557 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100558 error = legacy_resume(dev, dev->class->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200559 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400560 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200561 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800562 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100563 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100564
Alan Sterncd59abf2007-09-21 15:36:56 -0400565 TRACE_RESUME(error);
566 return error;
567}
568
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100569static void async_resume(void *data, async_cookie_t cookie)
570{
571 struct device *dev = (struct device *)data;
572 int error;
573
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100574 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100575 if (error)
576 pm_dev_err(dev, pm_transition, " async", error);
577 put_device(dev);
578}
579
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100580static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100581{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100582 return dev->power.async_suspend && pm_async_enabled
583 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100584}
585
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100586/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200587 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
588 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100589 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200590 * Execute the appropriate "resume" callback for all devices whose status
591 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400592 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200593static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400594{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100595 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100596 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400597
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200598 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100599 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200600 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200601
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100602 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100603 INIT_COMPLETION(dev->power.completion);
604 if (is_async(dev)) {
605 get_device(dev);
606 async_schedule(async_resume, dev);
607 }
608 }
609
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100610 while (!list_empty(&dpm_suspended_list)) {
611 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200612 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100613 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200614 int error;
615
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200616 mutex_unlock(&dpm_list_mtx);
617
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100618 error = device_resume(dev, state, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200619 if (error)
620 pm_dev_err(dev, state, "", error);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100621
622 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200623 }
624 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100625 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400627 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200628 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100629 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100630 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200631}
632
633/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200634 * device_complete - Complete a PM transition for given device.
635 * @dev: Device to handle.
636 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200637 */
Alan Sternd1616302009-05-24 22:05:42 +0200638static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200639{
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800640 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200641
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100642 if (dev->pwr_domain && dev->pwr_domain->ops.complete) {
643 pm_dev_dbg(dev, state, "completing power domain ");
644 dev->pwr_domain->ops.complete(dev);
645 }
646
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200647 if (dev->class && dev->class->pm && dev->class->pm->complete) {
648 pm_dev_dbg(dev, state, "completing class ");
649 dev->class->pm->complete(dev);
650 }
651
652 if (dev->type && dev->type->pm && dev->type->pm->complete) {
653 pm_dev_dbg(dev, state, "completing type ");
654 dev->type->pm->complete(dev);
655 }
656
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200657 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200658 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200659 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200660 }
661
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800662 device_unlock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200663}
664
665/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200666 * dpm_complete - Complete a PM transition for all non-sysdev devices.
667 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200668 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200669 * Execute the ->complete() callbacks for all devices whose PM status is not
670 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200671 */
672static void dpm_complete(pm_message_t state)
673{
674 struct list_head list;
675
676 INIT_LIST_HEAD(&list);
677 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100678 while (!list_empty(&dpm_prepared_list)) {
679 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200680
681 get_device(dev);
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100682 dev->power.in_suspend = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100683 list_move(&dev->power.entry, &list);
684 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200685
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100686 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200687
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100688 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200689 put_device(dev);
690 }
691 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400692 mutex_unlock(&dpm_list_mtx);
693}
694
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100695/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200696 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
697 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400698 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200699 * Execute "resume" callbacks for all devices and complete the PM transition of
700 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400701 */
Alan Sternd1616302009-05-24 22:05:42 +0200702void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400703{
704 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200705 dpm_resume(state);
706 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400707}
Alan Sternd1616302009-05-24 22:05:42 +0200708EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400709
710
Alan Sterncd59abf2007-09-21 15:36:56 -0400711/*------------------------- Suspend routines -------------------------*/
712
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200713/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200714 * resume_event - Return a "resume" message for given "suspend" sleep state.
715 * @sleep_state: PM message representing a sleep state.
716 *
717 * Return a PM message representing the resume event corresponding to given
718 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200719 */
720static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400721{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200722 switch (sleep_state.event) {
723 case PM_EVENT_SUSPEND:
724 return PMSG_RESUME;
725 case PM_EVENT_FREEZE:
726 case PM_EVENT_QUIESCE:
727 return PMSG_RECOVER;
728 case PM_EVENT_HIBERNATE:
729 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400730 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200731 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400732}
733
734/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200735 * device_suspend_noirq - Execute a "late suspend" callback for given device.
736 * @dev: Device to handle.
737 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100738 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200739 * The driver of @dev will not receive interrupts while this function is being
740 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100741 */
Alan Sternd1616302009-05-24 22:05:42 +0200742static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100743{
744 int error = 0;
745
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100746 if (dev->class && dev->class->pm) {
747 pm_dev_dbg(dev, state, "LATE class ");
748 error = pm_noirq_op(dev, dev->class->pm, state);
749 if (error)
750 goto End;
751 }
752
753 if (dev->type && dev->type->pm) {
754 pm_dev_dbg(dev, state, "LATE type ");
755 error = pm_noirq_op(dev, dev->type->pm, state);
756 if (error)
757 goto End;
758 }
759
Rafael J. Wysocki33c33742009-12-13 20:31:12 +0100760 if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200761 pm_dev_dbg(dev, state, "LATE ");
762 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100763 if (error)
764 goto End;
765 }
766
767 if (dev->pwr_domain) {
768 pm_dev_dbg(dev, state, "LATE power domain ");
769 pm_noirq_op(dev, &dev->pwr_domain->ops, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100770 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100771
772End:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100773 return error;
774}
775
776/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200777 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
778 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100779 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200780 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
781 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100782 */
Alan Sternd1616302009-05-24 22:05:42 +0200783int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100784{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100785 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100786 int error = 0;
787
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100788 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200789 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100790 while (!list_empty(&dpm_suspended_list)) {
791 struct device *dev = to_device(dpm_suspended_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100792
793 get_device(dev);
794 mutex_unlock(&dpm_list_mtx);
795
Alan Sternd1616302009-05-24 22:05:42 +0200796 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100797
798 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100799 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200800 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100801 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100802 break;
803 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100804 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100805 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100806 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100807 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200808 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100809 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200810 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100811 else
812 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100813 return error;
814}
Alan Sternd1616302009-05-24 22:05:42 +0200815EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100816
817/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100818 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800819 * @dev: Device to suspend.
820 * @state: PM transition of the system being carried out.
821 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100822 */
823static int legacy_suspend(struct device *dev, pm_message_t state,
824 int (*cb)(struct device *dev, pm_message_t state))
825{
826 int error;
827 ktime_t calltime;
828
829 calltime = initcall_debug_start(dev);
830
831 error = cb(dev, state);
832 suspend_report_result(cb, error);
833
834 initcall_debug_report(dev, calltime, error);
835
836 return error;
837}
838
839/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200840 * device_suspend - Execute "suspend" callbacks for given device.
841 * @dev: Device to handle.
842 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100843 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400844 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100845static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400846{
847 int error = 0;
848
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100849 dpm_wait_for_children(dev, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800850 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100851
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100852 if (async_error)
853 goto End;
854
Rafael J. Wysockid83f9052010-12-03 23:14:26 +0100855 if (pm_wakeup_pending()) {
856 async_error = -EBUSY;
857 goto End;
858 }
859
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200860 if (dev->class) {
861 if (dev->class->pm) {
862 pm_dev_dbg(dev, state, "class ");
863 error = pm_op(dev, dev->class->pm, state);
864 } else if (dev->class->suspend) {
865 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100866 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200867 }
868 if (error)
869 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400870 }
871
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200872 if (dev->type) {
873 if (dev->type->pm) {
874 pm_dev_dbg(dev, state, "type ");
875 error = pm_op(dev, dev->type->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200876 }
877 if (error)
878 goto End;
Alan Sterncd59abf2007-09-21 15:36:56 -0400879 }
880
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200881 if (dev->bus) {
882 if (dev->bus->pm) {
883 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200884 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200885 } else if (dev->bus->suspend) {
886 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100887 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200888 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100889 if (error)
890 goto End;
891 }
892
893 if (dev->pwr_domain) {
894 pm_dev_dbg(dev, state, "power domain ");
895 pm_op(dev, &dev->pwr_domain->ops, state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400896 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100897
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200898 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800899 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100900 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100901
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200902 if (error)
903 async_error = error;
904
Alan Sterncd59abf2007-09-21 15:36:56 -0400905 return error;
906}
907
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100908static void async_suspend(void *data, async_cookie_t cookie)
909{
910 struct device *dev = (struct device *)data;
911 int error;
912
913 error = __device_suspend(dev, pm_transition, true);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200914 if (error)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100915 pm_dev_err(dev, pm_transition, " async", error);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100916
917 put_device(dev);
918}
919
920static int device_suspend(struct device *dev)
921{
922 INIT_COMPLETION(dev->power.completion);
923
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100924 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100925 get_device(dev);
926 async_schedule(async_suspend, dev);
927 return 0;
928 }
929
930 return __device_suspend(dev, pm_transition, false);
931}
932
Alan Sterncd59abf2007-09-21 15:36:56 -0400933/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200934 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
935 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400936 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100937static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400938{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100939 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400940 int error = 0;
941
Alan Sterncd59abf2007-09-21 15:36:56 -0400942 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100943 pm_transition = state;
944 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100945 while (!list_empty(&dpm_prepared_list)) {
946 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400947
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200948 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100949 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200950
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100951 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200952
Alan Stern1b3cbec2008-02-29 11:50:22 -0500953 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100954 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200955 pm_dev_err(dev, state, "", error);
956 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100957 break;
958 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100959 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100960 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200961 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100962 if (async_error)
963 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100964 }
965 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100966 async_synchronize_full();
967 if (!error)
968 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100969 if (!error)
970 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200971 return error;
972}
973
974/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200975 * device_prepare - Prepare a device for system power transition.
976 * @dev: Device to handle.
977 * @state: PM transition of the system being carried out.
978 *
979 * Execute the ->prepare() callback(s) for given device. No new children of the
980 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200981 */
Alan Sternd1616302009-05-24 22:05:42 +0200982static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200983{
984 int error = 0;
985
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800986 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200987
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200988 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200989 pm_dev_dbg(dev, state, "preparing ");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200990 error = dev->bus->pm->prepare(dev);
991 suspend_report_result(dev->bus->pm->prepare, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200992 if (error)
993 goto End;
994 }
995
996 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
997 pm_dev_dbg(dev, state, "preparing type ");
998 error = dev->type->pm->prepare(dev);
999 suspend_report_result(dev->type->pm->prepare, error);
1000 if (error)
1001 goto End;
1002 }
1003
1004 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
1005 pm_dev_dbg(dev, state, "preparing class ");
1006 error = dev->class->pm->prepare(dev);
1007 suspend_report_result(dev->class->pm->prepare, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001008 if (error)
1009 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001010 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001011
1012 if (dev->pwr_domain && dev->pwr_domain->ops.prepare) {
1013 pm_dev_dbg(dev, state, "preparing power domain ");
1014 dev->pwr_domain->ops.prepare(dev);
1015 }
1016
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001017 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001018 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001019
1020 return error;
1021}
1022
1023/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001024 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1025 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001026 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001027 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001028 */
1029static int dpm_prepare(pm_message_t state)
1030{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001031 int error = 0;
1032
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001033 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001034 while (!list_empty(&dpm_list)) {
1035 struct device *dev = to_device(dpm_list.next);
1036
1037 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001038 mutex_unlock(&dpm_list_mtx);
1039
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001040 pm_runtime_get_noresume(dev);
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001041 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1042 pm_wakeup_event(dev, 0);
1043
Rafael J. Wysockie8665002011-02-12 01:42:41 +01001044 pm_runtime_put_sync(dev);
1045 error = pm_wakeup_pending() ?
1046 -EBUSY : device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001047
1048 mutex_lock(&dpm_list_mtx);
1049 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001050 if (error == -EAGAIN) {
1051 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001052 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001053 continue;
1054 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001055 printk(KERN_INFO "PM: Device %s not prepared "
1056 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001057 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001058 put_device(dev);
1059 break;
1060 }
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +01001061 dev->power.in_suspend = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001062 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001063 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001064 put_device(dev);
1065 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001066 mutex_unlock(&dpm_list_mtx);
1067 return error;
1068}
1069
1070/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001071 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1072 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001073 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001074 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1075 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001076 */
Alan Sternd1616302009-05-24 22:05:42 +02001077int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001078{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001079 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001080
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001081 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001082 error = dpm_prepare(state);
1083 if (!error)
1084 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001085 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001086}
Alan Sternd1616302009-05-24 22:05:42 +02001087EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001088
1089void __suspend_report_result(const char *function, void *fn, int ret)
1090{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001091 if (ret)
1092 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001093}
1094EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001095
1096/**
1097 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1098 * @dev: Device to wait for.
1099 * @subordinate: Device that needs to wait for @dev.
1100 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001101int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001102{
1103 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001104 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001105}
1106EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);