blob: 052dc53eef388db918f2a0f6ce961be366691577 [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>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070022#include <linux/mutex.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040023#include <linux/pm.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020024#include <linux/pm_runtime.h>
Alan Sterncd59abf2007-09-21 15:36:56 -040025#include <linux/resume-trace.h>
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +010026#include <linux/interrupt.h>
Arjan van de Venf2511772009-12-13 20:29:01 +010027#include <linux/sched.h>
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010028#include <linux/async.h>
Rafael J. Wysocki1e75227e2010-12-03 22:58:05 +010029#include <linux/suspend.h>
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070030
Alan Sterncd59abf2007-09-21 15:36:56 -040031#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "power.h"
33
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010034/*
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020035 * The entries in the dpm_list list are in a depth first order, simply
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010036 * because children are guaranteed to be discovered after parents, and
37 * are inserted at the back of the list on discovery.
38 *
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -080039 * Since device_pm_add() may be called with a device lock held,
40 * we must never try to acquire a device lock while holding
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010041 * dpm_list_mutex.
42 */
43
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020044LIST_HEAD(dpm_list);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +010045LIST_HEAD(dpm_prepared_list);
46LIST_HEAD(dpm_suspended_list);
47LIST_HEAD(dpm_noirq_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alan Sterncd59abf2007-09-21 15:36:56 -040049static DEFINE_MUTEX(dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010050static pm_message_t pm_transition;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysocki098dff72010-09-22 22:10:57 +020052static int async_error;
53
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020054/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020055 * device_pm_init - Initialize the PM-related part of a device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020056 * @dev: Device object being initialized.
57 */
58void device_pm_init(struct device *dev)
59{
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +010060 dev->power.in_suspend = false;
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +010061 init_completion(&dev->power.completion);
Colin Cross152e1d52010-09-03 01:24:07 +020062 complete_all(&dev->power.completion);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020063 dev->power.wakeup = NULL;
64 spin_lock_init(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020065 pm_runtime_init(dev);
66}
67
68/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020069 * device_pm_lock - Lock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020070 */
71void device_pm_lock(void)
72{
73 mutex_lock(&dpm_list_mtx);
74}
75
76/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020077 * device_pm_unlock - Unlock the list of active devices used by the PM core.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +020078 */
79void device_pm_unlock(void)
80{
81 mutex_unlock(&dpm_list_mtx);
82}
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010083
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010084/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +020085 * device_pm_add - Add a device to the PM core's list of active devices.
86 * @dev: Device to add to the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +010087 */
Alan Stern3b98aea2008-08-07 13:06:12 -040088void device_pm_add(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 pr_debug("PM: Adding info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +010091 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070092 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockib64959e2010-12-16 17:11:45 +010093 if (dev->parent && dev->parent->power.in_suspend)
94 dev_warn(dev, "parent %s should not be sleeping\n",
95 dev_name(dev->parent));
Alan Stern3b98aea2008-08-07 13:06:12 -040096 list_add_tail(&dev->power.entry, &dpm_list);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -070097 mutex_unlock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100100/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200101 * device_pm_remove - Remove a device from the PM core's list of active devices.
102 * @dev: Device to be removed from the list.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100103 */
Rafael J. Wysocki9cddad72007-06-13 15:53:34 +0200104void device_pm_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 pr_debug("PM: Removing info for %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100107 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100108 complete_all(&dev->power.completion);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700109 mutex_lock(&dpm_list_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 list_del_init(&dev->power.entry);
Matthias Kaehlcke11048dc2007-05-23 14:19:41 -0700111 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200112 device_wakeup_disable(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200113 pm_runtime_remove(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100114}
115
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200116/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200117 * device_pm_move_before - Move device in the PM core's list of active devices.
118 * @deva: Device to move in dpm_list.
119 * @devb: Device @deva should come before.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100120 */
121void device_pm_move_before(struct device *deva, struct device *devb)
122{
123 pr_debug("PM: Moving %s:%s before %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100124 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
125 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100126 /* Delete deva from dpm_list and reinsert before devb. */
127 list_move_tail(&deva->power.entry, &devb->power.entry);
128}
129
130/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200131 * device_pm_move_after - Move device in the PM core's list of active devices.
132 * @deva: Device to move in dpm_list.
133 * @devb: Device @deva should come after.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100134 */
135void device_pm_move_after(struct device *deva, struct device *devb)
136{
137 pr_debug("PM: Moving %s:%s after %s:%s\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100138 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
139 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100140 /* Delete deva from dpm_list and reinsert after devb. */
141 list_move(&deva->power.entry, &devb->power.entry);
142}
143
144/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200145 * device_pm_move_last - Move device to end of the PM core's list of devices.
146 * @dev: Device to move in dpm_list.
Cornelia Huckffa6a702009-03-04 12:44:00 +0100147 */
148void device_pm_move_last(struct device *dev)
149{
150 pr_debug("PM: Moving %s:%s to end of list\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100151 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
Cornelia Huckffa6a702009-03-04 12:44:00 +0100152 list_move_tail(&dev->power.entry, &dpm_list);
153}
154
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100155static ktime_t initcall_debug_start(struct device *dev)
156{
157 ktime_t calltime = ktime_set(0, 0);
158
159 if (initcall_debug) {
160 pr_info("calling %s+ @ %i\n",
161 dev_name(dev), task_pid_nr(current));
162 calltime = ktime_get();
163 }
164
165 return calltime;
166}
167
168static void initcall_debug_report(struct device *dev, ktime_t calltime,
169 int error)
170{
171 ktime_t delta, rettime;
172
173 if (initcall_debug) {
174 rettime = ktime_get();
175 delta = ktime_sub(rettime, calltime);
176 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
177 error, (unsigned long long)ktime_to_ns(delta) >> 10);
178 }
179}
180
Cornelia Huckffa6a702009-03-04 12:44:00 +0100181/**
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100182 * dpm_wait - Wait for a PM operation to complete.
183 * @dev: Device to wait for.
184 * @async: If unset, wait only if the device's power.async_suspend flag is set.
185 */
186static void dpm_wait(struct device *dev, bool async)
187{
188 if (!dev)
189 return;
190
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100191 if (async || (pm_async_enabled && dev->power.async_suspend))
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100192 wait_for_completion(&dev->power.completion);
193}
194
195static int dpm_wait_fn(struct device *dev, void *async_ptr)
196{
197 dpm_wait(dev, *((bool *)async_ptr));
198 return 0;
199}
200
201static void dpm_wait_for_children(struct device *dev, bool async)
202{
203 device_for_each_child(dev, &async, dpm_wait_fn);
204}
205
206/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200207 * pm_op - Execute the PM operation appropriate for given PM event.
208 * @dev: Device to handle.
209 * @ops: PM operations to choose from.
210 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200211 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200212static int pm_op(struct device *dev,
213 const struct dev_pm_ops *ops,
214 pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200215{
216 int error = 0;
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100217 ktime_t calltime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100218
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100219 calltime = initcall_debug_start(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200220
221 switch (state.event) {
222#ifdef CONFIG_SUSPEND
223 case PM_EVENT_SUSPEND:
224 if (ops->suspend) {
225 error = ops->suspend(dev);
226 suspend_report_result(ops->suspend, error);
227 }
228 break;
229 case PM_EVENT_RESUME:
230 if (ops->resume) {
231 error = ops->resume(dev);
232 suspend_report_result(ops->resume, error);
233 }
234 break;
235#endif /* CONFIG_SUSPEND */
236#ifdef CONFIG_HIBERNATION
237 case PM_EVENT_FREEZE:
238 case PM_EVENT_QUIESCE:
239 if (ops->freeze) {
240 error = ops->freeze(dev);
241 suspend_report_result(ops->freeze, error);
242 }
243 break;
244 case PM_EVENT_HIBERNATE:
245 if (ops->poweroff) {
246 error = ops->poweroff(dev);
247 suspend_report_result(ops->poweroff, error);
248 }
249 break;
250 case PM_EVENT_THAW:
251 case PM_EVENT_RECOVER:
252 if (ops->thaw) {
253 error = ops->thaw(dev);
254 suspend_report_result(ops->thaw, error);
255 }
256 break;
257 case PM_EVENT_RESTORE:
258 if (ops->restore) {
259 error = ops->restore(dev);
260 suspend_report_result(ops->restore, error);
261 }
262 break;
263#endif /* CONFIG_HIBERNATION */
264 default:
265 error = -EINVAL;
266 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100267
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100268 initcall_debug_report(dev, calltime, error);
Arjan van de Venf2511772009-12-13 20:29:01 +0100269
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200270 return error;
271}
272
273/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200274 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
275 * @dev: Device to handle.
276 * @ops: PM operations to choose from.
277 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200278 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200279 * The driver of @dev will not receive interrupts while this function is being
280 * executed.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200281 */
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200282static int pm_noirq_op(struct device *dev,
283 const struct dev_pm_ops *ops,
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200284 pm_message_t state)
285{
286 int error = 0;
Rafael J. Wysocki2cbb3ce2010-12-15 00:17:29 +0100287 ktime_t calltime = ktime_set(0, 0), delta, rettime;
Arjan van de Venf2511772009-12-13 20:29:01 +0100288
289 if (initcall_debug) {
Rafael J. Wysocki8cc6b392010-01-23 22:03:29 +0100290 pr_info("calling %s+ @ %i, parent: %s\n",
291 dev_name(dev), task_pid_nr(current),
292 dev->parent ? dev_name(dev->parent) : "none");
Arjan van de Venf2511772009-12-13 20:29:01 +0100293 calltime = ktime_get();
294 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200295
296 switch (state.event) {
297#ifdef CONFIG_SUSPEND
298 case PM_EVENT_SUSPEND:
299 if (ops->suspend_noirq) {
300 error = ops->suspend_noirq(dev);
301 suspend_report_result(ops->suspend_noirq, error);
302 }
303 break;
304 case PM_EVENT_RESUME:
305 if (ops->resume_noirq) {
306 error = ops->resume_noirq(dev);
307 suspend_report_result(ops->resume_noirq, error);
308 }
309 break;
310#endif /* CONFIG_SUSPEND */
311#ifdef CONFIG_HIBERNATION
312 case PM_EVENT_FREEZE:
313 case PM_EVENT_QUIESCE:
314 if (ops->freeze_noirq) {
315 error = ops->freeze_noirq(dev);
316 suspend_report_result(ops->freeze_noirq, error);
317 }
318 break;
319 case PM_EVENT_HIBERNATE:
320 if (ops->poweroff_noirq) {
321 error = ops->poweroff_noirq(dev);
322 suspend_report_result(ops->poweroff_noirq, error);
323 }
324 break;
325 case PM_EVENT_THAW:
326 case PM_EVENT_RECOVER:
327 if (ops->thaw_noirq) {
328 error = ops->thaw_noirq(dev);
329 suspend_report_result(ops->thaw_noirq, error);
330 }
331 break;
332 case PM_EVENT_RESTORE:
333 if (ops->restore_noirq) {
334 error = ops->restore_noirq(dev);
335 suspend_report_result(ops->restore_noirq, error);
336 }
337 break;
338#endif /* CONFIG_HIBERNATION */
339 default:
340 error = -EINVAL;
341 }
Arjan van de Venf2511772009-12-13 20:29:01 +0100342
343 if (initcall_debug) {
344 rettime = ktime_get();
345 delta = ktime_sub(rettime, calltime);
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100346 printk("initcall %s_i+ returned %d after %Ld usecs\n",
347 dev_name(dev), error,
348 (unsigned long long)ktime_to_ns(delta) >> 10);
Arjan van de Venf2511772009-12-13 20:29:01 +0100349 }
350
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200351 return error;
352}
353
354static char *pm_verb(int event)
355{
356 switch (event) {
357 case PM_EVENT_SUSPEND:
358 return "suspend";
359 case PM_EVENT_RESUME:
360 return "resume";
361 case PM_EVENT_FREEZE:
362 return "freeze";
363 case PM_EVENT_QUIESCE:
364 return "quiesce";
365 case PM_EVENT_HIBERNATE:
366 return "hibernate";
367 case PM_EVENT_THAW:
368 return "thaw";
369 case PM_EVENT_RESTORE:
370 return "restore";
371 case PM_EVENT_RECOVER:
372 return "recover";
373 default:
374 return "(unknown PM event)";
375 }
376}
377
378static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
379{
380 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
381 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
382 ", may wakeup" : "");
383}
384
385static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
386 int error)
387{
388 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +0100389 dev_name(dev), pm_verb(state.event), info, error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200390}
391
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100392static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
393{
394 ktime_t calltime;
Kevin Cernekee0702d9ee2010-09-20 22:32:10 +0200395 u64 usecs64;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100396 int usecs;
397
398 calltime = ktime_get();
399 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
400 do_div(usecs64, NSEC_PER_USEC);
401 usecs = usecs64;
402 if (usecs == 0)
403 usecs = 1;
404 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
405 info ?: "", info ? " " : "", pm_verb(state.event),
406 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
407}
408
Alan Sterncd59abf2007-09-21 15:36:56 -0400409/*------------------------- Resume routines -------------------------*/
410
411/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200412 * device_resume_noirq - Execute an "early resume" callback for given device.
413 * @dev: Device to handle.
414 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400415 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200416 * The driver of @dev will not receive interrupts while this function is being
417 * executed.
Alan Sterncd59abf2007-09-21 15:36:56 -0400418 */
Alan Sternd1616302009-05-24 22:05:42 +0200419static int device_resume_noirq(struct device *dev, pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400420{
421 int error = 0;
422
423 TRACE_DEVICE(dev);
424 TRACE_RESUME(0);
425
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100426 if (dev->pwr_domain) {
427 pm_dev_dbg(dev, state, "EARLY power domain ");
428 pm_noirq_op(dev, &dev->pwr_domain->ops, state);
429 }
430
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100431 if (dev->type && dev->type->pm) {
432 pm_dev_dbg(dev, state, "EARLY type ");
433 error = pm_noirq_op(dev, dev->type->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100434 } else if (dev->class && dev->class->pm) {
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100435 pm_dev_dbg(dev, state, "EARLY class ");
436 error = pm_noirq_op(dev, dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100437 } else if (dev->bus && dev->bus->pm) {
438 pm_dev_dbg(dev, state, "EARLY ");
439 error = pm_noirq_op(dev, dev->bus->pm, state);
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100440 }
441
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100442 TRACE_RESUME(error);
443 return error;
444}
445
446/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200447 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
448 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100449 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200450 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
451 * enable device drivers to receive interrupts.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100452 */
Alan Sternd1616302009-05-24 22:05:42 +0200453void dpm_resume_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100454{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100455 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100456
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200457 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100458 while (!list_empty(&dpm_noirq_list)) {
459 struct device *dev = to_device(dpm_noirq_list.next);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100460 int error;
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100461
462 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100463 list_move_tail(&dev->power.entry, &dpm_suspended_list);
464 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100465
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100466 error = device_resume_noirq(dev, state);
467 if (error)
468 pm_dev_err(dev, state, " early", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100469
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100470 mutex_lock(&dpm_list_mtx);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100471 put_device(dev);
472 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200473 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100474 dpm_show_time(starttime, state, "early");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100475 resume_device_irqs();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100476}
Alan Sternd1616302009-05-24 22:05:42 +0200477EXPORT_SYMBOL_GPL(dpm_resume_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100478
479/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100480 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800481 * @dev: Device to resume.
482 * @cb: Resume callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100483 */
484static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
485{
486 int error;
487 ktime_t calltime;
488
489 calltime = initcall_debug_start(dev);
490
491 error = cb(dev);
492 suspend_report_result(cb, error);
493
494 initcall_debug_report(dev, calltime, error);
495
496 return error;
497}
498
499/**
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100500 * device_resume - Execute "resume" callbacks for given device.
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200501 * @dev: Device to handle.
502 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100503 * @async: If true, the device is being resumed asynchronously.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100504 */
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100505static int device_resume(struct device *dev, pm_message_t state, bool async)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100506{
507 int error = 0;
508
509 TRACE_DEVICE(dev);
510 TRACE_RESUME(0);
Alan Sterncd59abf2007-09-21 15:36:56 -0400511
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100512 dpm_wait(dev->parent, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800513 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100514
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100515 dev->power.in_suspend = false;
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100516
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100517 if (dev->pwr_domain) {
518 pm_dev_dbg(dev, state, "power domain ");
519 pm_op(dev, &dev->pwr_domain->ops, state);
520 }
521
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100522 if (dev->type && dev->type->pm) {
523 pm_dev_dbg(dev, state, "type ");
524 error = pm_op(dev, dev->type->pm, state);
525 goto End;
526 }
527
528 if (dev->class) {
529 if (dev->class->pm) {
530 pm_dev_dbg(dev, state, "class ");
531 error = pm_op(dev, dev->class->pm, state);
532 goto End;
533 } else if (dev->class->resume) {
534 pm_dev_dbg(dev, state, "legacy class ");
535 error = legacy_resume(dev, dev->class->resume);
536 goto End;
537 }
538 }
539
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200540 if (dev->bus) {
541 if (dev->bus->pm) {
542 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200543 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200544 } else if (dev->bus->resume) {
545 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100546 error = legacy_resume(dev, dev->bus->resume);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200547 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400548 }
549
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200550 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800551 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100552 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100553
Alan Sterncd59abf2007-09-21 15:36:56 -0400554 TRACE_RESUME(error);
555 return error;
556}
557
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100558static void async_resume(void *data, async_cookie_t cookie)
559{
560 struct device *dev = (struct device *)data;
561 int error;
562
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100563 error = device_resume(dev, pm_transition, true);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100564 if (error)
565 pm_dev_err(dev, pm_transition, " async", error);
566 put_device(dev);
567}
568
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100569static bool is_async(struct device *dev)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100570{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100571 return dev->power.async_suspend && pm_async_enabled
572 && !pm_trace_is_enabled();
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100573}
574
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100575/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200576 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
577 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100578 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200579 * Execute the appropriate "resume" callback for all devices whose status
580 * indicates that they are suspended.
Alan Sterncd59abf2007-09-21 15:36:56 -0400581 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200582static void dpm_resume(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400583{
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100584 struct device *dev;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100585 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400586
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200587 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100588 pm_transition = state;
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200589 async_error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200590
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100591 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100592 INIT_COMPLETION(dev->power.completion);
593 if (is_async(dev)) {
594 get_device(dev);
595 async_schedule(async_resume, dev);
596 }
597 }
598
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100599 while (!list_empty(&dpm_suspended_list)) {
600 dev = to_device(dpm_suspended_list.next);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200601 get_device(dev);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100602 if (!is_async(dev)) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200603 int error;
604
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200605 mutex_unlock(&dpm_list_mtx);
606
Rafael J. Wysocki97df8c12010-01-23 22:25:31 +0100607 error = device_resume(dev, state, false);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200608 if (error)
609 pm_dev_err(dev, state, "", error);
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100610
611 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200612 }
613 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100614 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200615 put_device(dev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400616 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200617 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100618 async_synchronize_full();
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100619 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200620}
621
622/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200623 * device_complete - Complete a PM transition for given device.
624 * @dev: Device to handle.
625 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200626 */
Alan Sternd1616302009-05-24 22:05:42 +0200627static void device_complete(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200628{
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800629 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200630
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100631 if (dev->pwr_domain && dev->pwr_domain->ops.complete) {
632 pm_dev_dbg(dev, state, "completing power domain ");
633 dev->pwr_domain->ops.complete(dev);
634 }
635
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100636 if (dev->type && dev->type->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200637 pm_dev_dbg(dev, state, "completing type ");
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100638 if (dev->type->pm->complete)
639 dev->type->pm->complete(dev);
640 } else if (dev->class && dev->class->pm) {
641 pm_dev_dbg(dev, state, "completing class ");
642 if (dev->class->pm->complete)
643 dev->class->pm->complete(dev);
644 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200645 pm_dev_dbg(dev, state, "completing ");
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100646 if (dev->bus->pm->complete)
647 dev->bus->pm->complete(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200648 }
649
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800650 device_unlock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200651}
652
653/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200654 * dpm_complete - Complete a PM transition for all non-sysdev devices.
655 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200656 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200657 * Execute the ->complete() callbacks for all devices whose PM status is not
658 * DPM_ON (this allows new devices to be registered).
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200659 */
660static void dpm_complete(pm_message_t state)
661{
662 struct list_head list;
663
664 INIT_LIST_HEAD(&list);
665 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100666 while (!list_empty(&dpm_prepared_list)) {
667 struct device *dev = to_device(dpm_prepared_list.prev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200668
669 get_device(dev);
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +0100670 dev->power.in_suspend = false;
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100671 list_move(&dev->power.entry, &list);
672 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200673
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100674 device_complete(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200675
Rafael J. Wysocki5b219a52010-12-16 00:51:08 +0100676 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200677 put_device(dev);
678 }
679 list_splice(&list, &dpm_list);
Alan Sterncd59abf2007-09-21 15:36:56 -0400680 mutex_unlock(&dpm_list_mtx);
681}
682
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100683/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200684 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
685 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400686 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200687 * Execute "resume" callbacks for all devices and complete the PM transition of
688 * the system.
Alan Sterncd59abf2007-09-21 15:36:56 -0400689 */
Alan Sternd1616302009-05-24 22:05:42 +0200690void dpm_resume_end(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400691{
692 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200693 dpm_resume(state);
694 dpm_complete(state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400695}
Alan Sternd1616302009-05-24 22:05:42 +0200696EXPORT_SYMBOL_GPL(dpm_resume_end);
Alan Sterncd59abf2007-09-21 15:36:56 -0400697
698
Alan Sterncd59abf2007-09-21 15:36:56 -0400699/*------------------------- Suspend routines -------------------------*/
700
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200701/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200702 * resume_event - Return a "resume" message for given "suspend" sleep state.
703 * @sleep_state: PM message representing a sleep state.
704 *
705 * Return a PM message representing the resume event corresponding to given
706 * sleep state.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200707 */
708static pm_message_t resume_event(pm_message_t sleep_state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400709{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200710 switch (sleep_state.event) {
711 case PM_EVENT_SUSPEND:
712 return PMSG_RESUME;
713 case PM_EVENT_FREEZE:
714 case PM_EVENT_QUIESCE:
715 return PMSG_RECOVER;
716 case PM_EVENT_HIBERNATE:
717 return PMSG_RESTORE;
Alan Sterncd59abf2007-09-21 15:36:56 -0400718 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200719 return PMSG_ON;
Alan Sterncd59abf2007-09-21 15:36:56 -0400720}
721
722/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200723 * device_suspend_noirq - Execute a "late suspend" callback for given device.
724 * @dev: Device to handle.
725 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100726 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200727 * The driver of @dev will not receive interrupts while this function is being
728 * executed.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100729 */
Alan Sternd1616302009-05-24 22:05:42 +0200730static int device_suspend_noirq(struct device *dev, pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100731{
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100732 int error;
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100733
734 if (dev->type && dev->type->pm) {
735 pm_dev_dbg(dev, state, "LATE type ");
736 error = pm_noirq_op(dev, dev->type->pm, state);
737 if (error)
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100738 return error;
739 } else if (dev->class && dev->class->pm) {
740 pm_dev_dbg(dev, state, "LATE class ");
741 error = pm_noirq_op(dev, dev->class->pm, state);
742 if (error)
743 return error;
744 } else if (dev->bus && dev->bus->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200745 pm_dev_dbg(dev, state, "LATE ");
746 error = pm_noirq_op(dev, dev->bus->pm, state);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100747 if (error)
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100748 return error;
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100749 }
750
751 if (dev->pwr_domain) {
752 pm_dev_dbg(dev, state, "LATE power domain ");
753 pm_noirq_op(dev, &dev->pwr_domain->ops, state);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100754 }
Dominik Brodowskie7176a32010-03-15 21:43:11 +0100755
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100756 return 0;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100757}
758
759/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200760 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
761 * @state: PM transition of the system being carried out.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100762 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200763 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
764 * handlers for all non-sysdev devices.
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100765 */
Alan Sternd1616302009-05-24 22:05:42 +0200766int dpm_suspend_noirq(pm_message_t state)
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100767{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100768 ktime_t starttime = ktime_get();
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100769 int error = 0;
770
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100771 suspend_device_irqs();
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200772 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100773 while (!list_empty(&dpm_suspended_list)) {
774 struct device *dev = to_device(dpm_suspended_list.prev);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100775
776 get_device(dev);
777 mutex_unlock(&dpm_list_mtx);
778
Alan Sternd1616302009-05-24 22:05:42 +0200779 error = device_suspend_noirq(dev, state);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100780
781 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100782 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200783 pm_dev_err(dev, state, " late", error);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100784 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100785 break;
786 }
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100787 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100788 list_move(&dev->power.entry, &dpm_noirq_list);
Rafael J. Wysockid08a5ac2010-11-11 01:50:53 +0100789 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100790 }
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200791 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100792 if (error)
Alan Sternd1616302009-05-24 22:05:42 +0200793 dpm_resume_noirq(resume_event(state));
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100794 else
795 dpm_show_time(starttime, state, "late");
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100796 return error;
797}
Alan Sternd1616302009-05-24 22:05:42 +0200798EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100799
800/**
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100801 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
Randy Dunlap0a884222010-01-08 14:42:57 -0800802 * @dev: Device to suspend.
803 * @state: PM transition of the system being carried out.
804 * @cb: Suspend callback to execute.
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100805 */
806static int legacy_suspend(struct device *dev, pm_message_t state,
807 int (*cb)(struct device *dev, pm_message_t state))
808{
809 int error;
810 ktime_t calltime;
811
812 calltime = initcall_debug_start(dev);
813
814 error = cb(dev, state);
815 suspend_report_result(cb, error);
816
817 initcall_debug_report(dev, calltime, error);
818
819 return error;
820}
821
822/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200823 * device_suspend - Execute "suspend" callbacks for given device.
824 * @dev: Device to handle.
825 * @state: PM transition of the system being carried out.
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100826 * @async: If true, the device is being suspended asynchronously.
Alan Sterncd59abf2007-09-21 15:36:56 -0400827 */
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100828static int __device_suspend(struct device *dev, pm_message_t state, bool async)
Alan Sterncd59abf2007-09-21 15:36:56 -0400829{
830 int error = 0;
831
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100832 dpm_wait_for_children(dev, async);
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800833 device_lock(dev);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100834
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100835 if (async_error)
836 goto End;
837
Rafael J. Wysockid83f9052010-12-03 23:14:26 +0100838 if (pm_wakeup_pending()) {
839 async_error = -EBUSY;
840 goto End;
841 }
842
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100843 if (dev->type && dev->type->pm) {
844 pm_dev_dbg(dev, state, "type ");
845 error = pm_op(dev, dev->type->pm, state);
846 goto Domain;
847 }
848
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200849 if (dev->class) {
850 if (dev->class->pm) {
851 pm_dev_dbg(dev, state, "class ");
852 error = pm_op(dev, dev->class->pm, state);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100853 goto Domain;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200854 } else if (dev->class->suspend) {
855 pm_dev_dbg(dev, state, "legacy class ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100856 error = legacy_suspend(dev, state, dev->class->suspend);
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100857 goto Domain;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200858 }
Alan Sterncd59abf2007-09-21 15:36:56 -0400859 }
860
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200861 if (dev->bus) {
862 if (dev->bus->pm) {
863 pm_dev_dbg(dev, state, "");
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200864 error = pm_op(dev, dev->bus->pm, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200865 } else if (dev->bus->suspend) {
866 pm_dev_dbg(dev, state, "legacy ");
Rafael J. Wysocki875ab0b2009-12-18 01:57:31 +0100867 error = legacy_suspend(dev, state, dev->bus->suspend);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200868 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100869 }
870
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100871 Domain:
872 if (!error && dev->pwr_domain) {
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100873 pm_dev_dbg(dev, state, "power domain ");
874 pm_op(dev, &dev->pwr_domain->ops, state);
Alan Sterncd59abf2007-09-21 15:36:56 -0400875 }
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100876
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200877 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800878 device_unlock(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100879 complete_all(&dev->power.completion);
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100880
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200881 if (error)
882 async_error = error;
883
Alan Sterncd59abf2007-09-21 15:36:56 -0400884 return error;
885}
886
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100887static void async_suspend(void *data, async_cookie_t cookie)
888{
889 struct device *dev = (struct device *)data;
890 int error;
891
892 error = __device_suspend(dev, pm_transition, true);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +0200893 if (error)
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100894 pm_dev_err(dev, pm_transition, " async", error);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100895
896 put_device(dev);
897}
898
899static int device_suspend(struct device *dev)
900{
901 INIT_COMPLETION(dev->power.completion);
902
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100903 if (pm_async_enabled && dev->power.async_suspend) {
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100904 get_device(dev);
905 async_schedule(async_suspend, dev);
906 return 0;
907 }
908
909 return __device_suspend(dev, pm_transition, false);
910}
911
Alan Sterncd59abf2007-09-21 15:36:56 -0400912/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200913 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
914 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -0400915 */
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100916static int dpm_suspend(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -0400917{
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100918 ktime_t starttime = ktime_get();
Alan Sterncd59abf2007-09-21 15:36:56 -0400919 int error = 0;
920
Alan Sterncd59abf2007-09-21 15:36:56 -0400921 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100922 pm_transition = state;
923 async_error = 0;
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100924 while (!list_empty(&dpm_prepared_list)) {
925 struct device *dev = to_device(dpm_prepared_list.prev);
Alan Sterncd59abf2007-09-21 15:36:56 -0400926
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200927 get_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100928 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200929
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100930 error = device_suspend(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200931
Alan Stern1b3cbec2008-02-29 11:50:22 -0500932 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100933 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200934 pm_dev_err(dev, state, "", error);
935 put_device(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100936 break;
937 }
Rafael J. Wysocki7a8d37a2008-02-25 00:35:04 +0100938 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +0100939 list_move(&dev->power.entry, &dpm_suspended_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200940 put_device(dev);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100941 if (async_error)
942 break;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100943 }
944 mutex_unlock(&dpm_list_mtx);
Rafael J. Wysocki5af84b82010-01-23 22:23:32 +0100945 async_synchronize_full();
946 if (!error)
947 error = async_error;
Rafael J. Wysockiecf762b2009-12-18 01:57:47 +0100948 if (!error)
949 dpm_show_time(starttime, state, NULL);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200950 return error;
951}
952
953/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +0200954 * device_prepare - Prepare a device for system power transition.
955 * @dev: Device to handle.
956 * @state: PM transition of the system being carried out.
957 *
958 * Execute the ->prepare() callback(s) for given device. No new children of the
959 * device may be registered after this function has returned.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200960 */
Alan Sternd1616302009-05-24 22:05:42 +0200961static int device_prepare(struct device *dev, pm_message_t state)
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200962{
963 int error = 0;
964
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800965 device_lock(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200966
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100967 if (dev->type && dev->type->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200968 pm_dev_dbg(dev, state, "preparing type ");
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100969 if (dev->type->pm->prepare)
970 error = dev->type->pm->prepare(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200971 suspend_report_result(dev->type->pm->prepare, error);
972 if (error)
973 goto End;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100974 } else if (dev->class && dev->class->pm) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200975 pm_dev_dbg(dev, state, "preparing class ");
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100976 if (dev->class->pm->prepare)
977 error = dev->class->pm->prepare(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200978 suspend_report_result(dev->class->pm->prepare, error);
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100979 if (error)
980 goto End;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100981 } else if (dev->bus && dev->bus->pm) {
982 pm_dev_dbg(dev, state, "preparing ");
983 if (dev->bus->pm->prepare)
984 error = dev->bus->pm->prepare(dev);
985 suspend_report_result(dev->bus->pm->prepare, error);
986 if (error)
987 goto End;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200988 }
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100989
990 if (dev->pwr_domain && dev->pwr_domain->ops.prepare) {
991 pm_dev_dbg(dev, state, "preparing power domain ");
992 dev->pwr_domain->ops.prepare(dev);
993 }
994
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200995 End:
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800996 device_unlock(dev);
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +0100997
998 return error;
999}
1000
1001/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001002 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1003 * @state: PM transition of the system being carried out.
Alan Sterncd59abf2007-09-21 15:36:56 -04001004 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001005 * Execute the ->prepare() callback(s) for all devices.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001006 */
1007static int dpm_prepare(pm_message_t state)
1008{
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001009 int error = 0;
1010
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001011 mutex_lock(&dpm_list_mtx);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001012 while (!list_empty(&dpm_list)) {
1013 struct device *dev = to_device(dpm_list.next);
1014
1015 get_device(dev);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001016 mutex_unlock(&dpm_list_mtx);
1017
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001018 pm_runtime_get_noresume(dev);
Rafael J. Wysocki1e75227e2010-12-03 22:58:05 +01001019 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1020 pm_wakeup_event(dev, 0);
1021
Rafael J. Wysockie8665002011-02-12 01:42:41 +01001022 pm_runtime_put_sync(dev);
1023 error = pm_wakeup_pending() ?
1024 -EBUSY : device_prepare(dev, state);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001025
1026 mutex_lock(&dpm_list_mtx);
1027 if (error) {
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001028 if (error == -EAGAIN) {
1029 put_device(dev);
Sebastian Ott886a7a32009-07-08 13:26:05 +02001030 error = 0;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001031 continue;
1032 }
Rafael J. Wysocki1e75227e2010-12-03 22:58:05 +01001033 printk(KERN_INFO "PM: Device %s not prepared "
1034 "for power transition: code %d\n",
Rafael J. Wysocki5c1a07a2010-12-24 15:03:34 +01001035 dev_name(dev), error);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001036 put_device(dev);
1037 break;
1038 }
Rafael J. Wysockib8c76f62010-12-16 00:51:21 +01001039 dev->power.in_suspend = true;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001040 if (!list_empty(&dev->power.entry))
Rafael J. Wysocki8a43a9a2010-12-16 00:50:30 +01001041 list_move_tail(&dev->power.entry, &dpm_prepared_list);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001042 put_device(dev);
1043 }
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001044 mutex_unlock(&dpm_list_mtx);
1045 return error;
1046}
1047
1048/**
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001049 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1050 * @state: PM transition of the system being carried out.
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001051 *
Rafael J. Wysocki20d652d2009-08-20 20:25:52 +02001052 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1053 * callbacks for them.
Alan Sterncd59abf2007-09-21 15:36:56 -04001054 */
Alan Sternd1616302009-05-24 22:05:42 +02001055int dpm_suspend_start(pm_message_t state)
Alan Sterncd59abf2007-09-21 15:36:56 -04001056{
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001057 int error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001058
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001059 might_sleep();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +02001060 error = dpm_prepare(state);
1061 if (!error)
1062 error = dpm_suspend(state);
Alan Sterncd59abf2007-09-21 15:36:56 -04001063 return error;
Alan Sterncd59abf2007-09-21 15:36:56 -04001064}
Alan Sternd1616302009-05-24 22:05:42 +02001065EXPORT_SYMBOL_GPL(dpm_suspend_start);
Alan Sterncd59abf2007-09-21 15:36:56 -04001066
1067void __suspend_report_result(const char *function, void *fn, int ret)
1068{
Bjorn Helgaasc80cfb02008-10-15 22:01:35 -07001069 if (ret)
1070 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
Alan Sterncd59abf2007-09-21 15:36:56 -04001071}
1072EXPORT_SYMBOL_GPL(__suspend_report_result);
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001073
1074/**
1075 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1076 * @dev: Device to wait for.
1077 * @subordinate: Device that needs to wait for @dev.
1078 */
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001079int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001080{
1081 dpm_wait(dev, subordinate->power.async_suspend);
Rafael J. Wysocki098dff72010-09-22 22:10:57 +02001082 return async_error;
Rafael J. Wysockif8824ce2010-01-27 23:47:38 +01001083}
1084EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);