blob: 5528e59ae788fe64089154596c0a10948a81d9bb [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>
Zhonghui Fu431d4522015-03-18 15:54:27 +010026#include <linux/pm-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 }
217}
218
Cornelia Huckffa6a702009-03-04 12:44:00 +0100219/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100220 * dpm_wait - Wait for a PM operation to complete.
221 * @dev: Device to wait for.
222 * @async: If unset, wait only if the device's power.async_suspend flag is set.
223 */
224static void dpm_wait(struct device *dev, bool async)
225{
226 if (!dev)
227 return;
228
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100229 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100230 wait_for_completion(&dev->power.completion);
231}
232
233static int dpm_wait_fn(struct device *dev, void *async_ptr)
234{
235 dpm_wait(dev, *((bool *)async_ptr));
236 return 0;
237}
238
239static void dpm_wait_for_children(struct device *dev, bool async)
240{
241 device_for_each_child(dev, &async, dpm_wait_fn);
242}
243
244/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100245 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200246 * @ops: PM operations to choose from.
247 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200248 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100249static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200250{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200251 switch (state.event) {
252#ifdef CONFIG_SUSPEND
253 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100254 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200255 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100256 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200257#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200258#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200259 case PM_EVENT_FREEZE:
260 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100261 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200262 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100263 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200264 case PM_EVENT_THAW:
265 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100266 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200267 break;
268 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100269 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200270#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200271 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100272
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100273 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200274}
275
276/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100277 * pm_late_early_op - Return the PM operation appropriate for given PM event.
278 * @ops: PM operations to choose from.
279 * @state: PM transition of the system being carried out.
280 *
281 * Runtime PM is disabled for @dev while this function is being executed.
282 */
283static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
284 pm_message_t state)
285{
286 switch (state.event) {
287#ifdef CONFIG_SUSPEND
288 case PM_EVENT_SUSPEND:
289 return ops->suspend_late;
290 case PM_EVENT_RESUME:
291 return ops->resume_early;
292#endif /* CONFIG_SUSPEND */
293#ifdef CONFIG_HIBERNATE_CALLBACKS
294 case PM_EVENT_FREEZE:
295 case PM_EVENT_QUIESCE:
296 return ops->freeze_late;
297 case PM_EVENT_HIBERNATE:
298 return ops->poweroff_late;
299 case PM_EVENT_THAW:
300 case PM_EVENT_RECOVER:
301 return ops->thaw_early;
302 case PM_EVENT_RESTORE:
303 return ops->restore_early;
304#endif /* CONFIG_HIBERNATE_CALLBACKS */
305 }
306
307 return NULL;
308}
309
310/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100311 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200312 * @ops: PM operations to choose from.
313 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200314 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200315 * The driver of @dev will not receive interrupts while this function is being
316 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200317 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100318static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200319{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200320 switch (state.event) {
321#ifdef CONFIG_SUSPEND
322 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100323 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200324 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100325 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200326#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200327#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200328 case PM_EVENT_FREEZE:
329 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100330 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200331 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100332 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200333 case PM_EVENT_THAW:
334 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100335 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200336 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100337 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200338#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200339 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100340
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100341 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200342}
343
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200344static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
345{
346 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
347 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
348 ", may wakeup" : "");
349}
350
351static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
352 int error)
353{
354 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100355 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200356}
357
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100358static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
359{
360 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200361 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100362 int usecs;
363
364 calltime = ktime_get();
365 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
366 do_div(usecs64, NSEC_PER_USEC);
367 usecs = usecs64;
368 if (usecs == 0)
369 usecs = 1;
370 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
371 info ?: "", info ? " " : "", pm_verb(state.event),
372 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
373}
374
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100375static int dpm_run_callback(pm_callback_t cb, struct device *dev,
376 pm_message_t state, char *info)
377{
378 ktime_t calltime;
379 int error;
380
381 if (!cb)
382 return 0;
383
384 calltime = initcall_debug_start(dev);
385
386 pm_dev_dbg(dev, state, info);
Todd E Brandte8bca472014-06-10 07:31:22 -0700387 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100388 error = cb(dev);
Todd E Brandte8bca472014-06-10 07:31:22 -0700389 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100390 suspend_report_result(cb, error);
391
Shuah Khan53644672013-07-26 13:30:20 -0600392 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100393
394 return error;
395}
396
Benoit Goby70fea602013-10-17 10:48:46 -0700397#ifdef CONFIG_DPM_WATCHDOG
398struct dpm_watchdog {
399 struct device *dev;
400 struct task_struct *tsk;
401 struct timer_list timer;
402};
403
404#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
405 struct dpm_watchdog wd
406
407/**
408 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
409 * @data: Watchdog object address.
410 *
411 * Called when a driver has timed out suspending or resuming.
412 * There's not much we can do here to recover so panic() to
413 * capture a crash-dump in pstore.
414 */
415static void dpm_watchdog_handler(unsigned long data)
416{
417 struct dpm_watchdog *wd = (void *)data;
418
419 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
420 show_stack(wd->tsk, NULL);
421 panic("%s %s: unrecoverable failure\n",
422 dev_driver_string(wd->dev), dev_name(wd->dev));
423}
424
425/**
426 * dpm_watchdog_set - Enable pm watchdog for given device.
427 * @wd: Watchdog. Must be allocated on the stack.
428 * @dev: Device to handle.
429 */
430static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
431{
432 struct timer_list *timer = &wd->timer;
433
434 wd->dev = dev;
435 wd->tsk = current;
436
437 init_timer_on_stack(timer);
438 /* use same timeout value for both suspend and resume */
439 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
440 timer->function = dpm_watchdog_handler;
441 timer->data = (unsigned long)wd;
442 add_timer(timer);
443}
444
445/**
446 * dpm_watchdog_clear - Disable suspend/resume watchdog.
447 * @wd: Watchdog to disable.
448 */
449static void dpm_watchdog_clear(struct dpm_watchdog *wd)
450{
451 struct timer_list *timer = &wd->timer;
452
453 del_timer_sync(timer);
454 destroy_timer_on_stack(timer);
455}
456#else
457#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
458#define dpm_watchdog_set(x, y)
459#define dpm_watchdog_clear(x)
460#endif
461
Alan Sterncd59abf2007-09-21 15:36:56 -0400462/*------------------------- Resume routines -------------------------*/
463
464/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200465 * device_resume_noirq - Execute an "early resume" callback for given device.
466 * @dev: Device to handle.
467 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700468 * @async: If true, the device is being resumed asynchronously.
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. Wysocki2a8a8ce2014-09-30 02:21:34 +0200543void 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.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700598 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100599 *
600 * Runtime PM is disabled for @dev while this function is being executed.
601 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800602static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100603{
604 pm_callback_t callback = NULL;
605 char *info = NULL;
606 int error = 0;
607
608 TRACE_DEVICE(dev);
609 TRACE_RESUME(0);
610
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200611 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200612 goto Out;
613
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800614 if (!dev->power.is_late_suspended)
615 goto Out;
616
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800617 dpm_wait(dev->parent, async);
618
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100619 if (dev->pm_domain) {
620 info = "early power domain ";
621 callback = pm_late_early_op(&dev->pm_domain->ops, state);
622 } else if (dev->type && dev->type->pm) {
623 info = "early type ";
624 callback = pm_late_early_op(dev->type->pm, state);
625 } else if (dev->class && dev->class->pm) {
626 info = "early class ";
627 callback = pm_late_early_op(dev->class->pm, state);
628 } else if (dev->bus && dev->bus->pm) {
629 info = "early bus ";
630 callback = pm_late_early_op(dev->bus->pm, state);
631 }
632
633 if (!callback && dev->driver && dev->driver->pm) {
634 info = "early driver ";
635 callback = pm_late_early_op(dev->driver->pm, state);
636 }
637
638 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800639 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100640
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200641 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100642 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100643
644 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800645 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100646 return error;
647}
648
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800649static void async_resume_early(void *data, async_cookie_t cookie)
650{
651 struct device *dev = (struct device *)data;
652 int error;
653
654 error = device_resume_early(dev, pm_transition, true);
655 if (error)
656 pm_dev_err(dev, pm_transition, " async", error);
657
658 put_device(dev);
659}
660
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100661/**
662 * dpm_resume_early - Execute "early resume" callbacks for all devices.
663 * @state: PM transition of the system being carried out.
664 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200665void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100666{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800667 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100668 ktime_t starttime = ktime_get();
669
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700670 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100671 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800672 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100673
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800674 /*
675 * Advanced the async threads upfront,
676 * in case the starting of async threads is
677 * delayed by non-async resuming devices.
678 */
679 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
680 reinit_completion(&dev->power.completion);
681 if (is_async(dev)) {
682 get_device(dev);
683 async_schedule(async_resume_early, dev);
684 }
685 }
686
687 while (!list_empty(&dpm_late_early_list)) {
688 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100689 get_device(dev);
690 list_move_tail(&dev->power.entry, &dpm_suspended_list);
691 mutex_unlock(&dpm_list_mtx);
692
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800693 if (!is_async(dev)) {
694 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100695
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800696 error = device_resume_early(dev, state, false);
697 if (error) {
698 suspend_stats.failed_resume_early++;
699 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
700 dpm_save_failed_dev(dev_name(dev));
701 pm_dev_err(dev, state, " early", error);
702 }
703 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100704 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100705 put_device(dev);
706 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200707 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800708 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100709 dpm_show_time(starttime, state, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700710 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100711}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100712
713/**
714 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
715 * @state: PM transition of the system being carried out.
716 */
717void dpm_resume_start(pm_message_t state)
718{
719 dpm_resume_noirq(state);
720 dpm_resume_early(state);
721}
722EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100723
724/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100725 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200726 * @dev: Device to handle.
727 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100728 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100729 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100730static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100731{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100732 pm_callback_t callback = NULL;
733 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100734 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700735 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100736
737 TRACE_DEVICE(dev);
738 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400739
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200740 if (dev->power.syscore)
741 goto Complete;
742
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200743 if (dev->power.direct_complete) {
744 /* Match the pm_runtime_disable() in __device_suspend(). */
745 pm_runtime_enable(dev);
746 goto Complete;
747 }
748
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100749 dpm_wait(dev->parent, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700750 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800751 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100752
Alan Sternf76b168b2011-06-18 20:22:23 +0200753 /*
754 * This is a fib. But we'll allow new children to be added below
755 * a resumed device, even if the device hasn't been completed yet.
756 */
757 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100758
Alan Stern6d0e0e82011-06-18 22:42:09 +0200759 if (!dev->power.is_suspended)
760 goto Unlock;
761
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200762 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100763 info = "power domain ";
764 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100765 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100766 }
767
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100768 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100769 info = "type ";
770 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100771 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100772 }
773
774 if (dev->class) {
775 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100776 info = "class ";
777 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100778 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100779 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100780 info = "legacy class ";
781 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100782 goto End;
783 }
784 }
785
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200786 if (dev->bus) {
787 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100788 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100789 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200790 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100791 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100792 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100793 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200794 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400795 }
796
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100797 Driver:
798 if (!callback && dev->driver && dev->driver->pm) {
799 info = "driver ";
800 callback = pm_op(dev->driver->pm, state);
801 }
802
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200803 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100804 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200805 dev->power.is_suspended = false;
806
807 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800808 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700809 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200810
811 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100812 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100813
Alan Sterncd59abf2007-09-21 15:36:56 -0400814 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200815
Alan Sterncd59abf2007-09-21 15:36:56 -0400816 return error;
817}
818
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100819static void async_resume(void *data, async_cookie_t cookie)
820{
821 struct device *dev = (struct device *)data;
822 int error;
823
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100824 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100825 if (error)
826 pm_dev_err(dev, pm_transition, " async", error);
827 put_device(dev);
828}
829
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100830/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200831 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
832 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100833 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200834 * Execute the appropriate "resume" callback for all devices whose status
835 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400836 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200837void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400838{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100839 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100840 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400841
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700842 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200843 might_sleep();
844
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200845 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100846 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200847 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200848
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100849 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800850 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100851 if (is_async(dev)) {
852 get_device(dev);
853 async_schedule(async_resume, dev);
854 }
855 }
856
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100857 while (!list_empty(&dpm_suspended_list)) {
858 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200859 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100860 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200861 int error;
862
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200863 mutex_unlock(&dpm_list_mtx);
864
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100865 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200866 if (error) {
867 suspend_stats.failed_resume++;
868 dpm_save_failed_step(SUSPEND_RESUME);
869 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200870 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200871 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100872
873 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200874 }
875 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100876 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200877 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400878 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200879 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100880 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100881 dpm_show_time(starttime, state, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800882
883 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700884 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200885}
886
887/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200888 * device_complete - Complete a PM transition for given device.
889 * @dev: Device to handle.
890 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200891 */
Alan Sternd1616302009-05-24 22:05:42 +0200892static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200893{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100894 void (*callback)(struct device *) = NULL;
895 char *info = NULL;
896
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200897 if (dev->power.syscore)
898 return;
899
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800900 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200901
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200902 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100903 info = "completing power domain ";
904 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200905 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100906 info = "completing type ";
907 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100908 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100909 info = "completing class ";
910 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100911 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100912 info = "completing bus ";
913 callback = dev->bus->pm->complete;
914 }
915
916 if (!callback && dev->driver && dev->driver->pm) {
917 info = "completing driver ";
918 callback = dev->driver->pm->complete;
919 }
920
921 if (callback) {
922 pm_dev_dbg(dev, state, info);
923 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200924 }
925
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800926 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200927
Ulf Hanssonaf939332013-04-12 09:41:06 +0000928 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200929}
930
931/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200932 * dpm_complete - Complete a PM transition for all non-sysdev devices.
933 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200934 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200935 * Execute the ->complete() callbacks for all devices whose PM status is not
936 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200937 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200938void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200939{
940 struct list_head list;
941
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700942 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200943 might_sleep();
944
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200945 INIT_LIST_HEAD(&list);
946 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100947 while (!list_empty(&dpm_prepared_list)) {
948 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200949
950 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200951 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100952 list_move(&dev->power.entry, &list);
953 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954
Todd E Brandt32e8d682015-05-28 12:55:53 -0700955 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100956 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -0700957 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200958
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100959 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200960 put_device(dev);
961 }
962 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400963 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700964 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -0400965}
966
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100967/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200968 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
969 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400970 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200971 * Execute "resume" callbacks for all devices and complete the PM transition of
972 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400973 */
Alan Sternd1616302009-05-24 22:05:42 +0200974void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400975{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200976 dpm_resume(state);
977 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400978}
Alan Sternd1616302009-05-24 22:05:42 +0200979EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400980
981
Alan Sterncd59abf2007-09-21 15:36:56 -0400982/*------------------------- Suspend routines -------------------------*/
983
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200984/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200985 * resume_event - Return a "resume" message for given "suspend" sleep state.
986 * @sleep_state: PM message representing a sleep state.
987 *
988 * Return a PM message representing the resume event corresponding to given
989 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200990 */
991static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400992{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200993 switch (sleep_state.event) {
994 case PM_EVENT_SUSPEND:
995 return PMSG_RESUME;
996 case PM_EVENT_FREEZE:
997 case PM_EVENT_QUIESCE:
998 return PMSG_RECOVER;
999 case PM_EVENT_HIBERNATE:
1000 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001001 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001002 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001003}
1004
1005/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001006 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1007 * @dev: Device to handle.
1008 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001009 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001010 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001011 * The driver of @dev will not receive interrupts while this function is being
1012 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001013 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001014static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001015{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001016 pm_callback_t callback = NULL;
1017 char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001018 int error = 0;
1019
Zhonghui Fu431d4522015-03-18 15:54:27 +01001020 TRACE_DEVICE(dev);
1021 TRACE_SUSPEND(0);
1022
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001023 if (async_error)
1024 goto Complete;
1025
1026 if (pm_wakeup_pending()) {
1027 async_error = -EBUSY;
1028 goto Complete;
1029 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +01001030
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001031 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001032 goto Complete;
1033
1034 dpm_wait_for_children(dev, async);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001035
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001036 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001037 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001038 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001039 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001040 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001041 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001042 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001043 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001044 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001045 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001046 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001047 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001048 }
1049
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001050 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001051 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001052 callback = pm_noirq_op(dev->driver->pm, state);
1053 }
1054
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001055 error = dpm_run_callback(callback, dev, state, info);
1056 if (!error)
1057 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001058 else
1059 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001060
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001061Complete:
1062 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001063 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001064 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001065}
1066
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001067static void async_suspend_noirq(void *data, async_cookie_t cookie)
1068{
1069 struct device *dev = (struct device *)data;
1070 int error;
1071
1072 error = __device_suspend_noirq(dev, pm_transition, true);
1073 if (error) {
1074 dpm_save_failed_dev(dev_name(dev));
1075 pm_dev_err(dev, pm_transition, " async", error);
1076 }
1077
1078 put_device(dev);
1079}
1080
1081static int device_suspend_noirq(struct device *dev)
1082{
1083 reinit_completion(&dev->power.completion);
1084
Zhonghui Fu431d4522015-03-18 15:54:27 +01001085 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001086 get_device(dev);
1087 async_schedule(async_suspend_noirq, dev);
1088 return 0;
1089 }
1090 return __device_suspend_noirq(dev, pm_transition, false);
1091}
1092
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001093/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001094 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001095 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001096 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001097 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1098 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001099 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001100int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001101{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001102 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001103 int error = 0;
1104
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001105 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Preeti U Murthy8651f972012-07-09 10:12:56 +02001106 cpuidle_pause();
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +01001107 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001108 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001109 pm_transition = state;
1110 async_error = 0;
1111
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001112 while (!list_empty(&dpm_late_early_list)) {
1113 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001114
1115 get_device(dev);
1116 mutex_unlock(&dpm_list_mtx);
1117
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001118 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001119
1120 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001121 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001122 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001123 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001124 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001125 break;
1126 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001127 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001128 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001129 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001130
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001131 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001132 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001133 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001134 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001135 async_synchronize_full();
1136 if (!error)
1137 error = async_error;
1138
1139 if (error) {
1140 suspend_stats.failed_suspend_noirq++;
1141 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Alan Sternd1616302009-05-24 22:05:42 +02001142 dpm_resume_noirq(resume_event(state));
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001143 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001144 dpm_show_time(starttime, state, "noirq");
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001145 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001146 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001147 return error;
1148}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001149
1150/**
1151 * device_suspend_late - Execute a "late suspend" callback for given device.
1152 * @dev: Device to handle.
1153 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001154 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001155 *
1156 * Runtime PM is disabled for @dev while this function is being executed.
1157 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001158static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001159{
1160 pm_callback_t callback = NULL;
1161 char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001162 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001163
Zhonghui Fu431d4522015-03-18 15:54:27 +01001164 TRACE_DEVICE(dev);
1165 TRACE_SUSPEND(0);
1166
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001167 __pm_runtime_disable(dev, false);
1168
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001169 if (async_error)
1170 goto Complete;
1171
1172 if (pm_wakeup_pending()) {
1173 async_error = -EBUSY;
1174 goto Complete;
1175 }
1176
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001177 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001178 goto Complete;
1179
1180 dpm_wait_for_children(dev, async);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001181
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001182 if (dev->pm_domain) {
1183 info = "late power domain ";
1184 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1185 } else if (dev->type && dev->type->pm) {
1186 info = "late type ";
1187 callback = pm_late_early_op(dev->type->pm, state);
1188 } else if (dev->class && dev->class->pm) {
1189 info = "late class ";
1190 callback = pm_late_early_op(dev->class->pm, state);
1191 } else if (dev->bus && dev->bus->pm) {
1192 info = "late bus ";
1193 callback = pm_late_early_op(dev->bus->pm, state);
1194 }
1195
1196 if (!callback && dev->driver && dev->driver->pm) {
1197 info = "late driver ";
1198 callback = pm_late_early_op(dev->driver->pm, state);
1199 }
1200
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001201 error = dpm_run_callback(callback, dev, state, info);
1202 if (!error)
1203 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001204 else
1205 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001206
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001207Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001208 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001209 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001210 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001211}
1212
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001213static void async_suspend_late(void *data, async_cookie_t cookie)
1214{
1215 struct device *dev = (struct device *)data;
1216 int error;
1217
1218 error = __device_suspend_late(dev, pm_transition, true);
1219 if (error) {
1220 dpm_save_failed_dev(dev_name(dev));
1221 pm_dev_err(dev, pm_transition, " async", error);
1222 }
1223 put_device(dev);
1224}
1225
1226static int device_suspend_late(struct device *dev)
1227{
1228 reinit_completion(&dev->power.completion);
1229
Zhonghui Fu431d4522015-03-18 15:54:27 +01001230 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001231 get_device(dev);
1232 async_schedule(async_suspend_late, dev);
1233 return 0;
1234 }
1235
1236 return __device_suspend_late(dev, pm_transition, false);
1237}
1238
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001239/**
1240 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1241 * @state: PM transition of the system being carried out.
1242 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001243int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001244{
1245 ktime_t starttime = ktime_get();
1246 int error = 0;
1247
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001248 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001249 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001250 pm_transition = state;
1251 async_error = 0;
1252
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001253 while (!list_empty(&dpm_suspended_list)) {
1254 struct device *dev = to_device(dpm_suspended_list.prev);
1255
1256 get_device(dev);
1257 mutex_unlock(&dpm_list_mtx);
1258
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001259 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001260
1261 mutex_lock(&dpm_list_mtx);
1262 if (error) {
1263 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001264 dpm_save_failed_dev(dev_name(dev));
1265 put_device(dev);
1266 break;
1267 }
1268 if (!list_empty(&dev->power.entry))
1269 list_move(&dev->power.entry, &dpm_late_early_list);
1270 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001271
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001272 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001273 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001274 }
1275 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001276 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001277 if (!error)
1278 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001279 if (error) {
1280 suspend_stats.failed_suspend_late++;
1281 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001282 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001283 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001284 dpm_show_time(starttime, state, "late");
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001285 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001286 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001287 return error;
1288}
1289
1290/**
1291 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1292 * @state: PM transition of the system being carried out.
1293 */
1294int dpm_suspend_end(pm_message_t state)
1295{
1296 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001297 if (error)
1298 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001299
Colin Cross064b0212012-07-19 10:38:06 +02001300 error = dpm_suspend_noirq(state);
1301 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001302 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001303 return error;
1304 }
1305
1306 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001307}
1308EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001309
1310/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001311 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001312 * @dev: Device to suspend.
1313 * @state: PM transition of the system being carried out.
1314 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001315 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001316 */
1317static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001318 int (*cb)(struct device *dev, pm_message_t state),
1319 char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001320{
1321 int error;
1322 ktime_t calltime;
1323
1324 calltime = initcall_debug_start(dev);
1325
Todd E Brandte8bca472014-06-10 07:31:22 -07001326 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001327 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001328 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001329 suspend_report_result(cb, error);
1330
Shuah Khan53644672013-07-26 13:30:20 -06001331 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001332
1333 return error;
1334}
1335
1336/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001337 * device_suspend - Execute "suspend" callbacks for given device.
1338 * @dev: Device to handle.
1339 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001340 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001341 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001342static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001343{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001344 pm_callback_t callback = NULL;
1345 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001346 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001347 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001348
Zhonghui Fu431d4522015-03-18 15:54:27 +01001349 TRACE_DEVICE(dev);
1350 TRACE_SUSPEND(0);
1351
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001352 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001353
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001354 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001355 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001356
Alan Stern88d26132012-09-19 21:59:02 +02001357 /*
1358 * If a device configured to wake up the system from sleep states
1359 * has been suspended at run time and there's a resume request pending
1360 * for it, this is equivalent to the device signaling wakeup, so the
1361 * system suspend operation should be aborted.
1362 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001363 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1364 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001365
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001366 if (pm_wakeup_pending()) {
1367 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001368 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001369 }
1370
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001371 if (dev->power.syscore)
1372 goto Complete;
1373
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001374 if (dev->power.direct_complete) {
1375 if (pm_runtime_status_suspended(dev)) {
1376 pm_runtime_disable(dev);
1377 if (pm_runtime_suspended_if_enabled(dev))
1378 goto Complete;
1379
1380 pm_runtime_enable(dev);
1381 }
1382 dev->power.direct_complete = false;
1383 }
1384
Benoit Goby70fea602013-10-17 10:48:46 -07001385 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001386 device_lock(dev);
1387
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001388 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001389 info = "power domain ";
1390 callback = pm_op(&dev->pm_domain->ops, state);
1391 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001392 }
1393
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001394 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001395 info = "type ";
1396 callback = pm_op(dev->type->pm, state);
1397 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001398 }
1399
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001400 if (dev->class) {
1401 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001402 info = "class ";
1403 callback = pm_op(dev->class->pm, state);
1404 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001405 } else if (dev->class->suspend) {
1406 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001407 error = legacy_suspend(dev, state, dev->class->suspend,
1408 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001409 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001410 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001411 }
1412
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001413 if (dev->bus) {
1414 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001415 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001416 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001417 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001418 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001419 error = legacy_suspend(dev, state, dev->bus->suspend,
1420 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001421 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001422 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001423 }
1424
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001425 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001426 if (!callback && dev->driver && dev->driver->pm) {
1427 info = "driver ";
1428 callback = pm_op(dev->driver->pm, state);
1429 }
1430
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001431 error = dpm_run_callback(callback, dev, state, info);
1432
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001433 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001434 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001435 struct device *parent = dev->parent;
1436
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001437 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001438 if (parent) {
1439 spin_lock_irq(&parent->power.lock);
1440
1441 dev->parent->power.direct_complete = false;
1442 if (dev->power.wakeup_path
1443 && !dev->parent->power.ignore_children)
1444 dev->parent->power.wakeup_path = true;
1445
1446 spin_unlock_irq(&parent->power.lock);
1447 }
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001448 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001449
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001450 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001451 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001452
1453 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001454 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001455 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001456 async_error = error;
1457
Zhonghui Fu431d4522015-03-18 15:54:27 +01001458 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001459 return error;
1460}
1461
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001462static void async_suspend(void *data, async_cookie_t cookie)
1463{
1464 struct device *dev = (struct device *)data;
1465 int error;
1466
1467 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001468 if (error) {
1469 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001470 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001471 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001472
1473 put_device(dev);
1474}
1475
1476static int device_suspend(struct device *dev)
1477{
Wolfram Sang16735d02013-11-14 14:32:02 -08001478 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001479
Zhonghui Fu431d4522015-03-18 15:54:27 +01001480 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001481 get_device(dev);
1482 async_schedule(async_suspend, dev);
1483 return 0;
1484 }
1485
1486 return __device_suspend(dev, pm_transition, false);
1487}
1488
Alan Sterncd59abf2007-09-21 15:36:56 -04001489/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001490 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1491 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001492 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001493int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001494{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001495 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001496 int error = 0;
1497
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001498 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001499 might_sleep();
1500
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001501 cpufreq_suspend();
1502
Alan Sterncd59abf2007-09-21 15:36:56 -04001503 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001504 pm_transition = state;
1505 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001506 while (!list_empty(&dpm_prepared_list)) {
1507 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001508
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001509 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001510 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001511
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001512 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001513
Alan Stern1b3cbec2008-02-29 11:50:22 -05001514 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001515 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001516 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001517 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001518 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001519 break;
1520 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001521 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001522 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001523 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001524 if (async_error)
1525 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001526 }
1527 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001528 async_synchronize_full();
1529 if (!error)
1530 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001531 if (error) {
1532 suspend_stats.failed_suspend++;
1533 dpm_save_failed_step(SUSPEND_SUSPEND);
1534 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001535 dpm_show_time(starttime, state, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001536 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001537 return error;
1538}
1539
1540/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001541 * device_prepare - Prepare a device for system power transition.
1542 * @dev: Device to handle.
1543 * @state: PM transition of the system being carried out.
1544 *
1545 * Execute the ->prepare() callback(s) for given device. No new children of the
1546 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001547 */
Alan Sternd1616302009-05-24 22:05:42 +02001548static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001549{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001550 int (*callback)(struct device *) = NULL;
1551 char *info = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001552 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001553
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001554 if (dev->power.syscore)
1555 return 0;
1556
Alan Stern88d26132012-09-19 21:59:02 +02001557 /*
1558 * If a device's parent goes into runtime suspend at the wrong time,
1559 * it won't be possible to resume the device. To prevent this we
1560 * block runtime suspend here, during the prepare phase, and allow
1561 * it again during the complete phase.
1562 */
1563 pm_runtime_get_noresume(dev);
1564
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001565 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001566
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001567 dev->power.wakeup_path = device_may_wakeup(dev);
1568
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001569 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001570 info = "preparing power domain ";
1571 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001572 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001573 info = "preparing type ";
1574 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001575 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001576 info = "preparing class ";
1577 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001578 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001579 info = "preparing bus ";
1580 callback = dev->bus->pm->prepare;
1581 }
1582
1583 if (!callback && dev->driver && dev->driver->pm) {
1584 info = "preparing driver ";
1585 callback = dev->driver->pm->prepare;
1586 }
1587
Todd E Brandt32e8d682015-05-28 12:55:53 -07001588 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001589 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001590
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001591 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001592
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001593 if (ret < 0) {
1594 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001595 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001596 return ret;
1597 }
1598 /*
1599 * A positive return value from ->prepare() means "this device appears
1600 * to be runtime-suspended and its state is fine, so if it really is
1601 * runtime-suspended, you can leave it in that state provided that you
1602 * will do the same thing with all of its descendants". This only
1603 * applies to suspend transitions, however.
1604 */
1605 spin_lock_irq(&dev->power.lock);
1606 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1607 spin_unlock_irq(&dev->power.lock);
1608 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001609}
1610
1611/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001612 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1613 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001614 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001615 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001616 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001617int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001618{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001619 int error = 0;
1620
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001621 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001622 might_sleep();
1623
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001624 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001625 while (!list_empty(&dpm_list)) {
1626 struct device *dev = to_device(dpm_list.next);
1627
1628 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001629 mutex_unlock(&dpm_list_mtx);
1630
Todd E Brandt32e8d682015-05-28 12:55:53 -07001631 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001632 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001633 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001634
1635 mutex_lock(&dpm_list_mtx);
1636 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001637 if (error == -EAGAIN) {
1638 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001639 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001640 continue;
1641 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001642 printk(KERN_INFO "PM: Device %s not prepared "
1643 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001644 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001645 put_device(dev);
1646 break;
1647 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001648 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001649 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001650 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001651 put_device(dev);
1652 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001653 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001654 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001655 return error;
1656}
1657
1658/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001659 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1660 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001661 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001662 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1663 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001664 */
Alan Sternd1616302009-05-24 22:05:42 +02001665int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001666{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001667 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001668
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001669 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001670 if (error) {
1671 suspend_stats.failed_prepare++;
1672 dpm_save_failed_step(SUSPEND_PREPARE);
1673 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001674 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001675 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001676}
Alan Sternd1616302009-05-24 22:05:42 +02001677EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001678
1679void __suspend_report_result(const char *function, void *fn, int ret)
1680{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001681 if (ret)
1682 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001683}
1684EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001685
1686/**
1687 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1688 * @dev: Device to wait for.
1689 * @subordinate: Device that needs to wait for @dev.
1690 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001691int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001692{
1693 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001694 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001695}
1696EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001697
1698/**
1699 * dpm_for_each_dev - device iterator.
1700 * @data: data for the callback.
1701 * @fn: function to be called for each device.
1702 *
1703 * Iterate over devices in dpm_list, and call @fn for each device,
1704 * passing it @data.
1705 */
1706void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1707{
1708 struct device *dev;
1709
1710 if (!fn)
1711 return;
1712
1713 device_pm_lock();
1714 list_for_each_entry(dev, &dpm_list, power.entry)
1715 fn(dev, data);
1716 device_pm_unlock();
1717}
1718EXPORT_SYMBOL_GPL(dpm_for_each_dev);