blob: f64e5b819e5cc406f538d63bb617ed8d6143e3a3 [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>
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
Krzysztof Kozlowski952856d2017-06-12 17:19:31 +020065static const char *pm_verb(int event)
Shuah Khan53644672013-07-26 13:30:20 -060066{
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 Vizosoaa8e54b52016-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 Vizosoaa8e54b52016-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,
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200211 int error, pm_message_t state,
212 const char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100213{
Shuah Khan53644672013-07-26 13:30:20 -0600214 ktime_t rettime;
215 s64 nsecs;
216
217 rettime = ktime_get();
218 nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime));
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100219
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200220 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100221 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
Shuah Khan53644672013-07-26 13:30:20 -0600222 error, (unsigned long long)nsecs >> 10);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100223 }
224}
225
Cornelia Huckffa6a702009-03-04 12:44:00 +0100226/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100227 * dpm_wait - Wait for a PM operation to complete.
228 * @dev: Device to wait for.
229 * @async: If unset, wait only if the device's power.async_suspend flag is set.
230 */
231static void dpm_wait(struct device *dev, bool async)
232{
233 if (!dev)
234 return;
235
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100236 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100237 wait_for_completion(&dev->power.completion);
238}
239
240static int dpm_wait_fn(struct device *dev, void *async_ptr)
241{
242 dpm_wait(dev, *((bool *)async_ptr));
243 return 0;
244}
245
246static void dpm_wait_for_children(struct device *dev, bool async)
247{
248 device_for_each_child(dev, &async, dpm_wait_fn);
249}
250
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100251static void dpm_wait_for_suppliers(struct device *dev, bool async)
252{
253 struct device_link *link;
254 int idx;
255
256 idx = device_links_read_lock();
257
258 /*
259 * If the supplier goes away right after we've checked the link to it,
260 * we'll wait for its completion to change the state, but that's fine,
261 * because the only things that will block as a result are the SRCU
262 * callbacks freeing the link objects for the links in the list we're
263 * walking.
264 */
265 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
266 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
267 dpm_wait(link->supplier, async);
268
269 device_links_read_unlock(idx);
270}
271
272static void dpm_wait_for_superior(struct device *dev, bool async)
273{
274 dpm_wait(dev->parent, async);
275 dpm_wait_for_suppliers(dev, async);
276}
277
278static void dpm_wait_for_consumers(struct device *dev, bool async)
279{
280 struct device_link *link;
281 int idx;
282
283 idx = device_links_read_lock();
284
285 /*
286 * The status of a device link can only be changed from "dormant" by a
287 * probe, but that cannot happen during system suspend/resume. In
288 * theory it can change to "dormant" at that time, but then it is
289 * reasonable to wait for the target device anyway (eg. if it goes
290 * away, it's better to wait for it to go away completely and then
291 * continue instead of trying to continue in parallel with its
292 * unregistration).
293 */
294 list_for_each_entry_rcu(link, &dev->links.consumers, s_node)
295 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
296 dpm_wait(link->consumer, async);
297
298 device_links_read_unlock(idx);
299}
300
301static void dpm_wait_for_subordinate(struct device *dev, bool async)
302{
303 dpm_wait_for_children(dev, async);
304 dpm_wait_for_consumers(dev, async);
305}
306
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100307/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100308 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200309 * @ops: PM operations to choose from.
310 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200311 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100312static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200313{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200314 switch (state.event) {
315#ifdef CONFIG_SUSPEND
316 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100317 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200318 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100319 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200320#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200321#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200322 case PM_EVENT_FREEZE:
323 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100324 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200325 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100326 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200327 case PM_EVENT_THAW:
328 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100329 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200330 break;
331 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100332 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200333#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200334 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100335
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100336 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200337}
338
339/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100340 * pm_late_early_op - Return the PM operation appropriate for given PM event.
341 * @ops: PM operations to choose from.
342 * @state: PM transition of the system being carried out.
343 *
344 * Runtime PM is disabled for @dev while this function is being executed.
345 */
346static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
347 pm_message_t state)
348{
349 switch (state.event) {
350#ifdef CONFIG_SUSPEND
351 case PM_EVENT_SUSPEND:
352 return ops->suspend_late;
353 case PM_EVENT_RESUME:
354 return ops->resume_early;
355#endif /* CONFIG_SUSPEND */
356#ifdef CONFIG_HIBERNATE_CALLBACKS
357 case PM_EVENT_FREEZE:
358 case PM_EVENT_QUIESCE:
359 return ops->freeze_late;
360 case PM_EVENT_HIBERNATE:
361 return ops->poweroff_late;
362 case PM_EVENT_THAW:
363 case PM_EVENT_RECOVER:
364 return ops->thaw_early;
365 case PM_EVENT_RESTORE:
366 return ops->restore_early;
367#endif /* CONFIG_HIBERNATE_CALLBACKS */
368 }
369
370 return NULL;
371}
372
373/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100374 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200375 * @ops: PM operations to choose from.
376 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200377 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200378 * The driver of @dev will not receive interrupts while this function is being
379 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200380 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100381static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200382{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200383 switch (state.event) {
384#ifdef CONFIG_SUSPEND
385 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100386 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200387 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100388 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200389#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200390#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200391 case PM_EVENT_FREEZE:
392 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100393 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200394 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100395 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200396 case PM_EVENT_THAW:
397 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100398 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200399 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100400 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200401#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200402 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100403
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100404 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200405}
406
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200407static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200408{
409 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
410 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
411 ", may wakeup" : "");
412}
413
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200414static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200415 int error)
416{
417 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100418 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200419}
420
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200421static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200422 const char *info)
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100423{
424 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200425 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100426 int usecs;
427
428 calltime = ktime_get();
429 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
430 do_div(usecs64, NSEC_PER_USEC);
431 usecs = usecs64;
432 if (usecs == 0)
433 usecs = 1;
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200434
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200435 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200436 info ?: "", info ? " " : "", pm_verb(state.event),
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200437 error ? "aborted" : "complete",
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200438 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100439}
440
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100441static int dpm_run_callback(pm_callback_t cb, struct device *dev,
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200442 pm_message_t state, const char *info)
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100443{
444 ktime_t calltime;
445 int error;
446
447 if (!cb)
448 return 0;
449
450 calltime = initcall_debug_start(dev);
451
452 pm_dev_dbg(dev, state, info);
Todd E Brandte8bca472014-06-10 07:31:22 -0700453 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100454 error = cb(dev);
Todd E Brandte8bca472014-06-10 07:31:22 -0700455 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100456 suspend_report_result(cb, error);
457
Shuah Khan53644672013-07-26 13:30:20 -0600458 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100459
460 return error;
461}
462
Benoit Goby70fea602013-10-17 10:48:46 -0700463#ifdef CONFIG_DPM_WATCHDOG
464struct dpm_watchdog {
465 struct device *dev;
466 struct task_struct *tsk;
467 struct timer_list timer;
468};
469
470#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
471 struct dpm_watchdog wd
472
473/**
474 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
475 * @data: Watchdog object address.
476 *
477 * Called when a driver has timed out suspending or resuming.
478 * There's not much we can do here to recover so panic() to
479 * capture a crash-dump in pstore.
480 */
481static void dpm_watchdog_handler(unsigned long data)
482{
483 struct dpm_watchdog *wd = (void *)data;
484
485 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
486 show_stack(wd->tsk, NULL);
487 panic("%s %s: unrecoverable failure\n",
488 dev_driver_string(wd->dev), dev_name(wd->dev));
489}
490
491/**
492 * dpm_watchdog_set - Enable pm watchdog for given device.
493 * @wd: Watchdog. Must be allocated on the stack.
494 * @dev: Device to handle.
495 */
496static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
497{
498 struct timer_list *timer = &wd->timer;
499
500 wd->dev = dev;
501 wd->tsk = current;
502
503 init_timer_on_stack(timer);
504 /* use same timeout value for both suspend and resume */
505 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
506 timer->function = dpm_watchdog_handler;
507 timer->data = (unsigned long)wd;
508 add_timer(timer);
509}
510
511/**
512 * dpm_watchdog_clear - Disable suspend/resume watchdog.
513 * @wd: Watchdog to disable.
514 */
515static void dpm_watchdog_clear(struct dpm_watchdog *wd)
516{
517 struct timer_list *timer = &wd->timer;
518
519 del_timer_sync(timer);
520 destroy_timer_on_stack(timer);
521}
522#else
523#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
524#define dpm_watchdog_set(x, y)
525#define dpm_watchdog_clear(x)
526#endif
527
Alan Sterncd59abf2007-09-21 15:36:56 -0400528/*------------------------- Resume routines -------------------------*/
529
530/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200531 * device_resume_noirq - Execute an "early resume" callback for given device.
532 * @dev: Device to handle.
533 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700534 * @async: If true, the device is being resumed asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400535 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200536 * The driver of @dev will not receive interrupts while this function is being
537 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400538 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800539static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400540{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100541 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200542 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400543 int error = 0;
544
545 TRACE_DEVICE(dev);
546 TRACE_RESUME(0);
547
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200548 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200549 goto Out;
550
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800551 if (!dev->power.is_noirq_suspended)
552 goto Out;
553
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100554 dpm_wait_for_superior(dev, async);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800555
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200556 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100557 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100558 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200559 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100560 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100561 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100562 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100563 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100564 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100565 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100566 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100567 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100568 }
569
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100570 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100571 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100572 callback = pm_noirq_op(dev->driver->pm, state);
573 }
574
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100575 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800576 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100577
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200578 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800579 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100580 TRACE_RESUME(error);
581 return error;
582}
583
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800584static bool is_async(struct device *dev)
585{
586 return dev->power.async_suspend && pm_async_enabled
587 && !pm_trace_is_enabled();
588}
589
590static void async_resume_noirq(void *data, async_cookie_t cookie)
591{
592 struct device *dev = (struct device *)data;
593 int error;
594
595 error = device_resume_noirq(dev, pm_transition, true);
596 if (error)
597 pm_dev_err(dev, pm_transition, " async", error);
598
599 put_device(dev);
600}
601
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200602void dpm_noirq_resume_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100603{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800604 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100605 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100606
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700607 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200608 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800609 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100610
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800611 /*
612 * Advanced the async threads upfront,
613 * in case the starting of async threads is
614 * delayed by non-async resuming devices.
615 */
616 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
617 reinit_completion(&dev->power.completion);
618 if (is_async(dev)) {
619 get_device(dev);
620 async_schedule(async_resume_noirq, dev);
621 }
622 }
623
624 while (!list_empty(&dpm_noirq_list)) {
625 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100626 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100627 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100628 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100629
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800630 if (!is_async(dev)) {
631 int error;
632
633 error = device_resume_noirq(dev, state, false);
634 if (error) {
635 suspend_stats.failed_resume_noirq++;
636 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
637 dpm_save_failed_dev(dev_name(dev));
638 pm_dev_err(dev, state, " noirq", error);
639 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100640 }
641
642 mutex_lock(&dpm_list_mtx);
643 put_device(dev);
644 }
645 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800646 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200647 dpm_show_time(starttime, state, 0, "noirq");
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200648 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
649}
650
651void dpm_noirq_end(void)
652{
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100653 resume_device_irqs();
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700654 device_wakeup_disarm_wake_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200655 cpuidle_resume();
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200656}
657
658/**
659 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
660 * @state: PM transition of the system being carried out.
661 *
662 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
663 * allow device drivers' interrupt handlers to be called.
664 */
665void dpm_resume_noirq(pm_message_t state)
666{
667 dpm_noirq_resume_devices(state);
668 dpm_noirq_end();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100669}
670
671/**
672 * device_resume_early - Execute an "early resume" callback for given device.
673 * @dev: Device to handle.
674 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700675 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100676 *
677 * Runtime PM is disabled for @dev while this function is being executed.
678 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800679static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100680{
681 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200682 const char *info = NULL;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100683 int error = 0;
684
685 TRACE_DEVICE(dev);
686 TRACE_RESUME(0);
687
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200688 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200689 goto Out;
690
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800691 if (!dev->power.is_late_suspended)
692 goto Out;
693
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100694 dpm_wait_for_superior(dev, async);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800695
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100696 if (dev->pm_domain) {
697 info = "early power domain ";
698 callback = pm_late_early_op(&dev->pm_domain->ops, state);
699 } else if (dev->type && dev->type->pm) {
700 info = "early type ";
701 callback = pm_late_early_op(dev->type->pm, state);
702 } else if (dev->class && dev->class->pm) {
703 info = "early class ";
704 callback = pm_late_early_op(dev->class->pm, state);
705 } else if (dev->bus && dev->bus->pm) {
706 info = "early bus ";
707 callback = pm_late_early_op(dev->bus->pm, state);
708 }
709
710 if (!callback && dev->driver && dev->driver->pm) {
711 info = "early driver ";
712 callback = pm_late_early_op(dev->driver->pm, state);
713 }
714
715 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800716 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100717
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200718 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100719 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100720
721 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800722 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100723 return error;
724}
725
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800726static void async_resume_early(void *data, async_cookie_t cookie)
727{
728 struct device *dev = (struct device *)data;
729 int error;
730
731 error = device_resume_early(dev, pm_transition, true);
732 if (error)
733 pm_dev_err(dev, pm_transition, " async", error);
734
735 put_device(dev);
736}
737
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100738/**
739 * dpm_resume_early - Execute "early resume" callbacks for all devices.
740 * @state: PM transition of the system being carried out.
741 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200742void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100743{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800744 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100745 ktime_t starttime = ktime_get();
746
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700747 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100748 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800749 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100750
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800751 /*
752 * Advanced the async threads upfront,
753 * in case the starting of async threads is
754 * delayed by non-async resuming devices.
755 */
756 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
757 reinit_completion(&dev->power.completion);
758 if (is_async(dev)) {
759 get_device(dev);
760 async_schedule(async_resume_early, dev);
761 }
762 }
763
764 while (!list_empty(&dpm_late_early_list)) {
765 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100766 get_device(dev);
767 list_move_tail(&dev->power.entry, &dpm_suspended_list);
768 mutex_unlock(&dpm_list_mtx);
769
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800770 if (!is_async(dev)) {
771 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100772
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800773 error = device_resume_early(dev, state, false);
774 if (error) {
775 suspend_stats.failed_resume_early++;
776 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
777 dpm_save_failed_dev(dev_name(dev));
778 pm_dev_err(dev, state, " early", error);
779 }
780 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100781 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100782 put_device(dev);
783 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200784 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800785 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200786 dpm_show_time(starttime, state, 0, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700787 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100788}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100789
790/**
791 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
792 * @state: PM transition of the system being carried out.
793 */
794void dpm_resume_start(pm_message_t state)
795{
796 dpm_resume_noirq(state);
797 dpm_resume_early(state);
798}
799EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100800
801/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100802 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200803 * @dev: Device to handle.
804 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100805 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100806 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100807static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100808{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100809 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200810 const char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100811 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700812 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100813
814 TRACE_DEVICE(dev);
815 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400816
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200817 if (dev->power.syscore)
818 goto Complete;
819
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200820 if (dev->power.direct_complete) {
821 /* Match the pm_runtime_disable() in __device_suspend(). */
822 pm_runtime_enable(dev);
823 goto Complete;
824 }
825
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100826 dpm_wait_for_superior(dev, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700827 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800828 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100829
Alan Sternf76b168b2011-06-18 20:22:23 +0200830 /*
831 * This is a fib. But we'll allow new children to be added below
832 * a resumed device, even if the device hasn't been completed yet.
833 */
834 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100835
Alan Stern6d0e0e82011-06-18 22:42:09 +0200836 if (!dev->power.is_suspended)
837 goto Unlock;
838
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200839 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100840 info = "power domain ";
841 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100842 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100843 }
844
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100845 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100846 info = "type ";
847 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100848 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100849 }
850
851 if (dev->class) {
852 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100853 info = "class ";
854 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100855 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100856 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100857 info = "legacy class ";
858 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100859 goto End;
860 }
861 }
862
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200863 if (dev->bus) {
864 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100865 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100866 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200867 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100868 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100869 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100870 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200871 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400872 }
873
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100874 Driver:
875 if (!callback && dev->driver && dev->driver->pm) {
876 info = "driver ";
877 callback = pm_op(dev->driver->pm, state);
878 }
879
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200880 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100881 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200882 dev->power.is_suspended = false;
883
884 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800885 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700886 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200887
888 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100889 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100890
Alan Sterncd59abf2007-09-21 15:36:56 -0400891 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200892
Alan Sterncd59abf2007-09-21 15:36:56 -0400893 return error;
894}
895
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100896static void async_resume(void *data, async_cookie_t cookie)
897{
898 struct device *dev = (struct device *)data;
899 int error;
900
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100901 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100902 if (error)
903 pm_dev_err(dev, pm_transition, " async", error);
904 put_device(dev);
905}
906
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100907/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200908 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
909 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100910 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200911 * Execute the appropriate "resume" callback for all devices whose status
912 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400913 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200914void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400915{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100916 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100917 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400918
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700919 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200920 might_sleep();
921
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200922 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100923 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200924 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200925
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100926 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800927 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100928 if (is_async(dev)) {
929 get_device(dev);
930 async_schedule(async_resume, dev);
931 }
932 }
933
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100934 while (!list_empty(&dpm_suspended_list)) {
935 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200936 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100937 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200938 int error;
939
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200940 mutex_unlock(&dpm_list_mtx);
941
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100942 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200943 if (error) {
944 suspend_stats.failed_resume++;
945 dpm_save_failed_step(SUSPEND_RESUME);
946 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200947 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200948 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100949
950 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200951 }
952 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100953 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400955 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200956 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100957 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200958 dpm_show_time(starttime, state, 0, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800959
960 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700961 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200962}
963
964/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200965 * device_complete - Complete a PM transition for given device.
966 * @dev: Device to handle.
967 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968 */
Alan Sternd1616302009-05-24 22:05:42 +0200969static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200970{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100971 void (*callback)(struct device *) = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200972 const char *info = NULL;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100973
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200974 if (dev->power.syscore)
975 return;
976
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800977 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200978
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200979 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100980 info = "completing power domain ";
981 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200982 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100983 info = "completing type ";
984 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100985 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100986 info = "completing class ";
987 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100988 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100989 info = "completing bus ";
990 callback = dev->bus->pm->complete;
991 }
992
993 if (!callback && dev->driver && dev->driver->pm) {
994 info = "completing driver ";
995 callback = dev->driver->pm->complete;
996 }
997
998 if (callback) {
999 pm_dev_dbg(dev, state, info);
1000 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001001 }
1002
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001003 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +02001004
Ulf Hanssonaf939332013-04-12 09:41:06 +00001005 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001006}
1007
1008/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001009 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1010 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001011 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001012 * Execute the ->complete() callbacks for all devices whose PM status is not
1013 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001014 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001015void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001016{
1017 struct list_head list;
1018
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001019 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001020 might_sleep();
1021
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001022 INIT_LIST_HEAD(&list);
1023 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001024 while (!list_empty(&dpm_prepared_list)) {
1025 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001026
1027 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +02001028 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001029 list_move(&dev->power.entry, &list);
1030 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001031
Todd E Brandt32e8d682015-05-28 12:55:53 -07001032 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001033 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001034 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001035
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001036 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001037 put_device(dev);
1038 }
1039 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -04001040 mutex_unlock(&dpm_list_mtx);
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001041
1042 /* Allow device probing and trigger re-probing of deferred devices */
1043 device_unblock_probing();
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001044 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -04001045}
1046
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001047/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001048 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1049 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001050 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001051 * Execute "resume" callbacks for all devices and complete the PM transition of
1052 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -04001053 */
Alan Sternd1616302009-05-24 22:05:42 +02001054void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001055{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001056 dpm_resume(state);
1057 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001058}
Alan Sternd1616302009-05-24 22:05:42 +02001059EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -04001060
1061
Alan Sterncd59abf2007-09-21 15:36:56 -04001062/*------------------------- Suspend routines -------------------------*/
1063
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001064/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001065 * resume_event - Return a "resume" message for given "suspend" sleep state.
1066 * @sleep_state: PM message representing a sleep state.
1067 *
1068 * Return a PM message representing the resume event corresponding to given
1069 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001070 */
1071static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001072{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001073 switch (sleep_state.event) {
1074 case PM_EVENT_SUSPEND:
1075 return PMSG_RESUME;
1076 case PM_EVENT_FREEZE:
1077 case PM_EVENT_QUIESCE:
1078 return PMSG_RECOVER;
1079 case PM_EVENT_HIBERNATE:
1080 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001081 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001082 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001083}
1084
1085/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001086 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1087 * @dev: Device to handle.
1088 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001089 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001090 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001091 * The driver of @dev will not receive interrupts while this function is being
1092 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001093 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001094static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001095{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001096 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001097 const char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001098 int error = 0;
1099
Zhonghui Fu431d4522015-03-18 15:54:27 +01001100 TRACE_DEVICE(dev);
1101 TRACE_SUSPEND(0);
1102
Linus Torvalds098c3052016-12-13 11:42:18 -08001103 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001104
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001105 if (async_error)
1106 goto Complete;
1107
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001108 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001109 goto Complete;
1110
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001111 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001112 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001113 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001114 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001115 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001116 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001117 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001118 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001119 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001120 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001121 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001122 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001123 }
1124
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001125 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001126 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001127 callback = pm_noirq_op(dev->driver->pm, state);
1128 }
1129
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001130 error = dpm_run_callback(callback, dev, state, info);
1131 if (!error)
1132 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001133 else
1134 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001135
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001136Complete:
1137 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001138 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001139 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001140}
1141
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001142static void async_suspend_noirq(void *data, async_cookie_t cookie)
1143{
1144 struct device *dev = (struct device *)data;
1145 int error;
1146
1147 error = __device_suspend_noirq(dev, pm_transition, true);
1148 if (error) {
1149 dpm_save_failed_dev(dev_name(dev));
1150 pm_dev_err(dev, pm_transition, " async", error);
1151 }
1152
1153 put_device(dev);
1154}
1155
1156static int device_suspend_noirq(struct device *dev)
1157{
1158 reinit_completion(&dev->power.completion);
1159
Zhonghui Fu431d4522015-03-18 15:54:27 +01001160 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001161 get_device(dev);
1162 async_schedule(async_suspend_noirq, dev);
1163 return 0;
1164 }
1165 return __device_suspend_noirq(dev, pm_transition, false);
1166}
1167
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001168void dpm_noirq_begin(void)
1169{
1170 cpuidle_pause();
1171 device_wakeup_arm_wake_irqs();
1172 suspend_device_irqs();
1173}
1174
1175int dpm_noirq_suspend_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001176{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001177 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001178 int error = 0;
1179
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001180 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001181 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001182 pm_transition = state;
1183 async_error = 0;
1184
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001185 while (!list_empty(&dpm_late_early_list)) {
1186 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001187
1188 get_device(dev);
1189 mutex_unlock(&dpm_list_mtx);
1190
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001191 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001192
1193 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001194 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001195 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001196 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001197 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001198 break;
1199 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001200 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001201 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001202 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001203
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001204 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001205 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001206 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001207 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001208 async_synchronize_full();
1209 if (!error)
1210 error = async_error;
1211
1212 if (error) {
1213 suspend_stats.failed_suspend_noirq++;
1214 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001215 }
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001216 dpm_show_time(starttime, state, error, "noirq");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001217 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001218 return error;
1219}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001220
1221/**
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001222 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1223 * @state: PM transition of the system being carried out.
1224 *
1225 * Prevent device drivers' interrupt handlers from being called and invoke
1226 * "noirq" suspend callbacks for all non-sysdev devices.
1227 */
1228int dpm_suspend_noirq(pm_message_t state)
1229{
1230 int ret;
1231
1232 dpm_noirq_begin();
1233 ret = dpm_noirq_suspend_devices(state);
1234 if (ret)
1235 dpm_resume_noirq(resume_event(state));
1236
1237 return ret;
1238}
1239
1240/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001241 * device_suspend_late - Execute a "late suspend" callback for given device.
1242 * @dev: Device to handle.
1243 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001244 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001245 *
1246 * Runtime PM is disabled for @dev while this function is being executed.
1247 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001248static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001249{
1250 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001251 const char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001252 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001253
Zhonghui Fu431d4522015-03-18 15:54:27 +01001254 TRACE_DEVICE(dev);
1255 TRACE_SUSPEND(0);
1256
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001257 __pm_runtime_disable(dev, false);
1258
Linus Torvalds098c3052016-12-13 11:42:18 -08001259 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001260
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001261 if (async_error)
1262 goto Complete;
1263
1264 if (pm_wakeup_pending()) {
1265 async_error = -EBUSY;
1266 goto Complete;
1267 }
1268
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001269 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001270 goto Complete;
1271
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001272 if (dev->pm_domain) {
1273 info = "late power domain ";
1274 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1275 } else if (dev->type && dev->type->pm) {
1276 info = "late type ";
1277 callback = pm_late_early_op(dev->type->pm, state);
1278 } else if (dev->class && dev->class->pm) {
1279 info = "late class ";
1280 callback = pm_late_early_op(dev->class->pm, state);
1281 } else if (dev->bus && dev->bus->pm) {
1282 info = "late bus ";
1283 callback = pm_late_early_op(dev->bus->pm, state);
1284 }
1285
1286 if (!callback && dev->driver && dev->driver->pm) {
1287 info = "late driver ";
1288 callback = pm_late_early_op(dev->driver->pm, state);
1289 }
1290
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001291 error = dpm_run_callback(callback, dev, state, info);
1292 if (!error)
1293 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001294 else
1295 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001296
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001297Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001298 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001299 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001300 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001301}
1302
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001303static void async_suspend_late(void *data, async_cookie_t cookie)
1304{
1305 struct device *dev = (struct device *)data;
1306 int error;
1307
1308 error = __device_suspend_late(dev, pm_transition, true);
1309 if (error) {
1310 dpm_save_failed_dev(dev_name(dev));
1311 pm_dev_err(dev, pm_transition, " async", error);
1312 }
1313 put_device(dev);
1314}
1315
1316static int device_suspend_late(struct device *dev)
1317{
1318 reinit_completion(&dev->power.completion);
1319
Zhonghui Fu431d4522015-03-18 15:54:27 +01001320 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001321 get_device(dev);
1322 async_schedule(async_suspend_late, dev);
1323 return 0;
1324 }
1325
1326 return __device_suspend_late(dev, pm_transition, false);
1327}
1328
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001329/**
1330 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1331 * @state: PM transition of the system being carried out.
1332 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001333int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001334{
1335 ktime_t starttime = ktime_get();
1336 int error = 0;
1337
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001338 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001339 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001340 pm_transition = state;
1341 async_error = 0;
1342
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001343 while (!list_empty(&dpm_suspended_list)) {
1344 struct device *dev = to_device(dpm_suspended_list.prev);
1345
1346 get_device(dev);
1347 mutex_unlock(&dpm_list_mtx);
1348
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001349 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001350
1351 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki3a17fb32016-05-20 23:09:49 +02001352 if (!list_empty(&dev->power.entry))
1353 list_move(&dev->power.entry, &dpm_late_early_list);
1354
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001355 if (error) {
1356 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001357 dpm_save_failed_dev(dev_name(dev));
1358 put_device(dev);
1359 break;
1360 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001361 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001362
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001363 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001364 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001365 }
1366 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001367 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001368 if (!error)
1369 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001370 if (error) {
1371 suspend_stats.failed_suspend_late++;
1372 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001373 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001374 }
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001375 dpm_show_time(starttime, state, error, "late");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001376 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001377 return error;
1378}
1379
1380/**
1381 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1382 * @state: PM transition of the system being carried out.
1383 */
1384int dpm_suspend_end(pm_message_t state)
1385{
1386 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001387 if (error)
1388 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001389
Colin Cross064b0212012-07-19 10:38:06 +02001390 error = dpm_suspend_noirq(state);
1391 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001392 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001393 return error;
1394 }
1395
1396 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001397}
1398EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001399
1400/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001401 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001402 * @dev: Device to suspend.
1403 * @state: PM transition of the system being carried out.
1404 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001405 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001406 */
1407static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001408 int (*cb)(struct device *dev, pm_message_t state),
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001409 const char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001410{
1411 int error;
1412 ktime_t calltime;
1413
1414 calltime = initcall_debug_start(dev);
1415
Todd E Brandte8bca472014-06-10 07:31:22 -07001416 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001417 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001418 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001419 suspend_report_result(cb, error);
1420
Shuah Khan53644672013-07-26 13:30:20 -06001421 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001422
1423 return error;
1424}
1425
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001426static void dpm_clear_suppliers_direct_complete(struct device *dev)
1427{
1428 struct device_link *link;
1429 int idx;
1430
1431 idx = device_links_read_lock();
1432
1433 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1434 spin_lock_irq(&link->supplier->power.lock);
1435 link->supplier->power.direct_complete = false;
1436 spin_unlock_irq(&link->supplier->power.lock);
1437 }
1438
1439 device_links_read_unlock(idx);
1440}
1441
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001442/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001443 * device_suspend - Execute "suspend" callbacks for given device.
1444 * @dev: Device to handle.
1445 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001446 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001447 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001448static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001449{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001450 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001451 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001452 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001453 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001454
Zhonghui Fu431d4522015-03-18 15:54:27 +01001455 TRACE_DEVICE(dev);
1456 TRACE_SUSPEND(0);
1457
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001458 dpm_wait_for_subordinate(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001459
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001460 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001461 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001462
Alan Stern88d26132012-09-19 21:59:02 +02001463 /*
1464 * If a device configured to wake up the system from sleep states
1465 * has been suspended at run time and there's a resume request pending
1466 * for it, this is equivalent to the device signaling wakeup, so the
1467 * system suspend operation should be aborted.
1468 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001469 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1470 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001471
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001472 if (pm_wakeup_pending()) {
1473 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001474 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001475 }
1476
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001477 if (dev->power.syscore)
1478 goto Complete;
1479
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001480 if (dev->power.direct_complete) {
1481 if (pm_runtime_status_suspended(dev)) {
1482 pm_runtime_disable(dev);
Alan Stern019d8812015-07-15 14:40:06 +02001483 if (pm_runtime_status_suspended(dev))
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001484 goto Complete;
1485
1486 pm_runtime_enable(dev);
1487 }
1488 dev->power.direct_complete = false;
1489 }
1490
Benoit Goby70fea602013-10-17 10:48:46 -07001491 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001492 device_lock(dev);
1493
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001494 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001495 info = "power domain ";
1496 callback = pm_op(&dev->pm_domain->ops, state);
1497 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001498 }
1499
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001500 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001501 info = "type ";
1502 callback = pm_op(dev->type->pm, state);
1503 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001504 }
1505
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001506 if (dev->class) {
1507 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001508 info = "class ";
1509 callback = pm_op(dev->class->pm, state);
1510 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001511 } else if (dev->class->suspend) {
1512 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001513 error = legacy_suspend(dev, state, dev->class->suspend,
1514 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001515 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001516 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001517 }
1518
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001519 if (dev->bus) {
1520 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001521 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001522 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001523 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001524 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001525 error = legacy_suspend(dev, state, dev->bus->suspend,
1526 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001527 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001528 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001529 }
1530
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001531 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001532 if (!callback && dev->driver && dev->driver->pm) {
1533 info = "driver ";
1534 callback = pm_op(dev->driver->pm, state);
1535 }
1536
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001537 error = dpm_run_callback(callback, dev, state, info);
1538
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001539 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001540 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001541 struct device *parent = dev->parent;
1542
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001543 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001544 if (parent) {
1545 spin_lock_irq(&parent->power.lock);
1546
1547 dev->parent->power.direct_complete = false;
1548 if (dev->power.wakeup_path
1549 && !dev->parent->power.ignore_children)
1550 dev->parent->power.wakeup_path = true;
1551
1552 spin_unlock_irq(&parent->power.lock);
1553 }
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001554 dpm_clear_suppliers_direct_complete(dev);
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001555 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001556
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001557 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001558 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001559
1560 Complete:
Alan Stern88d26132012-09-19 21:59:02 +02001561 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001562 async_error = error;
1563
Sahitya Tummala05a92622016-12-07 20:10:32 +05301564 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001565 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001566 return error;
1567}
1568
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001569static void async_suspend(void *data, async_cookie_t cookie)
1570{
1571 struct device *dev = (struct device *)data;
1572 int error;
1573
1574 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001575 if (error) {
1576 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001577 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001578 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001579
1580 put_device(dev);
1581}
1582
1583static int device_suspend(struct device *dev)
1584{
Wolfram Sang16735d02013-11-14 14:32:02 -08001585 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001586
Zhonghui Fu431d4522015-03-18 15:54:27 +01001587 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001588 get_device(dev);
1589 async_schedule(async_suspend, dev);
1590 return 0;
1591 }
1592
1593 return __device_suspend(dev, pm_transition, false);
1594}
1595
Alan Sterncd59abf2007-09-21 15:36:56 -04001596/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001597 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1598 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001599 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001600int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001601{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001602 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001603 int error = 0;
1604
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001605 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001606 might_sleep();
1607
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001608 cpufreq_suspend();
1609
Alan Sterncd59abf2007-09-21 15:36:56 -04001610 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001611 pm_transition = state;
1612 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001613 while (!list_empty(&dpm_prepared_list)) {
1614 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001615
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001616 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001617 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001618
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001619 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001620
Alan Stern1b3cbec2008-02-29 11:50:22 -05001621 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001622 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001623 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001624 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001625 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001626 break;
1627 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001628 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001629 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001630 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001631 if (async_error)
1632 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001633 }
1634 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001635 async_synchronize_full();
1636 if (!error)
1637 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001638 if (error) {
1639 suspend_stats.failed_suspend++;
1640 dpm_save_failed_step(SUSPEND_SUSPEND);
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001641 }
1642 dpm_show_time(starttime, state, error, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001643 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001644 return error;
1645}
1646
1647/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001648 * device_prepare - Prepare a device for system power transition.
1649 * @dev: Device to handle.
1650 * @state: PM transition of the system being carried out.
1651 *
1652 * Execute the ->prepare() callback(s) for given device. No new children of the
1653 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001654 */
Alan Sternd1616302009-05-24 22:05:42 +02001655static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001656{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001657 int (*callback)(struct device *) = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001658 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001659
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001660 if (dev->power.syscore)
1661 return 0;
1662
Alan Stern88d26132012-09-19 21:59:02 +02001663 /*
1664 * If a device's parent goes into runtime suspend at the wrong time,
1665 * it won't be possible to resume the device. To prevent this we
1666 * block runtime suspend here, during the prepare phase, and allow
1667 * it again during the complete phase.
1668 */
1669 pm_runtime_get_noresume(dev);
1670
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001671 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001672
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001673 dev->power.wakeup_path = device_may_wakeup(dev);
1674
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001675 if (dev->power.no_pm_callbacks) {
1676 ret = 1; /* Let device go direct_complete */
1677 goto unlock;
1678 }
1679
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001680 if (dev->pm_domain)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001681 callback = dev->pm_domain->ops.prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001682 else if (dev->type && dev->type->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001683 callback = dev->type->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001684 else if (dev->class && dev->class->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001685 callback = dev->class->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001686 else if (dev->bus && dev->bus->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001687 callback = dev->bus->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001688
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001689 if (!callback && dev->driver && dev->driver->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001690 callback = dev->driver->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001691
Todd E Brandt32e8d682015-05-28 12:55:53 -07001692 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001693 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001694
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001695unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001696 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001697
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001698 if (ret < 0) {
1699 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001700 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001701 return ret;
1702 }
1703 /*
1704 * A positive return value from ->prepare() means "this device appears
1705 * to be runtime-suspended and its state is fine, so if it really is
1706 * runtime-suspended, you can leave it in that state provided that you
1707 * will do the same thing with all of its descendants". This only
1708 * applies to suspend transitions, however.
1709 */
1710 spin_lock_irq(&dev->power.lock);
1711 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1712 spin_unlock_irq(&dev->power.lock);
1713 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001714}
1715
1716/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001717 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1718 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001719 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001720 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001721 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001722int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001723{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001724 int error = 0;
1725
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001726 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001727 might_sleep();
1728
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001729 /*
1730 * Give a chance for the known devices to complete their probes, before
1731 * disable probing of devices. This sync point is important at least
1732 * at boot time + hibernation restore.
1733 */
1734 wait_for_device_probe();
1735 /*
1736 * It is unsafe if probing of devices will happen during suspend or
1737 * hibernation and system behavior will be unpredictable in this case.
1738 * So, let's prohibit device's probing here and defer their probes
1739 * instead. The normal behavior will be restored in dpm_complete().
1740 */
1741 device_block_probing();
1742
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001743 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001744 while (!list_empty(&dpm_list)) {
1745 struct device *dev = to_device(dpm_list.next);
1746
1747 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001748 mutex_unlock(&dpm_list_mtx);
1749
Todd E Brandt32e8d682015-05-28 12:55:53 -07001750 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001751 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001752 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001753
1754 mutex_lock(&dpm_list_mtx);
1755 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001756 if (error == -EAGAIN) {
1757 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001758 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001759 continue;
1760 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001761 printk(KERN_INFO "PM: Device %s not prepared "
1762 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001763 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001764 put_device(dev);
1765 break;
1766 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001767 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001768 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001769 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001770 put_device(dev);
1771 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001772 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001773 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001774 return error;
1775}
1776
1777/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001778 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1779 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001780 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001781 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1782 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001783 */
Alan Sternd1616302009-05-24 22:05:42 +02001784int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001785{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001786 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001787
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001788 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001789 if (error) {
1790 suspend_stats.failed_prepare++;
1791 dpm_save_failed_step(SUSPEND_PREPARE);
1792 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001793 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001794 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001795}
Alan Sternd1616302009-05-24 22:05:42 +02001796EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001797
1798void __suspend_report_result(const char *function, void *fn, int ret)
1799{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001800 if (ret)
1801 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001802}
1803EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001804
1805/**
1806 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1807 * @dev: Device to wait for.
1808 * @subordinate: Device that needs to wait for @dev.
1809 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001810int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001811{
1812 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001813 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001814}
1815EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001816
1817/**
1818 * dpm_for_each_dev - device iterator.
1819 * @data: data for the callback.
1820 * @fn: function to be called for each device.
1821 *
1822 * Iterate over devices in dpm_list, and call @fn for each device,
1823 * passing it @data.
1824 */
1825void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1826{
1827 struct device *dev;
1828
1829 if (!fn)
1830 return;
1831
1832 device_pm_lock();
1833 list_for_each_entry(dev, &dpm_list, power.entry)
1834 fn(dev, data);
1835 device_pm_unlock();
1836}
1837EXPORT_SYMBOL_GPL(dpm_for_each_dev);
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001838
1839static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1840{
1841 if (!ops)
1842 return true;
1843
1844 return !ops->prepare &&
1845 !ops->suspend &&
1846 !ops->suspend_late &&
1847 !ops->suspend_noirq &&
1848 !ops->resume_noirq &&
1849 !ops->resume_early &&
1850 !ops->resume &&
1851 !ops->complete;
1852}
1853
1854void device_pm_check_callbacks(struct device *dev)
1855{
1856 spin_lock_irq(&dev->power.lock);
1857 dev->power.no_pm_callbacks =
1858 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1859 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1860 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1861 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1862 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1863 spin_unlock_irq(&dev->power.lock);
1864}