blob: 9faee1c893e53c8dea6e14d472a73a8b7131bf96 [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. Wysocki2ed8d2b32009-03-16 22:34:06 +010028#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010029#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010030#include <linux/sched/debug.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010031#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010032#include <linux/suspend.h>
Shuah Khan53644672013-07-26 13:30:20 -060033#include <trace/events/power.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080034#include <linux/cpufreq.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020035#include <linux/cpuidle.h>
Benoit Goby70fea602013-10-17 10:48:46 -070036#include <linux/timer.h>
37
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. Wysocki9ed98952016-10-30 17:32:16 +0100135 dev->power.in_dpm_list = true;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200136 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100139/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200140 * device_pm_remove - Remove a device from the PM core's list of active devices.
141 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100142 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200143void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100146 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100147 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700148 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 list_del_init(&dev->power.entry);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100150 dev->power.in_dpm_list = false;
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700151 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200152 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200153 pm_runtime_remove(dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +0100154 device_pm_check_callbacks(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100155}
156
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200157/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200158 * device_pm_move_before - Move device in the PM core's list of active devices.
159 * @deva: Device to move in dpm_list.
160 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100161 */
162void device_pm_move_before(struct device *deva, struct device *devb)
163{
164 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100165 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
166 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100167 /* Delete deva from dpm_list and reinsert before devb. */
168 list_move_tail(&deva->power.entry, &devb->power.entry);
169}
170
171/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200172 * device_pm_move_after - Move device in the PM core's list of active devices.
173 * @deva: Device to move in dpm_list.
174 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100175 */
176void device_pm_move_after(struct device *deva, struct device *devb)
177{
178 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100179 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
180 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100181 /* Delete deva from dpm_list and reinsert after devb. */
182 list_move(&deva->power.entry, &devb->power.entry);
183}
184
185/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200186 * device_pm_move_last - Move device to end of the PM core's list of devices.
187 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100188 */
189void device_pm_move_last(struct device *dev)
190{
191 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100192 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100193 list_move_tail(&dev->power.entry, &dpm_list);
194}
195
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100196static ktime_t initcall_debug_start(struct device *dev)
197{
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100198 ktime_t calltime = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100199
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200200 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100201 pr_info("calling %s+ @ %i, parent: %s\n",
202 dev_name(dev), task_pid_nr(current),
203 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100204 calltime = ktime_get();
205 }
206
207 return calltime;
208}
209
210static void initcall_debug_report(struct device *dev, ktime_t calltime,
Shuah Khan53644672013-07-26 13:30:20 -0600211 int error, pm_message_t state, char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100212{
Shuah Khan53644672013-07-26 13:30:20 -0600213 ktime_t rettime;
214 s64 nsecs;
215
216 rettime = ktime_get();
217 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100218
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200219 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100220 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
Shuah Khan53644672013-07-26 13:30:20 -0600221 error, (unsigned long long)nsecs >> 10);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100222 }
223}
224
Cornelia Huckffa6a702009-03-04 12:44:00 +0100225/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100226 * dpm_wait - Wait for a PM operation to complete.
227 * @dev: Device to wait for.
228 * @async: If unset, wait only if the device's power.async_suspend flag is set.
229 */
230static void dpm_wait(struct device *dev, bool async)
231{
232 if (!dev)
233 return;
234
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100235 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100236 wait_for_completion(&dev->power.completion);
237}
238
239static int dpm_wait_fn(struct device *dev, void *async_ptr)
240{
241 dpm_wait(dev, *((bool *)async_ptr));
242 return 0;
243}
244
245static void dpm_wait_for_children(struct device *dev, bool async)
246{
247 device_for_each_child(dev, &async, dpm_wait_fn);
248}
249
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100250static void dpm_wait_for_suppliers(struct device *dev, bool async)
251{
252 struct device_link *link;
253 int idx;
254
255 idx = device_links_read_lock();
256
257 /*
258 * If the supplier goes away right after we've checked the link to it,
259 * we'll wait for its completion to change the state, but that's fine,
260 * because the only things that will block as a result are the SRCU
261 * callbacks freeing the link objects for the links in the list we're
262 * walking.
263 */
264 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
265 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
266 dpm_wait(link->supplier, async);
267
268 device_links_read_unlock(idx);
269}
270
271static void dpm_wait_for_superior(struct device *dev, bool async)
272{
273 dpm_wait(dev->parent, async);
274 dpm_wait_for_suppliers(dev, async);
275}
276
277static void dpm_wait_for_consumers(struct device *dev, bool async)
278{
279 struct device_link *link;
280 int idx;
281
282 idx = device_links_read_lock();
283
284 /*
285 * The status of a device link can only be changed from "dormant" by a
286 * probe, but that cannot happen during system suspend/resume. In
287 * theory it can change to "dormant" at that time, but then it is
288 * reasonable to wait for the target device anyway (eg. if it goes
289 * away, it's better to wait for it to go away completely and then
290 * continue instead of trying to continue in parallel with its
291 * unregistration).
292 */
293 list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
294 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
295 dpm_wait(link->consumer, async);
296
297 device_links_read_unlock(idx);
298}
299
300static void dpm_wait_for_subordinate(struct device *dev, bool async)
301{
302 dpm_wait_for_children(dev, async);
303 dpm_wait_for_consumers(dev, async);
304}
305
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100306/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100307 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200308 * @ops: PM operations to choose from.
309 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200310 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100311static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200312{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200313 switch (state.event) {
314#ifdef CONFIG_SUSPEND
315 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100316 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200317 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100318 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200319#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200320#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200321 case PM_EVENT_FREEZE:
322 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100323 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200324 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100325 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200326 case PM_EVENT_THAW:
327 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100328 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200329 break;
330 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100331 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200332#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200333 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100334
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100335 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200336}
337
338/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100339 * pm_late_early_op - Return the PM operation appropriate for given PM event.
340 * @ops: PM operations to choose from.
341 * @state: PM transition of the system being carried out.
342 *
343 * Runtime PM is disabled for @dev while this function is being executed.
344 */
345static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
346 pm_message_t state)
347{
348 switch (state.event) {
349#ifdef CONFIG_SUSPEND
350 case PM_EVENT_SUSPEND:
351 return ops->suspend_late;
352 case PM_EVENT_RESUME:
353 return ops->resume_early;
354#endif /* CONFIG_SUSPEND */
355#ifdef CONFIG_HIBERNATE_CALLBACKS
356 case PM_EVENT_FREEZE:
357 case PM_EVENT_QUIESCE:
358 return ops->freeze_late;
359 case PM_EVENT_HIBERNATE:
360 return ops->poweroff_late;
361 case PM_EVENT_THAW:
362 case PM_EVENT_RECOVER:
363 return ops->thaw_early;
364 case PM_EVENT_RESTORE:
365 return ops->restore_early;
366#endif /* CONFIG_HIBERNATE_CALLBACKS */
367 }
368
369 return NULL;
370}
371
372/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100373 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200374 * @ops: PM operations to choose from.
375 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200376 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200377 * The driver of @dev will not receive interrupts while this function is being
378 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200379 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100380static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200381{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200382 switch (state.event) {
383#ifdef CONFIG_SUSPEND
384 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100385 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200386 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100387 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200388#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200389#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200390 case PM_EVENT_FREEZE:
391 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100392 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200393 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100394 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200395 case PM_EVENT_THAW:
396 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100397 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200398 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100399 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200400#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200401 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100402
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100403 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200404}
405
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200406static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
407{
408 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
409 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
410 ", may wakeup" : "");
411}
412
413static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
414 int error)
415{
416 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100417 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200418}
419
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100420static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
421{
422 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200423 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100424 int usecs;
425
426 calltime = ktime_get();
427 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
428 do_div(usecs64, NSEC_PER_USEC);
429 usecs = usecs64;
430 if (usecs == 0)
431 usecs = 1;
432 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
433 info ?: "", info ? " " : "", pm_verb(state.event),
434 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
435}
436
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100437static int dpm_run_callback(pm_callback_t cb, struct device *dev,
438 pm_message_t state, char *info)
439{
440 ktime_t calltime;
441 int error;
442
443 if (!cb)
444 return 0;
445
446 calltime = initcall_debug_start(dev);
447
448 pm_dev_dbg(dev, state, info);
Todd E Brandte8bca472014-06-10 07:31:22 -0700449 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100450 error = cb(dev);
Todd E Brandte8bca472014-06-10 07:31:22 -0700451 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100452 suspend_report_result(cb, error);
453
Shuah Khan53644672013-07-26 13:30:20 -0600454 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100455
456 return error;
457}
458
Benoit Goby70fea602013-10-17 10:48:46 -0700459#ifdef CONFIG_DPM_WATCHDOG
460struct dpm_watchdog {
461 struct device *dev;
462 struct task_struct *tsk;
463 struct timer_list timer;
464};
465
466#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
467 struct dpm_watchdog wd
468
469/**
470 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
471 * @data: Watchdog object address.
472 *
473 * Called when a driver has timed out suspending or resuming.
474 * There's not much we can do here to recover so panic() to
475 * capture a crash-dump in pstore.
476 */
477static void dpm_watchdog_handler(unsigned long data)
478{
479 struct dpm_watchdog *wd = (void *)data;
480
481 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
482 show_stack(wd->tsk, NULL);
483 panic("%s %s: unrecoverable failure\n",
484 dev_driver_string(wd->dev), dev_name(wd->dev));
485}
486
487/**
488 * dpm_watchdog_set - Enable pm watchdog for given device.
489 * @wd: Watchdog. Must be allocated on the stack.
490 * @dev: Device to handle.
491 */
492static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
493{
494 struct timer_list *timer = &wd->timer;
495
496 wd->dev = dev;
497 wd->tsk = current;
498
499 init_timer_on_stack(timer);
500 /* use same timeout value for both suspend and resume */
501 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
502 timer->function = dpm_watchdog_handler;
503 timer->data = (unsigned long)wd;
504 add_timer(timer);
505}
506
507/**
508 * dpm_watchdog_clear - Disable suspend/resume watchdog.
509 * @wd: Watchdog to disable.
510 */
511static void dpm_watchdog_clear(struct dpm_watchdog *wd)
512{
513 struct timer_list *timer = &wd->timer;
514
515 del_timer_sync(timer);
516 destroy_timer_on_stack(timer);
517}
518#else
519#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
520#define dpm_watchdog_set(x, y)
521#define dpm_watchdog_clear(x)
522#endif
523
Alan Sterncd59abf2007-09-21 15:36:56 -0400524/*------------------------- Resume routines -------------------------*/
525
526/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200527 * device_resume_noirq - Execute an "early resume" callback for given device.
528 * @dev: Device to handle.
529 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700530 * @async: If true, the device is being resumed asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400531 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200532 * The driver of @dev will not receive interrupts while this function is being
533 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400534 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800535static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400536{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100537 pm_callback_t callback = NULL;
538 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400539 int error = 0;
540
541 TRACE_DEVICE(dev);
542 TRACE_RESUME(0);
543
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200544 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200545 goto Out;
546
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800547 if (!dev->power.is_noirq_suspended)
548 goto Out;
549
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100550 dpm_wait_for_superior(dev, async);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800551
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200552 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100553 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100554 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200555 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100556 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100557 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100558 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100559 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100560 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100561 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100562 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100563 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100564 }
565
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100566 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100567 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100568 callback = pm_noirq_op(dev->driver->pm, state);
569 }
570
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100571 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800572 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100573
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200574 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800575 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100576 TRACE_RESUME(error);
577 return error;
578}
579
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800580static bool is_async(struct device *dev)
581{
582 return dev->power.async_suspend && pm_async_enabled
583 && !pm_trace_is_enabled();
584}
585
586static void async_resume_noirq(void *data, async_cookie_t cookie)
587{
588 struct device *dev = (struct device *)data;
589 int error;
590
591 error = device_resume_noirq(dev, pm_transition, true);
592 if (error)
593 pm_dev_err(dev, pm_transition, " async", error);
594
595 put_device(dev);
596}
597
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100598/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100599 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200600 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100601 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100602 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200603 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100604 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200605void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100606{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800607 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100608 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100609
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700610 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200611 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800612 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100613
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800614 /*
615 * Advanced the async threads upfront,
616 * in case the starting of async threads is
617 * delayed by non-async resuming devices.
618 */
619 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
620 reinit_completion(&dev->power.completion);
621 if (is_async(dev)) {
622 get_device(dev);
623 async_schedule(async_resume_noirq, dev);
624 }
625 }
626
627 while (!list_empty(&dpm_noirq_list)) {
628 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100629 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100630 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100631 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100632
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800633 if (!is_async(dev)) {
634 int error;
635
636 error = device_resume_noirq(dev, state, false);
637 if (error) {
638 suspend_stats.failed_resume_noirq++;
639 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
640 dpm_save_failed_dev(dev_name(dev));
641 pm_dev_err(dev, state, " noirq", error);
642 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100643 }
644
645 mutex_lock(&dpm_list_mtx);
646 put_device(dev);
647 }
648 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800649 async_synchronize_full();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100650 dpm_show_time(starttime, state, "noirq");
651 resume_device_irqs();
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700652 device_wakeup_disarm_wake_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200653 cpuidle_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700654 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100655}
656
657/**
658 * device_resume_early - Execute an "early resume" callback for given device.
659 * @dev: Device to handle.
660 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700661 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100662 *
663 * Runtime PM is disabled for @dev while this function is being executed.
664 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800665static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100666{
667 pm_callback_t callback = NULL;
668 char *info = NULL;
669 int error = 0;
670
671 TRACE_DEVICE(dev);
672 TRACE_RESUME(0);
673
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200674 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200675 goto Out;
676
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800677 if (!dev->power.is_late_suspended)
678 goto Out;
679
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100680 dpm_wait_for_superior(dev, async);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800681
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100682 if (dev->pm_domain) {
683 info = "early power domain ";
684 callback = pm_late_early_op(&dev->pm_domain->ops, state);
685 } else if (dev->type && dev->type->pm) {
686 info = "early type ";
687 callback = pm_late_early_op(dev->type->pm, state);
688 } else if (dev->class && dev->class->pm) {
689 info = "early class ";
690 callback = pm_late_early_op(dev->class->pm, state);
691 } else if (dev->bus && dev->bus->pm) {
692 info = "early bus ";
693 callback = pm_late_early_op(dev->bus->pm, state);
694 }
695
696 if (!callback && dev->driver && dev->driver->pm) {
697 info = "early driver ";
698 callback = pm_late_early_op(dev->driver->pm, state);
699 }
700
701 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800702 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100703
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200704 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100705 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100706
707 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800708 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100709 return error;
710}
711
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800712static void async_resume_early(void *data, async_cookie_t cookie)
713{
714 struct device *dev = (struct device *)data;
715 int error;
716
717 error = device_resume_early(dev, pm_transition, true);
718 if (error)
719 pm_dev_err(dev, pm_transition, " async", error);
720
721 put_device(dev);
722}
723
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100724/**
725 * dpm_resume_early - Execute "early resume" callbacks for all devices.
726 * @state: PM transition of the system being carried out.
727 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200728void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100729{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800730 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100731 ktime_t starttime = ktime_get();
732
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700733 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100734 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800735 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100736
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800737 /*
738 * Advanced the async threads upfront,
739 * in case the starting of async threads is
740 * delayed by non-async resuming devices.
741 */
742 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
743 reinit_completion(&dev->power.completion);
744 if (is_async(dev)) {
745 get_device(dev);
746 async_schedule(async_resume_early, dev);
747 }
748 }
749
750 while (!list_empty(&dpm_late_early_list)) {
751 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100752 get_device(dev);
753 list_move_tail(&dev->power.entry, &dpm_suspended_list);
754 mutex_unlock(&dpm_list_mtx);
755
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800756 if (!is_async(dev)) {
757 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100758
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800759 error = device_resume_early(dev, state, false);
760 if (error) {
761 suspend_stats.failed_resume_early++;
762 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
763 dpm_save_failed_dev(dev_name(dev));
764 pm_dev_err(dev, state, " early", error);
765 }
766 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100767 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100768 put_device(dev);
769 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200770 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800771 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100772 dpm_show_time(starttime, state, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700773 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100774}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100775
776/**
777 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
778 * @state: PM transition of the system being carried out.
779 */
780void dpm_resume_start(pm_message_t state)
781{
782 dpm_resume_noirq(state);
783 dpm_resume_early(state);
784}
785EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100786
787/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100788 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200789 * @dev: Device to handle.
790 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100791 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100792 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100793static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100794{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100795 pm_callback_t callback = NULL;
796 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100797 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700798 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100799
800 TRACE_DEVICE(dev);
801 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400802
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200803 if (dev->power.syscore)
804 goto Complete;
805
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200806 if (dev->power.direct_complete) {
807 /* Match the pm_runtime_disable() in __device_suspend(). */
808 pm_runtime_enable(dev);
809 goto Complete;
810 }
811
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100812 dpm_wait_for_superior(dev, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700813 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800814 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100815
Alan Sternf76b168b2011-06-18 20:22:23 +0200816 /*
817 * This is a fib. But we'll allow new children to be added below
818 * a resumed device, even if the device hasn't been completed yet.
819 */
820 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100821
Alan Stern6d0e0e82011-06-18 22:42:09 +0200822 if (!dev->power.is_suspended)
823 goto Unlock;
824
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200825 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100826 info = "power domain ";
827 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100828 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100829 }
830
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100831 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100832 info = "type ";
833 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100834 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100835 }
836
837 if (dev->class) {
838 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100839 info = "class ";
840 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100841 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100842 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100843 info = "legacy class ";
844 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100845 goto End;
846 }
847 }
848
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200849 if (dev->bus) {
850 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100851 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100852 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200853 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100854 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100855 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100856 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200857 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400858 }
859
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100860 Driver:
861 if (!callback && dev->driver && dev->driver->pm) {
862 info = "driver ";
863 callback = pm_op(dev->driver->pm, state);
864 }
865
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200866 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100867 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200868 dev->power.is_suspended = false;
869
870 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800871 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700872 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200873
874 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100875 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100876
Alan Sterncd59abf2007-09-21 15:36:56 -0400877 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200878
Alan Sterncd59abf2007-09-21 15:36:56 -0400879 return error;
880}
881
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100882static void async_resume(void *data, async_cookie_t cookie)
883{
884 struct device *dev = (struct device *)data;
885 int error;
886
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100887 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100888 if (error)
889 pm_dev_err(dev, pm_transition, " async", error);
890 put_device(dev);
891}
892
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100893/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200894 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
895 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100896 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200897 * Execute the appropriate "resume" callback for all devices whose status
898 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400899 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200900void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400901{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100902 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100903 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400904
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700905 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200906 might_sleep();
907
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200908 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100909 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200910 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200911
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100912 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800913 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100914 if (is_async(dev)) {
915 get_device(dev);
916 async_schedule(async_resume, dev);
917 }
918 }
919
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100920 while (!list_empty(&dpm_suspended_list)) {
921 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200922 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100923 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200924 int error;
925
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200926 mutex_unlock(&dpm_list_mtx);
927
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100928 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200929 if (error) {
930 suspend_stats.failed_resume++;
931 dpm_save_failed_step(SUSPEND_RESUME);
932 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200933 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200934 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100935
936 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200937 }
938 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100939 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200940 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400941 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200942 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100943 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100944 dpm_show_time(starttime, state, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800945
946 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700947 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200948}
949
950/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200951 * device_complete - Complete a PM transition for given device.
952 * @dev: Device to handle.
953 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954 */
Alan Sternd1616302009-05-24 22:05:42 +0200955static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200956{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100957 void (*callback)(struct device *) = NULL;
958 char *info = NULL;
959
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200960 if (dev->power.syscore)
961 return;
962
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800963 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200964
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200965 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100966 info = "completing power domain ";
967 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200968 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100969 info = "completing type ";
970 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100971 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100972 info = "completing class ";
973 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100974 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100975 info = "completing bus ";
976 callback = dev->bus->pm->complete;
977 }
978
979 if (!callback && dev->driver && dev->driver->pm) {
980 info = "completing driver ";
981 callback = dev->driver->pm->complete;
982 }
983
984 if (callback) {
985 pm_dev_dbg(dev, state, info);
986 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200987 }
988
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800989 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200990
Ulf Hanssonaf939332013-04-12 09:41:06 +0000991 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200992}
993
994/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200995 * dpm_complete - Complete a PM transition for all non-sysdev devices.
996 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200997 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200998 * Execute the ->complete() callbacks for all devices whose PM status is not
999 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001000 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001001void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001002{
1003 struct list_head list;
1004
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001005 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001006 might_sleep();
1007
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001008 INIT_LIST_HEAD(&list);
1009 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001010 while (!list_empty(&dpm_prepared_list)) {
1011 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001012
1013 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +02001014 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001015 list_move(&dev->power.entry, &list);
1016 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001017
Todd E Brandt32e8d682015-05-28 12:55:53 -07001018 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001019 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001020 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001021
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001022 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001023 put_device(dev);
1024 }
1025 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -04001026 mutex_unlock(&dpm_list_mtx);
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001027
1028 /* Allow device probing and trigger re-probing of deferred devices */
1029 device_unblock_probing();
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001030 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -04001031}
1032
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001033/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001034 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1035 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001036 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001037 * Execute "resume" callbacks for all devices and complete the PM transition of
1038 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -04001039 */
Alan Sternd1616302009-05-24 22:05:42 +02001040void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001041{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001042 dpm_resume(state);
1043 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001044}
Alan Sternd1616302009-05-24 22:05:42 +02001045EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -04001046
1047
Alan Sterncd59abf2007-09-21 15:36:56 -04001048/*------------------------- Suspend routines -------------------------*/
1049
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001050/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001051 * resume_event - Return a "resume" message for given "suspend" sleep state.
1052 * @sleep_state: PM message representing a sleep state.
1053 *
1054 * Return a PM message representing the resume event corresponding to given
1055 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001056 */
1057static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001058{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001059 switch (sleep_state.event) {
1060 case PM_EVENT_SUSPEND:
1061 return PMSG_RESUME;
1062 case PM_EVENT_FREEZE:
1063 case PM_EVENT_QUIESCE:
1064 return PMSG_RECOVER;
1065 case PM_EVENT_HIBERNATE:
1066 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001067 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001068 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001069}
1070
1071/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001072 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1073 * @dev: Device to handle.
1074 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001075 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001076 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001077 * The driver of @dev will not receive interrupts while this function is being
1078 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001079 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001080static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001081{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001082 pm_callback_t callback = NULL;
1083 char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001084 int error = 0;
1085
Zhonghui Fu431d4522015-03-18 15:54:27 +01001086 TRACE_DEVICE(dev);
1087 TRACE_SUSPEND(0);
1088
Linus Torvalds098c3052016-12-13 11:42:18 -08001089 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001090
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001091 if (async_error)
1092 goto Complete;
1093
Rafael J. Wysockif3b7eaa2017-06-07 00:57:37 +02001094 if (pm_wakeup_pending()) {
1095 async_error = -EBUSY;
1096 goto Complete;
1097 }
1098
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001099 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001100 goto Complete;
1101
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001102 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001103 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001104 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001105 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001106 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001107 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001108 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001109 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001110 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001111 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001112 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001113 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001114 }
1115
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001116 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001117 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001118 callback = pm_noirq_op(dev->driver->pm, state);
1119 }
1120
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001121 error = dpm_run_callback(callback, dev, state, info);
1122 if (!error)
1123 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001124 else
1125 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001126
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001127Complete:
1128 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001129 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001130 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001131}
1132
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001133static void async_suspend_noirq(void *data, async_cookie_t cookie)
1134{
1135 struct device *dev = (struct device *)data;
1136 int error;
1137
1138 error = __device_suspend_noirq(dev, pm_transition, true);
1139 if (error) {
1140 dpm_save_failed_dev(dev_name(dev));
1141 pm_dev_err(dev, pm_transition, " async", error);
1142 }
1143
1144 put_device(dev);
1145}
1146
1147static int device_suspend_noirq(struct device *dev)
1148{
1149 reinit_completion(&dev->power.completion);
1150
Zhonghui Fu431d4522015-03-18 15:54:27 +01001151 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001152 get_device(dev);
1153 async_schedule(async_suspend_noirq, dev);
1154 return 0;
1155 }
1156 return __device_suspend_noirq(dev, pm_transition, false);
1157}
1158
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001159/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001160 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001161 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001162 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001163 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1164 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001165 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001166int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001167{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001168 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001169 int error = 0;
1170
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001171 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Preeti U Murthy8651f972012-07-09 10:12:56 +02001172 cpuidle_pause();
Tony Lindgren4990d4f2015-05-18 15:40:29 -07001173 device_wakeup_arm_wake_irqs();
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +01001174 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001175 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001176 pm_transition = state;
1177 async_error = 0;
1178
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001179 while (!list_empty(&dpm_late_early_list)) {
1180 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001181
1182 get_device(dev);
1183 mutex_unlock(&dpm_list_mtx);
1184
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001185 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001186
1187 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001188 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001189 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001190 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001191 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001192 break;
1193 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001194 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001195 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001196 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001197
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001198 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001199 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001200 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001201 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001202 async_synchronize_full();
1203 if (!error)
1204 error = async_error;
1205
1206 if (error) {
1207 suspend_stats.failed_suspend_noirq++;
1208 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Alan Sternd1616302009-05-24 22:05:42 +02001209 dpm_resume_noirq(resume_event(state));
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001210 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001211 dpm_show_time(starttime, state, "noirq");
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001212 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001213 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001214 return error;
1215}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001216
1217/**
1218 * device_suspend_late - Execute a "late suspend" callback for given device.
1219 * @dev: Device to handle.
1220 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001221 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001222 *
1223 * Runtime PM is disabled for @dev while this function is being executed.
1224 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001225static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001226{
1227 pm_callback_t callback = NULL;
1228 char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001229 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001230
Zhonghui Fu431d4522015-03-18 15:54:27 +01001231 TRACE_DEVICE(dev);
1232 TRACE_SUSPEND(0);
1233
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001234 __pm_runtime_disable(dev, false);
1235
Linus Torvalds098c3052016-12-13 11:42:18 -08001236 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001237
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001238 if (async_error)
1239 goto Complete;
1240
1241 if (pm_wakeup_pending()) {
1242 async_error = -EBUSY;
1243 goto Complete;
1244 }
1245
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001246 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001247 goto Complete;
1248
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001249 if (dev->pm_domain) {
1250 info = "late power domain ";
1251 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1252 } else if (dev->type && dev->type->pm) {
1253 info = "late type ";
1254 callback = pm_late_early_op(dev->type->pm, state);
1255 } else if (dev->class && dev->class->pm) {
1256 info = "late class ";
1257 callback = pm_late_early_op(dev->class->pm, state);
1258 } else if (dev->bus && dev->bus->pm) {
1259 info = "late bus ";
1260 callback = pm_late_early_op(dev->bus->pm, state);
1261 }
1262
1263 if (!callback && dev->driver && dev->driver->pm) {
1264 info = "late driver ";
1265 callback = pm_late_early_op(dev->driver->pm, state);
1266 }
1267
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001268 error = dpm_run_callback(callback, dev, state, info);
1269 if (!error)
1270 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001271 else
1272 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001273
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001274Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001275 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001276 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001277 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001278}
1279
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001280static void async_suspend_late(void *data, async_cookie_t cookie)
1281{
1282 struct device *dev = (struct device *)data;
1283 int error;
1284
1285 error = __device_suspend_late(dev, pm_transition, true);
1286 if (error) {
1287 dpm_save_failed_dev(dev_name(dev));
1288 pm_dev_err(dev, pm_transition, " async", error);
1289 }
1290 put_device(dev);
1291}
1292
1293static int device_suspend_late(struct device *dev)
1294{
1295 reinit_completion(&dev->power.completion);
1296
Zhonghui Fu431d4522015-03-18 15:54:27 +01001297 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001298 get_device(dev);
1299 async_schedule(async_suspend_late, dev);
1300 return 0;
1301 }
1302
1303 return __device_suspend_late(dev, pm_transition, false);
1304}
1305
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001306/**
1307 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1308 * @state: PM transition of the system being carried out.
1309 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001310int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001311{
1312 ktime_t starttime = ktime_get();
1313 int error = 0;
1314
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001315 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001316 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001317 pm_transition = state;
1318 async_error = 0;
1319
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001320 while (!list_empty(&dpm_suspended_list)) {
1321 struct device *dev = to_device(dpm_suspended_list.prev);
1322
1323 get_device(dev);
1324 mutex_unlock(&dpm_list_mtx);
1325
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001326 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001327
1328 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki3a17fb32016-05-20 23:09:49 +02001329 if (!list_empty(&dev->power.entry))
1330 list_move(&dev->power.entry, &dpm_late_early_list);
1331
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001332 if (error) {
1333 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001334 dpm_save_failed_dev(dev_name(dev));
1335 put_device(dev);
1336 break;
1337 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001338 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001339
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001340 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001341 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001342 }
1343 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001344 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001345 if (!error)
1346 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001347 if (error) {
1348 suspend_stats.failed_suspend_late++;
1349 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001350 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001351 } else {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001352 dpm_show_time(starttime, state, "late");
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001353 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001354 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001355 return error;
1356}
1357
1358/**
1359 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1360 * @state: PM transition of the system being carried out.
1361 */
1362int dpm_suspend_end(pm_message_t state)
1363{
1364 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001365 if (error)
1366 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001367
Colin Cross064b0212012-07-19 10:38:06 +02001368 error = dpm_suspend_noirq(state);
1369 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001370 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001371 return error;
1372 }
1373
1374 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001375}
1376EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001377
1378/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001379 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001380 * @dev: Device to suspend.
1381 * @state: PM transition of the system being carried out.
1382 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001383 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001384 */
1385static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001386 int (*cb)(struct device *dev, pm_message_t state),
1387 char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001388{
1389 int error;
1390 ktime_t calltime;
1391
1392 calltime = initcall_debug_start(dev);
1393
Todd E Brandte8bca472014-06-10 07:31:22 -07001394 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001395 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001396 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001397 suspend_report_result(cb, error);
1398
Shuah Khan53644672013-07-26 13:30:20 -06001399 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001400
1401 return error;
1402}
1403
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001404static void dpm_clear_suppliers_direct_complete(struct device *dev)
1405{
1406 struct device_link *link;
1407 int idx;
1408
1409 idx = device_links_read_lock();
1410
1411 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1412 spin_lock_irq(&link->supplier->power.lock);
1413 link->supplier->power.direct_complete = false;
1414 spin_unlock_irq(&link->supplier->power.lock);
1415 }
1416
1417 device_links_read_unlock(idx);
1418}
1419
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001420/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001421 * device_suspend - Execute "suspend" callbacks for given device.
1422 * @dev: Device to handle.
1423 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001424 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001425 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001426static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001427{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001428 pm_callback_t callback = NULL;
1429 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001430 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001431 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001432
Zhonghui Fu431d4522015-03-18 15:54:27 +01001433 TRACE_DEVICE(dev);
1434 TRACE_SUSPEND(0);
1435
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001436 dpm_wait_for_subordinate(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001437
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001438 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001439 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001440
Alan Stern88d26132012-09-19 21:59:02 +02001441 /*
1442 * If a device configured to wake up the system from sleep states
1443 * has been suspended at run time and there's a resume request pending
1444 * for it, this is equivalent to the device signaling wakeup, so the
1445 * system suspend operation should be aborted.
1446 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001447 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1448 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001449
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001450 if (pm_wakeup_pending()) {
1451 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001452 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001453 }
1454
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001455 if (dev->power.syscore)
1456 goto Complete;
1457
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001458 if (dev->power.direct_complete) {
1459 if (pm_runtime_status_suspended(dev)) {
1460 pm_runtime_disable(dev);
Alan Stern019d8812015-07-15 14:40:06 +02001461 if (pm_runtime_status_suspended(dev))
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001462 goto Complete;
1463
1464 pm_runtime_enable(dev);
1465 }
1466 dev->power.direct_complete = false;
1467 }
1468
Benoit Goby70fea602013-10-17 10:48:46 -07001469 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001470 device_lock(dev);
1471
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001472 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001473 info = "power domain ";
1474 callback = pm_op(&dev->pm_domain->ops, state);
1475 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001476 }
1477
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001478 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001479 info = "type ";
1480 callback = pm_op(dev->type->pm, state);
1481 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001482 }
1483
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001484 if (dev->class) {
1485 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001486 info = "class ";
1487 callback = pm_op(dev->class->pm, state);
1488 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001489 } else if (dev->class->suspend) {
1490 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001491 error = legacy_suspend(dev, state, dev->class->suspend,
1492 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001493 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001494 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001495 }
1496
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001497 if (dev->bus) {
1498 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001499 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001500 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001501 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001502 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001503 error = legacy_suspend(dev, state, dev->bus->suspend,
1504 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001505 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001506 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001507 }
1508
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001509 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001510 if (!callback && dev->driver && dev->driver->pm) {
1511 info = "driver ";
1512 callback = pm_op(dev->driver->pm, state);
1513 }
1514
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001515 error = dpm_run_callback(callback, dev, state, info);
1516
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001517 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001518 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001519 struct device *parent = dev->parent;
1520
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001521 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001522 if (parent) {
1523 spin_lock_irq(&parent->power.lock);
1524
1525 dev->parent->power.direct_complete = false;
1526 if (dev->power.wakeup_path
1527 && !dev->parent->power.ignore_children)
1528 dev->parent->power.wakeup_path = true;
1529
1530 spin_unlock_irq(&parent->power.lock);
1531 }
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001532 dpm_clear_suppliers_direct_complete(dev);
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001533 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001534
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001535 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001536 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001537
1538 Complete:
Alan Stern88d26132012-09-19 21:59:02 +02001539 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001540 async_error = error;
1541
Sahitya Tummala05a92622016-12-07 20:10:32 +05301542 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001543 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001544 return error;
1545}
1546
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001547static void async_suspend(void *data, async_cookie_t cookie)
1548{
1549 struct device *dev = (struct device *)data;
1550 int error;
1551
1552 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001553 if (error) {
1554 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001555 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001556 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001557
1558 put_device(dev);
1559}
1560
1561static int device_suspend(struct device *dev)
1562{
Wolfram Sang16735d02013-11-14 14:32:02 -08001563 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001564
Zhonghui Fu431d4522015-03-18 15:54:27 +01001565 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001566 get_device(dev);
1567 async_schedule(async_suspend, dev);
1568 return 0;
1569 }
1570
1571 return __device_suspend(dev, pm_transition, false);
1572}
1573
Alan Sterncd59abf2007-09-21 15:36:56 -04001574/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001575 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1576 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001577 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001578int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001579{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001580 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001581 int error = 0;
1582
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001583 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001584 might_sleep();
1585
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001586 cpufreq_suspend();
1587
Alan Sterncd59abf2007-09-21 15:36:56 -04001588 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001589 pm_transition = state;
1590 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001591 while (!list_empty(&dpm_prepared_list)) {
1592 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001593
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001594 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001595 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001596
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001597 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001598
Alan Stern1b3cbec2008-02-29 11:50:22 -05001599 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001600 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001601 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001602 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001603 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001604 break;
1605 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001606 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001607 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001608 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001609 if (async_error)
1610 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001611 }
1612 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001613 async_synchronize_full();
1614 if (!error)
1615 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001616 if (error) {
1617 suspend_stats.failed_suspend++;
1618 dpm_save_failed_step(SUSPEND_SUSPEND);
1619 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001620 dpm_show_time(starttime, state, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001621 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001622 return error;
1623}
1624
1625/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001626 * device_prepare - Prepare a device for system power transition.
1627 * @dev: Device to handle.
1628 * @state: PM transition of the system being carried out.
1629 *
1630 * Execute the ->prepare() callback(s) for given device. No new children of the
1631 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001632 */
Alan Sternd1616302009-05-24 22:05:42 +02001633static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001634{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001635 int (*callback)(struct device *) = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001636 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001637
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001638 if (dev->power.syscore)
1639 return 0;
1640
Alan Stern88d26132012-09-19 21:59:02 +02001641 /*
1642 * If a device's parent goes into runtime suspend at the wrong time,
1643 * it won't be possible to resume the device. To prevent this we
1644 * block runtime suspend here, during the prepare phase, and allow
1645 * it again during the complete phase.
1646 */
1647 pm_runtime_get_noresume(dev);
1648
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001649 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001650
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001651 dev->power.wakeup_path = device_may_wakeup(dev);
1652
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001653 if (dev->power.no_pm_callbacks) {
1654 ret = 1; /* Let device go direct_complete */
1655 goto unlock;
1656 }
1657
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001658 if (dev->pm_domain)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001659 callback = dev->pm_domain->ops.prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001660 else if (dev->type && dev->type->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001661 callback = dev->type->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001662 else if (dev->class && dev->class->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001663 callback = dev->class->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001664 else if (dev->bus && dev->bus->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001665 callback = dev->bus->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001666
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001667 if (!callback && dev->driver && dev->driver->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001668 callback = dev->driver->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001669
Todd E Brandt32e8d682015-05-28 12:55:53 -07001670 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001671 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001672
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001673unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001674 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001675
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001676 if (ret < 0) {
1677 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001678 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001679 return ret;
1680 }
1681 /*
1682 * A positive return value from ->prepare() means "this device appears
1683 * to be runtime-suspended and its state is fine, so if it really is
1684 * runtime-suspended, you can leave it in that state provided that you
1685 * will do the same thing with all of its descendants". This only
1686 * applies to suspend transitions, however.
1687 */
1688 spin_lock_irq(&dev->power.lock);
1689 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1690 spin_unlock_irq(&dev->power.lock);
1691 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001692}
1693
1694/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001695 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1696 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001697 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001698 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001699 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001700int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001701{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001702 int error = 0;
1703
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001704 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001705 might_sleep();
1706
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001707 /*
1708 * Give a chance for the known devices to complete their probes, before
1709 * disable probing of devices. This sync point is important at least
1710 * at boot time + hibernation restore.
1711 */
1712 wait_for_device_probe();
1713 /*
1714 * It is unsafe if probing of devices will happen during suspend or
1715 * hibernation and system behavior will be unpredictable in this case.
1716 * So, let's prohibit device's probing here and defer their probes
1717 * instead. The normal behavior will be restored in dpm_complete().
1718 */
1719 device_block_probing();
1720
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001721 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001722 while (!list_empty(&dpm_list)) {
1723 struct device *dev = to_device(dpm_list.next);
1724
1725 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001726 mutex_unlock(&dpm_list_mtx);
1727
Todd E Brandt32e8d682015-05-28 12:55:53 -07001728 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001729 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001730 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001731
1732 mutex_lock(&dpm_list_mtx);
1733 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001734 if (error == -EAGAIN) {
1735 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001736 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001737 continue;
1738 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001739 printk(KERN_INFO "PM: Device %s not prepared "
1740 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001741 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001742 put_device(dev);
1743 break;
1744 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001745 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001746 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001747 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001748 put_device(dev);
1749 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001750 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001751 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001752 return error;
1753}
1754
1755/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001756 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1757 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001758 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001759 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1760 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001761 */
Alan Sternd1616302009-05-24 22:05:42 +02001762int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001763{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001764 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001765
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001766 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001767 if (error) {
1768 suspend_stats.failed_prepare++;
1769 dpm_save_failed_step(SUSPEND_PREPARE);
1770 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001771 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001772 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001773}
Alan Sternd1616302009-05-24 22:05:42 +02001774EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001775
1776void __suspend_report_result(const char *function, void *fn, int ret)
1777{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001778 if (ret)
1779 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001780}
1781EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001782
1783/**
1784 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1785 * @dev: Device to wait for.
1786 * @subordinate: Device that needs to wait for @dev.
1787 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001788int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001789{
1790 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001791 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001792}
1793EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001794
1795/**
1796 * dpm_for_each_dev - device iterator.
1797 * @data: data for the callback.
1798 * @fn: function to be called for each device.
1799 *
1800 * Iterate over devices in dpm_list, and call @fn for each device,
1801 * passing it @data.
1802 */
1803void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1804{
1805 struct device *dev;
1806
1807 if (!fn)
1808 return;
1809
1810 device_pm_lock();
1811 list_for_each_entry(dev, &dpm_list, power.entry)
1812 fn(dev, data);
1813 device_pm_unlock();
1814}
1815EXPORT_SYMBOL_GPL(dpm_for_each_dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001816
1817static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1818{
1819 if (!ops)
1820 return true;
1821
1822 return !ops->prepare &&
1823 !ops->suspend &&
1824 !ops->suspend_late &&
1825 !ops->suspend_noirq &&
1826 !ops->resume_noirq &&
1827 !ops->resume_early &&
1828 !ops->resume &&
1829 !ops->complete;
1830}
1831
1832void device_pm_check_callbacks(struct device *dev)
1833{
1834 spin_lock_irq(&dev->power.lock);
1835 dev->power.no_pm_callbacks =
1836 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1837 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1838 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1839 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1840 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1841 spin_unlock_irq(&dev->power.lock);
1842}