blob: de3fe4fe350ad576b873945c2a4a13b191fa0994 [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. Wysocki2ed8d2b32009-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>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080032#include <linux/cpufreq.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020033#include <linux/cpuidle.h>
Benoit Goby70fea602013-10-17 10:48:46 -070034#include <linux/timer.h>
35
Alan Sterncd59abf2007-09-21 15:36:56 -040036#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "power.h"
38
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010039typedef int (*pm_callback_t)(struct device *);
40
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010041/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020042 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010043 * because children are guaranteed to be discovered after parents, and
44 * are inserted at the back of the list on discovery.
45 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080046 * Since device_pm_add() may be called with a device lock held,
47 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010048 * dpm_list_mutex.
49 */
50
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020051LIST_HEAD(dpm_list);
Sachin Kamat7664e962012-07-17 22:38:08 +020052static LIST_HEAD(dpm_prepared_list);
53static LIST_HEAD(dpm_suspended_list);
54static LIST_HEAD(dpm_late_early_list);
55static LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
ShuoX Liu2a77c462011-08-10 23:01:26 +020057struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040058static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010059static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020061static int async_error;
62
Shuah Khan53644672013-07-26 13:30:20 -060063static char *pm_verb(int event)
64{
65 switch (event) {
66 case PM_EVENT_SUSPEND:
67 return "suspend";
68 case PM_EVENT_RESUME:
69 return "resume";
70 case PM_EVENT_FREEZE:
71 return "freeze";
72 case PM_EVENT_QUIESCE:
73 return "quiesce";
74 case PM_EVENT_HIBERNATE:
75 return "hibernate";
76 case PM_EVENT_THAW:
77 return "thaw";
78 case PM_EVENT_RESTORE:
79 return "restore";
80 case PM_EVENT_RECOVER:
81 return "recover";
82 default:
83 return "(unknown PM event)";
84 }
85}
86
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020087/**
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020088 * device_pm_sleep_init - Initialize system suspend-related device fields.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020089 * @dev: Device object being initialized.
90 */
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020091void device_pm_sleep_init(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020092{
Alan Sternf76b168b2011-06-18 20:22:23 +020093 dev->power.is_prepared = false;
Alan Stern6d0e0e82011-06-18 22:42:09 +020094 dev->power.is_suspended = false;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +080095 dev->power.is_noirq_suspended = false;
96 dev->power.is_late_suspended = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010097 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020098 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020099 dev->power.wakeup = NULL;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200100 INIT_LIST_HEAD(&dev->power.entry);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200101}
102
103/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200104 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200105 */
106void device_pm_lock(void)
107{
108 mutex_lock(&dpm_list_mtx);
109}
110
111/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200112 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200113 */
114void device_pm_unlock(void)
115{
116 mutex_unlock(&dpm_list_mtx);
117}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100118
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100119/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200120 * device_pm_add - Add a device to the PM core's list of active devices.
121 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100122 */
Alan Stern3b98aea2008-08-07 13:06:12 -0400123void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100126 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700127 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +0200128 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +0100129 dev_warn(dev, "parent %s should not be sleeping\n",
130 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400131 list_add_tail(&dev->power.entry, &dpm_list);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200132 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100135/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200136 * device_pm_remove - Remove a device from the PM core's list of active devices.
137 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100138 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200139void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100142 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100143 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700144 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700146 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200147 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200148 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100149}
150
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200151/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200152 * device_pm_move_before - Move device in the PM core's list of active devices.
153 * @deva: Device to move in dpm_list.
154 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100155 */
156void device_pm_move_before(struct device *deva, struct device *devb)
157{
158 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100159 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
160 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100161 /* Delete deva from dpm_list and reinsert before devb. */
162 list_move_tail(&deva->power.entry, &devb->power.entry);
163}
164
165/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200166 * device_pm_move_after - Move device in the PM core's list of active devices.
167 * @deva: Device to move in dpm_list.
168 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100169 */
170void device_pm_move_after(struct device *deva, struct device *devb)
171{
172 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100173 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
174 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100175 /* Delete deva from dpm_list and reinsert after devb. */
176 list_move(&deva->power.entry, &devb->power.entry);
177}
178
179/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200180 * device_pm_move_last - Move device to end of the PM core's list of devices.
181 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100182 */
183void device_pm_move_last(struct device *dev)
184{
185 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100186 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100187 list_move_tail(&dev->power.entry, &dpm_list);
188}
189
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100190static ktime_t initcall_debug_start(struct device *dev)
191{
192 ktime_t calltime = ktime_set(0, 0);
193
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200194 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100195 pr_info("calling %s+ @ %i, parent: %s\n",
196 dev_name(dev), task_pid_nr(current),
197 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100198 calltime = ktime_get();
199 }
200
201 return calltime;
202}
203
204static void initcall_debug_report(struct device *dev, ktime_t calltime,
Shuah Khan53644672013-07-26 13:30:20 -0600205 int error, pm_message_t state, char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100206{
Shuah Khan53644672013-07-26 13:30:20 -0600207 ktime_t rettime;
208 s64 nsecs;
209
210 rettime = ktime_get();
211 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100212
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200213 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100214 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
Shuah Khan53644672013-07-26 13:30:20 -0600215 error, (unsigned long long)nsecs >> 10);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100216 }
Shuah Khan53644672013-07-26 13:30:20 -0600217
218 trace_device_pm_report_time(dev, info, nsecs, pm_verb(state.event),
219 error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100220}
221
Cornelia Huckffa6a702009-03-04 12:44:00 +0100222/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100223 * dpm_wait - Wait for a PM operation to complete.
224 * @dev: Device to wait for.
225 * @async: If unset, wait only if the device's power.async_suspend flag is set.
226 */
227static void dpm_wait(struct device *dev, bool async)
228{
229 if (!dev)
230 return;
231
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100232 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100233 wait_for_completion(&dev->power.completion);
234}
235
236static int dpm_wait_fn(struct device *dev, void *async_ptr)
237{
238 dpm_wait(dev, *((bool *)async_ptr));
239 return 0;
240}
241
242static void dpm_wait_for_children(struct device *dev, bool async)
243{
244 device_for_each_child(dev, &async, dpm_wait_fn);
245}
246
247/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100248 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200249 * @ops: PM operations to choose from.
250 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200251 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100252static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200253{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200254 switch (state.event) {
255#ifdef CONFIG_SUSPEND
256 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100257 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200258 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100259 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200260#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200261#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200262 case PM_EVENT_FREEZE:
263 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100264 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200265 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100266 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200267 case PM_EVENT_THAW:
268 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100269 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200270 break;
271 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100272 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200273#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200274 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100275
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100276 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200277}
278
279/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100280 * pm_late_early_op - Return the PM operation appropriate for given PM event.
281 * @ops: PM operations to choose from.
282 * @state: PM transition of the system being carried out.
283 *
284 * Runtime PM is disabled for @dev while this function is being executed.
285 */
286static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
287 pm_message_t state)
288{
289 switch (state.event) {
290#ifdef CONFIG_SUSPEND
291 case PM_EVENT_SUSPEND:
292 return ops->suspend_late;
293 case PM_EVENT_RESUME:
294 return ops->resume_early;
295#endif /* CONFIG_SUSPEND */
296#ifdef CONFIG_HIBERNATE_CALLBACKS
297 case PM_EVENT_FREEZE:
298 case PM_EVENT_QUIESCE:
299 return ops->freeze_late;
300 case PM_EVENT_HIBERNATE:
301 return ops->poweroff_late;
302 case PM_EVENT_THAW:
303 case PM_EVENT_RECOVER:
304 return ops->thaw_early;
305 case PM_EVENT_RESTORE:
306 return ops->restore_early;
307#endif /* CONFIG_HIBERNATE_CALLBACKS */
308 }
309
310 return NULL;
311}
312
313/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100314 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200315 * @ops: PM operations to choose from.
316 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200317 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200318 * The driver of @dev will not receive interrupts while this function is being
319 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200320 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100321static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200322{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200323 switch (state.event) {
324#ifdef CONFIG_SUSPEND
325 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100326 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200327 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100328 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200329#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200330#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200331 case PM_EVENT_FREEZE:
332 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100333 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200334 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100335 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200336 case PM_EVENT_THAW:
337 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100338 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200339 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100340 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200341#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200342 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100343
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100344 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200345}
346
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200347static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
348{
349 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
350 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
351 ", may wakeup" : "");
352}
353
354static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
355 int error)
356{
357 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100358 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200359}
360
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100361static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
362{
363 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200364 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100365 int usecs;
366
367 calltime = ktime_get();
368 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
369 do_div(usecs64, NSEC_PER_USEC);
370 usecs = usecs64;
371 if (usecs == 0)
372 usecs = 1;
373 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
374 info ?: "", info ? " " : "", pm_verb(state.event),
375 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
376}
377
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100378static int dpm_run_callback(pm_callback_t cb, struct device *dev,
379 pm_message_t state, char *info)
380{
381 ktime_t calltime;
382 int error;
383
384 if (!cb)
385 return 0;
386
387 calltime = initcall_debug_start(dev);
388
389 pm_dev_dbg(dev, state, info);
390 error = cb(dev);
391 suspend_report_result(cb, error);
392
Shuah Khan53644672013-07-26 13:30:20 -0600393 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100394
395 return error;
396}
397
Benoit Goby70fea602013-10-17 10:48:46 -0700398#ifdef CONFIG_DPM_WATCHDOG
399struct dpm_watchdog {
400 struct device *dev;
401 struct task_struct *tsk;
402 struct timer_list timer;
403};
404
405#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
406 struct dpm_watchdog wd
407
408/**
409 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
410 * @data: Watchdog object address.
411 *
412 * Called when a driver has timed out suspending or resuming.
413 * There's not much we can do here to recover so panic() to
414 * capture a crash-dump in pstore.
415 */
416static void dpm_watchdog_handler(unsigned long data)
417{
418 struct dpm_watchdog *wd = (void *)data;
419
420 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
421 show_stack(wd->tsk, NULL);
422 panic("%s %s: unrecoverable failure\n",
423 dev_driver_string(wd->dev), dev_name(wd->dev));
424}
425
426/**
427 * dpm_watchdog_set - Enable pm watchdog for given device.
428 * @wd: Watchdog. Must be allocated on the stack.
429 * @dev: Device to handle.
430 */
431static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
432{
433 struct timer_list *timer = &wd->timer;
434
435 wd->dev = dev;
436 wd->tsk = current;
437
438 init_timer_on_stack(timer);
439 /* use same timeout value for both suspend and resume */
440 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
441 timer->function = dpm_watchdog_handler;
442 timer->data = (unsigned long)wd;
443 add_timer(timer);
444}
445
446/**
447 * dpm_watchdog_clear - Disable suspend/resume watchdog.
448 * @wd: Watchdog to disable.
449 */
450static void dpm_watchdog_clear(struct dpm_watchdog *wd)
451{
452 struct timer_list *timer = &wd->timer;
453
454 del_timer_sync(timer);
455 destroy_timer_on_stack(timer);
456}
457#else
458#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
459#define dpm_watchdog_set(x, y)
460#define dpm_watchdog_clear(x)
461#endif
462
Alan Sterncd59abf2007-09-21 15:36:56 -0400463/*------------------------- Resume routines -------------------------*/
464
465/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200466 * device_resume_noirq - Execute an "early resume" callback for given device.
467 * @dev: Device to handle.
468 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400469 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200470 * The driver of @dev will not receive interrupts while this function is being
471 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400472 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800473static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400474{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100475 pm_callback_t callback = NULL;
476 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400477 int error = 0;
478
479 TRACE_DEVICE(dev);
480 TRACE_RESUME(0);
481
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200482 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200483 goto Out;
484
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800485 if (!dev->power.is_noirq_suspended)
486 goto Out;
487
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800488 dpm_wait(dev->parent, async);
489
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200490 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100491 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100492 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200493 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100494 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100495 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100496 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100497 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100498 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100499 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100500 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100501 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100502 }
503
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100504 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100505 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100506 callback = pm_noirq_op(dev->driver->pm, state);
507 }
508
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100509 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800510 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100511
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200512 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800513 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100514 TRACE_RESUME(error);
515 return error;
516}
517
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800518static bool is_async(struct device *dev)
519{
520 return dev->power.async_suspend && pm_async_enabled
521 && !pm_trace_is_enabled();
522}
523
524static void async_resume_noirq(void *data, async_cookie_t cookie)
525{
526 struct device *dev = (struct device *)data;
527 int error;
528
529 error = device_resume_noirq(dev, pm_transition, true);
530 if (error)
531 pm_dev_err(dev, pm_transition, " async", error);
532
533 put_device(dev);
534}
535
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100536/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100537 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200538 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100539 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100540 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200541 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100542 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100543static void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100544{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800545 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100546 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100547
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700548 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200549 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800550 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100551
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800552 /*
553 * Advanced the async threads upfront,
554 * in case the starting of async threads is
555 * delayed by non-async resuming devices.
556 */
557 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
558 reinit_completion(&dev->power.completion);
559 if (is_async(dev)) {
560 get_device(dev);
561 async_schedule(async_resume_noirq, dev);
562 }
563 }
564
565 while (!list_empty(&dpm_noirq_list)) {
566 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100567 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100568 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100569 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100570
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800571 if (!is_async(dev)) {
572 int error;
573
574 error = device_resume_noirq(dev, state, false);
575 if (error) {
576 suspend_stats.failed_resume_noirq++;
577 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
578 dpm_save_failed_dev(dev_name(dev));
579 pm_dev_err(dev, state, " noirq", error);
580 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100581 }
582
583 mutex_lock(&dpm_list_mtx);
584 put_device(dev);
585 }
586 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800587 async_synchronize_full();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100588 dpm_show_time(starttime, state, "noirq");
589 resume_device_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200590 cpuidle_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700591 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100592}
593
594/**
595 * device_resume_early - Execute an "early resume" callback for given device.
596 * @dev: Device to handle.
597 * @state: PM transition of the system being carried out.
598 *
599 * Runtime PM is disabled for @dev while this function is being executed.
600 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800601static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100602{
603 pm_callback_t callback = NULL;
604 char *info = NULL;
605 int error = 0;
606
607 TRACE_DEVICE(dev);
608 TRACE_RESUME(0);
609
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200610 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200611 goto Out;
612
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800613 if (!dev->power.is_late_suspended)
614 goto Out;
615
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800616 dpm_wait(dev->parent, async);
617
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100618 if (dev->pm_domain) {
619 info = "early power domain ";
620 callback = pm_late_early_op(&dev->pm_domain->ops, state);
621 } else if (dev->type && dev->type->pm) {
622 info = "early type ";
623 callback = pm_late_early_op(dev->type->pm, state);
624 } else if (dev->class && dev->class->pm) {
625 info = "early class ";
626 callback = pm_late_early_op(dev->class->pm, state);
627 } else if (dev->bus && dev->bus->pm) {
628 info = "early bus ";
629 callback = pm_late_early_op(dev->bus->pm, state);
630 }
631
632 if (!callback && dev->driver && dev->driver->pm) {
633 info = "early driver ";
634 callback = pm_late_early_op(dev->driver->pm, state);
635 }
636
637 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800638 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100639
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200640 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100641 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100642
643 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800644 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100645 return error;
646}
647
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800648static void async_resume_early(void *data, async_cookie_t cookie)
649{
650 struct device *dev = (struct device *)data;
651 int error;
652
653 error = device_resume_early(dev, pm_transition, true);
654 if (error)
655 pm_dev_err(dev, pm_transition, " async", error);
656
657 put_device(dev);
658}
659
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100660/**
661 * dpm_resume_early - Execute "early resume" callbacks for all devices.
662 * @state: PM transition of the system being carried out.
663 */
664static void dpm_resume_early(pm_message_t state)
665{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800666 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100667 ktime_t starttime = ktime_get();
668
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700669 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100670 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800671 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100672
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800673 /*
674 * Advanced the async threads upfront,
675 * in case the starting of async threads is
676 * delayed by non-async resuming devices.
677 */
678 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
679 reinit_completion(&dev->power.completion);
680 if (is_async(dev)) {
681 get_device(dev);
682 async_schedule(async_resume_early, dev);
683 }
684 }
685
686 while (!list_empty(&dpm_late_early_list)) {
687 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100688 get_device(dev);
689 list_move_tail(&dev->power.entry, &dpm_suspended_list);
690 mutex_unlock(&dpm_list_mtx);
691
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800692 if (!is_async(dev)) {
693 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100694
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800695 error = device_resume_early(dev, state, false);
696 if (error) {
697 suspend_stats.failed_resume_early++;
698 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
699 dpm_save_failed_dev(dev_name(dev));
700 pm_dev_err(dev, state, " early", error);
701 }
702 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100703 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100704 put_device(dev);
705 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200706 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800707 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100708 dpm_show_time(starttime, state, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700709 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100710}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100711
712/**
713 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
714 * @state: PM transition of the system being carried out.
715 */
716void dpm_resume_start(pm_message_t state)
717{
718 dpm_resume_noirq(state);
719 dpm_resume_early(state);
720}
721EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100722
723/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100724 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200725 * @dev: Device to handle.
726 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100727 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100728 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100729static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100730{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100731 pm_callback_t callback = NULL;
732 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100733 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700734 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100735
736 TRACE_DEVICE(dev);
737 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400738
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200739 if (dev->power.syscore)
740 goto Complete;
741
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200742 if (dev->power.direct_complete) {
743 /* Match the pm_runtime_disable() in __device_suspend(). */
744 pm_runtime_enable(dev);
745 goto Complete;
746 }
747
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100748 dpm_wait(dev->parent, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700749 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800750 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100751
Alan Sternf76b168b2011-06-18 20:22:23 +0200752 /*
753 * This is a fib. But we'll allow new children to be added below
754 * a resumed device, even if the device hasn't been completed yet.
755 */
756 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100757
Alan Stern6d0e0e82011-06-18 22:42:09 +0200758 if (!dev->power.is_suspended)
759 goto Unlock;
760
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200761 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100762 info = "power domain ";
763 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100764 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100765 }
766
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100767 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100768 info = "type ";
769 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100770 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100771 }
772
773 if (dev->class) {
774 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100775 info = "class ";
776 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100777 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100778 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100779 info = "legacy class ";
780 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100781 goto End;
782 }
783 }
784
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200785 if (dev->bus) {
786 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100787 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100788 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200789 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100790 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100791 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100792 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200793 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400794 }
795
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100796 Driver:
797 if (!callback && dev->driver && dev->driver->pm) {
798 info = "driver ";
799 callback = pm_op(dev->driver->pm, state);
800 }
801
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200802 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100803 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200804 dev->power.is_suspended = false;
805
806 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800807 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700808 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200809
810 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100811 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100812
Alan Sterncd59abf2007-09-21 15:36:56 -0400813 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200814
Alan Sterncd59abf2007-09-21 15:36:56 -0400815 return error;
816}
817
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100818static void async_resume(void *data, async_cookie_t cookie)
819{
820 struct device *dev = (struct device *)data;
821 int error;
822
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100823 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100824 if (error)
825 pm_dev_err(dev, pm_transition, " async", error);
826 put_device(dev);
827}
828
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100829/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200830 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
831 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100832 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200833 * Execute the appropriate "resume" callback for all devices whose status
834 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400835 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200836void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400837{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100838 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100839 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400840
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700841 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200842 might_sleep();
843
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200844 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100845 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200846 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200847
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100848 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800849 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100850 if (is_async(dev)) {
851 get_device(dev);
852 async_schedule(async_resume, dev);
853 }
854 }
855
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100856 while (!list_empty(&dpm_suspended_list)) {
857 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200858 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100859 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200860 int error;
861
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200862 mutex_unlock(&dpm_list_mtx);
863
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100864 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200865 if (error) {
866 suspend_stats.failed_resume++;
867 dpm_save_failed_step(SUSPEND_RESUME);
868 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200869 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200870 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100871
872 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200873 }
874 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100875 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200876 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400877 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200878 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100879 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100880 dpm_show_time(starttime, state, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800881
882 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700883 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200884}
885
886/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200887 * device_complete - Complete a PM transition for given device.
888 * @dev: Device to handle.
889 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200890 */
Alan Sternd1616302009-05-24 22:05:42 +0200891static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200892{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100893 void (*callback)(struct device *) = NULL;
894 char *info = NULL;
895
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200896 if (dev->power.syscore)
897 return;
898
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800899 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200900
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200901 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100902 info = "completing power domain ";
903 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200904 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100905 info = "completing type ";
906 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100907 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100908 info = "completing class ";
909 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100910 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100911 info = "completing bus ";
912 callback = dev->bus->pm->complete;
913 }
914
915 if (!callback && dev->driver && dev->driver->pm) {
916 info = "completing driver ";
917 callback = dev->driver->pm->complete;
918 }
919
920 if (callback) {
921 pm_dev_dbg(dev, state, info);
922 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200923 }
924
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800925 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200926
Ulf Hanssonaf939332013-04-12 09:41:06 +0000927 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200928}
929
930/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200931 * dpm_complete - Complete a PM transition for all non-sysdev devices.
932 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200933 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200934 * Execute the ->complete() callbacks for all devices whose PM status is not
935 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200936 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200937void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200938{
939 struct list_head list;
940
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700941 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200942 might_sleep();
943
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200944 INIT_LIST_HEAD(&list);
945 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100946 while (!list_empty(&dpm_prepared_list)) {
947 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200948
949 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200950 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100951 list_move(&dev->power.entry, &list);
952 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200953
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100954 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200955
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100956 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200957 put_device(dev);
958 }
959 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400960 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700961 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -0400962}
963
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100964/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200965 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
966 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400967 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200968 * Execute "resume" callbacks for all devices and complete the PM transition of
969 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400970 */
Alan Sternd1616302009-05-24 22:05:42 +0200971void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400972{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200973 dpm_resume(state);
974 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400975}
Alan Sternd1616302009-05-24 22:05:42 +0200976EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400977
978
Alan Sterncd59abf2007-09-21 15:36:56 -0400979/*------------------------- Suspend routines -------------------------*/
980
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200981/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200982 * resume_event - Return a "resume" message for given "suspend" sleep state.
983 * @sleep_state: PM message representing a sleep state.
984 *
985 * Return a PM message representing the resume event corresponding to given
986 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200987 */
988static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400989{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200990 switch (sleep_state.event) {
991 case PM_EVENT_SUSPEND:
992 return PMSG_RESUME;
993 case PM_EVENT_FREEZE:
994 case PM_EVENT_QUIESCE:
995 return PMSG_RECOVER;
996 case PM_EVENT_HIBERNATE:
997 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400998 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200999 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001000}
1001
1002/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001003 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1004 * @dev: Device to handle.
1005 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001006 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001007 * The driver of @dev will not receive interrupts while this function is being
1008 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001009 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001010static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001011{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001012 pm_callback_t callback = NULL;
1013 char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001014 int error = 0;
1015
1016 if (async_error)
1017 goto Complete;
1018
1019 if (pm_wakeup_pending()) {
1020 async_error = -EBUSY;
1021 goto Complete;
1022 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +01001023
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001024 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001025 goto Complete;
1026
1027 dpm_wait_for_children(dev, async);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001028
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001029 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001030 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001031 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001032 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001033 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001034 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001035 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001036 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001037 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001038 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001039 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001040 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001041 }
1042
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001043 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001044 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001045 callback = pm_noirq_op(dev->driver->pm, state);
1046 }
1047
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001048 error = dpm_run_callback(callback, dev, state, info);
1049 if (!error)
1050 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001051 else
1052 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001053
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001054Complete:
1055 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001056 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001057}
1058
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001059static void async_suspend_noirq(void *data, async_cookie_t cookie)
1060{
1061 struct device *dev = (struct device *)data;
1062 int error;
1063
1064 error = __device_suspend_noirq(dev, pm_transition, true);
1065 if (error) {
1066 dpm_save_failed_dev(dev_name(dev));
1067 pm_dev_err(dev, pm_transition, " async", error);
1068 }
1069
1070 put_device(dev);
1071}
1072
1073static int device_suspend_noirq(struct device *dev)
1074{
1075 reinit_completion(&dev->power.completion);
1076
1077 if (pm_async_enabled && dev->power.async_suspend) {
1078 get_device(dev);
1079 async_schedule(async_suspend_noirq, dev);
1080 return 0;
1081 }
1082 return __device_suspend_noirq(dev, pm_transition, false);
1083}
1084
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001085/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001086 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001087 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001088 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001089 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1090 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001091 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001092static int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001093{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001094 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001095 int error = 0;
1096
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001097 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Preeti U Murthy8651f972012-07-09 10:12:56 +02001098 cpuidle_pause();
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +01001099 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001100 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001101 pm_transition = state;
1102 async_error = 0;
1103
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001104 while (!list_empty(&dpm_late_early_list)) {
1105 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001106
1107 get_device(dev);
1108 mutex_unlock(&dpm_list_mtx);
1109
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001110 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001111
1112 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001113 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001114 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001115 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001116 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001117 break;
1118 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001119 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001120 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001121 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001122
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001123 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001124 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001125 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001126 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001127 async_synchronize_full();
1128 if (!error)
1129 error = async_error;
1130
1131 if (error) {
1132 suspend_stats.failed_suspend_noirq++;
1133 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Alan Sternd1616302009-05-24 22:05:42 +02001134 dpm_resume_noirq(resume_event(state));
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001135 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001136 dpm_show_time(starttime, state, "noirq");
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001137 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001138 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001139 return error;
1140}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001141
1142/**
1143 * device_suspend_late - Execute a "late suspend" callback for given device.
1144 * @dev: Device to handle.
1145 * @state: PM transition of the system being carried out.
1146 *
1147 * Runtime PM is disabled for @dev while this function is being executed.
1148 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001149static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001150{
1151 pm_callback_t callback = NULL;
1152 char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001153 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001154
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001155 __pm_runtime_disable(dev, false);
1156
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001157 if (async_error)
1158 goto Complete;
1159
1160 if (pm_wakeup_pending()) {
1161 async_error = -EBUSY;
1162 goto Complete;
1163 }
1164
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001165 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001166 goto Complete;
1167
1168 dpm_wait_for_children(dev, async);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001169
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001170 if (dev->pm_domain) {
1171 info = "late power domain ";
1172 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1173 } else if (dev->type && dev->type->pm) {
1174 info = "late type ";
1175 callback = pm_late_early_op(dev->type->pm, state);
1176 } else if (dev->class && dev->class->pm) {
1177 info = "late class ";
1178 callback = pm_late_early_op(dev->class->pm, state);
1179 } else if (dev->bus && dev->bus->pm) {
1180 info = "late bus ";
1181 callback = pm_late_early_op(dev->bus->pm, state);
1182 }
1183
1184 if (!callback && dev->driver && dev->driver->pm) {
1185 info = "late driver ";
1186 callback = pm_late_early_op(dev->driver->pm, state);
1187 }
1188
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001189 error = dpm_run_callback(callback, dev, state, info);
1190 if (!error)
1191 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001192 else
1193 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001194
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001195Complete:
1196 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001197 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001198}
1199
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001200static void async_suspend_late(void *data, async_cookie_t cookie)
1201{
1202 struct device *dev = (struct device *)data;
1203 int error;
1204
1205 error = __device_suspend_late(dev, pm_transition, true);
1206 if (error) {
1207 dpm_save_failed_dev(dev_name(dev));
1208 pm_dev_err(dev, pm_transition, " async", error);
1209 }
1210 put_device(dev);
1211}
1212
1213static int device_suspend_late(struct device *dev)
1214{
1215 reinit_completion(&dev->power.completion);
1216
1217 if (pm_async_enabled && dev->power.async_suspend) {
1218 get_device(dev);
1219 async_schedule(async_suspend_late, dev);
1220 return 0;
1221 }
1222
1223 return __device_suspend_late(dev, pm_transition, false);
1224}
1225
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001226/**
1227 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1228 * @state: PM transition of the system being carried out.
1229 */
1230static int dpm_suspend_late(pm_message_t state)
1231{
1232 ktime_t starttime = ktime_get();
1233 int error = 0;
1234
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001235 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001236 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001237 pm_transition = state;
1238 async_error = 0;
1239
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001240 while (!list_empty(&dpm_suspended_list)) {
1241 struct device *dev = to_device(dpm_suspended_list.prev);
1242
1243 get_device(dev);
1244 mutex_unlock(&dpm_list_mtx);
1245
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001246 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001247
1248 mutex_lock(&dpm_list_mtx);
1249 if (error) {
1250 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001251 dpm_save_failed_dev(dev_name(dev));
1252 put_device(dev);
1253 break;
1254 }
1255 if (!list_empty(&dev->power.entry))
1256 list_move(&dev->power.entry, &dpm_late_early_list);
1257 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001258
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001259 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001260 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001261 }
1262 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001263 async_synchronize_full();
1264 if (error) {
1265 suspend_stats.failed_suspend_late++;
1266 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001267 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001268 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001269 dpm_show_time(starttime, state, "late");
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001270 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001271 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001272 return error;
1273}
1274
1275/**
1276 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1277 * @state: PM transition of the system being carried out.
1278 */
1279int dpm_suspend_end(pm_message_t state)
1280{
1281 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001282 if (error)
1283 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001284
Colin Cross064b0212012-07-19 10:38:06 +02001285 error = dpm_suspend_noirq(state);
1286 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001287 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001288 return error;
1289 }
1290
1291 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001292}
1293EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001294
1295/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001296 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001297 * @dev: Device to suspend.
1298 * @state: PM transition of the system being carried out.
1299 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001300 */
1301static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001302 int (*cb)(struct device *dev, pm_message_t state),
1303 char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001304{
1305 int error;
1306 ktime_t calltime;
1307
1308 calltime = initcall_debug_start(dev);
1309
1310 error = cb(dev, state);
1311 suspend_report_result(cb, error);
1312
Shuah Khan53644672013-07-26 13:30:20 -06001313 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001314
1315 return error;
1316}
1317
1318/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001319 * device_suspend - Execute "suspend" callbacks for given device.
1320 * @dev: Device to handle.
1321 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001322 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001323 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001324static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001325{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001326 pm_callback_t callback = NULL;
1327 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001328 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001329 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001330
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001331 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001332
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001333 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001334 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001335
Alan Stern88d26132012-09-19 21:59:02 +02001336 /*
1337 * If a device configured to wake up the system from sleep states
1338 * has been suspended at run time and there's a resume request pending
1339 * for it, this is equivalent to the device signaling wakeup, so the
1340 * system suspend operation should be aborted.
1341 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001342 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1343 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001344
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001345 if (pm_wakeup_pending()) {
1346 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001347 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001348 }
1349
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001350 if (dev->power.syscore)
1351 goto Complete;
1352
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001353 if (dev->power.direct_complete) {
1354 if (pm_runtime_status_suspended(dev)) {
1355 pm_runtime_disable(dev);
1356 if (pm_runtime_suspended_if_enabled(dev))
1357 goto Complete;
1358
1359 pm_runtime_enable(dev);
1360 }
1361 dev->power.direct_complete = false;
1362 }
1363
Benoit Goby70fea602013-10-17 10:48:46 -07001364 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001365 device_lock(dev);
1366
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001367 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001368 info = "power domain ";
1369 callback = pm_op(&dev->pm_domain->ops, state);
1370 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001371 }
1372
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001373 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001374 info = "type ";
1375 callback = pm_op(dev->type->pm, state);
1376 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001377 }
1378
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001379 if (dev->class) {
1380 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001381 info = "class ";
1382 callback = pm_op(dev->class->pm, state);
1383 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001384 } else if (dev->class->suspend) {
1385 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001386 error = legacy_suspend(dev, state, dev->class->suspend,
1387 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001388 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001389 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001390 }
1391
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001392 if (dev->bus) {
1393 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001394 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001395 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001396 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001397 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001398 error = legacy_suspend(dev, state, dev->bus->suspend,
1399 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001400 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001401 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001402 }
1403
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001404 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001405 if (!callback && dev->driver && dev->driver->pm) {
1406 info = "driver ";
1407 callback = pm_op(dev->driver->pm, state);
1408 }
1409
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001410 error = dpm_run_callback(callback, dev, state, info);
1411
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001412 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001413 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001414 struct device *parent = dev->parent;
1415
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001416 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001417 if (parent) {
1418 spin_lock_irq(&parent->power.lock);
1419
1420 dev->parent->power.direct_complete = false;
1421 if (dev->power.wakeup_path
1422 && !dev->parent->power.ignore_children)
1423 dev->parent->power.wakeup_path = true;
1424
1425 spin_unlock_irq(&parent->power.lock);
1426 }
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001427 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001428
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001429 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001430 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001431
1432 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001433 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001434 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001435 async_error = error;
1436
Alan Sterncd59abf2007-09-21 15:36:56 -04001437 return error;
1438}
1439
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001440static void async_suspend(void *data, async_cookie_t cookie)
1441{
1442 struct device *dev = (struct device *)data;
1443 int error;
1444
1445 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001446 if (error) {
1447 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001448 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001449 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001450
1451 put_device(dev);
1452}
1453
1454static int device_suspend(struct device *dev)
1455{
Wolfram Sang16735d02013-11-14 14:32:02 -08001456 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001457
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +01001458 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001459 get_device(dev);
1460 async_schedule(async_suspend, dev);
1461 return 0;
1462 }
1463
1464 return __device_suspend(dev, pm_transition, false);
1465}
1466
Alan Sterncd59abf2007-09-21 15:36:56 -04001467/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001468 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1469 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001470 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001471int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001472{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001473 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001474 int error = 0;
1475
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001476 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001477 might_sleep();
1478
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001479 cpufreq_suspend();
1480
Alan Sterncd59abf2007-09-21 15:36:56 -04001481 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001482 pm_transition = state;
1483 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001484 while (!list_empty(&dpm_prepared_list)) {
1485 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001486
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001487 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001488 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001489
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001490 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001491
Alan Stern1b3cbec2008-02-29 11:50:22 -05001492 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001493 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001494 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001495 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001496 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001497 break;
1498 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001499 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001500 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001501 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001502 if (async_error)
1503 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001504 }
1505 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001506 async_synchronize_full();
1507 if (!error)
1508 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001509 if (error) {
1510 suspend_stats.failed_suspend++;
1511 dpm_save_failed_step(SUSPEND_SUSPEND);
1512 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001513 dpm_show_time(starttime, state, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001514 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001515 return error;
1516}
1517
1518/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001519 * device_prepare - Prepare a device for system power transition.
1520 * @dev: Device to handle.
1521 * @state: PM transition of the system being carried out.
1522 *
1523 * Execute the ->prepare() callback(s) for given device. No new children of the
1524 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001525 */
Alan Sternd1616302009-05-24 22:05:42 +02001526static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001527{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001528 int (*callback)(struct device *) = NULL;
1529 char *info = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001530 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001531
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001532 if (dev->power.syscore)
1533 return 0;
1534
Alan Stern88d26132012-09-19 21:59:02 +02001535 /*
1536 * If a device's parent goes into runtime suspend at the wrong time,
1537 * it won't be possible to resume the device. To prevent this we
1538 * block runtime suspend here, during the prepare phase, and allow
1539 * it again during the complete phase.
1540 */
1541 pm_runtime_get_noresume(dev);
1542
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001543 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001544
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001545 dev->power.wakeup_path = device_may_wakeup(dev);
1546
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001547 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001548 info = "preparing power domain ";
1549 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001550 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001551 info = "preparing type ";
1552 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001553 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001554 info = "preparing class ";
1555 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001556 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001557 info = "preparing bus ";
1558 callback = dev->bus->pm->prepare;
1559 }
1560
1561 if (!callback && dev->driver && dev->driver->pm) {
1562 info = "preparing driver ";
1563 callback = dev->driver->pm->prepare;
1564 }
1565
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001566 if (callback)
1567 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001568
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001569 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001570
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001571 if (ret < 0) {
1572 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001573 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001574 return ret;
1575 }
1576 /*
1577 * A positive return value from ->prepare() means "this device appears
1578 * to be runtime-suspended and its state is fine, so if it really is
1579 * runtime-suspended, you can leave it in that state provided that you
1580 * will do the same thing with all of its descendants". This only
1581 * applies to suspend transitions, however.
1582 */
1583 spin_lock_irq(&dev->power.lock);
1584 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1585 spin_unlock_irq(&dev->power.lock);
1586 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001587}
1588
1589/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001590 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1591 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001592 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001593 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001594 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001595int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001596{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001597 int error = 0;
1598
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001599 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001600 might_sleep();
1601
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001602 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001603 while (!list_empty(&dpm_list)) {
1604 struct device *dev = to_device(dpm_list.next);
1605
1606 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001607 mutex_unlock(&dpm_list_mtx);
1608
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001609 error = device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001610
1611 mutex_lock(&dpm_list_mtx);
1612 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001613 if (error == -EAGAIN) {
1614 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001615 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001616 continue;
1617 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001618 printk(KERN_INFO "PM: Device %s not prepared "
1619 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001620 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001621 put_device(dev);
1622 break;
1623 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001624 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001625 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001626 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001627 put_device(dev);
1628 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001629 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001630 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001631 return error;
1632}
1633
1634/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001635 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1636 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001637 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001638 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1639 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001640 */
Alan Sternd1616302009-05-24 22:05:42 +02001641int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001642{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001643 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001644
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001645 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001646 if (error) {
1647 suspend_stats.failed_prepare++;
1648 dpm_save_failed_step(SUSPEND_PREPARE);
1649 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001650 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001651 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001652}
Alan Sternd1616302009-05-24 22:05:42 +02001653EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001654
1655void __suspend_report_result(const char *function, void *fn, int ret)
1656{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001657 if (ret)
1658 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001659}
1660EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001661
1662/**
1663 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1664 * @dev: Device to wait for.
1665 * @subordinate: Device that needs to wait for @dev.
1666 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001667int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001668{
1669 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001670 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001671}
1672EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001673
1674/**
1675 * dpm_for_each_dev - device iterator.
1676 * @data: data for the callback.
1677 * @fn: function to be called for each device.
1678 *
1679 * Iterate over devices in dpm_list, and call @fn for each device,
1680 * passing it @data.
1681 */
1682void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1683{
1684 struct device *dev;
1685
1686 if (!fn)
1687 return;
1688
1689 device_pm_lock();
1690 list_for_each_entry(dev, &dpm_list, power.entry)
1691 fn(dev, data);
1692 device_pm_unlock();
1693}
1694EXPORT_SYMBOL_GPL(dpm_for_each_dev);