blob: 15beb500a4e4c1a837092e75554ea368dbb1ea1f [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>
Alan Sterncd59abf2007-09-21 15:36:56 -040026#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +010027#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010028#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010029#include <linux/async.h>
Rafael J. Wysocki1e752272010-12-03 22:58:05 +010030#include <linux/suspend.h>
Preeti U Murthy8651f972012-07-09 10:12:56 +020031#include <linux/cpuidle.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040032#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "power.h"
34
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +010035typedef int (*pm_callback_t)(struct device *);
36
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010037/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020038 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010039 * because children are guaranteed to be discovered after parents, and
40 * are inserted at the back of the list on discovery.
41 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080042 * Since device_pm_add() may be called with a device lock held,
43 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010044 * dpm_list_mutex.
45 */
46
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020047LIST_HEAD(dpm_list);
Sachin Kamat7664e962012-07-17 22:38:08 +020048static LIST_HEAD(dpm_prepared_list);
49static LIST_HEAD(dpm_suspended_list);
50static LIST_HEAD(dpm_late_early_list);
51static LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
ShuoX Liu2a77c462011-08-10 23:01:26 +020053struct suspend_stats suspend_stats;
Alan Sterncd59abf2007-09-21 15:36:56 -040054static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010055static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020057static int async_error;
58
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020059/**
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020060 * device_pm_sleep_init - Initialize system suspend-related device fields.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020061 * @dev: Device object being initialized.
62 */
Rafael J. Wysockie91c11b2012-08-06 01:44:28 +020063void device_pm_sleep_init(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020064{
Alan Sternf76b168b2011-06-18 20:22:23 +020065 dev->power.is_prepared = false;
Alan Stern6d0e0e82011-06-18 22:42:09 +020066 dev->power.is_suspended = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010067 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020068 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020069 dev->power.wakeup = NULL;
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +020070 INIT_LIST_HEAD(&dev->power.entry);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020071}
72
73/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020074 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020075 */
76void device_pm_lock(void)
77{
78 mutex_lock(&dpm_list_mtx);
79}
80
81/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020082 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020083 */
84void device_pm_unlock(void)
85{
86 mutex_unlock(&dpm_list_mtx);
87}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010088
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010089/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020090 * device_pm_add - Add a device to the PM core's list of active devices.
91 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010092 */
Alan Stern3b98aea2008-08-07 13:06:12 -040093void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +010096 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070097 mutex_lock(&dpm_list_mtx);
Alan Sternf76b168b2011-06-18 20:22:23 +020098 if (dev->parent && dev->parent->power.is_prepared)
Rafael J. Wysockib64959e2010-12-16 17:11:45 +010099 dev_warn(dev, "parent %s should not be sleeping\n",
100 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -0400101 list_add_tail(&dev->power.entry, &dpm_list);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200102 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100105/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200106 * device_pm_remove - Remove a device from the PM core's list of active devices.
107 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100108 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200109void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100112 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100113 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700114 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700116 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200117 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200118 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100119}
120
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200121/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200122 * device_pm_move_before - Move device in the PM core's list of active devices.
123 * @deva: Device to move in dpm_list.
124 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100125 */
126void device_pm_move_before(struct device *deva, struct device *devb)
127{
128 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100129 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
130 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100131 /* Delete deva from dpm_list and reinsert before devb. */
132 list_move_tail(&deva->power.entry, &devb->power.entry);
133}
134
135/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200136 * device_pm_move_after - Move device in the PM core's list of active devices.
137 * @deva: Device to move in dpm_list.
138 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100139 */
140void device_pm_move_after(struct device *deva, struct device *devb)
141{
142 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100143 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
144 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100145 /* Delete deva from dpm_list and reinsert after devb. */
146 list_move(&deva->power.entry, &devb->power.entry);
147}
148
149/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200150 * device_pm_move_last - Move device to end of the PM core's list of devices.
151 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100152 */
153void device_pm_move_last(struct device *dev)
154{
155 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100156 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100157 list_move_tail(&dev->power.entry, &dpm_list);
158}
159
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100160static ktime_t initcall_debug_start(struct device *dev)
161{
162 ktime_t calltime = ktime_set(0, 0);
163
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200164 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100165 pr_info("calling %s+ @ %i, parent: %s\n",
166 dev_name(dev), task_pid_nr(current),
167 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100168 calltime = ktime_get();
169 }
170
171 return calltime;
172}
173
174static void initcall_debug_report(struct device *dev, ktime_t calltime,
175 int error)
176{
177 ktime_t delta, rettime;
178
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200179 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100180 rettime = ktime_get();
181 delta = ktime_sub(rettime, calltime);
182 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
183 error, (unsigned long long)ktime_to_ns(delta) >> 10);
184 }
185}
186
Cornelia Huckffa6a702009-03-04 12:44:00 +0100187/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100188 * dpm_wait - Wait for a PM operation to complete.
189 * @dev: Device to wait for.
190 * @async: If unset, wait only if the device's power.async_suspend flag is set.
191 */
192static void dpm_wait(struct device *dev, bool async)
193{
194 if (!dev)
195 return;
196
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100197 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100198 wait_for_completion(&dev->power.completion);
199}
200
201static int dpm_wait_fn(struct device *dev, void *async_ptr)
202{
203 dpm_wait(dev, *((bool *)async_ptr));
204 return 0;
205}
206
207static void dpm_wait_for_children(struct device *dev, bool async)
208{
209 device_for_each_child(dev, &async, dpm_wait_fn);
210}
211
212/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100213 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200214 * @ops: PM operations to choose from.
215 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200216 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100217static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200218{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200219 switch (state.event) {
220#ifdef CONFIG_SUSPEND
221 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100222 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200223 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100224 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200225#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200226#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200227 case PM_EVENT_FREEZE:
228 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100229 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200230 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100231 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200232 case PM_EVENT_THAW:
233 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100234 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200235 break;
236 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100237 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200238#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200239 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100240
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100241 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200242}
243
244/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100245 * pm_late_early_op - Return the PM operation appropriate for given PM event.
246 * @ops: PM operations to choose from.
247 * @state: PM transition of the system being carried out.
248 *
249 * Runtime PM is disabled for @dev while this function is being executed.
250 */
251static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
252 pm_message_t state)
253{
254 switch (state.event) {
255#ifdef CONFIG_SUSPEND
256 case PM_EVENT_SUSPEND:
257 return ops->suspend_late;
258 case PM_EVENT_RESUME:
259 return ops->resume_early;
260#endif /* CONFIG_SUSPEND */
261#ifdef CONFIG_HIBERNATE_CALLBACKS
262 case PM_EVENT_FREEZE:
263 case PM_EVENT_QUIESCE:
264 return ops->freeze_late;
265 case PM_EVENT_HIBERNATE:
266 return ops->poweroff_late;
267 case PM_EVENT_THAW:
268 case PM_EVENT_RECOVER:
269 return ops->thaw_early;
270 case PM_EVENT_RESTORE:
271 return ops->restore_early;
272#endif /* CONFIG_HIBERNATE_CALLBACKS */
273 }
274
275 return NULL;
276}
277
278/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100279 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200280 * @ops: PM operations to choose from.
281 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200282 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200283 * The driver of @dev will not receive interrupts while this function is being
284 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200285 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100286static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200287{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200288 switch (state.event) {
289#ifdef CONFIG_SUSPEND
290 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100291 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200292 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100293 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200294#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200295#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200296 case PM_EVENT_FREEZE:
297 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100298 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200299 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100300 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200301 case PM_EVENT_THAW:
302 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100303 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200304 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100305 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200306#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200307 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100308
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100309 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200310}
311
312static char *pm_verb(int event)
313{
314 switch (event) {
315 case PM_EVENT_SUSPEND:
316 return "suspend";
317 case PM_EVENT_RESUME:
318 return "resume";
319 case PM_EVENT_FREEZE:
320 return "freeze";
321 case PM_EVENT_QUIESCE:
322 return "quiesce";
323 case PM_EVENT_HIBERNATE:
324 return "hibernate";
325 case PM_EVENT_THAW:
326 return "thaw";
327 case PM_EVENT_RESTORE:
328 return "restore";
329 case PM_EVENT_RECOVER:
330 return "recover";
331 default:
332 return "(unknown PM event)";
333 }
334}
335
336static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
337{
338 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
339 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
340 ", may wakeup" : "");
341}
342
343static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
344 int error)
345{
346 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100347 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200348}
349
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100350static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
351{
352 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200353 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100354 int usecs;
355
356 calltime = ktime_get();
357 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
358 do_div(usecs64, NSEC_PER_USEC);
359 usecs = usecs64;
360 if (usecs == 0)
361 usecs = 1;
362 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
363 info ?: "", info ? " " : "", pm_verb(state.event),
364 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
365}
366
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100367static int dpm_run_callback(pm_callback_t cb, struct device *dev,
368 pm_message_t state, char *info)
369{
370 ktime_t calltime;
371 int error;
372
373 if (!cb)
374 return 0;
375
376 calltime = initcall_debug_start(dev);
377
378 pm_dev_dbg(dev, state, info);
379 error = cb(dev);
380 suspend_report_result(cb, error);
381
382 initcall_debug_report(dev, calltime, error);
383
384 return error;
385}
386
Alan Sterncd59abf2007-09-21 15:36:56 -0400387/*------------------------- Resume routines -------------------------*/
388
389/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200390 * device_resume_noirq - Execute an "early resume" callback for given device.
391 * @dev: Device to handle.
392 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400393 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200394 * The driver of @dev will not receive interrupts while this function is being
395 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400396 */
Alan Sternd1616302009-05-24 22:05:42 +0200397static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400398{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100399 pm_callback_t callback = NULL;
400 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400401 int error = 0;
402
403 TRACE_DEVICE(dev);
404 TRACE_RESUME(0);
405
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200406 if (dev->power.syscore)
407 goto Out;
408
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200409 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100410 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100411 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200412 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100413 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100414 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100415 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100416 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100417 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100418 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100419 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100420 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100421 }
422
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100423 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100424 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100425 callback = pm_noirq_op(dev->driver->pm, state);
426 }
427
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100428 error = dpm_run_callback(callback, dev, state, info);
429
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200430 Out:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100431 TRACE_RESUME(error);
432 return error;
433}
434
435/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100436 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200437 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100438 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100439 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200440 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100441 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100442static void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100443{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100444 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100445
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200446 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100447 while (!list_empty(&dpm_noirq_list)) {
448 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100449 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100450
451 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100452 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100453 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100455 error = device_resume_noirq(dev, state);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200456 if (error) {
457 suspend_stats.failed_resume_noirq++;
458 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
459 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100460 pm_dev_err(dev, state, " noirq", error);
461 }
462
463 mutex_lock(&dpm_list_mtx);
464 put_device(dev);
465 }
466 mutex_unlock(&dpm_list_mtx);
467 dpm_show_time(starttime, state, "noirq");
468 resume_device_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200469 cpuidle_resume();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100470}
471
472/**
473 * device_resume_early - Execute an "early resume" callback for given device.
474 * @dev: Device to handle.
475 * @state: PM transition of the system being carried out.
476 *
477 * Runtime PM is disabled for @dev while this function is being executed.
478 */
479static int device_resume_early(struct device *dev, pm_message_t state)
480{
481 pm_callback_t callback = NULL;
482 char *info = NULL;
483 int error = 0;
484
485 TRACE_DEVICE(dev);
486 TRACE_RESUME(0);
487
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200488 if (dev->power.syscore)
489 goto Out;
490
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100491 if (dev->pm_domain) {
492 info = "early power domain ";
493 callback = pm_late_early_op(&dev->pm_domain->ops, state);
494 } else if (dev->type && dev->type->pm) {
495 info = "early type ";
496 callback = pm_late_early_op(dev->type->pm, state);
497 } else if (dev->class && dev->class->pm) {
498 info = "early class ";
499 callback = pm_late_early_op(dev->class->pm, state);
500 } else if (dev->bus && dev->bus->pm) {
501 info = "early bus ";
502 callback = pm_late_early_op(dev->bus->pm, state);
503 }
504
505 if (!callback && dev->driver && dev->driver->pm) {
506 info = "early driver ";
507 callback = pm_late_early_op(dev->driver->pm, state);
508 }
509
510 error = dpm_run_callback(callback, dev, state, info);
511
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200512 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100513 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100514
515 pm_runtime_enable(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100516 return error;
517}
518
519/**
520 * dpm_resume_early - Execute "early resume" callbacks for all devices.
521 * @state: PM transition of the system being carried out.
522 */
523static void dpm_resume_early(pm_message_t state)
524{
525 ktime_t starttime = ktime_get();
526
527 mutex_lock(&dpm_list_mtx);
528 while (!list_empty(&dpm_late_early_list)) {
529 struct device *dev = to_device(dpm_late_early_list.next);
530 int error;
531
532 get_device(dev);
533 list_move_tail(&dev->power.entry, &dpm_suspended_list);
534 mutex_unlock(&dpm_list_mtx);
535
536 error = device_resume_early(dev, state);
537 if (error) {
538 suspend_stats.failed_resume_early++;
539 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
540 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100541 pm_dev_err(dev, state, " early", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200542 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100543
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100544 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100545 put_device(dev);
546 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200547 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100548 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100549}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100550
551/**
552 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
553 * @state: PM transition of the system being carried out.
554 */
555void dpm_resume_start(pm_message_t state)
556{
557 dpm_resume_noirq(state);
558 dpm_resume_early(state);
559}
560EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100561
562/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100563 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200564 * @dev: Device to handle.
565 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100566 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100567 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100568static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100569{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100570 pm_callback_t callback = NULL;
571 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100572 int error = 0;
573
574 TRACE_DEVICE(dev);
575 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400576
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200577 if (dev->power.syscore)
578 goto Complete;
579
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100580 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800581 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100582
Alan Sternf76b168b2011-06-18 20:22:23 +0200583 /*
584 * This is a fib. But we'll allow new children to be added below
585 * a resumed device, even if the device hasn't been completed yet.
586 */
587 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100588
Alan Stern6d0e0e82011-06-18 22:42:09 +0200589 if (!dev->power.is_suspended)
590 goto Unlock;
591
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200592 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100593 info = "power domain ";
594 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100595 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100596 }
597
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100598 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100599 info = "type ";
600 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100601 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100602 }
603
604 if (dev->class) {
605 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100606 info = "class ";
607 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100608 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100609 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100610 info = "legacy class ";
611 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100612 goto End;
613 }
614 }
615
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200616 if (dev->bus) {
617 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100618 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100619 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200620 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100621 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100622 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100623 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200624 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400625 }
626
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100627 Driver:
628 if (!callback && dev->driver && dev->driver->pm) {
629 info = "driver ";
630 callback = pm_op(dev->driver->pm, state);
631 }
632
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200633 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100634 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200635 dev->power.is_suspended = false;
636
637 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800638 device_unlock(dev);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200639
640 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100641 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100642
Alan Sterncd59abf2007-09-21 15:36:56 -0400643 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200644
Alan Sterncd59abf2007-09-21 15:36:56 -0400645 return error;
646}
647
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100648static void async_resume(void *data, async_cookie_t cookie)
649{
650 struct device *dev = (struct device *)data;
651 int error;
652
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100653 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100654 if (error)
655 pm_dev_err(dev, pm_transition, " async", error);
656 put_device(dev);
657}
658
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100659static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100660{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100661 return dev->power.async_suspend && pm_async_enabled
662 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100663}
664
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100665/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200666 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
667 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100668 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200669 * Execute the appropriate "resume" callback for all devices whose status
670 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400671 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200672void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400673{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100674 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100675 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400676
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200677 might_sleep();
678
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200679 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100680 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200681 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200682
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100683 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100684 INIT_COMPLETION(dev->power.completion);
685 if (is_async(dev)) {
686 get_device(dev);
687 async_schedule(async_resume, dev);
688 }
689 }
690
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100691 while (!list_empty(&dpm_suspended_list)) {
692 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200693 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100694 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200695 int error;
696
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200697 mutex_unlock(&dpm_list_mtx);
698
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100699 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200700 if (error) {
701 suspend_stats.failed_resume++;
702 dpm_save_failed_step(SUSPEND_RESUME);
703 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200704 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200705 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100706
707 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200708 }
709 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100710 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200711 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400712 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200713 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100714 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100715 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200716}
717
718/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200719 * device_complete - Complete a PM transition for given device.
720 * @dev: Device to handle.
721 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200722 */
Alan Sternd1616302009-05-24 22:05:42 +0200723static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200724{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100725 void (*callback)(struct device *) = NULL;
726 char *info = NULL;
727
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200728 if (dev->power.syscore)
729 return;
730
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800731 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200732
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200733 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100734 info = "completing power domain ";
735 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200736 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100737 info = "completing type ";
738 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100739 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100740 info = "completing class ";
741 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100742 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100743 info = "completing bus ";
744 callback = dev->bus->pm->complete;
745 }
746
747 if (!callback && dev->driver && dev->driver->pm) {
748 info = "completing driver ";
749 callback = dev->driver->pm->complete;
750 }
751
752 if (callback) {
753 pm_dev_dbg(dev, state, info);
754 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200755 }
756
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800757 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200758
759 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200760}
761
762/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200763 * dpm_complete - Complete a PM transition for all non-sysdev devices.
764 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200765 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200766 * Execute the ->complete() callbacks for all devices whose PM status is not
767 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200768 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200769void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200770{
771 struct list_head list;
772
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200773 might_sleep();
774
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200775 INIT_LIST_HEAD(&list);
776 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100777 while (!list_empty(&dpm_prepared_list)) {
778 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200779
780 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200781 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100782 list_move(&dev->power.entry, &list);
783 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200784
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100785 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200786
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100787 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200788 put_device(dev);
789 }
790 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400791 mutex_unlock(&dpm_list_mtx);
792}
793
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100794/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200795 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
796 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400797 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200798 * Execute "resume" callbacks for all devices and complete the PM transition of
799 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400800 */
Alan Sternd1616302009-05-24 22:05:42 +0200801void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400802{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200803 dpm_resume(state);
804 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400805}
Alan Sternd1616302009-05-24 22:05:42 +0200806EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400807
808
Alan Sterncd59abf2007-09-21 15:36:56 -0400809/*------------------------- Suspend routines -------------------------*/
810
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200811/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200812 * resume_event - Return a "resume" message for given "suspend" sleep state.
813 * @sleep_state: PM message representing a sleep state.
814 *
815 * Return a PM message representing the resume event corresponding to given
816 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200817 */
818static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400819{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200820 switch (sleep_state.event) {
821 case PM_EVENT_SUSPEND:
822 return PMSG_RESUME;
823 case PM_EVENT_FREEZE:
824 case PM_EVENT_QUIESCE:
825 return PMSG_RECOVER;
826 case PM_EVENT_HIBERNATE:
827 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400828 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200829 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400830}
831
832/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200833 * device_suspend_noirq - Execute a "late suspend" callback for given device.
834 * @dev: Device to handle.
835 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100836 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200837 * The driver of @dev will not receive interrupts while this function is being
838 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100839 */
Alan Sternd1616302009-05-24 22:05:42 +0200840static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100841{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100842 pm_callback_t callback = NULL;
843 char *info = NULL;
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100844
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200845 if (dev->power.syscore)
846 return 0;
847
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200848 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100849 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100850 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200851 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100852 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100853 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100854 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100855 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100856 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100857 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100858 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100859 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100860 }
861
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100862 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100863 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100864 callback = pm_noirq_op(dev->driver->pm, state);
865 }
866
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100867 return dpm_run_callback(callback, dev, state, info);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100868}
869
870/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100871 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200872 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100873 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200874 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
875 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100876 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100877static int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100878{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100879 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100880 int error = 0;
881
Preeti U Murthy8651f972012-07-09 10:12:56 +0200882 cpuidle_pause();
Rafael J. Wysocki2ed8d2b32009-03-16 22:34:06 +0100883 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200884 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100885 while (!list_empty(&dpm_late_early_list)) {
886 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100887
888 get_device(dev);
889 mutex_unlock(&dpm_list_mtx);
890
Alan Sternd1616302009-05-24 22:05:42 +0200891 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100892
893 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100894 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100895 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200896 suspend_stats.failed_suspend_noirq++;
897 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
898 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100899 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100900 break;
901 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100902 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100903 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100904 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200905
906 if (pm_wakeup_pending()) {
907 error = -EBUSY;
908 break;
909 }
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100910 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200911 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100912 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200913 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100914 else
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100915 dpm_show_time(starttime, state, "noirq");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100916 return error;
917}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100918
919/**
920 * device_suspend_late - Execute a "late suspend" callback for given device.
921 * @dev: Device to handle.
922 * @state: PM transition of the system being carried out.
923 *
924 * Runtime PM is disabled for @dev while this function is being executed.
925 */
926static int device_suspend_late(struct device *dev, pm_message_t state)
927{
928 pm_callback_t callback = NULL;
929 char *info = NULL;
930
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100931 __pm_runtime_disable(dev, false);
932
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200933 if (dev->power.syscore)
934 return 0;
935
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100936 if (dev->pm_domain) {
937 info = "late power domain ";
938 callback = pm_late_early_op(&dev->pm_domain->ops, state);
939 } else if (dev->type && dev->type->pm) {
940 info = "late type ";
941 callback = pm_late_early_op(dev->type->pm, state);
942 } else if (dev->class && dev->class->pm) {
943 info = "late class ";
944 callback = pm_late_early_op(dev->class->pm, state);
945 } else if (dev->bus && dev->bus->pm) {
946 info = "late bus ";
947 callback = pm_late_early_op(dev->bus->pm, state);
948 }
949
950 if (!callback && dev->driver && dev->driver->pm) {
951 info = "late driver ";
952 callback = pm_late_early_op(dev->driver->pm, state);
953 }
954
955 return dpm_run_callback(callback, dev, state, info);
956}
957
958/**
959 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
960 * @state: PM transition of the system being carried out.
961 */
962static int dpm_suspend_late(pm_message_t state)
963{
964 ktime_t starttime = ktime_get();
965 int error = 0;
966
967 mutex_lock(&dpm_list_mtx);
968 while (!list_empty(&dpm_suspended_list)) {
969 struct device *dev = to_device(dpm_suspended_list.prev);
970
971 get_device(dev);
972 mutex_unlock(&dpm_list_mtx);
973
974 error = device_suspend_late(dev, state);
975
976 mutex_lock(&dpm_list_mtx);
977 if (error) {
978 pm_dev_err(dev, state, " late", error);
979 suspend_stats.failed_suspend_late++;
980 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
981 dpm_save_failed_dev(dev_name(dev));
982 put_device(dev);
983 break;
984 }
985 if (!list_empty(&dev->power.entry))
986 list_move(&dev->power.entry, &dpm_late_early_list);
987 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200988
989 if (pm_wakeup_pending()) {
990 error = -EBUSY;
991 break;
992 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100993 }
994 mutex_unlock(&dpm_list_mtx);
995 if (error)
996 dpm_resume_early(resume_event(state));
997 else
998 dpm_show_time(starttime, state, "late");
999
1000 return error;
1001}
1002
1003/**
1004 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1005 * @state: PM transition of the system being carried out.
1006 */
1007int dpm_suspend_end(pm_message_t state)
1008{
1009 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001010 if (error)
1011 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001012
Colin Cross064b0212012-07-19 10:38:06 +02001013 error = dpm_suspend_noirq(state);
1014 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001015 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001016 return error;
1017 }
1018
1019 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001020}
1021EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001022
1023/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001024 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001025 * @dev: Device to suspend.
1026 * @state: PM transition of the system being carried out.
1027 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001028 */
1029static int legacy_suspend(struct device *dev, pm_message_t state,
1030 int (*cb)(struct device *dev, pm_message_t state))
1031{
1032 int error;
1033 ktime_t calltime;
1034
1035 calltime = initcall_debug_start(dev);
1036
1037 error = cb(dev, state);
1038 suspend_report_result(cb, error);
1039
1040 initcall_debug_report(dev, calltime, error);
1041
1042 return error;
1043}
1044
1045/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001046 * device_suspend - Execute "suspend" callbacks for given device.
1047 * @dev: Device to handle.
1048 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001049 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001050 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001051static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001052{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001053 pm_callback_t callback = NULL;
1054 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001055 int error = 0;
1056
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001057 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001058
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001059 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001060 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001061
Alan Stern88d26132012-09-19 21:59:02 +02001062 /*
1063 * If a device configured to wake up the system from sleep states
1064 * has been suspended at run time and there's a resume request pending
1065 * for it, this is equivalent to the device signaling wakeup, so the
1066 * system suspend operation should be aborted.
1067 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001068 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1069 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001070
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001071 if (pm_wakeup_pending()) {
1072 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001073 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001074 }
1075
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001076 if (dev->power.syscore)
1077 goto Complete;
1078
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001079 device_lock(dev);
1080
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001081 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001082 info = "power domain ";
1083 callback = pm_op(&dev->pm_domain->ops, state);
1084 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001085 }
1086
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001087 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001088 info = "type ";
1089 callback = pm_op(dev->type->pm, state);
1090 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001091 }
1092
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001093 if (dev->class) {
1094 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001095 info = "class ";
1096 callback = pm_op(dev->class->pm, state);
1097 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001098 } else if (dev->class->suspend) {
1099 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001100 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001101 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001102 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001103 }
1104
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001105 if (dev->bus) {
1106 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001107 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001108 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001109 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001110 pm_dev_dbg(dev, state, "legacy bus ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001111 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001112 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001113 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001114 }
1115
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001116 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001117 if (!callback && dev->driver && dev->driver->pm) {
1118 info = "driver ";
1119 callback = pm_op(dev->driver->pm, state);
1120 }
1121
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001122 error = dpm_run_callback(callback, dev, state, info);
1123
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001124 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001125 if (!error) {
1126 dev->power.is_suspended = true;
Rafael J. Wysocki8b258cc2011-11-17 21:39:33 +01001127 if (dev->power.wakeup_path
1128 && dev->parent && !dev->parent->power.ignore_children)
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001129 dev->parent->power.wakeup_path = true;
1130 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001131
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001132 device_unlock(dev);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001133
1134 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001135 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001136 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001137 async_error = error;
1138
Alan Sterncd59abf2007-09-21 15:36:56 -04001139 return error;
1140}
1141
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001142static void async_suspend(void *data, async_cookie_t cookie)
1143{
1144 struct device *dev = (struct device *)data;
1145 int error;
1146
1147 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001148 if (error) {
1149 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001150 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001151 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001152
1153 put_device(dev);
1154}
1155
1156static int device_suspend(struct device *dev)
1157{
1158 INIT_COMPLETION(dev->power.completion);
1159
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +01001160 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001161 get_device(dev);
1162 async_schedule(async_suspend, dev);
1163 return 0;
1164 }
1165
1166 return __device_suspend(dev, pm_transition, false);
1167}
1168
Alan Sterncd59abf2007-09-21 15:36:56 -04001169/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001170 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1171 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001172 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001173int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001174{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001175 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001176 int error = 0;
1177
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001178 might_sleep();
1179
Alan Sterncd59abf2007-09-21 15:36:56 -04001180 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001181 pm_transition = state;
1182 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001183 while (!list_empty(&dpm_prepared_list)) {
1184 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001185
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001186 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001187 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001188
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001189 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001190
Alan Stern1b3cbec2008-02-29 11:50:22 -05001191 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001192 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001193 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001194 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001195 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001196 break;
1197 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001198 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001199 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001200 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001201 if (async_error)
1202 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001203 }
1204 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001205 async_synchronize_full();
1206 if (!error)
1207 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001208 if (error) {
1209 suspend_stats.failed_suspend++;
1210 dpm_save_failed_step(SUSPEND_SUSPEND);
1211 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001212 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001213 return error;
1214}
1215
1216/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001217 * device_prepare - Prepare a device for system power transition.
1218 * @dev: Device to handle.
1219 * @state: PM transition of the system being carried out.
1220 *
1221 * Execute the ->prepare() callback(s) for given device. No new children of the
1222 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001223 */
Alan Sternd1616302009-05-24 22:05:42 +02001224static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001225{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001226 int (*callback)(struct device *) = NULL;
1227 char *info = NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001228 int error = 0;
1229
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001230 if (dev->power.syscore)
1231 return 0;
1232
Alan Stern88d26132012-09-19 21:59:02 +02001233 /*
1234 * If a device's parent goes into runtime suspend at the wrong time,
1235 * it won't be possible to resume the device. To prevent this we
1236 * block runtime suspend here, during the prepare phase, and allow
1237 * it again during the complete phase.
1238 */
1239 pm_runtime_get_noresume(dev);
1240
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001241 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001242
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001243 dev->power.wakeup_path = device_may_wakeup(dev);
1244
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001245 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001246 info = "preparing power domain ";
1247 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001248 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001249 info = "preparing type ";
1250 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001251 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001252 info = "preparing class ";
1253 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001254 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001255 info = "preparing bus ";
1256 callback = dev->bus->pm->prepare;
1257 }
1258
1259 if (!callback && dev->driver && dev->driver->pm) {
1260 info = "preparing driver ";
1261 callback = dev->driver->pm->prepare;
1262 }
1263
1264 if (callback) {
1265 error = callback(dev);
1266 suspend_report_result(callback, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001267 }
1268
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001269 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001270
1271 return error;
1272}
1273
1274/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001275 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1276 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001277 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001278 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001279 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001280int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001281{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001282 int error = 0;
1283
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001284 might_sleep();
1285
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001286 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001287 while (!list_empty(&dpm_list)) {
1288 struct device *dev = to_device(dpm_list.next);
1289
1290 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001291 mutex_unlock(&dpm_list_mtx);
1292
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001293 error = device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001294
1295 mutex_lock(&dpm_list_mtx);
1296 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001297 if (error == -EAGAIN) {
1298 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001299 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001300 continue;
1301 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001302 printk(KERN_INFO "PM: Device %s not prepared "
1303 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001304 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001305 put_device(dev);
1306 break;
1307 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001308 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001309 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001310 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001311 put_device(dev);
1312 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001313 mutex_unlock(&dpm_list_mtx);
1314 return error;
1315}
1316
1317/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001318 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1319 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001320 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001321 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1322 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001323 */
Alan Sternd1616302009-05-24 22:05:42 +02001324int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001325{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001326 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001327
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001328 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001329 if (error) {
1330 suspend_stats.failed_prepare++;
1331 dpm_save_failed_step(SUSPEND_PREPARE);
1332 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001333 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001334 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001335}
Alan Sternd1616302009-05-24 22:05:42 +02001336EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001337
1338void __suspend_report_result(const char *function, void *fn, int ret)
1339{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001340 if (ret)
1341 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001342}
1343EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001344
1345/**
1346 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1347 * @dev: Device to wait for.
1348 * @subordinate: Device that needs to wait for @dev.
1349 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001350int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001351{
1352 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001353 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001354}
1355EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001356
1357/**
1358 * dpm_for_each_dev - device iterator.
1359 * @data: data for the callback.
1360 * @fn: function to be called for each device.
1361 *
1362 * Iterate over devices in dpm_list, and call @fn for each device,
1363 * passing it @data.
1364 */
1365void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1366{
1367 struct device *dev;
1368
1369 if (!fn)
1370 return;
1371
1372 device_pm_lock();
1373 list_for_each_entry(dev, &dpm_list, power.entry)
1374 fn(dev, data);
1375 device_pm_unlock();
1376}
1377EXPORT_SYMBOL_GPL(dpm_for_each_dev);