blob: 2b7f77d3fcb0d518d2a1f4e041f29b61d6e4521b [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. Wysocki2ed8d2b2009-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);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200102 dev_pm_qos_constraints_init(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200103 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100106/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200107 * device_pm_remove - Remove a device from the PM core's list of active devices.
108 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100109 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200110void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100113 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100114 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700115 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200116 dev_pm_qos_constraints_destroy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700118 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200119 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200120 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100121}
122
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200123/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200124 * device_pm_move_before - Move device in the PM core's list of active devices.
125 * @deva: Device to move in dpm_list.
126 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100127 */
128void device_pm_move_before(struct device *deva, struct device *devb)
129{
130 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100131 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
132 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100133 /* Delete deva from dpm_list and reinsert before devb. */
134 list_move_tail(&deva->power.entry, &devb->power.entry);
135}
136
137/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200138 * device_pm_move_after - Move device in the PM core's list of active devices.
139 * @deva: Device to move in dpm_list.
140 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100141 */
142void device_pm_move_after(struct device *deva, struct device *devb)
143{
144 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100145 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
146 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100147 /* Delete deva from dpm_list and reinsert after devb. */
148 list_move(&deva->power.entry, &devb->power.entry);
149}
150
151/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200152 * device_pm_move_last - Move device to end of the PM core's list of devices.
153 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100154 */
155void device_pm_move_last(struct device *dev)
156{
157 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100158 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100159 list_move_tail(&dev->power.entry, &dpm_list);
160}
161
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100162static ktime_t initcall_debug_start(struct device *dev)
163{
164 ktime_t calltime = ktime_set(0, 0);
165
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200166 if (pm_print_times_enabled) {
Rafael J. Wysocki0c6aebe2011-12-03 00:23:43 +0100167 pr_info("calling %s+ @ %i, parent: %s\n",
168 dev_name(dev), task_pid_nr(current),
169 dev->parent ? dev_name(dev->parent) : "none");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100170 calltime = ktime_get();
171 }
172
173 return calltime;
174}
175
176static void initcall_debug_report(struct device *dev, ktime_t calltime,
177 int error)
178{
179 ktime_t delta, rettime;
180
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200181 if (pm_print_times_enabled) {
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100182 rettime = ktime_get();
183 delta = ktime_sub(rettime, calltime);
184 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
185 error, (unsigned long long)ktime_to_ns(delta) >> 10);
186 }
187}
188
Cornelia Huckffa6a702009-03-04 12:44:00 +0100189/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100190 * dpm_wait - Wait for a PM operation to complete.
191 * @dev: Device to wait for.
192 * @async: If unset, wait only if the device's power.async_suspend flag is set.
193 */
194static void dpm_wait(struct device *dev, bool async)
195{
196 if (!dev)
197 return;
198
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100199 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100200 wait_for_completion(&dev->power.completion);
201}
202
203static int dpm_wait_fn(struct device *dev, void *async_ptr)
204{
205 dpm_wait(dev, *((bool *)async_ptr));
206 return 0;
207}
208
209static void dpm_wait_for_children(struct device *dev, bool async)
210{
211 device_for_each_child(dev, &async, dpm_wait_fn);
212}
213
214/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100215 * pm_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200216 * @ops: PM operations to choose from.
217 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200218 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100219static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200220{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200221 switch (state.event) {
222#ifdef CONFIG_SUSPEND
223 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100224 return ops->suspend;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200225 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100226 return ops->resume;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200227#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200228#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200229 case PM_EVENT_FREEZE:
230 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100231 return ops->freeze;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200232 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100233 return ops->poweroff;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200234 case PM_EVENT_THAW:
235 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100236 return ops->thaw;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200237 break;
238 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100239 return ops->restore;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200240#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200241 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100242
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100243 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200244}
245
246/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100247 * pm_late_early_op - Return the PM operation appropriate for given PM event.
248 * @ops: PM operations to choose from.
249 * @state: PM transition of the system being carried out.
250 *
251 * Runtime PM is disabled for @dev while this function is being executed.
252 */
253static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
254 pm_message_t state)
255{
256 switch (state.event) {
257#ifdef CONFIG_SUSPEND
258 case PM_EVENT_SUSPEND:
259 return ops->suspend_late;
260 case PM_EVENT_RESUME:
261 return ops->resume_early;
262#endif /* CONFIG_SUSPEND */
263#ifdef CONFIG_HIBERNATE_CALLBACKS
264 case PM_EVENT_FREEZE:
265 case PM_EVENT_QUIESCE:
266 return ops->freeze_late;
267 case PM_EVENT_HIBERNATE:
268 return ops->poweroff_late;
269 case PM_EVENT_THAW:
270 case PM_EVENT_RECOVER:
271 return ops->thaw_early;
272 case PM_EVENT_RESTORE:
273 return ops->restore_early;
274#endif /* CONFIG_HIBERNATE_CALLBACKS */
275 }
276
277 return NULL;
278}
279
280/**
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100281 * pm_noirq_op - Return the PM operation appropriate for given PM event.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200282 * @ops: PM operations to choose from.
283 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200284 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200285 * The driver of @dev will not receive interrupts while this function is being
286 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200287 */
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100288static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200289{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200290 switch (state.event) {
291#ifdef CONFIG_SUSPEND
292 case PM_EVENT_SUSPEND:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100293 return ops->suspend_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200294 case PM_EVENT_RESUME:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100295 return ops->resume_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200296#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200297#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200298 case PM_EVENT_FREEZE:
299 case PM_EVENT_QUIESCE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100300 return ops->freeze_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200301 case PM_EVENT_HIBERNATE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100302 return ops->poweroff_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200303 case PM_EVENT_THAW:
304 case PM_EVENT_RECOVER:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100305 return ops->thaw_noirq;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200306 case PM_EVENT_RESTORE:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100307 return ops->restore_noirq;
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200308#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100310
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100311 return NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200312}
313
314static char *pm_verb(int event)
315{
316 switch (event) {
317 case PM_EVENT_SUSPEND:
318 return "suspend";
319 case PM_EVENT_RESUME:
320 return "resume";
321 case PM_EVENT_FREEZE:
322 return "freeze";
323 case PM_EVENT_QUIESCE:
324 return "quiesce";
325 case PM_EVENT_HIBERNATE:
326 return "hibernate";
327 case PM_EVENT_THAW:
328 return "thaw";
329 case PM_EVENT_RESTORE:
330 return "restore";
331 case PM_EVENT_RECOVER:
332 return "recover";
333 default:
334 return "(unknown PM event)";
335 }
336}
337
338static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
339{
340 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
341 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
342 ", may wakeup" : "");
343}
344
345static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
346 int error)
347{
348 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100349 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200350}
351
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100352static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
353{
354 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200355 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100356 int usecs;
357
358 calltime = ktime_get();
359 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
360 do_div(usecs64, NSEC_PER_USEC);
361 usecs = usecs64;
362 if (usecs == 0)
363 usecs = 1;
364 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
365 info ?: "", info ? " " : "", pm_verb(state.event),
366 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
367}
368
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100369static int dpm_run_callback(pm_callback_t cb, struct device *dev,
370 pm_message_t state, char *info)
371{
372 ktime_t calltime;
373 int error;
374
375 if (!cb)
376 return 0;
377
378 calltime = initcall_debug_start(dev);
379
380 pm_dev_dbg(dev, state, info);
381 error = cb(dev);
382 suspend_report_result(cb, error);
383
384 initcall_debug_report(dev, calltime, error);
385
386 return error;
387}
388
Alan Sterncd59abf2007-09-21 15:36:56 -0400389/*------------------------- Resume routines -------------------------*/
390
391/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200392 * device_resume_noirq - Execute an "early resume" callback for given device.
393 * @dev: Device to handle.
394 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400395 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200396 * The driver of @dev will not receive interrupts while this function is being
397 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400398 */
Alan Sternd1616302009-05-24 22:05:42 +0200399static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400400{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100401 pm_callback_t callback = NULL;
402 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -0400403 int error = 0;
404
405 TRACE_DEVICE(dev);
406 TRACE_RESUME(0);
407
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200408 if (dev->power.syscore)
409 goto Out;
410
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200411 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100412 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100413 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200414 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100415 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100416 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100417 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100418 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100419 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100420 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100421 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100422 callback = pm_noirq_op(dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100423 }
424
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100425 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100426 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100427 callback = pm_noirq_op(dev->driver->pm, state);
428 }
429
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100430 error = dpm_run_callback(callback, dev, state, info);
431
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200432 Out:
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100433 TRACE_RESUME(error);
434 return error;
435}
436
437/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100438 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200439 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100440 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100441 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200442 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100443 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100444static void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100445{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100446 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100447
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200448 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100449 while (!list_empty(&dpm_noirq_list)) {
450 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100451 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100452
453 get_device(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100454 list_move_tail(&dev->power.entry, &dpm_late_early_list);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100455 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100456
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100457 error = device_resume_noirq(dev, state);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200458 if (error) {
459 suspend_stats.failed_resume_noirq++;
460 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
461 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100462 pm_dev_err(dev, state, " noirq", error);
463 }
464
465 mutex_lock(&dpm_list_mtx);
466 put_device(dev);
467 }
468 mutex_unlock(&dpm_list_mtx);
469 dpm_show_time(starttime, state, "noirq");
470 resume_device_irqs();
Preeti U Murthy8651f972012-07-09 10:12:56 +0200471 cpuidle_resume();
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100472}
473
474/**
475 * device_resume_early - Execute an "early resume" callback for given device.
476 * @dev: Device to handle.
477 * @state: PM transition of the system being carried out.
478 *
479 * Runtime PM is disabled for @dev while this function is being executed.
480 */
481static int device_resume_early(struct device *dev, pm_message_t state)
482{
483 pm_callback_t callback = NULL;
484 char *info = NULL;
485 int error = 0;
486
487 TRACE_DEVICE(dev);
488 TRACE_RESUME(0);
489
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200490 if (dev->power.syscore)
491 goto Out;
492
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100493 if (dev->pm_domain) {
494 info = "early power domain ";
495 callback = pm_late_early_op(&dev->pm_domain->ops, state);
496 } else if (dev->type && dev->type->pm) {
497 info = "early type ";
498 callback = pm_late_early_op(dev->type->pm, state);
499 } else if (dev->class && dev->class->pm) {
500 info = "early class ";
501 callback = pm_late_early_op(dev->class->pm, state);
502 } else if (dev->bus && dev->bus->pm) {
503 info = "early bus ";
504 callback = pm_late_early_op(dev->bus->pm, state);
505 }
506
507 if (!callback && dev->driver && dev->driver->pm) {
508 info = "early driver ";
509 callback = pm_late_early_op(dev->driver->pm, state);
510 }
511
512 error = dpm_run_callback(callback, dev, state, info);
513
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200514 Out:
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100515 TRACE_RESUME(error);
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100516
517 pm_runtime_enable(dev);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100518 return error;
519}
520
521/**
522 * dpm_resume_early - Execute "early resume" callbacks for all devices.
523 * @state: PM transition of the system being carried out.
524 */
525static void dpm_resume_early(pm_message_t state)
526{
527 ktime_t starttime = ktime_get();
528
529 mutex_lock(&dpm_list_mtx);
530 while (!list_empty(&dpm_late_early_list)) {
531 struct device *dev = to_device(dpm_late_early_list.next);
532 int error;
533
534 get_device(dev);
535 list_move_tail(&dev->power.entry, &dpm_suspended_list);
536 mutex_unlock(&dpm_list_mtx);
537
538 error = device_resume_early(dev, state);
539 if (error) {
540 suspend_stats.failed_resume_early++;
541 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
542 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100543 pm_dev_err(dev, state, " early", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200544 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100545
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100546 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100547 put_device(dev);
548 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200549 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100550 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100551}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100552
553/**
554 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
555 * @state: PM transition of the system being carried out.
556 */
557void dpm_resume_start(pm_message_t state)
558{
559 dpm_resume_noirq(state);
560 dpm_resume_early(state);
561}
562EXPORT_SYMBOL_GPL(dpm_resume_start);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100563
564/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100565 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200566 * @dev: Device to handle.
567 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100568 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100569 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100570static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100571{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100572 pm_callback_t callback = NULL;
573 char *info = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100574 int error = 0;
575
576 TRACE_DEVICE(dev);
577 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400578
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200579 if (dev->power.syscore)
580 goto Complete;
581
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100582 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800583 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100584
Alan Sternf76b168b2011-06-18 20:22:23 +0200585 /*
586 * This is a fib. But we'll allow new children to be added below
587 * a resumed device, even if the device hasn't been completed yet.
588 */
589 dev->power.is_prepared = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100590
Alan Stern6d0e0e82011-06-18 22:42:09 +0200591 if (!dev->power.is_suspended)
592 goto Unlock;
593
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200594 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100595 info = "power domain ";
596 callback = pm_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100597 goto Driver;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100598 }
599
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100600 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100601 info = "type ";
602 callback = pm_op(dev->type->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100603 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100604 }
605
606 if (dev->class) {
607 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100608 info = "class ";
609 callback = pm_op(dev->class->pm, state);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100610 goto Driver;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100611 } else if (dev->class->resume) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100612 info = "legacy class ";
613 callback = dev->class->resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100614 goto End;
615 }
616 }
617
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200618 if (dev->bus) {
619 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100620 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100621 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200622 } else if (dev->bus->resume) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100623 info = "legacy bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100624 callback = dev->bus->resume;
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100625 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400627 }
628
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100629 Driver:
630 if (!callback && dev->driver && dev->driver->pm) {
631 info = "driver ";
632 callback = pm_op(dev->driver->pm, state);
633 }
634
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200635 End:
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100636 error = dpm_run_callback(callback, dev, state, info);
Alan Stern6d0e0e82011-06-18 22:42:09 +0200637 dev->power.is_suspended = false;
638
639 Unlock:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800640 device_unlock(dev);
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200641
642 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100643 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100644
Alan Sterncd59abf2007-09-21 15:36:56 -0400645 TRACE_RESUME(error);
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +0200646
Alan Sterncd59abf2007-09-21 15:36:56 -0400647 return error;
648}
649
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100650static void async_resume(void *data, async_cookie_t cookie)
651{
652 struct device *dev = (struct device *)data;
653 int error;
654
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100655 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100656 if (error)
657 pm_dev_err(dev, pm_transition, " async", error);
658 put_device(dev);
659}
660
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100661static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100662{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100663 return dev->power.async_suspend && pm_async_enabled
664 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100665}
666
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100667/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200668 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
669 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100670 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200671 * Execute the appropriate "resume" callback for all devices whose status
672 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400673 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200674void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400675{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100676 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100677 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400678
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200679 might_sleep();
680
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200681 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100682 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200683 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200684
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100685 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100686 INIT_COMPLETION(dev->power.completion);
687 if (is_async(dev)) {
688 get_device(dev);
689 async_schedule(async_resume, dev);
690 }
691 }
692
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100693 while (!list_empty(&dpm_suspended_list)) {
694 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200695 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100696 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200697 int error;
698
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200699 mutex_unlock(&dpm_list_mtx);
700
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100701 error = device_resume(dev, state, false);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200702 if (error) {
703 suspend_stats.failed_resume++;
704 dpm_save_failed_step(SUSPEND_RESUME);
705 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200706 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200707 }
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100708
709 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200710 }
711 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100712 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200713 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400714 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200715 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100716 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100717 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200718}
719
720/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200721 * device_complete - Complete a PM transition for given device.
722 * @dev: Device to handle.
723 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200724 */
Alan Sternd1616302009-05-24 22:05:42 +0200725static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200726{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100727 void (*callback)(struct device *) = NULL;
728 char *info = NULL;
729
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200730 if (dev->power.syscore)
731 return;
732
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800733 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200734
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200735 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100736 info = "completing power domain ";
737 callback = dev->pm_domain->ops.complete;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200738 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100739 info = "completing type ";
740 callback = dev->type->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100741 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100742 info = "completing class ";
743 callback = dev->class->pm->complete;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100744 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100745 info = "completing bus ";
746 callback = dev->bus->pm->complete;
747 }
748
749 if (!callback && dev->driver && dev->driver->pm) {
750 info = "completing driver ";
751 callback = dev->driver->pm->complete;
752 }
753
754 if (callback) {
755 pm_dev_dbg(dev, state, info);
756 callback(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200757 }
758
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800759 device_unlock(dev);
Alan Stern88d26132012-09-19 21:59:02 +0200760
761 pm_runtime_put_sync(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200762}
763
764/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200765 * dpm_complete - Complete a PM transition for all non-sysdev devices.
766 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200767 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200768 * Execute the ->complete() callbacks for all devices whose PM status is not
769 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200770 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200771void dpm_complete(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200772{
773 struct list_head list;
774
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200775 might_sleep();
776
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200777 INIT_LIST_HEAD(&list);
778 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100779 while (!list_empty(&dpm_prepared_list)) {
780 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200781
782 get_device(dev);
Alan Sternf76b168b2011-06-18 20:22:23 +0200783 dev->power.is_prepared = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100784 list_move(&dev->power.entry, &list);
785 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200786
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100787 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200788
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100789 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200790 put_device(dev);
791 }
792 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400793 mutex_unlock(&dpm_list_mtx);
794}
795
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100796/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200797 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
798 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400799 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200800 * Execute "resume" callbacks for all devices and complete the PM transition of
801 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400802 */
Alan Sternd1616302009-05-24 22:05:42 +0200803void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400804{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200805 dpm_resume(state);
806 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400807}
Alan Sternd1616302009-05-24 22:05:42 +0200808EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400809
810
Alan Sterncd59abf2007-09-21 15:36:56 -0400811/*------------------------- Suspend routines -------------------------*/
812
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200813/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200814 * resume_event - Return a "resume" message for given "suspend" sleep state.
815 * @sleep_state: PM message representing a sleep state.
816 *
817 * Return a PM message representing the resume event corresponding to given
818 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200819 */
820static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400821{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200822 switch (sleep_state.event) {
823 case PM_EVENT_SUSPEND:
824 return PMSG_RESUME;
825 case PM_EVENT_FREEZE:
826 case PM_EVENT_QUIESCE:
827 return PMSG_RECOVER;
828 case PM_EVENT_HIBERNATE:
829 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400830 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200831 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400832}
833
834/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200835 * device_suspend_noirq - Execute a "late suspend" callback for given device.
836 * @dev: Device to handle.
837 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100838 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200839 * The driver of @dev will not receive interrupts while this function is being
840 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100841 */
Alan Sternd1616302009-05-24 22:05:42 +0200842static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100843{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100844 pm_callback_t callback = NULL;
845 char *info = NULL;
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100846
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200847 if (dev->power.syscore)
848 return 0;
849
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200850 if (dev->pm_domain) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100851 info = "noirq power domain ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100852 callback = pm_noirq_op(&dev->pm_domain->ops, state);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200853 } else if (dev->type && dev->type->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100854 info = "noirq type ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100855 callback = pm_noirq_op(dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100856 } else if (dev->class && dev->class->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100857 info = "noirq class ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100858 callback = pm_noirq_op(dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100859 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100860 info = "noirq bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100861 callback = pm_noirq_op(dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100862 }
863
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100864 if (!callback && dev->driver && dev->driver->pm) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100865 info = "noirq driver ";
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100866 callback = pm_noirq_op(dev->driver->pm, state);
867 }
868
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +0100869 return dpm_run_callback(callback, dev, state, info);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100870}
871
872/**
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100873 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200874 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100875 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200876 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
877 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100878 */
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100879static int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100880{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100881 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100882 int error = 0;
883
Preeti U Murthy8651f972012-07-09 10:12:56 +0200884 cpuidle_pause();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100885 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200886 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100887 while (!list_empty(&dpm_late_early_list)) {
888 struct device *dev = to_device(dpm_late_early_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100889
890 get_device(dev);
891 mutex_unlock(&dpm_list_mtx);
892
Alan Sternd1616302009-05-24 22:05:42 +0200893 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100894
895 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100896 if (error) {
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100897 pm_dev_err(dev, state, " noirq", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200898 suspend_stats.failed_suspend_noirq++;
899 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
900 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100901 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100902 break;
903 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100904 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100905 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100906 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200907
908 if (pm_wakeup_pending()) {
909 error = -EBUSY;
910 break;
911 }
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100912 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200913 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100914 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200915 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100916 else
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100917 dpm_show_time(starttime, state, "noirq");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100918 return error;
919}
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100920
921/**
922 * device_suspend_late - Execute a "late suspend" callback for given device.
923 * @dev: Device to handle.
924 * @state: PM transition of the system being carried out.
925 *
926 * Runtime PM is disabled for @dev while this function is being executed.
927 */
928static int device_suspend_late(struct device *dev, pm_message_t state)
929{
930 pm_callback_t callback = NULL;
931 char *info = NULL;
932
Rafael J. Wysocki9f6d8f62012-12-22 23:59:01 +0100933 __pm_runtime_disable(dev, false);
934
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200935 if (dev->power.syscore)
936 return 0;
937
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100938 if (dev->pm_domain) {
939 info = "late power domain ";
940 callback = pm_late_early_op(&dev->pm_domain->ops, state);
941 } else if (dev->type && dev->type->pm) {
942 info = "late type ";
943 callback = pm_late_early_op(dev->type->pm, state);
944 } else if (dev->class && dev->class->pm) {
945 info = "late class ";
946 callback = pm_late_early_op(dev->class->pm, state);
947 } else if (dev->bus && dev->bus->pm) {
948 info = "late bus ";
949 callback = pm_late_early_op(dev->bus->pm, state);
950 }
951
952 if (!callback && dev->driver && dev->driver->pm) {
953 info = "late driver ";
954 callback = pm_late_early_op(dev->driver->pm, state);
955 }
956
957 return dpm_run_callback(callback, dev, state, info);
958}
959
960/**
961 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
962 * @state: PM transition of the system being carried out.
963 */
964static int dpm_suspend_late(pm_message_t state)
965{
966 ktime_t starttime = ktime_get();
967 int error = 0;
968
969 mutex_lock(&dpm_list_mtx);
970 while (!list_empty(&dpm_suspended_list)) {
971 struct device *dev = to_device(dpm_suspended_list.prev);
972
973 get_device(dev);
974 mutex_unlock(&dpm_list_mtx);
975
976 error = device_suspend_late(dev, state);
977
978 mutex_lock(&dpm_list_mtx);
979 if (error) {
980 pm_dev_err(dev, state, " late", error);
981 suspend_stats.failed_suspend_late++;
982 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
983 dpm_save_failed_dev(dev_name(dev));
984 put_device(dev);
985 break;
986 }
987 if (!list_empty(&dev->power.entry))
988 list_move(&dev->power.entry, &dpm_late_early_list);
989 put_device(dev);
Rafael J. Wysocki52d136c2012-04-29 22:52:19 +0200990
991 if (pm_wakeup_pending()) {
992 error = -EBUSY;
993 break;
994 }
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100995 }
996 mutex_unlock(&dpm_list_mtx);
997 if (error)
998 dpm_resume_early(resume_event(state));
999 else
1000 dpm_show_time(starttime, state, "late");
1001
1002 return error;
1003}
1004
1005/**
1006 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1007 * @state: PM transition of the system being carried out.
1008 */
1009int dpm_suspend_end(pm_message_t state)
1010{
1011 int error = dpm_suspend_late(state);
Colin Cross064b0212012-07-19 10:38:06 +02001012 if (error)
1013 return error;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001014
Colin Cross064b0212012-07-19 10:38:06 +02001015 error = dpm_suspend_noirq(state);
1016 if (error) {
Feng Hong997a0312012-09-19 14:16:00 +02001017 dpm_resume_early(resume_event(state));
Colin Cross064b0212012-07-19 10:38:06 +02001018 return error;
1019 }
1020
1021 return 0;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +01001022}
1023EXPORT_SYMBOL_GPL(dpm_suspend_end);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001024
1025/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001026 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -08001027 * @dev: Device to suspend.
1028 * @state: PM transition of the system being carried out.
1029 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001030 */
1031static int legacy_suspend(struct device *dev, pm_message_t state,
1032 int (*cb)(struct device *dev, pm_message_t state))
1033{
1034 int error;
1035 ktime_t calltime;
1036
1037 calltime = initcall_debug_start(dev);
1038
1039 error = cb(dev, state);
1040 suspend_report_result(cb, error);
1041
1042 initcall_debug_report(dev, calltime, error);
1043
1044 return error;
1045}
1046
1047/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001048 * device_suspend - Execute "suspend" callbacks for given device.
1049 * @dev: Device to handle.
1050 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001051 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -04001052 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001053static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -04001054{
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001055 pm_callback_t callback = NULL;
1056 char *info = NULL;
Alan Sterncd59abf2007-09-21 15:36:56 -04001057 int error = 0;
1058
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001059 dpm_wait_for_children(dev, async);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001060
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001061 if (async_error)
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001062 goto Complete;
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001063
Alan Stern88d26132012-09-19 21:59:02 +02001064 /*
1065 * If a device configured to wake up the system from sleep states
1066 * has been suspended at run time and there's a resume request pending
1067 * for it, this is equivalent to the device signaling wakeup, so the
1068 * system suspend operation should be aborted.
1069 */
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001070 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1071 pm_wakeup_event(dev, 0);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001072
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001073 if (pm_wakeup_pending()) {
1074 async_error = -EBUSY;
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001075 goto Complete;
Rafael J. Wysockid83f9052010-12-03 23:14:26 +01001076 }
1077
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001078 if (dev->power.syscore)
1079 goto Complete;
1080
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001081 device_lock(dev);
1082
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001083 if (dev->pm_domain) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001084 info = "power domain ";
1085 callback = pm_op(&dev->pm_domain->ops, state);
1086 goto Run;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001087 }
1088
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001089 if (dev->type && dev->type->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001090 info = "type ";
1091 callback = pm_op(dev->type->pm, state);
1092 goto Run;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001093 }
1094
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001095 if (dev->class) {
1096 if (dev->class->pm) {
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001097 info = "class ";
1098 callback = pm_op(dev->class->pm, state);
1099 goto Run;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001100 } else if (dev->class->suspend) {
1101 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001102 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001103 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001104 }
Alan Sterncd59abf2007-09-21 15:36:56 -04001105 }
1106
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001107 if (dev->bus) {
1108 if (dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001109 info = "bus ";
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001110 callback = pm_op(dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001111 } else if (dev->bus->suspend) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001112 pm_dev_dbg(dev, state, "legacy bus ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +01001113 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001114 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001115 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001116 }
1117
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001118 Run:
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001119 if (!callback && dev->driver && dev->driver->pm) {
1120 info = "driver ";
1121 callback = pm_op(dev->driver->pm, state);
1122 }
1123
Rafael J. Wysocki9cf519d2011-12-18 00:34:01 +01001124 error = dpm_run_callback(callback, dev, state, info);
1125
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001126 End:
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001127 if (!error) {
1128 dev->power.is_suspended = true;
Rafael J. Wysocki8b258cc2011-11-17 21:39:33 +01001129 if (dev->power.wakeup_path
1130 && dev->parent && !dev->parent->power.ignore_children)
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001131 dev->parent->power.wakeup_path = true;
1132 }
Alan Stern6d0e0e82011-06-18 22:42:09 +02001133
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001134 device_unlock(dev);
Mandeep Singh Baines1f758b22012-06-24 23:31:09 +02001135
1136 Complete:
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001137 complete_all(&dev->power.completion);
Alan Stern88d26132012-09-19 21:59:02 +02001138 if (error)
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001139 async_error = error;
1140
Alan Sterncd59abf2007-09-21 15:36:56 -04001141 return error;
1142}
1143
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001144static void async_suspend(void *data, async_cookie_t cookie)
1145{
1146 struct device *dev = (struct device *)data;
1147 int error;
1148
1149 error = __device_suspend(dev, pm_transition, true);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001150 if (error) {
1151 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001152 pm_dev_err(dev, pm_transition, " async", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001153 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001154
1155 put_device(dev);
1156}
1157
1158static int device_suspend(struct device *dev)
1159{
1160 INIT_COMPLETION(dev->power.completion);
1161
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +01001162 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001163 get_device(dev);
1164 async_schedule(async_suspend, dev);
1165 return 0;
1166 }
1167
1168 return __device_suspend(dev, pm_transition, false);
1169}
1170
Alan Sterncd59abf2007-09-21 15:36:56 -04001171/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001172 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1173 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001174 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001175int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001176{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001177 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -04001178 int error = 0;
1179
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001180 might_sleep();
1181
Alan Sterncd59abf2007-09-21 15:36:56 -04001182 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001183 pm_transition = state;
1184 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001185 while (!list_empty(&dpm_prepared_list)) {
1186 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -04001187
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001188 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001189 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001190
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001191 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001192
Alan Stern1b3cbec2008-02-29 11:50:22 -05001193 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001194 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001195 pm_dev_err(dev, state, "", error);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001196 dpm_save_failed_dev(dev_name(dev));
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001197 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001198 break;
1199 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +01001200 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001201 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001202 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001203 if (async_error)
1204 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001205 }
1206 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +01001207 async_synchronize_full();
1208 if (!error)
1209 error = async_error;
ShuoX Liu2a77c462011-08-10 23:01:26 +02001210 if (error) {
1211 suspend_stats.failed_suspend++;
1212 dpm_save_failed_step(SUSPEND_SUSPEND);
1213 } else
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +01001214 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001215 return error;
1216}
1217
1218/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001219 * device_prepare - Prepare a device for system power transition.
1220 * @dev: Device to handle.
1221 * @state: PM transition of the system being carried out.
1222 *
1223 * Execute the ->prepare() callback(s) for given device. No new children of the
1224 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001225 */
Alan Sternd1616302009-05-24 22:05:42 +02001226static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001227{
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001228 int (*callback)(struct device *) = NULL;
1229 char *info = NULL;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001230 int error = 0;
1231
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001232 if (dev->power.syscore)
1233 return 0;
1234
Alan Stern88d26132012-09-19 21:59:02 +02001235 /*
1236 * If a device's parent goes into runtime suspend at the wrong time,
1237 * it won't be possible to resume the device. To prevent this we
1238 * block runtime suspend here, during the prepare phase, and allow
1239 * it again during the complete phase.
1240 */
1241 pm_runtime_get_noresume(dev);
1242
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001243 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001244
Rafael J. Wysocki4ca46ff2011-10-16 23:34:36 +02001245 dev->power.wakeup_path = device_may_wakeup(dev);
1246
Rafael J. Wysocki564b9052011-06-23 01:52:55 +02001247 if (dev->pm_domain) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001248 info = "preparing power domain ";
1249 callback = dev->pm_domain->ops.prepare;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +02001250 } else if (dev->type && dev->type->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001251 info = "preparing type ";
1252 callback = dev->type->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001253 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001254 info = "preparing class ";
1255 callback = dev->class->pm->prepare;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +01001256 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +01001257 info = "preparing bus ";
1258 callback = dev->bus->pm->prepare;
1259 }
1260
1261 if (!callback && dev->driver && dev->driver->pm) {
1262 info = "preparing driver ";
1263 callback = dev->driver->pm->prepare;
1264 }
1265
1266 if (callback) {
1267 error = callback(dev);
1268 suspend_report_result(callback, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01001269 }
1270
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -08001271 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001272
1273 return error;
1274}
1275
1276/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001277 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1278 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001279 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001280 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001281 */
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001282int dpm_prepare(pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001283{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001284 int error = 0;
1285
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +02001286 might_sleep();
1287
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001288 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001289 while (!list_empty(&dpm_list)) {
1290 struct device *dev = to_device(dpm_list.next);
1291
1292 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001293 mutex_unlock(&dpm_list_mtx);
1294
Rafael J. Wysocki1e2ef052011-07-06 10:51:58 +02001295 error = device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001296
1297 mutex_lock(&dpm_list_mtx);
1298 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001299 if (error == -EAGAIN) {
1300 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001301 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001302 continue;
1303 }
Rafael J. Wysocki1e752272010-12-03 22:58:05 +01001304 printk(KERN_INFO "PM: Device %s not prepared "
1305 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001306 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001307 put_device(dev);
1308 break;
1309 }
Alan Sternf76b168b2011-06-18 20:22:23 +02001310 dev->power.is_prepared = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001311 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001312 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001313 put_device(dev);
1314 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001315 mutex_unlock(&dpm_list_mtx);
1316 return error;
1317}
1318
1319/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001320 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1321 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001322 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001323 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1324 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001325 */
Alan Sternd1616302009-05-24 22:05:42 +02001326int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001327{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001328 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001329
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001330 error = dpm_prepare(state);
ShuoX Liu2a77c462011-08-10 23:01:26 +02001331 if (error) {
1332 suspend_stats.failed_prepare++;
1333 dpm_save_failed_step(SUSPEND_PREPARE);
1334 } else
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001335 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001336 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001337}
Alan Sternd1616302009-05-24 22:05:42 +02001338EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001339
1340void __suspend_report_result(const char *function, void *fn, int ret)
1341{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001342 if (ret)
1343 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001344}
1345EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001346
1347/**
1348 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1349 * @dev: Device to wait for.
1350 * @subordinate: Device that needs to wait for @dev.
1351 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001352int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001353{
1354 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001355 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001356}
1357EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
Ming Leidfe32122012-08-17 22:06:59 +08001358
1359/**
1360 * dpm_for_each_dev - device iterator.
1361 * @data: data for the callback.
1362 * @fn: function to be called for each device.
1363 *
1364 * Iterate over devices in dpm_list, and call @fn for each device,
1365 * passing it @data.
1366 */
1367void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1368{
1369 struct device *dev;
1370
1371 if (!fn)
1372 return;
1373
1374 device_pm_lock();
1375 list_for_each_entry(dev, &dpm_list, power.entry)
1376 fn(dev, data);
1377 device_pm_unlock();
1378}
1379EXPORT_SYMBOL_GPL(dpm_for_each_dev);