blob: c1e56c3f845500160d5051b606849d627df6fa0d [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>
Tony Lindgren4990d4f2015-05-18 15:40:29 -070027#include <linux/pm_wakeirq.h>
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +010028#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010029#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010030#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010031#include <linux/suspend.h>
Shuah Khan53644672013-07-26 13:30:20 -060032#include <trace/events/power.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080033#include <linux/cpufreq.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020034#include <linux/cpuidle.h>
Benoit Goby70fea602013-10-17 10:48:46 -070035#include <linux/timer.h>
Ruchi Kandoiddd401e2014-10-29 10:36:27 -070036#include <linux/wakeup_reason.h>
Benoit Goby70fea602013-10-17 10:48:46 -070037
Alan Sterncd59abf2007-09-21 15:36:56 -040038#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "power.h"
40
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010041typedef int (*pm_callback_t)(struct device *);
42
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010043/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020044 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010045 * because children are guaranteed to be discovered after parents, and
46 * are inserted at the back of the list on discovery.
47 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080048 * Since device_pm_add() may be called with a device lock held,
49 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010050 * dpm_list_mutex.
51 */
52
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020053LIST_HEAD(dpm_list);
Sachin Kamat7664e962012-07-17 22:38:08 +020054static LIST_HEAD(dpm_prepared_list);
55static LIST_HEAD(dpm_suspended_list);
56static LIST_HEAD(dpm_late_early_list);
57static LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
ShuoX Liu2a77c462011-08-10 23:01:26 +020059struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040060static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010061static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020063static int async_error;
64
Shuah Khan53644672013-07-26 13:30:20 -060065static char *pm_verb(int event)
66{
67 switch (event) {
68 case PM_EVENT_SUSPEND:
69 return "suspend";
70 case PM_EVENT_RESUME:
71 return "resume";
72 case PM_EVENT_FREEZE:
73 return "freeze";
74 case PM_EVENT_QUIESCE:
75 return "quiesce";
76 case PM_EVENT_HIBERNATE:
77 return "hibernate";
78 case PM_EVENT_THAW:
79 return "thaw";
80 case PM_EVENT_RESTORE:
81 return "restore";
82 case PM_EVENT_RECOVER:
83 return "recover";
84 default:
85 return "(unknown PM event)";
86 }
87}
88
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020089/**
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020090 * device_pm_sleep_init - Initialize system suspend-related device fields.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020091 * @dev: Device object being initialized.
92 */
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020093void device_pm_sleep_init(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020094{
Alan Sternf76b168b2011-06-18 20:22:23 +020095 dev->power.is_prepared = false;
Alan Stern6d0e0e82011-06-18 22:42:09 +020096 dev->power.is_suspended = false;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +080097 dev->power.is_noirq_suspended = false;
98 dev->power.is_late_suspended = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010099 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +0200100 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200101 dev->power.wakeup = NULL;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200102 INIT_LIST_HEAD(&dev->power.entry);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200103}
104
105/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200106 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200107 */
108void device_pm_lock(void)
109{
110 mutex_lock(&dpm_list_mtx);
111}
112
113/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200114 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200115 */
116void device_pm_unlock(void)
117{
118 mutex_unlock(&dpm_list_mtx);
119}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100120
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100121/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200122 * device_pm_add - Add a device to the PM core's list of active devices.
123 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100124 */
Alan Stern3b98aea2008-08-07 13:06:12 -0400125void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100128 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +0100129 device_pm_check_callbacks(dev);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700130 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +0200131 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +0100132 dev_warn(dev, "parent %s should not be sleeping\n",
133 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400134 list_add_tail(&dev->power.entry, &dpm_list);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200135 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100138/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200139 * device_pm_remove - Remove a device from the PM core's list of active devices.
140 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100141 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200142void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100145 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100146 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700147 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700149 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200150 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200151 pm_runtime_remove(dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +0100152 device_pm_check_callbacks(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100153}
154
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200155/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200156 * device_pm_move_before - Move device in the PM core's list of active devices.
157 * @deva: Device to move in dpm_list.
158 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100159 */
160void device_pm_move_before(struct device *deva, struct device *devb)
161{
162 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100163 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
164 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Sravan Kumar Ambapuram245504f2014-03-25 15:00:54 +0530165 if (!((devb->pm_domain) || (devb->type && devb->type->pm)
166 || (devb->class && (devb->class->pm || devb->class->resume))
167 || (devb->bus && (devb->bus->pm || devb->bus->resume)) ||
168 (devb->driver && devb->driver->pm))) {
169 device_pm_add(devb);
170 }
Cornelia Huckffa6a702009-03-04 12:44:00 +0100171 /* Delete deva from dpm_list and reinsert before devb. */
172 list_move_tail(&deva->power.entry, &devb->power.entry);
173}
174
175/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200176 * device_pm_move_after - Move device in the PM core's list of active devices.
177 * @deva: Device to move in dpm_list.
178 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100179 */
180void device_pm_move_after(struct device *deva, struct device *devb)
181{
182 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100183 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
184 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Sravan Kumar Ambapuram245504f2014-03-25 15:00:54 +0530185 if (!((devb->pm_domain) || (devb->type && devb->type->pm)
186 || (devb->class && (devb->class->pm || devb->class->resume))
187 || (devb->bus && (devb->bus->pm || devb->bus->resume)) ||
188 (devb->driver && devb->driver->pm))) {
189 device_pm_add(devb);
190 }
Cornelia Huckffa6a702009-03-04 12:44:00 +0100191 /* Delete deva from dpm_list and reinsert after devb. */
192 list_move(&deva->power.entry, &devb->power.entry);
193}
194
195/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200196 * device_pm_move_last - Move device to end of the PM core's list of devices.
197 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100198 */
199void device_pm_move_last(struct device *dev)
200{
201 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100202 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100203 list_move_tail(&dev->power.entry, &dpm_list);
204}
205
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100206static ktime_t initcall_debug_start(struct device *dev)
207{
208 ktime_t calltime = ktime_set(0, 0);
209
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200210 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100211 pr_info("calling %s+ @ %i, parent: %s\n",
212 dev_name(dev), task_pid_nr(current),
213 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100214 calltime = ktime_get();
215 }
216
217 return calltime;
218}
219
220static void initcall_debug_report(struct device *dev, ktime_t calltime,
Shuah Khan53644672013-07-26 13:30:20 -0600221 int error, pm_message_t state, char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100222{
Shuah Khan53644672013-07-26 13:30:20 -0600223 ktime_t rettime;
224 s64 nsecs;
225
226 rettime = ktime_get();
227 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100228
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200229 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100230 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
Shuah Khan53644672013-07-26 13:30:20 -0600231 error, (unsigned long long)nsecs >> 10);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100232 }
233}
234
Cornelia Huckffa6a702009-03-04 12:44:00 +0100235/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100236 * dpm_wait - Wait for a PM operation to complete.
237 * @dev: Device to wait for.
238 * @async: If unset, wait only if the device's power.async_suspend flag is set.
239 */
240static void dpm_wait(struct device *dev, bool async)
241{
242 if (!dev)
243 return;
244
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100245 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100246 wait_for_completion(&dev->power.completion);
247}
248
249static int dpm_wait_fn(struct device *dev, void *async_ptr)
250{
251 dpm_wait(dev, *((bool *)async_ptr));
252 return 0;
253}
254
255static void dpm_wait_for_children(struct device *dev, bool async)
256{
257 device_for_each_child(dev, &async, dpm_wait_fn);
258}
259
260/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100261 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200262 * @ops: PM operations to choose from.
263 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200264 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100265static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200266{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200267 switch (state.event) {
268#ifdef CONFIG_SUSPEND
269 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100270 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200271 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100272 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200273#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200274#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200275 case PM_EVENT_FREEZE:
276 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100277 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200278 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100279 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200280 case PM_EVENT_THAW:
281 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100282 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200283 break;
284 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100285 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200286#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200287 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100288
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100289 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200290}
291
292/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100293 * pm_late_early_op - Return the PM operation appropriate for given PM event.
294 * @ops: PM operations to choose from.
295 * @state: PM transition of the system being carried out.
296 *
297 * Runtime PM is disabled for @dev while this function is being executed.
298 */
299static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
300 pm_message_t state)
301{
302 switch (state.event) {
303#ifdef CONFIG_SUSPEND
304 case PM_EVENT_SUSPEND:
305 return ops->suspend_late;
306 case PM_EVENT_RESUME:
307 return ops->resume_early;
308#endif /* CONFIG_SUSPEND */
309#ifdef CONFIG_HIBERNATE_CALLBACKS
310 case PM_EVENT_FREEZE:
311 case PM_EVENT_QUIESCE:
312 return ops->freeze_late;
313 case PM_EVENT_HIBERNATE:
314 return ops->poweroff_late;
315 case PM_EVENT_THAW:
316 case PM_EVENT_RECOVER:
317 return ops->thaw_early;
318 case PM_EVENT_RESTORE:
319 return ops->restore_early;
320#endif /* CONFIG_HIBERNATE_CALLBACKS */
321 }
322
323 return NULL;
324}
325
326/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100327 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200328 * @ops: PM operations to choose from.
329 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200330 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200331 * The driver of @dev will not receive interrupts while this function is being
332 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200333 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100334static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200335{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200336 switch (state.event) {
337#ifdef CONFIG_SUSPEND
338 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100339 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200340 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100341 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200342#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200343#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200344 case PM_EVENT_FREEZE:
345 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100346 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200347 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100348 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200349 case PM_EVENT_THAW:
350 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100351 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200352 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100353 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200354#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200355 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100356
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100357 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200358}
359
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200360static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
361{
362 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
363 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
364 ", may wakeup" : "");
365}
366
367static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
368 int error)
369{
370 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100371 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200372}
373
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100374static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
375{
376 ktime_t calltime;
Kevin Cernekee0702d9e2010-09-20 22:32:10 +0200377 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100378 int usecs;
379
380 calltime = ktime_get();
381 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
382 do_div(usecs64, NSEC_PER_USEC);
383 usecs = usecs64;
384 if (usecs == 0)
385 usecs = 1;
386 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
387 info ?: "", info ? " " : "", pm_verb(state.event),
388 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
389}
390
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100391static int dpm_run_callback(pm_callback_t cb, struct device *dev,
392 pm_message_t state, char *info)
393{
394 ktime_t calltime;
395 int error;
396
397 if (!cb)
398 return 0;
399
400 calltime = initcall_debug_start(dev);
401
402 pm_dev_dbg(dev, state, info);
Todd E Brandte8bca472014-06-10 07:31:22 -0700403 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100404 error = cb(dev);
Todd E Brandte8bca472014-06-10 07:31:22 -0700405 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100406 suspend_report_result(cb, error);
407
Shuah Khan53644672013-07-26 13:30:20 -0600408 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100409
410 return error;
411}
412
Benoit Goby70fea602013-10-17 10:48:46 -0700413#ifdef CONFIG_DPM_WATCHDOG
414struct dpm_watchdog {
415 struct device *dev;
416 struct task_struct *tsk;
417 struct timer_list timer;
418};
419
420#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
421 struct dpm_watchdog wd
422
423/**
424 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
425 * @data: Watchdog object address.
426 *
427 * Called when a driver has timed out suspending or resuming.
428 * There's not much we can do here to recover so panic() to
429 * capture a crash-dump in pstore.
430 */
431static void dpm_watchdog_handler(unsigned long data)
432{
433 struct dpm_watchdog *wd = (void *)data;
434
435 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
436 show_stack(wd->tsk, NULL);
437 panic("%s %s: unrecoverable failure\n",
438 dev_driver_string(wd->dev), dev_name(wd->dev));
439}
440
441/**
442 * dpm_watchdog_set - Enable pm watchdog for given device.
443 * @wd: Watchdog. Must be allocated on the stack.
444 * @dev: Device to handle.
445 */
446static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
447{
448 struct timer_list *timer = &wd->timer;
449
450 wd->dev = dev;
451 wd->tsk = current;
452
453 init_timer_on_stack(timer);
454 /* use same timeout value for both suspend and resume */
455 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
456 timer->function = dpm_watchdog_handler;
457 timer->data = (unsigned long)wd;
458 add_timer(timer);
459}
460
461/**
462 * dpm_watchdog_clear - Disable suspend/resume watchdog.
463 * @wd: Watchdog to disable.
464 */
465static void dpm_watchdog_clear(struct dpm_watchdog *wd)
466{
467 struct timer_list *timer = &wd->timer;
468
469 del_timer_sync(timer);
470 destroy_timer_on_stack(timer);
471}
472#else
473#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
474#define dpm_watchdog_set(x, y)
475#define dpm_watchdog_clear(x)
476#endif
477
Alan Sterncd59abf2007-09-21 15:36:56 -0400478/*------------------------- Resume routines -------------------------*/
479
480/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200481 * device_resume_noirq - Execute an "early resume" callback for given device.
482 * @dev: Device to handle.
483 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700484 * @async: If true, the device is being resumed asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400485 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200486 * The driver of @dev will not receive interrupts while this function is being
487 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400488 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800489static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400490{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100491 pm_callback_t callback = NULL;
492 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400493 int error = 0;
494
495 TRACE_DEVICE(dev);
496 TRACE_RESUME(0);
497
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200498 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200499 goto Out;
500
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800501 if (!dev->power.is_noirq_suspended)
502 goto Out;
503
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800504 dpm_wait(dev->parent, async);
505
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200506 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100507 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100508 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200509 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100510 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100511 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100512 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100513 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100514 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100515 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100516 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100517 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100518 }
519
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100520 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100521 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100522 callback = pm_noirq_op(dev->driver->pm, state);
523 }
524
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100525 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800526 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100527
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200528 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800529 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100530 TRACE_RESUME(error);
531 return error;
532}
533
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800534static bool is_async(struct device *dev)
535{
536 return dev->power.async_suspend && pm_async_enabled
537 && !pm_trace_is_enabled();
538}
539
540static void async_resume_noirq(void *data, async_cookie_t cookie)
541{
542 struct device *dev = (struct device *)data;
543 int error;
544
545 error = device_resume_noirq(dev, pm_transition, true);
546 if (error)
547 pm_dev_err(dev, pm_transition, " async", error);
548
549 put_device(dev);
550}
551
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100552/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100553 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200554 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100555 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100556 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200557 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100558 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200559void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100560{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800561 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100562 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100563
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700564 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200565 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800566 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100567
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800568 /*
569 * Advanced the async threads upfront,
570 * in case the starting of async threads is
571 * delayed by non-async resuming devices.
572 */
573 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
574 reinit_completion(&dev->power.completion);
575 if (is_async(dev)) {
576 get_device(dev);
577 async_schedule(async_resume_noirq, dev);
578 }
579 }
580
581 while (!list_empty(&dpm_noirq_list)) {
582 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100583 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100584 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100585 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100586
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800587 if (!is_async(dev)) {
588 int error;
589
590 error = device_resume_noirq(dev, state, false);
591 if (error) {
592 suspend_stats.failed_resume_noirq++;
593 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
594 dpm_save_failed_dev(dev_name(dev));
595 pm_dev_err(dev, state, " noirq", error);
596 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100597 }
598
599 mutex_lock(&dpm_list_mtx);
600 put_device(dev);
601 }
602 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800603 async_synchronize_full();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100604 dpm_show_time(starttime, state, "noirq");
605 resume_device_irqs();
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700606 device_wakeup_disarm_wake_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200607 cpuidle_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700608 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100609}
610
611/**
612 * device_resume_early - Execute an "early resume" callback for given device.
613 * @dev: Device to handle.
614 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700615 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100616 *
617 * Runtime PM is disabled for @dev while this function is being executed.
618 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800619static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100620{
621 pm_callback_t callback = NULL;
622 char *info = NULL;
623 int error = 0;
624
625 TRACE_DEVICE(dev);
626 TRACE_RESUME(0);
627
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200628 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200629 goto Out;
630
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800631 if (!dev->power.is_late_suspended)
632 goto Out;
633
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800634 dpm_wait(dev->parent, async);
635
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100636 if (dev->pm_domain) {
637 info = "early power domain ";
638 callback = pm_late_early_op(&dev->pm_domain->ops, state);
639 } else if (dev->type && dev->type->pm) {
640 info = "early type ";
641 callback = pm_late_early_op(dev->type->pm, state);
642 } else if (dev->class && dev->class->pm) {
643 info = "early class ";
644 callback = pm_late_early_op(dev->class->pm, state);
645 } else if (dev->bus && dev->bus->pm) {
646 info = "early bus ";
647 callback = pm_late_early_op(dev->bus->pm, state);
648 }
649
650 if (!callback && dev->driver && dev->driver->pm) {
651 info = "early driver ";
652 callback = pm_late_early_op(dev->driver->pm, state);
653 }
654
655 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800656 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100657
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200658 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100659 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100660
661 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800662 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100663 return error;
664}
665
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800666static void async_resume_early(void *data, async_cookie_t cookie)
667{
668 struct device *dev = (struct device *)data;
669 int error;
670
671 error = device_resume_early(dev, pm_transition, true);
672 if (error)
673 pm_dev_err(dev, pm_transition, " async", error);
674
675 put_device(dev);
676}
677
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100678/**
679 * dpm_resume_early - Execute "early resume" callbacks for all devices.
680 * @state: PM transition of the system being carried out.
681 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200682void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100683{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800684 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100685 ktime_t starttime = ktime_get();
686
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700687 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100688 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800689 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100690
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800691 /*
692 * Advanced the async threads upfront,
693 * in case the starting of async threads is
694 * delayed by non-async resuming devices.
695 */
696 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
697 reinit_completion(&dev->power.completion);
698 if (is_async(dev)) {
699 get_device(dev);
700 async_schedule(async_resume_early, dev);
701 }
702 }
703
704 while (!list_empty(&dpm_late_early_list)) {
705 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100706 get_device(dev);
707 list_move_tail(&dev->power.entry, &dpm_suspended_list);
708 mutex_unlock(&dpm_list_mtx);
709
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800710 if (!is_async(dev)) {
711 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100712
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800713 error = device_resume_early(dev, state, false);
714 if (error) {
715 suspend_stats.failed_resume_early++;
716 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
717 dpm_save_failed_dev(dev_name(dev));
718 pm_dev_err(dev, state, " early", error);
719 }
720 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100721 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100722 put_device(dev);
723 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200724 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800725 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100726 dpm_show_time(starttime, state, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700727 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100728}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100729
730/**
731 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
732 * @state: PM transition of the system being carried out.
733 */
734void dpm_resume_start(pm_message_t state)
735{
736 dpm_resume_noirq(state);
737 dpm_resume_early(state);
738}
739EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100740
741/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100742 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200743 * @dev: Device to handle.
744 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100745 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100746 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100747static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100748{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100749 pm_callback_t callback = NULL;
750 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100751 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700752 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100753
754 TRACE_DEVICE(dev);
755 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400756
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200757 if (dev->power.syscore)
758 goto Complete;
759
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200760 if (dev->power.direct_complete) {
761 /* Match the pm_runtime_disable() in __device_suspend(). */
762 pm_runtime_enable(dev);
763 goto Complete;
764 }
765
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100766 dpm_wait(dev->parent, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700767 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800768 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100769
Alan Sternf76b168b2011-06-18 20:22:23 +0200770 /*
771 * This is a fib. But we'll allow new children to be added below
772 * a resumed device, even if the device hasn't been completed yet.
773 */
774 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100775
Alan Stern6d0e0e82011-06-18 22:42:09 +0200776 if (!dev->power.is_suspended)
777 goto Unlock;
778
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200779 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100780 info = "power domain ";
781 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100782 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100783 }
784
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100785 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100786 info = "type ";
787 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100788 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100789 }
790
791 if (dev->class) {
792 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100793 info = "class ";
794 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100795 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100796 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100797 info = "legacy class ";
798 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100799 goto End;
800 }
801 }
802
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200803 if (dev->bus) {
804 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100805 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100806 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200807 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100808 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100809 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100810 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200811 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400812 }
813
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100814 Driver:
815 if (!callback && dev->driver && dev->driver->pm) {
816 info = "driver ";
817 callback = pm_op(dev->driver->pm, state);
818 }
819
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200820 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100821 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200822 dev->power.is_suspended = false;
823
824 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800825 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700826 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200827
828 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100829 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100830
Alan Sterncd59abf2007-09-21 15:36:56 -0400831 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200832
Alan Sterncd59abf2007-09-21 15:36:56 -0400833 return error;
834}
835
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100836static void async_resume(void *data, async_cookie_t cookie)
837{
838 struct device *dev = (struct device *)data;
839 int error;
840
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100841 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100842 if (error)
843 pm_dev_err(dev, pm_transition, " async", error);
844 put_device(dev);
845}
846
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100847/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200848 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
849 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100850 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200851 * Execute the appropriate "resume" callback for all devices whose status
852 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400853 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200854void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400855{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100856 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100857 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400858
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700859 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200860 might_sleep();
861
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200862 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100863 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200864 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200865
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100866 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800867 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100868 if (is_async(dev)) {
869 get_device(dev);
870 async_schedule(async_resume, dev);
871 }
872 }
873
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100874 while (!list_empty(&dpm_suspended_list)) {
875 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200876 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100877 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200878 int error;
879
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200880 mutex_unlock(&dpm_list_mtx);
881
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100882 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200883 if (error) {
884 suspend_stats.failed_resume++;
885 dpm_save_failed_step(SUSPEND_RESUME);
886 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200887 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200888 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100889
890 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200891 }
892 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100893 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200894 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400895 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200896 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100897 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100898 dpm_show_time(starttime, state, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800899
900 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700901 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200902}
903
904/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200905 * device_complete - Complete a PM transition for given device.
906 * @dev: Device to handle.
907 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200908 */
Alan Sternd1616302009-05-24 22:05:42 +0200909static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200910{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100911 void (*callback)(struct device *) = NULL;
912 char *info = NULL;
913
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200914 if (dev->power.syscore)
915 return;
916
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800917 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200918
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200919 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100920 info = "completing power domain ";
921 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200922 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100923 info = "completing type ";
924 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100925 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100926 info = "completing class ";
927 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100928 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100929 info = "completing bus ";
930 callback = dev->bus->pm->complete;
931 }
932
933 if (!callback && dev->driver && dev->driver->pm) {
934 info = "completing driver ";
935 callback = dev->driver->pm->complete;
936 }
937
938 if (callback) {
939 pm_dev_dbg(dev, state, info);
940 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200941 }
942
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800943 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200944
Ulf Hanssonaf939332013-04-12 09:41:06 +0000945 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200946}
947
948/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200949 * dpm_complete - Complete a PM transition for all non-sysdev devices.
950 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200951 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200952 * Execute the ->complete() callbacks for all devices whose PM status is not
953 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200955void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200956{
957 struct list_head list;
958
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700959 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200960 might_sleep();
961
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200962 INIT_LIST_HEAD(&list);
963 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100964 while (!list_empty(&dpm_prepared_list)) {
965 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200966
967 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200968 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100969 list_move(&dev->power.entry, &list);
970 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200971
Todd E Brandt32e8d682015-05-28 12:55:53 -0700972 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100973 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -0700974 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200975
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100976 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200977 put_device(dev);
978 }
979 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400980 mutex_unlock(&dpm_list_mtx);
Strashko, Grygorii013c0742015-11-10 11:42:34 +0200981
982 /* Allow device probing and trigger re-probing of deferred devices */
983 device_unblock_probing();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700984 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -0400985}
986
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100987/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200988 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
989 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400990 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200991 * Execute "resume" callbacks for all devices and complete the PM transition of
992 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400993 */
Alan Sternd1616302009-05-24 22:05:42 +0200994void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400995{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200996 dpm_resume(state);
997 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400998}
Alan Sternd1616302009-05-24 22:05:42 +0200999EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -04001000
1001
Alan Sterncd59abf2007-09-21 15:36:56 -04001002/*------------------------- Suspend routines -------------------------*/
1003
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001004/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001005 * resume_event - Return a "resume" message for given "suspend" sleep state.
1006 * @sleep_state: PM message representing a sleep state.
1007 *
1008 * Return a PM message representing the resume event corresponding to given
1009 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001010 */
1011static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001012{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001013 switch (sleep_state.event) {
1014 case PM_EVENT_SUSPEND:
1015 return PMSG_RESUME;
1016 case PM_EVENT_FREEZE:
1017 case PM_EVENT_QUIESCE:
1018 return PMSG_RECOVER;
1019 case PM_EVENT_HIBERNATE:
1020 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001021 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001022 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001023}
1024
1025/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001026 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1027 * @dev: Device to handle.
1028 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001029 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001030 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001031 * The driver of @dev will not receive interrupts while this function is being
1032 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001033 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001034static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001035{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001036 pm_callback_t callback = NULL;
1037 char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001038 int error = 0;
1039
Zhonghui Fu431d4522015-03-18 15:54:27 +01001040 TRACE_DEVICE(dev);
1041 TRACE_SUSPEND(0);
1042
Brian Norris6f75c3f2016-11-09 17:21:08 -08001043 dpm_wait_for_children(dev, async);
1044
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001045 if (async_error)
1046 goto Complete;
1047
1048 if (pm_wakeup_pending()) {
1049 async_error = -EBUSY;
1050 goto Complete;
1051 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +01001052
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001053 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001054 goto Complete;
1055
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001056 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001057 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001058 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001059 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001060 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001061 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001062 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001063 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001064 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001065 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001066 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001067 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001068 }
1069
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001070 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001071 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001072 callback = pm_noirq_op(dev->driver->pm, state);
1073 }
1074
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001075 error = dpm_run_callback(callback, dev, state, info);
1076 if (!error)
1077 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001078 else
1079 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001080
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001081Complete:
1082 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001083 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001084 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001085}
1086
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001087static void async_suspend_noirq(void *data, async_cookie_t cookie)
1088{
1089 struct device *dev = (struct device *)data;
1090 int error;
1091
1092 error = __device_suspend_noirq(dev, pm_transition, true);
1093 if (error) {
1094 dpm_save_failed_dev(dev_name(dev));
1095 pm_dev_err(dev, pm_transition, " async", error);
1096 }
1097
1098 put_device(dev);
1099}
1100
1101static int device_suspend_noirq(struct device *dev)
1102{
1103 reinit_completion(&dev->power.completion);
1104
Zhonghui Fu431d4522015-03-18 15:54:27 +01001105 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001106 get_device(dev);
1107 async_schedule(async_suspend_noirq, dev);
1108 return 0;
1109 }
1110 return __device_suspend_noirq(dev, pm_transition, false);
1111}
1112
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001113/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001114 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001115 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001116 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001117 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1118 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001119 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001120int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001121{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001122 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001123 int error = 0;
1124
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001125 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Preeti U Murthy8651f972012-07-09 10:12:56 +02001126 cpuidle_pause();
Tony Lindgren4990d4f2015-05-18 15:40:29 -07001127 device_wakeup_arm_wake_irqs();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +01001128 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001129 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001130 pm_transition = state;
1131 async_error = 0;
1132
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001133 while (!list_empty(&dpm_late_early_list)) {
1134 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001135
1136 get_device(dev);
1137 mutex_unlock(&dpm_list_mtx);
1138
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001139 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001140
1141 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001142 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001143 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001144 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001145 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001146 break;
1147 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001148 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001149 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001150 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001151
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001152 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001153 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001154 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001155 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001156 async_synchronize_full();
1157 if (!error)
1158 error = async_error;
1159
1160 if (error) {
1161 suspend_stats.failed_suspend_noirq++;
1162 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Alan Sternd1616302009-05-24 22:05:42 +02001163 dpm_resume_noirq(resume_event(state));
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001164 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001165 dpm_show_time(starttime, state, "noirq");
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001166 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001167 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001168 return error;
1169}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001170
1171/**
1172 * device_suspend_late - Execute a "late suspend" callback for given device.
1173 * @dev: Device to handle.
1174 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001175 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001176 *
1177 * Runtime PM is disabled for @dev while this function is being executed.
1178 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001179static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001180{
1181 pm_callback_t callback = NULL;
1182 char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001183 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001184
Zhonghui Fu431d4522015-03-18 15:54:27 +01001185 TRACE_DEVICE(dev);
1186 TRACE_SUSPEND(0);
1187
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001188 __pm_runtime_disable(dev, false);
1189
Brian Norris6f75c3f2016-11-09 17:21:08 -08001190 dpm_wait_for_children(dev, async);
1191
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001192 if (async_error)
1193 goto Complete;
1194
1195 if (pm_wakeup_pending()) {
1196 async_error = -EBUSY;
1197 goto Complete;
1198 }
1199
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001200 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001201 goto Complete;
1202
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001203 if (dev->pm_domain) {
1204 info = "late power domain ";
1205 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1206 } else if (dev->type && dev->type->pm) {
1207 info = "late type ";
1208 callback = pm_late_early_op(dev->type->pm, state);
1209 } else if (dev->class && dev->class->pm) {
1210 info = "late class ";
1211 callback = pm_late_early_op(dev->class->pm, state);
1212 } else if (dev->bus && dev->bus->pm) {
1213 info = "late bus ";
1214 callback = pm_late_early_op(dev->bus->pm, state);
1215 }
1216
1217 if (!callback && dev->driver && dev->driver->pm) {
1218 info = "late driver ";
1219 callback = pm_late_early_op(dev->driver->pm, state);
1220 }
1221
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001222 error = dpm_run_callback(callback, dev, state, info);
1223 if (!error)
1224 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001225 else
1226 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001227
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001228Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001229 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001230 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001231 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001232}
1233
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001234static void async_suspend_late(void *data, async_cookie_t cookie)
1235{
1236 struct device *dev = (struct device *)data;
1237 int error;
1238
1239 error = __device_suspend_late(dev, pm_transition, true);
1240 if (error) {
1241 dpm_save_failed_dev(dev_name(dev));
1242 pm_dev_err(dev, pm_transition, " async", error);
1243 }
1244 put_device(dev);
1245}
1246
1247static int device_suspend_late(struct device *dev)
1248{
1249 reinit_completion(&dev->power.completion);
1250
Zhonghui Fu431d4522015-03-18 15:54:27 +01001251 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001252 get_device(dev);
1253 async_schedule(async_suspend_late, dev);
1254 return 0;
1255 }
1256
1257 return __device_suspend_late(dev, pm_transition, false);
1258}
1259
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001260/**
1261 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1262 * @state: PM transition of the system being carried out.
1263 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001264int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001265{
1266 ktime_t starttime = ktime_get();
1267 int error = 0;
1268
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001269 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001270 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001271 pm_transition = state;
1272 async_error = 0;
1273
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001274 while (!list_empty(&dpm_suspended_list)) {
1275 struct device *dev = to_device(dpm_suspended_list.prev);
1276
1277 get_device(dev);
1278 mutex_unlock(&dpm_list_mtx);
1279
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001280 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001281
1282 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki3a17fb32016-05-20 23:09:49 +02001283 if (!list_empty(&dev->power.entry))
1284 list_move(&dev->power.entry, &dpm_late_early_list);
1285
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001286 if (error) {
1287 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001288 dpm_save_failed_dev(dev_name(dev));
1289 put_device(dev);
1290 break;
1291 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001292 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001293
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001294 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001295 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001296 }
1297 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001298 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001299 if (!error)
1300 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001301 if (error) {
1302 suspend_stats.failed_suspend_late++;
1303 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001304 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001305 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001306 dpm_show_time(starttime, state, "late");
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001307 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001308 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001309 return error;
1310}
1311
1312/**
1313 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1314 * @state: PM transition of the system being carried out.
1315 */
1316int dpm_suspend_end(pm_message_t state)
1317{
1318 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001319 if (error)
1320 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001321
Colin Cross064b0212012-07-19 10:38:06 +02001322 error = dpm_suspend_noirq(state);
1323 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001324 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001325 return error;
1326 }
1327
1328 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001329}
1330EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001331
1332/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001333 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001334 * @dev: Device to suspend.
1335 * @state: PM transition of the system being carried out.
1336 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001337 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001338 */
1339static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001340 int (*cb)(struct device *dev, pm_message_t state),
1341 char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001342{
1343 int error;
1344 ktime_t calltime;
1345
1346 calltime = initcall_debug_start(dev);
1347
Todd E Brandte8bca472014-06-10 07:31:22 -07001348 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001349 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001350 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001351 suspend_report_result(cb, error);
1352
Shuah Khan53644672013-07-26 13:30:20 -06001353 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001354
1355 return error;
1356}
1357
1358/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001359 * device_suspend - Execute "suspend" callbacks for given device.
1360 * @dev: Device to handle.
1361 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001362 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001363 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001364static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001365{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001366 pm_callback_t callback = NULL;
1367 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001368 int error = 0;
Ruchi Kandoiddd401e2014-10-29 10:36:27 -07001369 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
Benoit Goby70fea602013-10-17 10:48:46 -07001370 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001371
Zhonghui Fu431d4522015-03-18 15:54:27 +01001372 TRACE_DEVICE(dev);
1373 TRACE_SUSPEND(0);
1374
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001375 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001376
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001377 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001378 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001379
Alan Stern88d26132012-09-19 21:59:02 +02001380 /*
1381 * If a device configured to wake up the system from sleep states
1382 * has been suspended at run time and there's a resume request pending
1383 * for it, this is equivalent to the device signaling wakeup, so the
1384 * system suspend operation should be aborted.
1385 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001386 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1387 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001388
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001389 if (pm_wakeup_pending()) {
Ruchi Kandoiddd401e2014-10-29 10:36:27 -07001390 pm_get_active_wakeup_sources(suspend_abort,
1391 MAX_SUSPEND_ABORT_LEN);
1392 log_suspend_abort_reason(suspend_abort);
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001393 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001394 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001395 }
1396
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001397 if (dev->power.syscore)
1398 goto Complete;
1399
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001400 if (dev->power.direct_complete) {
1401 if (pm_runtime_status_suspended(dev)) {
1402 pm_runtime_disable(dev);
Alan Stern019d8812015-07-15 14:40:06 +02001403 if (pm_runtime_status_suspended(dev))
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001404 goto Complete;
1405
1406 pm_runtime_enable(dev);
1407 }
1408 dev->power.direct_complete = false;
1409 }
1410
Benoit Goby70fea602013-10-17 10:48:46 -07001411 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001412 device_lock(dev);
1413
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001414 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001415 info = "power domain ";
1416 callback = pm_op(&dev->pm_domain->ops, state);
1417 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001418 }
1419
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001420 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001421 info = "type ";
1422 callback = pm_op(dev->type->pm, state);
1423 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001424 }
1425
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001426 if (dev->class) {
1427 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001428 info = "class ";
1429 callback = pm_op(dev->class->pm, state);
1430 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001431 } else if (dev->class->suspend) {
1432 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001433 error = legacy_suspend(dev, state, dev->class->suspend,
1434 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001435 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001436 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001437 }
1438
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001439 if (dev->bus) {
1440 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001441 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001442 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001443 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001444 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001445 error = legacy_suspend(dev, state, dev->bus->suspend,
1446 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001447 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001448 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001449 }
1450
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001451 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001452 if (!callback && dev->driver && dev->driver->pm) {
1453 info = "driver ";
1454 callback = pm_op(dev->driver->pm, state);
1455 }
1456
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001457 error = dpm_run_callback(callback, dev, state, info);
1458
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001459 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001460 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001461 struct device *parent = dev->parent;
1462
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001463 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001464 if (parent) {
1465 spin_lock_irq(&parent->power.lock);
1466
1467 dev->parent->power.direct_complete = false;
1468 if (dev->power.wakeup_path
1469 && !dev->parent->power.ignore_children)
1470 dev->parent->power.wakeup_path = true;
1471
1472 spin_unlock_irq(&parent->power.lock);
1473 }
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001474 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001475
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001476 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001477 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001478
1479 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001480 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001481 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001482 async_error = error;
1483
Zhonghui Fu431d4522015-03-18 15:54:27 +01001484 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001485 return error;
1486}
1487
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001488static void async_suspend(void *data, async_cookie_t cookie)
1489{
1490 struct device *dev = (struct device *)data;
1491 int error;
1492
1493 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001494 if (error) {
1495 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001496 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001497 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001498
1499 put_device(dev);
1500}
1501
1502static int device_suspend(struct device *dev)
1503{
Wolfram Sang16735d02013-11-14 14:32:02 -08001504 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001505
Zhonghui Fu431d4522015-03-18 15:54:27 +01001506 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001507 get_device(dev);
1508 async_schedule(async_suspend, dev);
1509 return 0;
1510 }
1511
1512 return __device_suspend(dev, pm_transition, false);
1513}
1514
Alan Sterncd59abf2007-09-21 15:36:56 -04001515/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001516 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1517 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001518 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001519int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001520{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001521 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001522 int error = 0;
1523
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001524 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001525 might_sleep();
1526
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001527 cpufreq_suspend();
1528
Alan Sterncd59abf2007-09-21 15:36:56 -04001529 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001530 pm_transition = state;
1531 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001532 while (!list_empty(&dpm_prepared_list)) {
1533 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001534
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001535 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001536 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001537
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001538 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001539
Alan Stern1b3cbec2008-02-29 11:50:22 -05001540 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001541 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001542 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001543 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001544 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001545 break;
1546 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001547 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001548 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001549 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001550 if (async_error)
1551 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001552 }
1553 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001554 async_synchronize_full();
1555 if (!error)
1556 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001557 if (error) {
1558 suspend_stats.failed_suspend++;
1559 dpm_save_failed_step(SUSPEND_SUSPEND);
1560 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001561 dpm_show_time(starttime, state, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001562 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001563 return error;
1564}
1565
1566/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001567 * device_prepare - Prepare a device for system power transition.
1568 * @dev: Device to handle.
1569 * @state: PM transition of the system being carried out.
1570 *
1571 * Execute the ->prepare() callback(s) for given device. No new children of the
1572 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001573 */
Alan Sternd1616302009-05-24 22:05:42 +02001574static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001575{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001576 int (*callback)(struct device *) = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001577 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001578
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001579 if (dev->power.syscore)
1580 return 0;
1581
Alan Stern88d26132012-09-19 21:59:02 +02001582 /*
1583 * If a device's parent goes into runtime suspend at the wrong time,
1584 * it won't be possible to resume the device. To prevent this we
1585 * block runtime suspend here, during the prepare phase, and allow
1586 * it again during the complete phase.
1587 */
1588 pm_runtime_get_noresume(dev);
1589
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001590 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001591
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001592 dev->power.wakeup_path = device_may_wakeup(dev);
1593
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001594 if (dev->power.no_pm_callbacks) {
1595 ret = 1; /* Let device go direct_complete */
1596 goto unlock;
1597 }
1598
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001599 if (dev->pm_domain)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001600 callback = dev->pm_domain->ops.prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001601 else if (dev->type && dev->type->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001602 callback = dev->type->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001603 else if (dev->class && dev->class->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001604 callback = dev->class->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001605 else if (dev->bus && dev->bus->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001606 callback = dev->bus->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001607
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001608 if (!callback && dev->driver && dev->driver->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001609 callback = dev->driver->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001610
Todd E Brandt32e8d682015-05-28 12:55:53 -07001611 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001612 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001613
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001614unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001615 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001616
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001617 if (ret < 0) {
1618 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001619 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001620 return ret;
1621 }
1622 /*
1623 * A positive return value from ->prepare() means "this device appears
1624 * to be runtime-suspended and its state is fine, so if it really is
1625 * runtime-suspended, you can leave it in that state provided that you
1626 * will do the same thing with all of its descendants". This only
1627 * applies to suspend transitions, however.
1628 */
1629 spin_lock_irq(&dev->power.lock);
1630 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1631 spin_unlock_irq(&dev->power.lock);
1632 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001633}
1634
1635/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001636 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1637 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001638 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001639 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001640 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001641int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001642{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001643 int error = 0;
1644
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001645 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001646 might_sleep();
1647
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001648 /*
1649 * Give a chance for the known devices to complete their probes, before
1650 * disable probing of devices. This sync point is important at least
1651 * at boot time + hibernation restore.
1652 */
1653 wait_for_device_probe();
1654 /*
1655 * It is unsafe if probing of devices will happen during suspend or
1656 * hibernation and system behavior will be unpredictable in this case.
1657 * So, let's prohibit device's probing here and defer their probes
1658 * instead. The normal behavior will be restored in dpm_complete().
1659 */
1660 device_block_probing();
1661
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001662 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001663 while (!list_empty(&dpm_list)) {
1664 struct device *dev = to_device(dpm_list.next);
1665
1666 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001667 mutex_unlock(&dpm_list_mtx);
1668
Todd E Brandt32e8d682015-05-28 12:55:53 -07001669 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001670 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001671 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001672
1673 mutex_lock(&dpm_list_mtx);
1674 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001675 if (error == -EAGAIN) {
1676 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001677 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001678 continue;
1679 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001680 printk(KERN_INFO "PM: Device %s not prepared "
1681 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001682 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001683 put_device(dev);
1684 break;
1685 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001686 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001687 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001688 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001689 put_device(dev);
1690 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001691 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001692 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001693 return error;
1694}
1695
1696/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001697 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1698 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001699 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001700 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1701 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001702 */
Alan Sternd1616302009-05-24 22:05:42 +02001703int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001704{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001705 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001706
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001707 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001708 if (error) {
1709 suspend_stats.failed_prepare++;
1710 dpm_save_failed_step(SUSPEND_PREPARE);
1711 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001712 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001713 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001714}
Alan Sternd1616302009-05-24 22:05:42 +02001715EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001716
1717void __suspend_report_result(const char *function, void *fn, int ret)
1718{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001719 if (ret)
1720 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001721}
1722EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001723
1724/**
1725 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1726 * @dev: Device to wait for.
1727 * @subordinate: Device that needs to wait for @dev.
1728 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001729int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001730{
1731 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001732 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001733}
1734EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001735
1736/**
1737 * dpm_for_each_dev - device iterator.
1738 * @data: data for the callback.
1739 * @fn: function to be called for each device.
1740 *
1741 * Iterate over devices in dpm_list, and call @fn for each device,
1742 * passing it @data.
1743 */
1744void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1745{
1746 struct device *dev;
1747
1748 if (!fn)
1749 return;
1750
1751 device_pm_lock();
1752 list_for_each_entry(dev, &dpm_list, power.entry)
1753 fn(dev, data);
1754 device_pm_unlock();
1755}
1756EXPORT_SYMBOL_GPL(dpm_for_each_dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001757
1758static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1759{
1760 if (!ops)
1761 return true;
1762
1763 return !ops->prepare &&
1764 !ops->suspend &&
1765 !ops->suspend_late &&
1766 !ops->suspend_noirq &&
1767 !ops->resume_noirq &&
1768 !ops->resume_early &&
1769 !ops->resume &&
1770 !ops->complete;
1771}
1772
1773void device_pm_check_callbacks(struct device *dev)
1774{
1775 spin_lock_irq(&dev->power.lock);
1776 dev->power.no_pm_callbacks =
1777 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1778 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1779 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1780 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1781 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1782 spin_unlock_irq(&dev->power.lock);
1783}