blob: ae47b2ec84b47ddfc7ef5d0f562025b515514349 [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 */
Kees Cook9c6c2732017-10-04 16:26:57 -0700481static void dpm_watchdog_handler(struct timer_list *t)
Benoit Goby70fea602013-10-17 10:48:46 -0700482{
Kees Cook9c6c2732017-10-04 16:26:57 -0700483 struct dpm_watchdog *wd = from_timer(wd, t, timer);
Benoit Goby70fea602013-10-17 10:48:46 -0700484
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
Kees Cook9c6c2732017-10-04 16:26:57 -0700503 timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
Benoit Goby70fea602013-10-17 10:48:46 -0700504 /* use same timeout value for both suspend and resume */
505 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
Benoit Goby70fea602013-10-17 10:48:46 -0700506 add_timer(timer);
507}
508
509/**
510 * dpm_watchdog_clear - Disable suspend/resume watchdog.
511 * @wd: Watchdog to disable.
512 */
513static void dpm_watchdog_clear(struct dpm_watchdog *wd)
514{
515 struct timer_list *timer = &wd->timer;
516
517 del_timer_sync(timer);
518 destroy_timer_on_stack(timer);
519}
520#else
521#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
522#define dpm_watchdog_set(x, y)
523#define dpm_watchdog_clear(x)
524#endif
525
Alan Sterncd59abf2007-09-21 15:36:56 -0400526/*------------------------- Resume routines -------------------------*/
527
528/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200529 * device_resume_noirq - Execute an "early resume" callback for given device.
530 * @dev: Device to handle.
531 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700532 * @async: If true, the device is being resumed asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400533 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200534 * The driver of @dev will not receive interrupts while this function is being
535 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400536 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800537static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400538{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100539 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200540 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400541 int error = 0;
542
543 TRACE_DEVICE(dev);
544 TRACE_RESUME(0);
545
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200546 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200547 goto Out;
548
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800549 if (!dev->power.is_noirq_suspended)
550 goto Out;
551
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100552 dpm_wait_for_superior(dev, async);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800553
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200554 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100555 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100556 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200557 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100558 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100559 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100560 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100561 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100562 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100563 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100564 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100565 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100566 }
567
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100568 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100569 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100570 callback = pm_noirq_op(dev->driver->pm, state);
571 }
572
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100573 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800574 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100575
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200576 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800577 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100578 TRACE_RESUME(error);
579 return error;
580}
581
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800582static bool is_async(struct device *dev)
583{
584 return dev->power.async_suspend && pm_async_enabled
585 && !pm_trace_is_enabled();
586}
587
588static void async_resume_noirq(void *data, async_cookie_t cookie)
589{
590 struct device *dev = (struct device *)data;
591 int error;
592
593 error = device_resume_noirq(dev, pm_transition, true);
594 if (error)
595 pm_dev_err(dev, pm_transition, " async", error);
596
597 put_device(dev);
598}
599
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200600void dpm_noirq_resume_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100601{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800602 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100603 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100604
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700605 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200606 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800607 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100608
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800609 /*
610 * Advanced the async threads upfront,
611 * in case the starting of async threads is
612 * delayed by non-async resuming devices.
613 */
614 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
615 reinit_completion(&dev->power.completion);
616 if (is_async(dev)) {
617 get_device(dev);
618 async_schedule(async_resume_noirq, dev);
619 }
620 }
621
622 while (!list_empty(&dpm_noirq_list)) {
623 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100624 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100625 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100626 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100627
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800628 if (!is_async(dev)) {
629 int error;
630
631 error = device_resume_noirq(dev, state, false);
632 if (error) {
633 suspend_stats.failed_resume_noirq++;
634 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
635 dpm_save_failed_dev(dev_name(dev));
636 pm_dev_err(dev, state, " noirq", error);
637 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100638 }
639
640 mutex_lock(&dpm_list_mtx);
641 put_device(dev);
642 }
643 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800644 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200645 dpm_show_time(starttime, state, 0, "noirq");
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200646 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
647}
648
649void dpm_noirq_end(void)
650{
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100651 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();
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200654}
655
656/**
657 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
658 * @state: PM transition of the system being carried out.
659 *
660 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
661 * allow device drivers' interrupt handlers to be called.
662 */
663void dpm_resume_noirq(pm_message_t state)
664{
665 dpm_noirq_resume_devices(state);
666 dpm_noirq_end();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100667}
668
669/**
670 * device_resume_early - Execute an "early resume" callback for given device.
671 * @dev: Device to handle.
672 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700673 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100674 *
675 * Runtime PM is disabled for @dev while this function is being executed.
676 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800677static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100678{
679 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200680 const char *info = NULL;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100681 int error = 0;
682
683 TRACE_DEVICE(dev);
684 TRACE_RESUME(0);
685
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200686 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200687 goto Out;
688
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800689 if (!dev->power.is_late_suspended)
690 goto Out;
691
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100692 dpm_wait_for_superior(dev, async);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800693
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100694 if (dev->pm_domain) {
695 info = "early power domain ";
696 callback = pm_late_early_op(&dev->pm_domain->ops, state);
697 } else if (dev->type && dev->type->pm) {
698 info = "early type ";
699 callback = pm_late_early_op(dev->type->pm, state);
700 } else if (dev->class && dev->class->pm) {
701 info = "early class ";
702 callback = pm_late_early_op(dev->class->pm, state);
703 } else if (dev->bus && dev->bus->pm) {
704 info = "early bus ";
705 callback = pm_late_early_op(dev->bus->pm, state);
706 }
707
708 if (!callback && dev->driver && dev->driver->pm) {
709 info = "early driver ";
710 callback = pm_late_early_op(dev->driver->pm, state);
711 }
712
713 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800714 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100715
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200716 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100717 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100718
719 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800720 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100721 return error;
722}
723
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800724static void async_resume_early(void *data, async_cookie_t cookie)
725{
726 struct device *dev = (struct device *)data;
727 int error;
728
729 error = device_resume_early(dev, pm_transition, true);
730 if (error)
731 pm_dev_err(dev, pm_transition, " async", error);
732
733 put_device(dev);
734}
735
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100736/**
737 * dpm_resume_early - Execute "early resume" callbacks for all devices.
738 * @state: PM transition of the system being carried out.
739 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200740void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100741{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800742 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100743 ktime_t starttime = ktime_get();
744
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700745 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100746 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800747 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100748
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800749 /*
750 * Advanced the async threads upfront,
751 * in case the starting of async threads is
752 * delayed by non-async resuming devices.
753 */
754 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
755 reinit_completion(&dev->power.completion);
756 if (is_async(dev)) {
757 get_device(dev);
758 async_schedule(async_resume_early, dev);
759 }
760 }
761
762 while (!list_empty(&dpm_late_early_list)) {
763 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100764 get_device(dev);
765 list_move_tail(&dev->power.entry, &dpm_suspended_list);
766 mutex_unlock(&dpm_list_mtx);
767
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800768 if (!is_async(dev)) {
769 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100770
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800771 error = device_resume_early(dev, state, false);
772 if (error) {
773 suspend_stats.failed_resume_early++;
774 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
775 dpm_save_failed_dev(dev_name(dev));
776 pm_dev_err(dev, state, " early", error);
777 }
778 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100779 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100780 put_device(dev);
781 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200782 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800783 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200784 dpm_show_time(starttime, state, 0, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700785 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100786}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100787
788/**
789 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
790 * @state: PM transition of the system being carried out.
791 */
792void dpm_resume_start(pm_message_t state)
793{
794 dpm_resume_noirq(state);
795 dpm_resume_early(state);
796}
797EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100798
799/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100800 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200801 * @dev: Device to handle.
802 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100803 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100804 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100805static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100806{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100807 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200808 const char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100809 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700810 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100811
812 TRACE_DEVICE(dev);
813 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400814
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200815 if (dev->power.syscore)
816 goto Complete;
817
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200818 if (dev->power.direct_complete) {
819 /* Match the pm_runtime_disable() in __device_suspend(). */
820 pm_runtime_enable(dev);
821 goto Complete;
822 }
823
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100824 dpm_wait_for_superior(dev, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700825 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800826 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100827
Alan Sternf76b168b2011-06-18 20:22:23 +0200828 /*
829 * This is a fib. But we'll allow new children to be added below
830 * a resumed device, even if the device hasn't been completed yet.
831 */
832 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100833
Alan Stern6d0e0e82011-06-18 22:42:09 +0200834 if (!dev->power.is_suspended)
835 goto Unlock;
836
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200837 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100838 info = "power domain ";
839 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100840 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100841 }
842
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100843 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100844 info = "type ";
845 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100846 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100847 }
848
849 if (dev->class) {
850 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100851 info = "class ";
852 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100853 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100854 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100855 info = "legacy class ";
856 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100857 goto End;
858 }
859 }
860
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200861 if (dev->bus) {
862 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100863 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100864 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200865 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100866 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100867 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100868 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200869 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400870 }
871
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100872 Driver:
873 if (!callback && dev->driver && dev->driver->pm) {
874 info = "driver ";
875 callback = pm_op(dev->driver->pm, state);
876 }
877
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200878 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100879 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200880 dev->power.is_suspended = false;
881
882 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800883 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700884 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200885
886 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100887 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100888
Alan Sterncd59abf2007-09-21 15:36:56 -0400889 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200890
Alan Sterncd59abf2007-09-21 15:36:56 -0400891 return error;
892}
893
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100894static void async_resume(void *data, async_cookie_t cookie)
895{
896 struct device *dev = (struct device *)data;
897 int error;
898
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100899 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100900 if (error)
901 pm_dev_err(dev, pm_transition, " async", error);
902 put_device(dev);
903}
904
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100905/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200906 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
907 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100908 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200909 * Execute the appropriate "resume" callback for all devices whose status
910 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400911 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200912void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400913{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100914 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100915 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400916
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700917 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200918 might_sleep();
919
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200920 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100921 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200922 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200923
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100924 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800925 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100926 if (is_async(dev)) {
927 get_device(dev);
928 async_schedule(async_resume, dev);
929 }
930 }
931
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100932 while (!list_empty(&dpm_suspended_list)) {
933 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200934 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100935 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200936 int error;
937
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200938 mutex_unlock(&dpm_list_mtx);
939
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100940 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200941 if (error) {
942 suspend_stats.failed_resume++;
943 dpm_save_failed_step(SUSPEND_RESUME);
944 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200945 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200946 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100947
948 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200949 }
950 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100951 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200952 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400953 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100955 async_synchronize_full();
Rafael J. Wysocki48059c092017-07-21 02:10:22 +0200956 dpm_show_time(starttime, state, 0, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800957
958 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700959 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200960}
961
962/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200963 * device_complete - Complete a PM transition for given device.
964 * @dev: Device to handle.
965 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200966 */
Alan Sternd1616302009-05-24 22:05:42 +0200967static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100969 void (*callback)(struct device *) = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200970 const char *info = NULL;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100971
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200972 if (dev->power.syscore)
973 return;
974
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800975 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200976
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200977 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100978 info = "completing power domain ";
979 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200980 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100981 info = "completing type ";
982 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100983 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100984 info = "completing class ";
985 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100986 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100987 info = "completing bus ";
988 callback = dev->bus->pm->complete;
989 }
990
991 if (!callback && dev->driver && dev->driver->pm) {
992 info = "completing driver ";
993 callback = dev->driver->pm->complete;
994 }
995
996 if (callback) {
997 pm_dev_dbg(dev, state, info);
998 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200999 }
1000
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001001 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +02001002
Ulf Hanssonaf939332013-04-12 09:41:06 +00001003 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001004}
1005
1006/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001007 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1008 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001009 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001010 * Execute the ->complete() callbacks for all devices whose PM status is not
1011 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001012 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001013void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001014{
1015 struct list_head list;
1016
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001017 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001018 might_sleep();
1019
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001020 INIT_LIST_HEAD(&list);
1021 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001022 while (!list_empty(&dpm_prepared_list)) {
1023 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001024
1025 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +02001026 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001027 list_move(&dev->power.entry, &list);
1028 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001029
Todd E Brandt32e8d682015-05-28 12:55:53 -07001030 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001031 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001032 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001033
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001034 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001035 put_device(dev);
1036 }
1037 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -04001038 mutex_unlock(&dpm_list_mtx);
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001039
1040 /* Allow device probing and trigger re-probing of deferred devices */
1041 device_unblock_probing();
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001042 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -04001043}
1044
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001045/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001046 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1047 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001048 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001049 * Execute "resume" callbacks for all devices and complete the PM transition of
1050 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -04001051 */
Alan Sternd1616302009-05-24 22:05:42 +02001052void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001053{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001054 dpm_resume(state);
1055 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001056}
Alan Sternd1616302009-05-24 22:05:42 +02001057EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -04001058
1059
Alan Sterncd59abf2007-09-21 15:36:56 -04001060/*------------------------- Suspend routines -------------------------*/
1061
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001062/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001063 * resume_event - Return a "resume" message for given "suspend" sleep state.
1064 * @sleep_state: PM message representing a sleep state.
1065 *
1066 * Return a PM message representing the resume event corresponding to given
1067 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001068 */
1069static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001070{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001071 switch (sleep_state.event) {
1072 case PM_EVENT_SUSPEND:
1073 return PMSG_RESUME;
1074 case PM_EVENT_FREEZE:
1075 case PM_EVENT_QUIESCE:
1076 return PMSG_RECOVER;
1077 case PM_EVENT_HIBERNATE:
1078 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001079 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001080 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001081}
1082
1083/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001084 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1085 * @dev: Device to handle.
1086 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001087 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001088 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001089 * The driver of @dev will not receive interrupts while this function is being
1090 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001091 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001092static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001093{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001094 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001095 const char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001096 int error = 0;
1097
Zhonghui Fu431d4522015-03-18 15:54:27 +01001098 TRACE_DEVICE(dev);
1099 TRACE_SUSPEND(0);
1100
Linus Torvalds098c3052016-12-13 11:42:18 -08001101 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001102
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001103 if (async_error)
1104 goto Complete;
1105
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +02001106 if (pm_wakeup_pending()) {
1107 async_error = -EBUSY;
1108 goto Complete;
1109 }
1110
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001111 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001112 goto Complete;
1113
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001114 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001115 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001116 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001117 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001118 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001119 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001120 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001121 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001122 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001123 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001124 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001125 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001126 }
1127
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001128 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001129 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001130 callback = pm_noirq_op(dev->driver->pm, state);
1131 }
1132
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001133 error = dpm_run_callback(callback, dev, state, info);
1134 if (!error)
1135 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001136 else
1137 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001138
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001139Complete:
1140 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001141 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001142 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001143}
1144
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001145static void async_suspend_noirq(void *data, async_cookie_t cookie)
1146{
1147 struct device *dev = (struct device *)data;
1148 int error;
1149
1150 error = __device_suspend_noirq(dev, pm_transition, true);
1151 if (error) {
1152 dpm_save_failed_dev(dev_name(dev));
1153 pm_dev_err(dev, pm_transition, " async", error);
1154 }
1155
1156 put_device(dev);
1157}
1158
1159static int device_suspend_noirq(struct device *dev)
1160{
1161 reinit_completion(&dev->power.completion);
1162
Zhonghui Fu431d4522015-03-18 15:54:27 +01001163 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001164 get_device(dev);
1165 async_schedule(async_suspend_noirq, dev);
1166 return 0;
1167 }
1168 return __device_suspend_noirq(dev, pm_transition, false);
1169}
1170
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001171void dpm_noirq_begin(void)
1172{
1173 cpuidle_pause();
1174 device_wakeup_arm_wake_irqs();
1175 suspend_device_irqs();
1176}
1177
1178int dpm_noirq_suspend_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001179{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001180 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001181 int error = 0;
1182
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001183 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001184 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001185 pm_transition = state;
1186 async_error = 0;
1187
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001188 while (!list_empty(&dpm_late_early_list)) {
1189 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001190
1191 get_device(dev);
1192 mutex_unlock(&dpm_list_mtx);
1193
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001194 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001195
1196 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001197 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001198 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001199 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001200 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001201 break;
1202 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001203 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001204 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001205 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001206
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001207 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001208 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001209 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001210 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001211 async_synchronize_full();
1212 if (!error)
1213 error = async_error;
1214
1215 if (error) {
1216 suspend_stats.failed_suspend_noirq++;
1217 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001218 }
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001219 dpm_show_time(starttime, state, error, "noirq");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001220 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001221 return error;
1222}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001223
1224/**
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001225 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1226 * @state: PM transition of the system being carried out.
1227 *
1228 * Prevent device drivers' interrupt handlers from being called and invoke
1229 * "noirq" suspend callbacks for all non-sysdev devices.
1230 */
1231int dpm_suspend_noirq(pm_message_t state)
1232{
1233 int ret;
1234
1235 dpm_noirq_begin();
1236 ret = dpm_noirq_suspend_devices(state);
1237 if (ret)
1238 dpm_resume_noirq(resume_event(state));
1239
1240 return ret;
1241}
1242
1243/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001244 * device_suspend_late - Execute a "late suspend" callback for given device.
1245 * @dev: Device to handle.
1246 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001247 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001248 *
1249 * Runtime PM is disabled for @dev while this function is being executed.
1250 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001251static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001252{
1253 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001254 const char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001255 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001256
Zhonghui Fu431d4522015-03-18 15:54:27 +01001257 TRACE_DEVICE(dev);
1258 TRACE_SUSPEND(0);
1259
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001260 __pm_runtime_disable(dev, false);
1261
Linus Torvalds098c3052016-12-13 11:42:18 -08001262 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001263
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001264 if (async_error)
1265 goto Complete;
1266
1267 if (pm_wakeup_pending()) {
1268 async_error = -EBUSY;
1269 goto Complete;
1270 }
1271
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001272 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001273 goto Complete;
1274
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001275 if (dev->pm_domain) {
1276 info = "late power domain ";
1277 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1278 } else if (dev->type && dev->type->pm) {
1279 info = "late type ";
1280 callback = pm_late_early_op(dev->type->pm, state);
1281 } else if (dev->class && dev->class->pm) {
1282 info = "late class ";
1283 callback = pm_late_early_op(dev->class->pm, state);
1284 } else if (dev->bus && dev->bus->pm) {
1285 info = "late bus ";
1286 callback = pm_late_early_op(dev->bus->pm, state);
1287 }
1288
1289 if (!callback && dev->driver && dev->driver->pm) {
1290 info = "late driver ";
1291 callback = pm_late_early_op(dev->driver->pm, state);
1292 }
1293
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001294 error = dpm_run_callback(callback, dev, state, info);
1295 if (!error)
1296 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001297 else
1298 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001299
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001300Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001301 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001302 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001303 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001304}
1305
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001306static void async_suspend_late(void *data, async_cookie_t cookie)
1307{
1308 struct device *dev = (struct device *)data;
1309 int error;
1310
1311 error = __device_suspend_late(dev, pm_transition, true);
1312 if (error) {
1313 dpm_save_failed_dev(dev_name(dev));
1314 pm_dev_err(dev, pm_transition, " async", error);
1315 }
1316 put_device(dev);
1317}
1318
1319static int device_suspend_late(struct device *dev)
1320{
1321 reinit_completion(&dev->power.completion);
1322
Zhonghui Fu431d4522015-03-18 15:54:27 +01001323 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001324 get_device(dev);
1325 async_schedule(async_suspend_late, dev);
1326 return 0;
1327 }
1328
1329 return __device_suspend_late(dev, pm_transition, false);
1330}
1331
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001332/**
1333 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1334 * @state: PM transition of the system being carried out.
1335 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001336int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001337{
1338 ktime_t starttime = ktime_get();
1339 int error = 0;
1340
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001341 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001342 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001343 pm_transition = state;
1344 async_error = 0;
1345
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001346 while (!list_empty(&dpm_suspended_list)) {
1347 struct device *dev = to_device(dpm_suspended_list.prev);
1348
1349 get_device(dev);
1350 mutex_unlock(&dpm_list_mtx);
1351
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001352 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001353
1354 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki3a17fb32016-05-20 23:09:49 +02001355 if (!list_empty(&dev->power.entry))
1356 list_move(&dev->power.entry, &dpm_late_early_list);
1357
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001358 if (error) {
1359 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001360 dpm_save_failed_dev(dev_name(dev));
1361 put_device(dev);
1362 break;
1363 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001364 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001365
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001366 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001367 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001368 }
1369 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001370 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001371 if (!error)
1372 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001373 if (error) {
1374 suspend_stats.failed_suspend_late++;
1375 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001376 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001377 }
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001378 dpm_show_time(starttime, state, error, "late");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001379 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001380 return error;
1381}
1382
1383/**
1384 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1385 * @state: PM transition of the system being carried out.
1386 */
1387int dpm_suspend_end(pm_message_t state)
1388{
1389 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001390 if (error)
1391 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001392
Colin Cross064b0212012-07-19 10:38:06 +02001393 error = dpm_suspend_noirq(state);
1394 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001395 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001396 return error;
1397 }
1398
1399 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001400}
1401EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001402
1403/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001404 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001405 * @dev: Device to suspend.
1406 * @state: PM transition of the system being carried out.
1407 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001408 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001409 */
1410static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001411 int (*cb)(struct device *dev, pm_message_t state),
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001412 const char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001413{
1414 int error;
1415 ktime_t calltime;
1416
1417 calltime = initcall_debug_start(dev);
1418
Todd E Brandte8bca472014-06-10 07:31:22 -07001419 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001420 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001421 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001422 suspend_report_result(cb, error);
1423
Shuah Khan53644672013-07-26 13:30:20 -06001424 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001425
1426 return error;
1427}
1428
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001429static void dpm_clear_suppliers_direct_complete(struct device *dev)
1430{
1431 struct device_link *link;
1432 int idx;
1433
1434 idx = device_links_read_lock();
1435
1436 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1437 spin_lock_irq(&link->supplier->power.lock);
1438 link->supplier->power.direct_complete = false;
1439 spin_unlock_irq(&link->supplier->power.lock);
1440 }
1441
1442 device_links_read_unlock(idx);
1443}
1444
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001445/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001446 * device_suspend - Execute "suspend" callbacks for given device.
1447 * @dev: Device to handle.
1448 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001449 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001450 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001451static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001452{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001453 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001454 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001455 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001456 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001457
Zhonghui Fu431d4522015-03-18 15:54:27 +01001458 TRACE_DEVICE(dev);
1459 TRACE_SUSPEND(0);
1460
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001461 dpm_wait_for_subordinate(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001462
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001463 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001464 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001465
Alan Stern88d26132012-09-19 21:59:02 +02001466 /*
1467 * If a device configured to wake up the system from sleep states
1468 * has been suspended at run time and there's a resume request pending
1469 * for it, this is equivalent to the device signaling wakeup, so the
1470 * system suspend operation should be aborted.
1471 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001472 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1473 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001474
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001475 if (pm_wakeup_pending()) {
1476 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001477 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001478 }
1479
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001480 if (dev->power.syscore)
1481 goto Complete;
1482
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001483 if (dev->power.direct_complete) {
1484 if (pm_runtime_status_suspended(dev)) {
1485 pm_runtime_disable(dev);
Alan Stern019d8812015-07-15 14:40:06 +02001486 if (pm_runtime_status_suspended(dev))
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001487 goto Complete;
1488
1489 pm_runtime_enable(dev);
1490 }
1491 dev->power.direct_complete = false;
1492 }
1493
Benoit Goby70fea602013-10-17 10:48:46 -07001494 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001495 device_lock(dev);
1496
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001497 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001498 info = "power domain ";
1499 callback = pm_op(&dev->pm_domain->ops, state);
1500 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001501 }
1502
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001503 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001504 info = "type ";
1505 callback = pm_op(dev->type->pm, state);
1506 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001507 }
1508
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001509 if (dev->class) {
1510 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001511 info = "class ";
1512 callback = pm_op(dev->class->pm, state);
1513 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001514 } else if (dev->class->suspend) {
1515 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001516 error = legacy_suspend(dev, state, dev->class->suspend,
1517 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001518 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001519 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001520 }
1521
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001522 if (dev->bus) {
1523 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001524 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001525 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001526 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001527 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001528 error = legacy_suspend(dev, state, dev->bus->suspend,
1529 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001530 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001531 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001532 }
1533
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001534 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001535 if (!callback && dev->driver && dev->driver->pm) {
1536 info = "driver ";
1537 callback = pm_op(dev->driver->pm, state);
1538 }
1539
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001540 error = dpm_run_callback(callback, dev, state, info);
1541
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001542 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001543 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001544 struct device *parent = dev->parent;
1545
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001546 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001547 if (parent) {
1548 spin_lock_irq(&parent->power.lock);
1549
1550 dev->parent->power.direct_complete = false;
1551 if (dev->power.wakeup_path
1552 && !dev->parent->power.ignore_children)
1553 dev->parent->power.wakeup_path = true;
1554
1555 spin_unlock_irq(&parent->power.lock);
1556 }
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001557 dpm_clear_suppliers_direct_complete(dev);
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001558 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001559
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001560 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001561 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001562
1563 Complete:
Alan Stern88d26132012-09-19 21:59:02 +02001564 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001565 async_error = error;
1566
Sahitya Tummala05a92622016-12-07 20:10:32 +05301567 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001568 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001569 return error;
1570}
1571
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001572static void async_suspend(void *data, async_cookie_t cookie)
1573{
1574 struct device *dev = (struct device *)data;
1575 int error;
1576
1577 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001578 if (error) {
1579 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001580 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001581 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001582
1583 put_device(dev);
1584}
1585
1586static int device_suspend(struct device *dev)
1587{
Wolfram Sang16735d02013-11-14 14:32:02 -08001588 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001589
Zhonghui Fu431d4522015-03-18 15:54:27 +01001590 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001591 get_device(dev);
1592 async_schedule(async_suspend, dev);
1593 return 0;
1594 }
1595
1596 return __device_suspend(dev, pm_transition, false);
1597}
1598
Alan Sterncd59abf2007-09-21 15:36:56 -04001599/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001600 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1601 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001602 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001603int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001604{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001605 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001606 int error = 0;
1607
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001608 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001609 might_sleep();
1610
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001611 cpufreq_suspend();
1612
Alan Sterncd59abf2007-09-21 15:36:56 -04001613 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001614 pm_transition = state;
1615 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001616 while (!list_empty(&dpm_prepared_list)) {
1617 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001618
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001619 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001620 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001621
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001622 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001623
Alan Stern1b3cbec2008-02-29 11:50:22 -05001624 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001625 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001626 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001627 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001628 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001629 break;
1630 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001631 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001632 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001633 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001634 if (async_error)
1635 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001636 }
1637 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001638 async_synchronize_full();
1639 if (!error)
1640 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001641 if (error) {
1642 suspend_stats.failed_suspend++;
1643 dpm_save_failed_step(SUSPEND_SUSPEND);
Rafael J. Wysocki48059c092017-07-21 02:10:22 +02001644 }
1645 dpm_show_time(starttime, state, error, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001646 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001647 return error;
1648}
1649
1650/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001651 * device_prepare - Prepare a device for system power transition.
1652 * @dev: Device to handle.
1653 * @state: PM transition of the system being carried out.
1654 *
1655 * Execute the ->prepare() callback(s) for given device. No new children of the
1656 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001657 */
Alan Sternd1616302009-05-24 22:05:42 +02001658static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001659{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001660 int (*callback)(struct device *) = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001661 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001662
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001663 if (dev->power.syscore)
1664 return 0;
1665
Alan Stern88d26132012-09-19 21:59:02 +02001666 /*
1667 * If a device's parent goes into runtime suspend at the wrong time,
1668 * it won't be possible to resume the device. To prevent this we
1669 * block runtime suspend here, during the prepare phase, and allow
1670 * it again during the complete phase.
1671 */
1672 pm_runtime_get_noresume(dev);
1673
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001674 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001675
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001676 dev->power.wakeup_path = device_may_wakeup(dev);
1677
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001678 if (dev->power.no_pm_callbacks) {
1679 ret = 1; /* Let device go direct_complete */
1680 goto unlock;
1681 }
1682
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001683 if (dev->pm_domain)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001684 callback = dev->pm_domain->ops.prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001685 else if (dev->type && dev->type->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001686 callback = dev->type->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001687 else if (dev->class && dev->class->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001688 callback = dev->class->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001689 else if (dev->bus && dev->bus->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001690 callback = dev->bus->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001691
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001692 if (!callback && dev->driver && dev->driver->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001693 callback = dev->driver->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001694
Todd E Brandt32e8d682015-05-28 12:55:53 -07001695 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001696 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001697
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001698unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001699 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001700
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001701 if (ret < 0) {
1702 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001703 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001704 return ret;
1705 }
1706 /*
1707 * A positive return value from ->prepare() means "this device appears
1708 * to be runtime-suspended and its state is fine, so if it really is
1709 * runtime-suspended, you can leave it in that state provided that you
1710 * will do the same thing with all of its descendants". This only
1711 * applies to suspend transitions, however.
1712 */
1713 spin_lock_irq(&dev->power.lock);
1714 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1715 spin_unlock_irq(&dev->power.lock);
1716 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001717}
1718
1719/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001720 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1721 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001722 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001723 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001724 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001725int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001726{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001727 int error = 0;
1728
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001729 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001730 might_sleep();
1731
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001732 /*
1733 * Give a chance for the known devices to complete their probes, before
1734 * disable probing of devices. This sync point is important at least
1735 * at boot time + hibernation restore.
1736 */
1737 wait_for_device_probe();
1738 /*
1739 * It is unsafe if probing of devices will happen during suspend or
1740 * hibernation and system behavior will be unpredictable in this case.
1741 * So, let's prohibit device's probing here and defer their probes
1742 * instead. The normal behavior will be restored in dpm_complete().
1743 */
1744 device_block_probing();
1745
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001746 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001747 while (!list_empty(&dpm_list)) {
1748 struct device *dev = to_device(dpm_list.next);
1749
1750 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001751 mutex_unlock(&dpm_list_mtx);
1752
Todd E Brandt32e8d682015-05-28 12:55:53 -07001753 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001754 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001755 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001756
1757 mutex_lock(&dpm_list_mtx);
1758 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001759 if (error == -EAGAIN) {
1760 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001761 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001762 continue;
1763 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001764 printk(KERN_INFO "PM: Device %s not prepared "
1765 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001766 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001767 put_device(dev);
1768 break;
1769 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001770 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001771 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001772 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001773 put_device(dev);
1774 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001775 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001776 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001777 return error;
1778}
1779
1780/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001781 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1782 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001783 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001784 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1785 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001786 */
Alan Sternd1616302009-05-24 22:05:42 +02001787int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001788{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001789 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001790
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001791 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001792 if (error) {
1793 suspend_stats.failed_prepare++;
1794 dpm_save_failed_step(SUSPEND_PREPARE);
1795 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001796 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001797 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001798}
Alan Sternd1616302009-05-24 22:05:42 +02001799EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001800
1801void __suspend_report_result(const char *function, void *fn, int ret)
1802{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001803 if (ret)
1804 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001805}
1806EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001807
1808/**
1809 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1810 * @dev: Device to wait for.
1811 * @subordinate: Device that needs to wait for @dev.
1812 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001813int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001814{
1815 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001816 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001817}
1818EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001819
1820/**
1821 * dpm_for_each_dev - device iterator.
1822 * @data: data for the callback.
1823 * @fn: function to be called for each device.
1824 *
1825 * Iterate over devices in dpm_list, and call @fn for each device,
1826 * passing it @data.
1827 */
1828void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1829{
1830 struct device *dev;
1831
1832 if (!fn)
1833 return;
1834
1835 device_pm_lock();
1836 list_for_each_entry(dev, &dpm_list, power.entry)
1837 fn(dev, data);
1838 device_pm_unlock();
1839}
1840EXPORT_SYMBOL_GPL(dpm_for_each_dev);
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001841
1842static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1843{
1844 if (!ops)
1845 return true;
1846
1847 return !ops->prepare &&
1848 !ops->suspend &&
1849 !ops->suspend_late &&
1850 !ops->suspend_noirq &&
1851 !ops->resume_noirq &&
1852 !ops->resume_early &&
1853 !ops->resume &&
1854 !ops->complete;
1855}
1856
1857void device_pm_check_callbacks(struct device *dev)
1858{
1859 spin_lock_irq(&dev->power.lock);
1860 dev->power.no_pm_callbacks =
Rafael J. Wysocki157c4602017-09-19 02:22:39 +02001861 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
1862 !dev->bus->suspend && !dev->bus->resume)) &&
1863 (!dev->class || (pm_ops_is_empty(dev->class->pm) &&
1864 !dev->class->suspend && !dev->class->resume)) &&
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001865 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1866 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
Rafael J. Wysocki157c4602017-09-19 02:22:39 +02001867 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
1868 !dev->driver->suspend && !dev->driver->resume));
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +01001869 spin_unlock_irq(&dev->power.lock);
1870}