blob: 97db7552e90a94ad0a3ba399cc4e2d229b62365c [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>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040022#include <linux/export.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070023#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040024#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020025#include <linux/pm_runtime.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040026#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +010027#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010028#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010029#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010030#include <linux/suspend.h>
Shuah Khan53644672013-07-26 13:30:20 -060031#include <trace/events/power.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020032#include <linux/cpuidle.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040033#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "power.h"
35
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010036typedef int (*pm_callback_t)(struct device *);
37
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010038/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020039 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010040 * because children are guaranteed to be discovered after parents, and
41 * are inserted at the back of the list on discovery.
42 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080043 * Since device_pm_add() may be called with a device lock held,
44 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010045 * dpm_list_mutex.
46 */
47
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020048LIST_HEAD(dpm_list);
Sachin Kamat7664e962012-07-17 22:38:08 +020049static LIST_HEAD(dpm_prepared_list);
50static LIST_HEAD(dpm_suspended_list);
51static LIST_HEAD(dpm_late_early_list);
52static LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
ShuoX Liu2a77c462011-08-10 23:01:26 +020054struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040055static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010056static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020058static int async_error;
59
Shuah Khan53644672013-07-26 13:30:20 -060060static char *pm_verb(int event)
61{
62 switch (event) {
63 case PM_EVENT_SUSPEND:
64 return "suspend";
65 case PM_EVENT_RESUME:
66 return "resume";
67 case PM_EVENT_FREEZE:
68 return "freeze";
69 case PM_EVENT_QUIESCE:
70 return "quiesce";
71 case PM_EVENT_HIBERNATE:
72 return "hibernate";
73 case PM_EVENT_THAW:
74 return "thaw";
75 case PM_EVENT_RESTORE:
76 return "restore";
77 case PM_EVENT_RECOVER:
78 return "recover";
79 default:
80 return "(unknown PM event)";
81 }
82}
83
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020084/**
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020085 * device_pm_sleep_init - Initialize system suspend-related device fields.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020086 * @dev: Device object being initialized.
87 */
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020088void device_pm_sleep_init(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020089{
Alan Sternf76b168b2011-06-18 20:22:23 +020090 dev->power.is_prepared = false;
Alan Stern6d0e0e82011-06-18 22:42:09 +020091 dev->power.is_suspended = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010092 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020093 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020094 dev->power.wakeup = NULL;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +020095 INIT_LIST_HEAD(&dev->power.entry);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020096}
97
98/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020099 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200100 */
101void device_pm_lock(void)
102{
103 mutex_lock(&dpm_list_mtx);
104}
105
106/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200107 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200108 */
109void device_pm_unlock(void)
110{
111 mutex_unlock(&dpm_list_mtx);
112}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100113
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100114/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200115 * device_pm_add - Add a device to the PM core's list of active devices.
116 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100117 */
Alan Stern3b98aea2008-08-07 13:06:12 -0400118void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100121 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700122 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +0200123 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +0100124 dev_warn(dev, "parent %s should not be sleeping\n",
125 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400126 list_add_tail(&dev->power.entry, &dpm_list);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200127 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100130/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200131 * device_pm_remove - Remove a device from the PM core's list of active devices.
132 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100133 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200134void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100137 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100138 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700139 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700141 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200142 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200143 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100144}
145
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200146/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200147 * device_pm_move_before - Move device in the PM core's list of active devices.
148 * @deva: Device to move in dpm_list.
149 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100150 */
151void device_pm_move_before(struct device *deva, struct device *devb)
152{
153 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100154 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
155 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100156 /* Delete deva from dpm_list and reinsert before devb. */
157 list_move_tail(&deva->power.entry, &devb->power.entry);
158}
159
160/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200161 * device_pm_move_after - Move device in the PM core's list of active devices.
162 * @deva: Device to move in dpm_list.
163 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100164 */
165void device_pm_move_after(struct device *deva, struct device *devb)
166{
167 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100168 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
169 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100170 /* Delete deva from dpm_list and reinsert after devb. */
171 list_move(&deva->power.entry, &devb->power.entry);
172}
173
174/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200175 * device_pm_move_last - Move device to end of the PM core's list of devices.
176 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100177 */
178void device_pm_move_last(struct device *dev)
179{
180 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100181 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100182 list_move_tail(&dev->power.entry, &dpm_list);
183}
184
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100185static ktime_t initcall_debug_start(struct device *dev)
186{
187 ktime_t calltime = ktime_set(0, 0);
188
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200189 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100190 pr_info("calling %s+ @ %i, parent: %s\n",
191 dev_name(dev), task_pid_nr(current),
192 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100193 calltime = ktime_get();
194 }
195
196 return calltime;
197}
198
199static void initcall_debug_report(struct device *dev, ktime_t calltime,
Shuah Khan53644672013-07-26 13:30:20 -0600200 int error, pm_message_t state, char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100201{
Shuah Khan53644672013-07-26 13:30:20 -0600202 ktime_t rettime;
203 s64 nsecs;
204
205 rettime = ktime_get();
206 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100207
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200208 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100209 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
Shuah Khan53644672013-07-26 13:30:20 -0600210 error, (unsigned long long)nsecs >> 10);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100211 }
Shuah Khan53644672013-07-26 13:30:20 -0600212
213 trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event),
214 error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100215}
216
Cornelia Huckffa6a702009-03-04 12:44:00 +0100217/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100218 * dpm_wait - Wait for a PM operation to complete.
219 * @dev: Device to wait for.
220 * @async: If unset, wait only if the device's power.async_suspend flag is set.
221 */
222static void dpm_wait(struct device *dev, bool async)
223{
224 if (!dev)
225 return;
226
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100227 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100228 wait_for_completion(&dev->power.completion);
229}
230
231static int dpm_wait_fn(struct device *dev, void *async_ptr)
232{
233 dpm_wait(dev, *((bool *)async_ptr));
234 return 0;
235}
236
237static void dpm_wait_for_children(struct device *dev, bool async)
238{
239 device_for_each_child(dev, &async, dpm_wait_fn);
240}
241
242/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100243 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200244 * @ops: PM operations to choose from.
245 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200246 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100247static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200248{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200249 switch (state.event) {
250#ifdef CONFIG_SUSPEND
251 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100252 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200253 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100254 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200255#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200256#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200257 case PM_EVENT_FREEZE:
258 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100259 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200260 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100261 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200262 case PM_EVENT_THAW:
263 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100264 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200265 break;
266 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100267 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200268#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200269 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100270
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100271 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200272}
273
274/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100275 * pm_late_early_op - Return the PM operation appropriate for given PM event.
276 * @ops: PM operations to choose from.
277 * @state: PM transition of the system being carried out.
278 *
279 * Runtime PM is disabled for @dev while this function is being executed.
280 */
281static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
282 pm_message_t state)
283{
284 switch (state.event) {
285#ifdef CONFIG_SUSPEND
286 case PM_EVENT_SUSPEND:
287 return ops->suspend_late;
288 case PM_EVENT_RESUME:
289 return ops->resume_early;
290#endif /* CONFIG_SUSPEND */
291#ifdef CONFIG_HIBERNATE_CALLBACKS
292 case PM_EVENT_FREEZE:
293 case PM_EVENT_QUIESCE:
294 return ops->freeze_late;
295 case PM_EVENT_HIBERNATE:
296 return ops->poweroff_late;
297 case PM_EVENT_THAW:
298 case PM_EVENT_RECOVER:
299 return ops->thaw_early;
300 case PM_EVENT_RESTORE:
301 return ops->restore_early;
302#endif /* CONFIG_HIBERNATE_CALLBACKS */
303 }
304
305 return NULL;
306}
307
308/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100309 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200310 * @ops: PM operations to choose from.
311 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200312 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200313 * The driver of @dev will not receive interrupts while this function is being
314 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200315 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100316static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200317{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200318 switch (state.event) {
319#ifdef CONFIG_SUSPEND
320 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100321 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200322 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100323 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200324#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200325#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200326 case PM_EVENT_FREEZE:
327 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100328 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200329 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100330 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200331 case PM_EVENT_THAW:
332 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100333 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200334 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100335 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200336#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200337 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100338
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100339 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200340}
341
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200342static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
343{
344 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
345 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
346 ", may wakeup" : "");
347}
348
349static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
350 int error)
351{
352 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100353 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200354}
355
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100356static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
357{
358 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200359 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100360 int usecs;
361
362 calltime = ktime_get();
363 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
364 do_div(usecs64, NSEC_PER_USEC);
365 usecs = usecs64;
366 if (usecs == 0)
367 usecs = 1;
368 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
369 info ?: "", info ? " " : "", pm_verb(state.event),
370 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
371}
372
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100373static int dpm_run_callback(pm_callback_t cb, struct device *dev,
374 pm_message_t state, char *info)
375{
376 ktime_t calltime;
377 int error;
378
379 if (!cb)
380 return 0;
381
382 calltime = initcall_debug_start(dev);
383
384 pm_dev_dbg(dev, state, info);
385 error = cb(dev);
386 suspend_report_result(cb, error);
387
Shuah Khan53644672013-07-26 13:30:20 -0600388 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100389
390 return error;
391}
392
Alan Sterncd59abf2007-09-21 15:36:56 -0400393/*------------------------- Resume routines -------------------------*/
394
395/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200396 * device_resume_noirq - Execute an "early resume" callback for given device.
397 * @dev: Device to handle.
398 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400399 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200400 * The driver of @dev will not receive interrupts while this function is being
401 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400402 */
Alan Sternd1616302009-05-24 22:05:42 +0200403static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400404{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100405 pm_callback_t callback = NULL;
406 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400407 int error = 0;
408
409 TRACE_DEVICE(dev);
410 TRACE_RESUME(0);
411
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200412 if (dev->power.syscore)
413 goto Out;
414
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200415 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100416 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100417 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200418 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100419 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100420 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100421 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100422 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100423 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100424 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100425 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100426 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100427 }
428
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100429 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100430 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100431 callback = pm_noirq_op(dev->driver->pm, state);
432 }
433
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100434 error = dpm_run_callback(callback, dev, state, info);
435
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200436 Out:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100437 TRACE_RESUME(error);
438 return error;
439}
440
441/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100442 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200443 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100444 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100445 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200446 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100447 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100448static void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100449{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100450 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100451
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200452 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100453 while (!list_empty(&dpm_noirq_list)) {
454 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100455 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100456
457 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100458 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100459 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100460
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100461 error = device_resume_noirq(dev, state);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200462 if (error) {
463 suspend_stats.failed_resume_noirq++;
464 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
465 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100466 pm_dev_err(dev, state, " noirq", error);
467 }
468
469 mutex_lock(&dpm_list_mtx);
470 put_device(dev);
471 }
472 mutex_unlock(&dpm_list_mtx);
473 dpm_show_time(starttime, state, "noirq");
474 resume_device_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200475 cpuidle_resume();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100476}
477
478/**
479 * device_resume_early - Execute an "early resume" callback for given device.
480 * @dev: Device to handle.
481 * @state: PM transition of the system being carried out.
482 *
483 * Runtime PM is disabled for @dev while this function is being executed.
484 */
485static int device_resume_early(struct device *dev, pm_message_t state)
486{
487 pm_callback_t callback = NULL;
488 char *info = NULL;
489 int error = 0;
490
491 TRACE_DEVICE(dev);
492 TRACE_RESUME(0);
493
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200494 if (dev->power.syscore)
495 goto Out;
496
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100497 if (dev->pm_domain) {
498 info = "early power domain ";
499 callback = pm_late_early_op(&dev->pm_domain->ops, state);
500 } else if (dev->type && dev->type->pm) {
501 info = "early type ";
502 callback = pm_late_early_op(dev->type->pm, state);
503 } else if (dev->class && dev->class->pm) {
504 info = "early class ";
505 callback = pm_late_early_op(dev->class->pm, state);
506 } else if (dev->bus && dev->bus->pm) {
507 info = "early bus ";
508 callback = pm_late_early_op(dev->bus->pm, state);
509 }
510
511 if (!callback && dev->driver && dev->driver->pm) {
512 info = "early driver ";
513 callback = pm_late_early_op(dev->driver->pm, state);
514 }
515
516 error = dpm_run_callback(callback, dev, state, info);
517
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200518 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100519 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100520
521 pm_runtime_enable(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100522 return error;
523}
524
525/**
526 * dpm_resume_early - Execute "early resume" callbacks for all devices.
527 * @state: PM transition of the system being carried out.
528 */
529static void dpm_resume_early(pm_message_t state)
530{
531 ktime_t starttime = ktime_get();
532
533 mutex_lock(&dpm_list_mtx);
534 while (!list_empty(&dpm_late_early_list)) {
535 struct device *dev = to_device(dpm_late_early_list.next);
536 int error;
537
538 get_device(dev);
539 list_move_tail(&dev->power.entry, &dpm_suspended_list);
540 mutex_unlock(&dpm_list_mtx);
541
542 error = device_resume_early(dev, state);
543 if (error) {
544 suspend_stats.failed_resume_early++;
545 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
546 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100547 pm_dev_err(dev, state, " early", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200548 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100549
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100550 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100551 put_device(dev);
552 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200553 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100554 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100555}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100556
557/**
558 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
559 * @state: PM transition of the system being carried out.
560 */
561void dpm_resume_start(pm_message_t state)
562{
563 dpm_resume_noirq(state);
564 dpm_resume_early(state);
565}
566EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100567
568/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100569 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200570 * @dev: Device to handle.
571 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100572 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100573 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100574static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100575{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100576 pm_callback_t callback = NULL;
577 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100578 int error = 0;
579
580 TRACE_DEVICE(dev);
581 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400582
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200583 if (dev->power.syscore)
584 goto Complete;
585
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100586 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800587 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100588
Alan Sternf76b168b2011-06-18 20:22:23 +0200589 /*
590 * This is a fib. But we'll allow new children to be added below
591 * a resumed device, even if the device hasn't been completed yet.
592 */
593 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100594
Alan Stern6d0e0e82011-06-18 22:42:09 +0200595 if (!dev->power.is_suspended)
596 goto Unlock;
597
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200598 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100599 info = "power domain ";
600 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100601 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100602 }
603
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100604 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100605 info = "type ";
606 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100607 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100608 }
609
610 if (dev->class) {
611 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100612 info = "class ";
613 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100614 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100615 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100616 info = "legacy class ";
617 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100618 goto End;
619 }
620 }
621
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200622 if (dev->bus) {
623 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100624 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100625 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100627 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100628 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100629 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200630 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400631 }
632
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100633 Driver:
634 if (!callback && dev->driver && dev->driver->pm) {
635 info = "driver ";
636 callback = pm_op(dev->driver->pm, state);
637 }
638
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200639 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100640 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200641 dev->power.is_suspended = false;
642
643 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800644 device_unlock(dev);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200645
646 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100647 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100648
Alan Sterncd59abf2007-09-21 15:36:56 -0400649 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200650
Alan Sterncd59abf2007-09-21 15:36:56 -0400651 return error;
652}
653
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100654static void async_resume(void *data, async_cookie_t cookie)
655{
656 struct device *dev = (struct device *)data;
657 int error;
658
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100659 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100660 if (error)
661 pm_dev_err(dev, pm_transition, " async", error);
662 put_device(dev);
663}
664
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100665static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100666{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100667 return dev->power.async_suspend && pm_async_enabled
668 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100669}
670
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100671/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200672 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
673 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100674 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200675 * Execute the appropriate "resume" callback for all devices whose status
676 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400677 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200678void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400679{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100680 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100681 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400682
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200683 might_sleep();
684
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200685 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100686 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200687 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200688
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100689 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100690 INIT_COMPLETION(dev->power.completion);
691 if (is_async(dev)) {
692 get_device(dev);
693 async_schedule(async_resume, dev);
694 }
695 }
696
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100697 while (!list_empty(&dpm_suspended_list)) {
698 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200699 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100700 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200701 int error;
702
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200703 mutex_unlock(&dpm_list_mtx);
704
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100705 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200706 if (error) {
707 suspend_stats.failed_resume++;
708 dpm_save_failed_step(SUSPEND_RESUME);
709 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200710 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200711 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100712
713 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200714 }
715 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100716 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200717 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400718 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200719 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100720 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100721 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200722}
723
724/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200725 * device_complete - Complete a PM transition for given device.
726 * @dev: Device to handle.
727 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200728 */
Alan Sternd1616302009-05-24 22:05:42 +0200729static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200730{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100731 void (*callback)(struct device *) = NULL;
732 char *info = NULL;
733
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200734 if (dev->power.syscore)
735 return;
736
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800737 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200738
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200739 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100740 info = "completing power domain ";
741 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200742 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100743 info = "completing type ";
744 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100745 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100746 info = "completing class ";
747 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100748 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100749 info = "completing bus ";
750 callback = dev->bus->pm->complete;
751 }
752
753 if (!callback && dev->driver && dev->driver->pm) {
754 info = "completing driver ";
755 callback = dev->driver->pm->complete;
756 }
757
758 if (callback) {
759 pm_dev_dbg(dev, state, info);
760 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200761 }
762
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800763 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200764
Ulf Hanssonaf939332013-04-12 09:41:06 +0000765 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200766}
767
768/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200769 * dpm_complete - Complete a PM transition for all non-sysdev devices.
770 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200771 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200772 * Execute the ->complete() callbacks for all devices whose PM status is not
773 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200774 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200775void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200776{
777 struct list_head list;
778
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200779 might_sleep();
780
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200781 INIT_LIST_HEAD(&list);
782 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100783 while (!list_empty(&dpm_prepared_list)) {
784 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200785
786 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200787 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100788 list_move(&dev->power.entry, &list);
789 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200790
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100791 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200792
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100793 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200794 put_device(dev);
795 }
796 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400797 mutex_unlock(&dpm_list_mtx);
798}
799
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100800/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200801 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
802 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400803 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200804 * Execute "resume" callbacks for all devices and complete the PM transition of
805 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400806 */
Alan Sternd1616302009-05-24 22:05:42 +0200807void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400808{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200809 dpm_resume(state);
810 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400811}
Alan Sternd1616302009-05-24 22:05:42 +0200812EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400813
814
Alan Sterncd59abf2007-09-21 15:36:56 -0400815/*------------------------- Suspend routines -------------------------*/
816
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200817/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200818 * resume_event - Return a "resume" message for given "suspend" sleep state.
819 * @sleep_state: PM message representing a sleep state.
820 *
821 * Return a PM message representing the resume event corresponding to given
822 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200823 */
824static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400825{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200826 switch (sleep_state.event) {
827 case PM_EVENT_SUSPEND:
828 return PMSG_RESUME;
829 case PM_EVENT_FREEZE:
830 case PM_EVENT_QUIESCE:
831 return PMSG_RECOVER;
832 case PM_EVENT_HIBERNATE:
833 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400834 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200835 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400836}
837
838/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200839 * device_suspend_noirq - Execute a "late suspend" callback for given device.
840 * @dev: Device to handle.
841 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100842 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200843 * The driver of @dev will not receive interrupts while this function is being
844 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100845 */
Alan Sternd1616302009-05-24 22:05:42 +0200846static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100847{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100848 pm_callback_t callback = NULL;
849 char *info = NULL;
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100850
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200851 if (dev->power.syscore)
852 return 0;
853
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200854 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100855 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100856 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200857 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100858 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100859 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100860 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100861 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100862 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100863 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100864 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100865 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100866 }
867
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100868 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100869 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100870 callback = pm_noirq_op(dev->driver->pm, state);
871 }
872
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100873 return dpm_run_callback(callback, dev, state, info);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100874}
875
876/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100877 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200878 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100879 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200880 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
881 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100882 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100883static int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100884{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100885 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100886 int error = 0;
887
Preeti U Murthy8651f972012-07-09 10:12:56 +0200888 cpuidle_pause();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100889 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200890 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100891 while (!list_empty(&dpm_late_early_list)) {
892 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100893
894 get_device(dev);
895 mutex_unlock(&dpm_list_mtx);
896
Alan Sternd1616302009-05-24 22:05:42 +0200897 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100898
899 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100900 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100901 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200902 suspend_stats.failed_suspend_noirq++;
903 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
904 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100905 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100906 break;
907 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100908 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100909 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100910 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200911
912 if (pm_wakeup_pending()) {
913 error = -EBUSY;
914 break;
915 }
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100916 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200917 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100918 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200919 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100920 else
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100921 dpm_show_time(starttime, state, "noirq");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100922 return error;
923}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100924
925/**
926 * device_suspend_late - Execute a "late suspend" callback for given device.
927 * @dev: Device to handle.
928 * @state: PM transition of the system being carried out.
929 *
930 * Runtime PM is disabled for @dev while this function is being executed.
931 */
932static int device_suspend_late(struct device *dev, pm_message_t state)
933{
934 pm_callback_t callback = NULL;
935 char *info = NULL;
936
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100937 __pm_runtime_disable(dev, false);
938
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200939 if (dev->power.syscore)
940 return 0;
941
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100942 if (dev->pm_domain) {
943 info = "late power domain ";
944 callback = pm_late_early_op(&dev->pm_domain->ops, state);
945 } else if (dev->type && dev->type->pm) {
946 info = "late type ";
947 callback = pm_late_early_op(dev->type->pm, state);
948 } else if (dev->class && dev->class->pm) {
949 info = "late class ";
950 callback = pm_late_early_op(dev->class->pm, state);
951 } else if (dev->bus && dev->bus->pm) {
952 info = "late bus ";
953 callback = pm_late_early_op(dev->bus->pm, state);
954 }
955
956 if (!callback && dev->driver && dev->driver->pm) {
957 info = "late driver ";
958 callback = pm_late_early_op(dev->driver->pm, state);
959 }
960
961 return dpm_run_callback(callback, dev, state, info);
962}
963
964/**
965 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
966 * @state: PM transition of the system being carried out.
967 */
968static int dpm_suspend_late(pm_message_t state)
969{
970 ktime_t starttime = ktime_get();
971 int error = 0;
972
973 mutex_lock(&dpm_list_mtx);
974 while (!list_empty(&dpm_suspended_list)) {
975 struct device *dev = to_device(dpm_suspended_list.prev);
976
977 get_device(dev);
978 mutex_unlock(&dpm_list_mtx);
979
980 error = device_suspend_late(dev, state);
981
982 mutex_lock(&dpm_list_mtx);
983 if (error) {
984 pm_dev_err(dev, state, " late", error);
985 suspend_stats.failed_suspend_late++;
986 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
987 dpm_save_failed_dev(dev_name(dev));
988 put_device(dev);
989 break;
990 }
991 if (!list_empty(&dev->power.entry))
992 list_move(&dev->power.entry, &dpm_late_early_list);
993 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200994
995 if (pm_wakeup_pending()) {
996 error = -EBUSY;
997 break;
998 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100999 }
1000 mutex_unlock(&dpm_list_mtx);
1001 if (error)
1002 dpm_resume_early(resume_event(state));
1003 else
1004 dpm_show_time(starttime, state, "late");
1005
1006 return error;
1007}
1008
1009/**
1010 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1011 * @state: PM transition of the system being carried out.
1012 */
1013int dpm_suspend_end(pm_message_t state)
1014{
1015 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001016 if (error)
1017 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001018
Colin Cross064b0212012-07-19 10:38:06 +02001019 error = dpm_suspend_noirq(state);
1020 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001021 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001022 return error;
1023 }
1024
1025 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001026}
1027EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001028
1029/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001030 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001031 * @dev: Device to suspend.
1032 * @state: PM transition of the system being carried out.
1033 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001034 */
1035static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001036 int (*cb)(struct device *dev, pm_message_t state),
1037 char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001038{
1039 int error;
1040 ktime_t calltime;
1041
1042 calltime = initcall_debug_start(dev);
1043
1044 error = cb(dev, state);
1045 suspend_report_result(cb, error);
1046
Shuah Khan53644672013-07-26 13:30:20 -06001047 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001048
1049 return error;
1050}
1051
1052/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001053 * device_suspend - Execute "suspend" callbacks for given device.
1054 * @dev: Device to handle.
1055 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001056 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001057 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001058static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001059{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001060 pm_callback_t callback = NULL;
1061 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001062 int error = 0;
1063
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001064 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001065
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001066 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001067 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001068
Alan Stern88d26132012-09-19 21:59:02 +02001069 /*
1070 * If a device configured to wake up the system from sleep states
1071 * has been suspended at run time and there's a resume request pending
1072 * for it, this is equivalent to the device signaling wakeup, so the
1073 * system suspend operation should be aborted.
1074 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001075 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1076 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001077
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001078 if (pm_wakeup_pending()) {
1079 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001080 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001081 }
1082
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001083 if (dev->power.syscore)
1084 goto Complete;
1085
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001086 device_lock(dev);
1087
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001088 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001089 info = "power domain ";
1090 callback = pm_op(&dev->pm_domain->ops, state);
1091 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001092 }
1093
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001094 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001095 info = "type ";
1096 callback = pm_op(dev->type->pm, state);
1097 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001098 }
1099
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001100 if (dev->class) {
1101 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001102 info = "class ";
1103 callback = pm_op(dev->class->pm, state);
1104 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001105 } else if (dev->class->suspend) {
1106 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001107 error = legacy_suspend(dev, state, dev->class->suspend,
1108 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001109 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001110 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001111 }
1112
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001113 if (dev->bus) {
1114 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001115 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001116 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001117 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001118 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001119 error = legacy_suspend(dev, state, dev->bus->suspend,
1120 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001121 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001122 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001123 }
1124
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001125 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001126 if (!callback && dev->driver && dev->driver->pm) {
1127 info = "driver ";
1128 callback = pm_op(dev->driver->pm, state);
1129 }
1130
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001131 error = dpm_run_callback(callback, dev, state, info);
1132
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001133 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001134 if (!error) {
1135 dev->power.is_suspended = true;
Rafael J. Wysocki8b258cc2011-11-17 21:39:33 +01001136 if (dev->power.wakeup_path
1137 && dev->parent && !dev->parent->power.ignore_children)
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001138 dev->parent->power.wakeup_path = true;
1139 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001140
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001141 device_unlock(dev);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001142
1143 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001144 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001145 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001146 async_error = error;
1147
Alan Sterncd59abf2007-09-21 15:36:56 -04001148 return error;
1149}
1150
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001151static void async_suspend(void *data, async_cookie_t cookie)
1152{
1153 struct device *dev = (struct device *)data;
1154 int error;
1155
1156 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001157 if (error) {
1158 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001159 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001160 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001161
1162 put_device(dev);
1163}
1164
1165static int device_suspend(struct device *dev)
1166{
1167 INIT_COMPLETION(dev->power.completion);
1168
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +01001169 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001170 get_device(dev);
1171 async_schedule(async_suspend, dev);
1172 return 0;
1173 }
1174
1175 return __device_suspend(dev, pm_transition, false);
1176}
1177
Alan Sterncd59abf2007-09-21 15:36:56 -04001178/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001179 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1180 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001181 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001182int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001183{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001184 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001185 int error = 0;
1186
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001187 might_sleep();
1188
Alan Sterncd59abf2007-09-21 15:36:56 -04001189 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001190 pm_transition = state;
1191 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001192 while (!list_empty(&dpm_prepared_list)) {
1193 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001194
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001195 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001196 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001197
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001198 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001199
Alan Stern1b3cbec2008-02-29 11:50:22 -05001200 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001201 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001202 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001203 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001204 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001205 break;
1206 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001207 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001208 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001209 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001210 if (async_error)
1211 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001212 }
1213 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001214 async_synchronize_full();
1215 if (!error)
1216 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001217 if (error) {
1218 suspend_stats.failed_suspend++;
1219 dpm_save_failed_step(SUSPEND_SUSPEND);
1220 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001221 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001222 return error;
1223}
1224
1225/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001226 * device_prepare - Prepare a device for system power transition.
1227 * @dev: Device to handle.
1228 * @state: PM transition of the system being carried out.
1229 *
1230 * Execute the ->prepare() callback(s) for given device. No new children of the
1231 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001232 */
Alan Sternd1616302009-05-24 22:05:42 +02001233static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001234{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001235 int (*callback)(struct device *) = NULL;
1236 char *info = NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001237 int error = 0;
1238
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001239 if (dev->power.syscore)
1240 return 0;
1241
Alan Stern88d26132012-09-19 21:59:02 +02001242 /*
1243 * If a device's parent goes into runtime suspend at the wrong time,
1244 * it won't be possible to resume the device. To prevent this we
1245 * block runtime suspend here, during the prepare phase, and allow
1246 * it again during the complete phase.
1247 */
1248 pm_runtime_get_noresume(dev);
1249
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001250 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001251
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001252 dev->power.wakeup_path = device_may_wakeup(dev);
1253
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001254 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001255 info = "preparing power domain ";
1256 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001257 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001258 info = "preparing type ";
1259 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001260 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001261 info = "preparing class ";
1262 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001263 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001264 info = "preparing bus ";
1265 callback = dev->bus->pm->prepare;
1266 }
1267
1268 if (!callback && dev->driver && dev->driver->pm) {
1269 info = "preparing driver ";
1270 callback = dev->driver->pm->prepare;
1271 }
1272
1273 if (callback) {
1274 error = callback(dev);
1275 suspend_report_result(callback, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001276 }
1277
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001278 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001279
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001280 if (error)
1281 pm_runtime_put(dev);
1282
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001283 return error;
1284}
1285
1286/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001287 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1288 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001289 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001290 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001291 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001292int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001293{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001294 int error = 0;
1295
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001296 might_sleep();
1297
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001298 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001299 while (!list_empty(&dpm_list)) {
1300 struct device *dev = to_device(dpm_list.next);
1301
1302 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001303 mutex_unlock(&dpm_list_mtx);
1304
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001305 error = device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001306
1307 mutex_lock(&dpm_list_mtx);
1308 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001309 if (error == -EAGAIN) {
1310 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001311 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001312 continue;
1313 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001314 printk(KERN_INFO "PM: Device %s not prepared "
1315 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001316 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001317 put_device(dev);
1318 break;
1319 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001320 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001321 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001322 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001323 put_device(dev);
1324 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001325 mutex_unlock(&dpm_list_mtx);
1326 return error;
1327}
1328
1329/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001330 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1331 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001332 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001333 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1334 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001335 */
Alan Sternd1616302009-05-24 22:05:42 +02001336int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001337{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001338 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001339
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001340 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001341 if (error) {
1342 suspend_stats.failed_prepare++;
1343 dpm_save_failed_step(SUSPEND_PREPARE);
1344 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001345 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001346 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001347}
Alan Sternd1616302009-05-24 22:05:42 +02001348EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001349
1350void __suspend_report_result(const char *function, void *fn, int ret)
1351{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001352 if (ret)
1353 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001354}
1355EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001356
1357/**
1358 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1359 * @dev: Device to wait for.
1360 * @subordinate: Device that needs to wait for @dev.
1361 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001362int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001363{
1364 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001365 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001366}
1367EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001368
1369/**
1370 * dpm_for_each_dev - device iterator.
1371 * @data: data for the callback.
1372 * @fn: function to be called for each device.
1373 *
1374 * Iterate over devices in dpm_list, and call @fn for each device,
1375 * passing it @data.
1376 */
1377void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1378{
1379 struct device *dev;
1380
1381 if (!fn)
1382 return;
1383
1384 device_pm_lock();
1385 list_for_each_entry(dev, &dpm_list, power.entry)
1386 fn(dev, data);
1387 device_pm_unlock();
1388}
1389EXPORT_SYMBOL_GPL(dpm_for_each_dev);