blob: ea1732ed7a9d9071db73784c7e37d505ed8dea0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/power/main.c - Where the driver meets power management.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
9 *
10 * The driver model core calls device_pm_add() when a device is registered.
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011 * This will initialize the embedded device_pm_info object in the device
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
14 *
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020015 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/device.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040021#include <linux/kallsyms.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040022#include <linux/export.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070023#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040024#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020025#include <linux/pm_runtime.h>
Zhonghui Fu431d4522015-03-18 15:54:27 +010026#include <linux/pm-trace.h>
Tony Lindgren4990d4f2015-05-18 15:40:29 -070027#include <linux/pm_wakeirq.h>
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +010028#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010029#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010030#include <linux/sched/debug.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010031#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010032#include <linux/suspend.h>
Shuah Khan53644672013-07-26 13:30:20 -060033#include <trace/events/power.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080034#include <linux/cpufreq.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020035#include <linux/cpuidle.h>
Benoit Goby70fea602013-10-17 10:48:46 -070036#include <linux/timer.h>
37
Alan Sterncd59abf2007-09-21 15:36:56 -040038#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "power.h"
40
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010041typedef int (*pm_callback_t)(struct device *);
42
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010043/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020044 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010045 * because children are guaranteed to be discovered after parents, and
46 * are inserted at the back of the list on discovery.
47 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080048 * Since device_pm_add() may be called with a device lock held,
49 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010050 * dpm_list_mutex.
51 */
52
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020053LIST_HEAD(dpm_list);
Sachin Kamat7664e962012-07-17 22:38:08 +020054static LIST_HEAD(dpm_prepared_list);
55static LIST_HEAD(dpm_suspended_list);
56static LIST_HEAD(dpm_late_early_list);
57static LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
ShuoX Liu2a77c462011-08-10 23:01:26 +020059struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040060static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010061static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020063static int async_error;
64
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 Vizosoaa8e54b2016-01-07 16:46:14 +0100129 device_pm_check_callbacks(dev);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700130 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +0200131 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +0100132 dev_warn(dev, "parent %s should not be sleeping\n",
133 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400134 list_add_tail(&dev->power.entry, &dpm_list);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100135 dev->power.in_dpm_list = true;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200136 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100139/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200140 * device_pm_remove - Remove a device from the PM core's list of active devices.
141 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100142 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200143void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100146 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100147 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700148 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 list_del_init(&dev->power.entry);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +0100150 dev->power.in_dpm_list = false;
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700151 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200152 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200153 pm_runtime_remove(dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +0100154 device_pm_check_callbacks(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100155}
156
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200157/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200158 * device_pm_move_before - Move device in the PM core's list of active devices.
159 * @deva: Device to move in dpm_list.
160 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100161 */
162void device_pm_move_before(struct device *deva, struct device *devb)
163{
164 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100165 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
166 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100167 /* Delete deva from dpm_list and reinsert before devb. */
168 list_move_tail(&deva->power.entry, &devb->power.entry);
169}
170
171/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200172 * device_pm_move_after - Move device in the PM core's list of active devices.
173 * @deva: Device to move in dpm_list.
174 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100175 */
176void device_pm_move_after(struct device *deva, struct device *devb)
177{
178 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100179 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
180 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100181 /* Delete deva from dpm_list and reinsert after devb. */
182 list_move(&deva->power.entry, &devb->power.entry);
183}
184
185/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200186 * device_pm_move_last - Move device to end of the PM core's list of devices.
187 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100188 */
189void device_pm_move_last(struct device *dev)
190{
191 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100192 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100193 list_move_tail(&dev->power.entry, &dpm_list);
194}
195
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100196static ktime_t initcall_debug_start(struct device *dev)
197{
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100198 ktime_t calltime = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100199
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200200 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100201 pr_info("calling %s+ @ %i, parent: %s\n",
202 dev_name(dev), task_pid_nr(current),
203 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100204 calltime = ktime_get();
205 }
206
207 return calltime;
208}
209
210static void initcall_debug_report(struct device *dev, ktime_t calltime,
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. Wysocki48059c02017-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. Wysocki48059c02017-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. Wysocki48059c02017-07-21 02:10:22 +0200437 error ? "aborted" : "complete",
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200438 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100439}
440
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100441static int dpm_run_callback(pm_callback_t cb, struct device *dev,
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200442 pm_message_t state, const char *info)
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100443{
444 ktime_t calltime;
445 int error;
446
447 if (!cb)
448 return 0;
449
450 calltime = initcall_debug_start(dev);
451
452 pm_dev_dbg(dev, state, info);
Todd E Brandte8bca472014-06-10 07:31:22 -0700453 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100454 error = cb(dev);
Todd E Brandte8bca472014-06-10 07:31:22 -0700455 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100456 suspend_report_result(cb, error);
457
Shuah Khan53644672013-07-26 13:30:20 -0600458 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100459
460 return error;
461}
462
Benoit Goby70fea602013-10-17 10:48:46 -0700463#ifdef CONFIG_DPM_WATCHDOG
464struct dpm_watchdog {
465 struct device *dev;
466 struct task_struct *tsk;
467 struct timer_list timer;
468};
469
470#define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
471 struct dpm_watchdog wd
472
473/**
474 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
475 * @data: Watchdog object address.
476 *
477 * Called when a driver has timed out suspending or resuming.
478 * There's not much we can do here to recover so panic() to
479 * capture a crash-dump in pstore.
480 */
481static void dpm_watchdog_handler(unsigned long data)
482{
483 struct dpm_watchdog *wd = (void *)data;
484
485 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
486 show_stack(wd->tsk, NULL);
487 panic("%s %s: unrecoverable failure\n",
488 dev_driver_string(wd->dev), dev_name(wd->dev));
489}
490
491/**
492 * dpm_watchdog_set - Enable pm watchdog for given device.
493 * @wd: Watchdog. Must be allocated on the stack.
494 * @dev: Device to handle.
495 */
496static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
497{
498 struct timer_list *timer = &wd->timer;
499
500 wd->dev = dev;
501 wd->tsk = current;
502
503 init_timer_on_stack(timer);
504 /* use same timeout value for both suspend and resume */
505 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
506 timer->function = dpm_watchdog_handler;
507 timer->data = (unsigned long)wd;
508 add_timer(timer);
509}
510
511/**
512 * dpm_watchdog_clear - Disable suspend/resume watchdog.
513 * @wd: Watchdog to disable.
514 */
515static void dpm_watchdog_clear(struct dpm_watchdog *wd)
516{
517 struct timer_list *timer = &wd->timer;
518
519 del_timer_sync(timer);
520 destroy_timer_on_stack(timer);
521}
522#else
523#define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
524#define dpm_watchdog_set(x, y)
525#define dpm_watchdog_clear(x)
526#endif
527
Alan Sterncd59abf2007-09-21 15:36:56 -0400528/*------------------------- Resume routines -------------------------*/
529
530/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200531 * device_resume_noirq - Execute an "early resume" callback for given device.
532 * @dev: Device to handle.
533 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700534 * @async: If true, the device is being resumed asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400535 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200536 * The driver of @dev will not receive interrupts while this function is being
537 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400538 */
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800539static int device_resume_noirq(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400540{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100541 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200542 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400543 int error = 0;
544
545 TRACE_DEVICE(dev);
546 TRACE_RESUME(0);
547
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200548 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200549 goto Out;
550
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800551 if (!dev->power.is_noirq_suspended)
552 goto Out;
553
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100554 dpm_wait_for_superior(dev, async);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800555
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200556 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100557 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100558 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200559 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100560 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100561 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100562 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100563 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100564 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100565 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100566 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100567 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100568 }
569
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100570 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100571 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100572 callback = pm_noirq_op(dev->driver->pm, state);
573 }
574
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100575 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800576 dev->power.is_noirq_suspended = false;
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100577
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200578 Out:
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800579 complete_all(&dev->power.completion);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100580 TRACE_RESUME(error);
581 return error;
582}
583
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800584static bool is_async(struct device *dev)
585{
586 return dev->power.async_suspend && pm_async_enabled
587 && !pm_trace_is_enabled();
588}
589
590static void async_resume_noirq(void *data, async_cookie_t cookie)
591{
592 struct device *dev = (struct device *)data;
593 int error;
594
595 error = device_resume_noirq(dev, pm_transition, true);
596 if (error)
597 pm_dev_err(dev, pm_transition, " async", error);
598
599 put_device(dev);
600}
601
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200602void dpm_noirq_resume_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100603{
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800604 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100605 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100606
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700607 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200608 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800609 pm_transition = state;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100610
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800611 /*
612 * Advanced the async threads upfront,
613 * in case the starting of async threads is
614 * delayed by non-async resuming devices.
615 */
616 list_for_each_entry(dev, &dpm_noirq_list, power.entry) {
617 reinit_completion(&dev->power.completion);
618 if (is_async(dev)) {
619 get_device(dev);
620 async_schedule(async_resume_noirq, dev);
621 }
622 }
623
624 while (!list_empty(&dpm_noirq_list)) {
625 dev = to_device(dpm_noirq_list.next);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100626 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100627 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100628 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100629
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800630 if (!is_async(dev)) {
631 int error;
632
633 error = device_resume_noirq(dev, state, false);
634 if (error) {
635 suspend_stats.failed_resume_noirq++;
636 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
637 dpm_save_failed_dev(dev_name(dev));
638 pm_dev_err(dev, state, " noirq", error);
639 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100640 }
641
642 mutex_lock(&dpm_list_mtx);
643 put_device(dev);
644 }
645 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng76569fa2014-02-18 10:28:45 +0800646 async_synchronize_full();
Rafael J. Wysocki48059c02017-07-21 02:10:22 +0200647 dpm_show_time(starttime, state, 0, "noirq");
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200648 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
649}
650
651void dpm_noirq_end(void)
652{
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100653 resume_device_irqs();
Tony Lindgren4990d4f2015-05-18 15:40:29 -0700654 device_wakeup_disarm_wake_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200655 cpuidle_resume();
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +0200656}
657
658/**
659 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
660 * @state: PM transition of the system being carried out.
661 *
662 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
663 * allow device drivers' interrupt handlers to be called.
664 */
665void dpm_resume_noirq(pm_message_t state)
666{
667 dpm_noirq_resume_devices(state);
668 dpm_noirq_end();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100669}
670
671/**
672 * device_resume_early - Execute an "early resume" callback for given device.
673 * @dev: Device to handle.
674 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -0700675 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100676 *
677 * Runtime PM is disabled for @dev while this function is being executed.
678 */
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800679static int device_resume_early(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100680{
681 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200682 const char *info = NULL;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100683 int error = 0;
684
685 TRACE_DEVICE(dev);
686 TRACE_RESUME(0);
687
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200688 if (dev->power.syscore || dev->power.direct_complete)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200689 goto Out;
690
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800691 if (!dev->power.is_late_suspended)
692 goto Out;
693
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100694 dpm_wait_for_superior(dev, async);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800695
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100696 if (dev->pm_domain) {
697 info = "early power domain ";
698 callback = pm_late_early_op(&dev->pm_domain->ops, state);
699 } else if (dev->type && dev->type->pm) {
700 info = "early type ";
701 callback = pm_late_early_op(dev->type->pm, state);
702 } else if (dev->class && dev->class->pm) {
703 info = "early class ";
704 callback = pm_late_early_op(dev->class->pm, state);
705 } else if (dev->bus && dev->bus->pm) {
706 info = "early bus ";
707 callback = pm_late_early_op(dev->bus->pm, state);
708 }
709
710 if (!callback && dev->driver && dev->driver->pm) {
711 info = "early driver ";
712 callback = pm_late_early_op(dev->driver->pm, state);
713 }
714
715 error = dpm_run_callback(callback, dev, state, info);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +0800716 dev->power.is_late_suspended = false;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100717
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200718 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100719 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100720
721 pm_runtime_enable(dev);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800722 complete_all(&dev->power.completion);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100723 return error;
724}
725
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800726static void async_resume_early(void *data, async_cookie_t cookie)
727{
728 struct device *dev = (struct device *)data;
729 int error;
730
731 error = device_resume_early(dev, pm_transition, true);
732 if (error)
733 pm_dev_err(dev, pm_transition, " async", error);
734
735 put_device(dev);
736}
737
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100738/**
739 * dpm_resume_early - Execute "early resume" callbacks for all devices.
740 * @state: PM transition of the system being carried out.
741 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200742void dpm_resume_early(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100743{
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800744 struct device *dev;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100745 ktime_t starttime = ktime_get();
746
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700747 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100748 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800749 pm_transition = state;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100750
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800751 /*
752 * Advanced the async threads upfront,
753 * in case the starting of async threads is
754 * delayed by non-async resuming devices.
755 */
756 list_for_each_entry(dev, &dpm_late_early_list, power.entry) {
757 reinit_completion(&dev->power.completion);
758 if (is_async(dev)) {
759 get_device(dev);
760 async_schedule(async_resume_early, dev);
761 }
762 }
763
764 while (!list_empty(&dpm_late_early_list)) {
765 dev = to_device(dpm_late_early_list.next);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100766 get_device(dev);
767 list_move_tail(&dev->power.entry, &dpm_suspended_list);
768 mutex_unlock(&dpm_list_mtx);
769
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800770 if (!is_async(dev)) {
771 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100772
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800773 error = device_resume_early(dev, state, false);
774 if (error) {
775 suspend_stats.failed_resume_early++;
776 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
777 dpm_save_failed_dev(dev_name(dev));
778 pm_dev_err(dev, state, " early", error);
779 }
780 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100781 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100782 put_device(dev);
783 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200784 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng9e5e7912014-02-18 10:28:46 +0800785 async_synchronize_full();
Rafael J. Wysocki48059c02017-07-21 02:10:22 +0200786 dpm_show_time(starttime, state, 0, "early");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700787 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100788}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100789
790/**
791 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
792 * @state: PM transition of the system being carried out.
793 */
794void dpm_resume_start(pm_message_t state)
795{
796 dpm_resume_noirq(state);
797 dpm_resume_early(state);
798}
799EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100800
801/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100802 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200803 * @dev: Device to handle.
804 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100805 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100806 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100807static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100808{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100809 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200810 const char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100811 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -0700812 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100813
814 TRACE_DEVICE(dev);
815 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400816
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200817 if (dev->power.syscore)
818 goto Complete;
819
Rafael J. Wysockiaae45182014-05-16 02:46:50 +0200820 if (dev->power.direct_complete) {
821 /* Match the pm_runtime_disable() in __device_suspend(). */
822 pm_runtime_enable(dev);
823 goto Complete;
824 }
825
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +0100826 dpm_wait_for_superior(dev, async);
Benoit Goby70fea602013-10-17 10:48:46 -0700827 dpm_watchdog_set(&wd, dev);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800828 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100829
Alan Sternf76b168b2011-06-18 20:22:23 +0200830 /*
831 * This is a fib. But we'll allow new children to be added below
832 * a resumed device, even if the device hasn't been completed yet.
833 */
834 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100835
Alan Stern6d0e0e82011-06-18 22:42:09 +0200836 if (!dev->power.is_suspended)
837 goto Unlock;
838
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200839 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100840 info = "power domain ";
841 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100842 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100843 }
844
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100845 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100846 info = "type ";
847 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100848 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100849 }
850
851 if (dev->class) {
852 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100853 info = "class ";
854 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100855 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100856 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100857 info = "legacy class ";
858 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100859 goto End;
860 }
861 }
862
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200863 if (dev->bus) {
864 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100865 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100866 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200867 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100868 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100869 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100870 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200871 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400872 }
873
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100874 Driver:
875 if (!callback && dev->driver && dev->driver->pm) {
876 info = "driver ";
877 callback = pm_op(dev->driver->pm, state);
878 }
879
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200880 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100881 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200882 dev->power.is_suspended = false;
883
884 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800885 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -0700886 dpm_watchdog_clear(&wd);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200887
888 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100889 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100890
Alan Sterncd59abf2007-09-21 15:36:56 -0400891 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200892
Alan Sterncd59abf2007-09-21 15:36:56 -0400893 return error;
894}
895
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100896static void async_resume(void *data, async_cookie_t cookie)
897{
898 struct device *dev = (struct device *)data;
899 int error;
900
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100901 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100902 if (error)
903 pm_dev_err(dev, pm_transition, " async", error);
904 put_device(dev);
905}
906
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100907/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200908 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
909 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100910 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200911 * Execute the appropriate "resume" callback for all devices whose status
912 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400913 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200914void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400915{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100916 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100917 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400918
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700919 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200920 might_sleep();
921
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200922 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100923 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200924 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200925
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100926 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Wolfram Sang16735d02013-11-14 14:32:02 -0800927 reinit_completion(&dev->power.completion);
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100928 if (is_async(dev)) {
929 get_device(dev);
930 async_schedule(async_resume, dev);
931 }
932 }
933
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100934 while (!list_empty(&dpm_suspended_list)) {
935 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200936 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100937 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200938 int error;
939
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200940 mutex_unlock(&dpm_list_mtx);
941
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100942 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200943 if (error) {
944 suspend_stats.failed_resume++;
945 dpm_save_failed_step(SUSPEND_RESUME);
946 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200947 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200948 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100949
950 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200951 }
952 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100953 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200954 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400955 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200956 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100957 async_synchronize_full();
Rafael J. Wysocki48059c02017-07-21 02:10:22 +0200958 dpm_show_time(starttime, state, 0, NULL);
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800959
960 cpufreq_resume();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700961 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200962}
963
964/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200965 * device_complete - Complete a PM transition for given device.
966 * @dev: Device to handle.
967 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968 */
Alan Sternd1616302009-05-24 22:05:42 +0200969static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200970{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100971 void (*callback)(struct device *) = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +0200972 const char *info = NULL;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100973
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200974 if (dev->power.syscore)
975 return;
976
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800977 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200978
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200979 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100980 info = "completing power domain ";
981 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200982 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100983 info = "completing type ";
984 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100985 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100986 info = "completing class ";
987 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100988 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100989 info = "completing bus ";
990 callback = dev->bus->pm->complete;
991 }
992
993 if (!callback && dev->driver && dev->driver->pm) {
994 info = "completing driver ";
995 callback = dev->driver->pm->complete;
996 }
997
998 if (callback) {
999 pm_dev_dbg(dev, state, info);
1000 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001001 }
1002
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001003 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +02001004
Ulf Hanssonaf939332013-04-12 09:41:06 +00001005 pm_runtime_put(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001006}
1007
1008/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001009 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1010 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001011 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001012 * Execute the ->complete() callbacks for all devices whose PM status is not
1013 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001014 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001015void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001016{
1017 struct list_head list;
1018
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001019 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001020 might_sleep();
1021
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001022 INIT_LIST_HEAD(&list);
1023 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001024 while (!list_empty(&dpm_prepared_list)) {
1025 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001026
1027 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +02001028 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001029 list_move(&dev->power.entry, &list);
1030 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001031
Todd E Brandt32e8d682015-05-28 12:55:53 -07001032 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001033 device_complete(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001034 trace_device_pm_callback_end(dev, 0);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001035
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +01001036 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001037 put_device(dev);
1038 }
1039 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -04001040 mutex_unlock(&dpm_list_mtx);
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001041
1042 /* Allow device probing and trigger re-probing of deferred devices */
1043 device_unblock_probing();
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001044 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
Alan Sterncd59abf2007-09-21 15:36:56 -04001045}
1046
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001047/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001048 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1049 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001050 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001051 * Execute "resume" callbacks for all devices and complete the PM transition of
1052 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -04001053 */
Alan Sternd1616302009-05-24 22:05:42 +02001054void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001055{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001056 dpm_resume(state);
1057 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001058}
Alan Sternd1616302009-05-24 22:05:42 +02001059EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -04001060
1061
Alan Sterncd59abf2007-09-21 15:36:56 -04001062/*------------------------- Suspend routines -------------------------*/
1063
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001064/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001065 * resume_event - Return a "resume" message for given "suspend" sleep state.
1066 * @sleep_state: PM message representing a sleep state.
1067 *
1068 * Return a PM message representing the resume event corresponding to given
1069 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001070 */
1071static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001072{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001073 switch (sleep_state.event) {
1074 case PM_EVENT_SUSPEND:
1075 return PMSG_RESUME;
1076 case PM_EVENT_FREEZE:
1077 case PM_EVENT_QUIESCE:
1078 return PMSG_RECOVER;
1079 case PM_EVENT_HIBERNATE:
1080 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -04001081 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001082 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -04001083}
1084
1085/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001086 * device_suspend_noirq - Execute a "late suspend" callback for given device.
1087 * @dev: Device to handle.
1088 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001089 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001090 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001091 * The driver of @dev will not receive interrupts while this function is being
1092 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001093 */
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001094static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001095{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001096 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001097 const char *info = NULL;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001098 int error = 0;
1099
Zhonghui Fu431d4522015-03-18 15:54:27 +01001100 TRACE_DEVICE(dev);
1101 TRACE_SUSPEND(0);
1102
Linus Torvalds098c3052016-12-13 11:42:18 -08001103 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001104
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001105 if (async_error)
1106 goto Complete;
1107
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +02001108 if (pm_wakeup_pending()) {
1109 async_error = -EBUSY;
1110 goto Complete;
1111 }
1112
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001113 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001114 goto Complete;
1115
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001116 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001117 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001118 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001119 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001120 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001121 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001122 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001123 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001124 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001125 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001126 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001127 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001128 }
1129
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001130 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001131 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001132 callback = pm_noirq_op(dev->driver->pm, state);
1133 }
1134
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001135 error = dpm_run_callback(callback, dev, state, info);
1136 if (!error)
1137 dev->power.is_noirq_suspended = true;
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001138 else
1139 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001140
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001141Complete:
1142 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001143 TRACE_SUSPEND(error);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001144 return error;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001145}
1146
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001147static void async_suspend_noirq(void *data, async_cookie_t cookie)
1148{
1149 struct device *dev = (struct device *)data;
1150 int error;
1151
1152 error = __device_suspend_noirq(dev, pm_transition, true);
1153 if (error) {
1154 dpm_save_failed_dev(dev_name(dev));
1155 pm_dev_err(dev, pm_transition, " async", error);
1156 }
1157
1158 put_device(dev);
1159}
1160
1161static int device_suspend_noirq(struct device *dev)
1162{
1163 reinit_completion(&dev->power.completion);
1164
Zhonghui Fu431d4522015-03-18 15:54:27 +01001165 if (is_async(dev)) {
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001166 get_device(dev);
1167 async_schedule(async_suspend_noirq, dev);
1168 return 0;
1169 }
1170 return __device_suspend_noirq(dev, pm_transition, false);
1171}
1172
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001173void dpm_noirq_begin(void)
1174{
1175 cpuidle_pause();
1176 device_wakeup_arm_wake_irqs();
1177 suspend_device_irqs();
1178}
1179
1180int dpm_noirq_suspend_devices(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001181{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001182 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001183 int error = 0;
1184
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001185 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001186 mutex_lock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001187 pm_transition = state;
1188 async_error = 0;
1189
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001190 while (!list_empty(&dpm_late_early_list)) {
1191 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001192
1193 get_device(dev);
1194 mutex_unlock(&dpm_list_mtx);
1195
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001196 error = device_suspend_noirq(dev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001197
1198 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001199 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001200 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001201 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001202 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001203 break;
1204 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001205 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001206 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +01001207 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001208
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001209 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001210 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001211 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +02001212 mutex_unlock(&dpm_list_mtx);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001213 async_synchronize_full();
1214 if (!error)
1215 error = async_error;
1216
1217 if (error) {
1218 suspend_stats.failed_suspend_noirq++;
1219 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
Liu, Chuansheng28b6fd62014-02-18 10:28:47 +08001220 }
Rafael J. Wysocki48059c02017-07-21 02:10:22 +02001221 dpm_show_time(starttime, state, error, "noirq");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001222 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001223 return error;
1224}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001225
1226/**
Rafael J. Wysocki786f41f2017-07-21 02:09:22 +02001227 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1228 * @state: PM transition of the system being carried out.
1229 *
1230 * Prevent device drivers' interrupt handlers from being called and invoke
1231 * "noirq" suspend callbacks for all non-sysdev devices.
1232 */
1233int dpm_suspend_noirq(pm_message_t state)
1234{
1235 int ret;
1236
1237 dpm_noirq_begin();
1238 ret = dpm_noirq_suspend_devices(state);
1239 if (ret)
1240 dpm_resume_noirq(resume_event(state));
1241
1242 return ret;
1243}
1244
1245/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001246 * device_suspend_late - Execute a "late suspend" callback for given device.
1247 * @dev: Device to handle.
1248 * @state: PM transition of the system being carried out.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001249 * @async: If true, the device is being suspended asynchronously.
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001250 *
1251 * Runtime PM is disabled for @dev while this function is being executed.
1252 */
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001253static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001254{
1255 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001256 const char *info = NULL;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001257 int error = 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001258
Zhonghui Fu431d4522015-03-18 15:54:27 +01001259 TRACE_DEVICE(dev);
1260 TRACE_SUSPEND(0);
1261
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +01001262 __pm_runtime_disable(dev, false);
1263
Linus Torvalds098c3052016-12-13 11:42:18 -08001264 dpm_wait_for_subordinate(dev, async);
Brian Norris6f75c3f2016-11-09 17:21:08 -08001265
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001266 if (async_error)
1267 goto Complete;
1268
1269 if (pm_wakeup_pending()) {
1270 async_error = -EBUSY;
1271 goto Complete;
1272 }
1273
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001274 if (dev->power.syscore || dev->power.direct_complete)
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001275 goto Complete;
1276
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001277 if (dev->pm_domain) {
1278 info = "late power domain ";
1279 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1280 } else if (dev->type && dev->type->pm) {
1281 info = "late type ";
1282 callback = pm_late_early_op(dev->type->pm, state);
1283 } else if (dev->class && dev->class->pm) {
1284 info = "late class ";
1285 callback = pm_late_early_op(dev->class->pm, state);
1286 } else if (dev->bus && dev->bus->pm) {
1287 info = "late bus ";
1288 callback = pm_late_early_op(dev->bus->pm, state);
1289 }
1290
1291 if (!callback && dev->driver && dev->driver->pm) {
1292 info = "late driver ";
1293 callback = pm_late_early_op(dev->driver->pm, state);
1294 }
1295
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001296 error = dpm_run_callback(callback, dev, state, info);
1297 if (!error)
1298 dev->power.is_late_suspended = true;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001299 else
1300 async_error = error;
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001301
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001302Complete:
Zhonghui Fu431d4522015-03-18 15:54:27 +01001303 TRACE_SUSPEND(error);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001304 complete_all(&dev->power.completion);
Liu, Chuansheng3d2699b2014-02-18 10:28:44 +08001305 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001306}
1307
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001308static void async_suspend_late(void *data, async_cookie_t cookie)
1309{
1310 struct device *dev = (struct device *)data;
1311 int error;
1312
1313 error = __device_suspend_late(dev, pm_transition, true);
1314 if (error) {
1315 dpm_save_failed_dev(dev_name(dev));
1316 pm_dev_err(dev, pm_transition, " async", error);
1317 }
1318 put_device(dev);
1319}
1320
1321static int device_suspend_late(struct device *dev)
1322{
1323 reinit_completion(&dev->power.completion);
1324
Zhonghui Fu431d4522015-03-18 15:54:27 +01001325 if (is_async(dev)) {
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001326 get_device(dev);
1327 async_schedule(async_suspend_late, dev);
1328 return 0;
1329 }
1330
1331 return __device_suspend_late(dev, pm_transition, false);
1332}
1333
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001334/**
1335 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1336 * @state: PM transition of the system being carried out.
1337 */
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +02001338int dpm_suspend_late(pm_message_t state)
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001339{
1340 ktime_t starttime = ktime_get();
1341 int error = 0;
1342
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001343 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001344 mutex_lock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001345 pm_transition = state;
1346 async_error = 0;
1347
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001348 while (!list_empty(&dpm_suspended_list)) {
1349 struct device *dev = to_device(dpm_suspended_list.prev);
1350
1351 get_device(dev);
1352 mutex_unlock(&dpm_list_mtx);
1353
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001354 error = device_suspend_late(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001355
1356 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki3a17fb32016-05-20 23:09:49 +02001357 if (!list_empty(&dev->power.entry))
1358 list_move(&dev->power.entry, &dpm_late_early_list);
1359
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001360 if (error) {
1361 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001362 dpm_save_failed_dev(dev_name(dev));
1363 put_device(dev);
1364 break;
1365 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001366 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001367
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001368 if (async_error)
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +02001369 break;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001370 }
1371 mutex_unlock(&dpm_list_mtx);
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001372 async_synchronize_full();
Imre Deak246ef762014-10-24 20:29:09 +03001373 if (!error)
1374 error = async_error;
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001375 if (error) {
1376 suspend_stats.failed_suspend_late++;
1377 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001378 dpm_resume_early(resume_event(state));
Liu, Chuanshengde377b32014-02-18 10:28:48 +08001379 }
Rafael J. Wysocki48059c02017-07-21 02:10:22 +02001380 dpm_show_time(starttime, state, error, "late");
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001381 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001382 return error;
1383}
1384
1385/**
1386 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1387 * @state: PM transition of the system being carried out.
1388 */
1389int dpm_suspend_end(pm_message_t state)
1390{
1391 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001392 if (error)
1393 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001394
Colin Cross064b0212012-07-19 10:38:06 +02001395 error = dpm_suspend_noirq(state);
1396 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001397 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001398 return error;
1399 }
1400
1401 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001402}
1403EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001404
1405/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001406 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001407 * @dev: Device to suspend.
1408 * @state: PM transition of the system being carried out.
1409 * @cb: Suspend callback to execute.
Randy Dunlap58c256a2014-07-27 16:17:15 -07001410 * @info: string description of caller.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001411 */
1412static int legacy_suspend(struct device *dev, pm_message_t state,
Shuah Khan53644672013-07-26 13:30:20 -06001413 int (*cb)(struct device *dev, pm_message_t state),
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001414 const char *info)
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001415{
1416 int error;
1417 ktime_t calltime;
1418
1419 calltime = initcall_debug_start(dev);
1420
Todd E Brandte8bca472014-06-10 07:31:22 -07001421 trace_device_pm_callback_start(dev, info, state.event);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001422 error = cb(dev, state);
Todd E Brandte8bca472014-06-10 07:31:22 -07001423 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001424 suspend_report_result(cb, error);
1425
Shuah Khan53644672013-07-26 13:30:20 -06001426 initcall_debug_report(dev, calltime, error, state, info);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001427
1428 return error;
1429}
1430
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001431static void dpm_clear_suppliers_direct_complete(struct device *dev)
1432{
1433 struct device_link *link;
1434 int idx;
1435
1436 idx = device_links_read_lock();
1437
1438 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
1439 spin_lock_irq(&link->supplier->power.lock);
1440 link->supplier->power.direct_complete = false;
1441 spin_unlock_irq(&link->supplier->power.lock);
1442 }
1443
1444 device_links_read_unlock(idx);
1445}
1446
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001447/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001448 * device_suspend - Execute "suspend" callbacks for given device.
1449 * @dev: Device to handle.
1450 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001451 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001452 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001453static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001454{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001455 pm_callback_t callback = NULL;
Krzysztof Kozlowskie3771fa2017-06-12 17:19:32 +02001456 const char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001457 int error = 0;
Benoit Goby70fea602013-10-17 10:48:46 -07001458 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
Alan Sterncd59abf2007-09-21 15:36:56 -04001459
Zhonghui Fu431d4522015-03-18 15:54:27 +01001460 TRACE_DEVICE(dev);
1461 TRACE_SUSPEND(0);
1462
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001463 dpm_wait_for_subordinate(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001464
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001465 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001466 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001467
Alan Stern88d26132012-09-19 21:59:02 +02001468 /*
1469 * If a device configured to wake up the system from sleep states
1470 * has been suspended at run time and there's a resume request pending
1471 * for it, this is equivalent to the device signaling wakeup, so the
1472 * system suspend operation should be aborted.
1473 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001474 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1475 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001476
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001477 if (pm_wakeup_pending()) {
1478 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001479 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001480 }
1481
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001482 if (dev->power.syscore)
1483 goto Complete;
1484
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001485 if (dev->power.direct_complete) {
1486 if (pm_runtime_status_suspended(dev)) {
1487 pm_runtime_disable(dev);
Alan Stern019d8812015-07-15 14:40:06 +02001488 if (pm_runtime_status_suspended(dev))
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001489 goto Complete;
1490
1491 pm_runtime_enable(dev);
1492 }
1493 dev->power.direct_complete = false;
1494 }
1495
Benoit Goby70fea602013-10-17 10:48:46 -07001496 dpm_watchdog_set(&wd, dev);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001497 device_lock(dev);
1498
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001499 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001500 info = "power domain ";
1501 callback = pm_op(&dev->pm_domain->ops, state);
1502 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001503 }
1504
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001505 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001506 info = "type ";
1507 callback = pm_op(dev->type->pm, state);
1508 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001509 }
1510
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001511 if (dev->class) {
1512 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001513 info = "class ";
1514 callback = pm_op(dev->class->pm, state);
1515 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001516 } else if (dev->class->suspend) {
1517 pm_dev_dbg(dev, state, "legacy class ");
Shuah Khan53644672013-07-26 13:30:20 -06001518 error = legacy_suspend(dev, state, dev->class->suspend,
1519 "legacy class ");
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001520 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001521 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001522 }
1523
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001524 if (dev->bus) {
1525 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001526 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001527 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001528 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001529 pm_dev_dbg(dev, state, "legacy bus ");
Shuah Khan53644672013-07-26 13:30:20 -06001530 error = legacy_suspend(dev, state, dev->bus->suspend,
1531 "legacy bus ");
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001532 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001533 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001534 }
1535
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001536 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001537 if (!callback && dev->driver && dev->driver->pm) {
1538 info = "driver ";
1539 callback = pm_op(dev->driver->pm, state);
1540 }
1541
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001542 error = dpm_run_callback(callback, dev, state, info);
1543
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001544 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001545 if (!error) {
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001546 struct device *parent = dev->parent;
1547
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001548 dev->power.is_suspended = true;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001549 if (parent) {
1550 spin_lock_irq(&parent->power.lock);
1551
1552 dev->parent->power.direct_complete = false;
1553 if (dev->power.wakeup_path
1554 && !dev->parent->power.ignore_children)
1555 dev->parent->power.wakeup_path = true;
1556
1557 spin_unlock_irq(&parent->power.lock);
1558 }
Rafael J. Wysocki8c73b422016-10-30 17:28:49 +01001559 dpm_clear_suppliers_direct_complete(dev);
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001560 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001561
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001562 device_unlock(dev);
Benoit Goby70fea602013-10-17 10:48:46 -07001563 dpm_watchdog_clear(&wd);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001564
1565 Complete:
Alan Stern88d26132012-09-19 21:59:02 +02001566 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001567 async_error = error;
1568
Sahitya Tummala05a92622016-12-07 20:10:32 +05301569 complete_all(&dev->power.completion);
Zhonghui Fu431d4522015-03-18 15:54:27 +01001570 TRACE_SUSPEND(error);
Alan Sterncd59abf2007-09-21 15:36:56 -04001571 return error;
1572}
1573
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001574static void async_suspend(void *data, async_cookie_t cookie)
1575{
1576 struct device *dev = (struct device *)data;
1577 int error;
1578
1579 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001580 if (error) {
1581 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001582 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001583 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001584
1585 put_device(dev);
1586}
1587
1588static int device_suspend(struct device *dev)
1589{
Wolfram Sang16735d02013-11-14 14:32:02 -08001590 reinit_completion(&dev->power.completion);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001591
Zhonghui Fu431d4522015-03-18 15:54:27 +01001592 if (is_async(dev)) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001593 get_device(dev);
1594 async_schedule(async_suspend, dev);
1595 return 0;
1596 }
1597
1598 return __device_suspend(dev, pm_transition, false);
1599}
1600
Alan Sterncd59abf2007-09-21 15:36:56 -04001601/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001602 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1603 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001604 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001605int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001606{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001607 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001608 int error = 0;
1609
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001610 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001611 might_sleep();
1612
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001613 cpufreq_suspend();
1614
Alan Sterncd59abf2007-09-21 15:36:56 -04001615 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001616 pm_transition = state;
1617 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001618 while (!list_empty(&dpm_prepared_list)) {
1619 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001620
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001621 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001622 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001623
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001624 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001625
Alan Stern1b3cbec2008-02-29 11:50:22 -05001626 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001627 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001628 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001629 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001630 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001631 break;
1632 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001633 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001634 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001635 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001636 if (async_error)
1637 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001638 }
1639 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001640 async_synchronize_full();
1641 if (!error)
1642 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001643 if (error) {
1644 suspend_stats.failed_suspend++;
1645 dpm_save_failed_step(SUSPEND_SUSPEND);
Rafael J. Wysocki48059c02017-07-21 02:10:22 +02001646 }
1647 dpm_show_time(starttime, state, error, NULL);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001648 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001649 return error;
1650}
1651
1652/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001653 * device_prepare - Prepare a device for system power transition.
1654 * @dev: Device to handle.
1655 * @state: PM transition of the system being carried out.
1656 *
1657 * Execute the ->prepare() callback(s) for given device. No new children of the
1658 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001659 */
Alan Sternd1616302009-05-24 22:05:42 +02001660static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001661{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001662 int (*callback)(struct device *) = NULL;
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001663 int ret = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001664
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001665 if (dev->power.syscore)
1666 return 0;
1667
Alan Stern88d26132012-09-19 21:59:02 +02001668 /*
1669 * If a device's parent goes into runtime suspend at the wrong time,
1670 * it won't be possible to resume the device. To prevent this we
1671 * block runtime suspend here, during the prepare phase, and allow
1672 * it again during the complete phase.
1673 */
1674 pm_runtime_get_noresume(dev);
1675
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001676 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001677
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001678 dev->power.wakeup_path = device_may_wakeup(dev);
1679
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001680 if (dev->power.no_pm_callbacks) {
1681 ret = 1; /* Let device go direct_complete */
1682 goto unlock;
1683 }
1684
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001685 if (dev->pm_domain)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001686 callback = dev->pm_domain->ops.prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001687 else if (dev->type && dev->type->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001688 callback = dev->type->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001689 else if (dev->class && dev->class->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001690 callback = dev->class->pm->prepare;
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001691 else if (dev->bus && dev->bus->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001692 callback = dev->bus->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001693
Thierry Redingfba1fbf2016-04-28 14:42:34 +02001694 if (!callback && dev->driver && dev->driver->pm)
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001695 callback = dev->driver->pm->prepare;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001696
Todd E Brandt32e8d682015-05-28 12:55:53 -07001697 if (callback)
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001698 ret = callback(dev);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001699
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001700unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001701 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001702
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001703 if (ret < 0) {
1704 suspend_report_result(callback, ret);
Ulf Hanssonaa1b9f12013-11-13 15:45:03 +01001705 pm_runtime_put(dev);
Rafael J. Wysockiaae45182014-05-16 02:46:50 +02001706 return ret;
1707 }
1708 /*
1709 * A positive return value from ->prepare() means "this device appears
1710 * to be runtime-suspended and its state is fine, so if it really is
1711 * runtime-suspended, you can leave it in that state provided that you
1712 * will do the same thing with all of its descendants". This only
1713 * applies to suspend transitions, however.
1714 */
1715 spin_lock_irq(&dev->power.lock);
1716 dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND;
1717 spin_unlock_irq(&dev->power.lock);
1718 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001719}
1720
1721/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001722 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1723 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001724 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001725 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001726 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001727int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001728{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001729 int error = 0;
1730
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001731 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001732 might_sleep();
1733
Strashko, Grygorii013c0742015-11-10 11:42:34 +02001734 /*
1735 * Give a chance for the known devices to complete their probes, before
1736 * disable probing of devices. This sync point is important at least
1737 * at boot time + hibernation restore.
1738 */
1739 wait_for_device_probe();
1740 /*
1741 * It is unsafe if probing of devices will happen during suspend or
1742 * hibernation and system behavior will be unpredictable in this case.
1743 * So, let's prohibit device's probing here and defer their probes
1744 * instead. The normal behavior will be restored in dpm_complete().
1745 */
1746 device_block_probing();
1747
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001748 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001749 while (!list_empty(&dpm_list)) {
1750 struct device *dev = to_device(dpm_list.next);
1751
1752 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001753 mutex_unlock(&dpm_list_mtx);
1754
Todd E Brandt32e8d682015-05-28 12:55:53 -07001755 trace_device_pm_callback_start(dev, "", state.event);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001756 error = device_prepare(dev, state);
Todd E Brandt32e8d682015-05-28 12:55:53 -07001757 trace_device_pm_callback_end(dev, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001758
1759 mutex_lock(&dpm_list_mtx);
1760 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001761 if (error == -EAGAIN) {
1762 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001763 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001764 continue;
1765 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001766 printk(KERN_INFO "PM: Device %s not prepared "
1767 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001768 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001769 put_device(dev);
1770 break;
1771 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001772 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001773 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001774 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001775 put_device(dev);
1776 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001777 mutex_unlock(&dpm_list_mtx);
Todd E Brandtbb3632c2014-06-06 05:40:17 -07001778 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001779 return error;
1780}
1781
1782/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001783 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1784 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001785 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001786 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1787 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001788 */
Alan Sternd1616302009-05-24 22:05:42 +02001789int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001790{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001791 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001792
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001793 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001794 if (error) {
1795 suspend_stats.failed_prepare++;
1796 dpm_save_failed_step(SUSPEND_PREPARE);
1797 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001798 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001799 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001800}
Alan Sternd1616302009-05-24 22:05:42 +02001801EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001802
1803void __suspend_report_result(const char *function, void *fn, int ret)
1804{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001805 if (ret)
1806 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001807}
1808EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001809
1810/**
1811 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1812 * @dev: Device to wait for.
1813 * @subordinate: Device that needs to wait for @dev.
1814 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001815int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001816{
1817 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001818 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001819}
1820EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001821
1822/**
1823 * dpm_for_each_dev - device iterator.
1824 * @data: data for the callback.
1825 * @fn: function to be called for each device.
1826 *
1827 * Iterate over devices in dpm_list, and call @fn for each device,
1828 * passing it @data.
1829 */
1830void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1831{
1832 struct device *dev;
1833
1834 if (!fn)
1835 return;
1836
1837 device_pm_lock();
1838 list_for_each_entry(dev, &dpm_list, power.entry)
1839 fn(dev, data);
1840 device_pm_unlock();
1841}
1842EXPORT_SYMBOL_GPL(dpm_for_each_dev);
Tomeu Vizosoaa8e54b2016-01-07 16:46:14 +01001843
1844static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
1845{
1846 if (!ops)
1847 return true;
1848
1849 return !ops->prepare &&
1850 !ops->suspend &&
1851 !ops->suspend_late &&
1852 !ops->suspend_noirq &&
1853 !ops->resume_noirq &&
1854 !ops->resume_early &&
1855 !ops->resume &&
1856 !ops->complete;
1857}
1858
1859void device_pm_check_callbacks(struct device *dev)
1860{
1861 spin_lock_irq(&dev->power.lock);
1862 dev->power.no_pm_callbacks =
1863 (!dev->bus || pm_ops_is_empty(dev->bus->pm)) &&
1864 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
1865 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
1866 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
1867 (!dev->driver || pm_ops_is_empty(dev->driver->pm));
1868 spin_unlock_irq(&dev->power.lock);
1869}