blob: aecb2a887ed7e0aeb31834167a612a16aad90bcd [file] [log] [blame]
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001/*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02002 * drivers/base/power/runtime.c - Helper functions for device runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02003 *
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Alan Stern1bfee5b2010-09-25 23:35:00 +02005 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02006 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/sched.h>
11#include <linux/pm_runtime.h>
Alan Stern7490e442010-09-25 23:35:15 +020012#include "power.h"
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020013
Alan Stern140a6c92010-09-25 23:35:07 +020014static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020015static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020016
17/**
Alan Stern47693732010-09-25 23:34:46 +020018 * update_pm_runtime_accounting - Update the time accounting of power states
19 * @dev: Device to update the accounting for
20 *
21 * In order to be able to have time accounting of the various power states
22 * (as used by programs such as PowerTOP to show the effectiveness of runtime
23 * PM), we need to track the time spent in each state.
24 * update_pm_runtime_accounting must be called each time before the
25 * runtime_status field is updated, to account the time in the old state
26 * correctly.
27 */
28void update_pm_runtime_accounting(struct device *dev)
29{
30 unsigned long now = jiffies;
31 int delta;
32
33 delta = now - dev->power.accounting_timestamp;
34
35 if (delta < 0)
36 delta = 0;
37
38 dev->power.accounting_timestamp = now;
39
40 if (dev->power.disable_depth > 0)
41 return;
42
43 if (dev->power.runtime_status == RPM_SUSPENDED)
44 dev->power.suspended_jiffies += delta;
45 else
46 dev->power.active_jiffies += delta;
47}
48
49static void __update_runtime_status(struct device *dev, enum rpm_status status)
50{
51 update_pm_runtime_accounting(dev);
52 dev->power.runtime_status = status;
53}
54
55/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020056 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
57 * @dev: Device to handle.
58 */
59static void pm_runtime_deactivate_timer(struct device *dev)
60{
61 if (dev->power.timer_expires > 0) {
62 del_timer(&dev->power.suspend_timer);
63 dev->power.timer_expires = 0;
64 }
65}
66
67/**
68 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
69 * @dev: Device to handle.
70 */
71static void pm_runtime_cancel_pending(struct device *dev)
72{
73 pm_runtime_deactivate_timer(dev);
74 /*
75 * In case there's a request pending, make sure its work function will
76 * return without doing anything.
77 */
78 dev->power.request = RPM_REQ_NONE;
79}
80
Alan Stern15bcb912010-09-25 23:35:21 +020081/*
82 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
83 * @dev: Device to handle.
84 *
85 * Compute the autosuspend-delay expiration time based on the device's
86 * power.last_busy time. If the delay has already expired or is disabled
87 * (negative) or the power.use_autosuspend flag isn't set, return 0.
88 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
89 *
90 * This function may be called either with or without dev->power.lock held.
91 * Either way it can be racy, since power.last_busy may be updated at any time.
92 */
93unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
94{
95 int autosuspend_delay;
96 long elapsed;
97 unsigned long last_busy;
98 unsigned long expires = 0;
99
100 if (!dev->power.use_autosuspend)
101 goto out;
102
103 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
104 if (autosuspend_delay < 0)
105 goto out;
106
107 last_busy = ACCESS_ONCE(dev->power.last_busy);
108 elapsed = jiffies - last_busy;
109 if (elapsed < 0)
110 goto out; /* jiffies has wrapped around. */
111
112 /*
113 * If the autosuspend_delay is >= 1 second, align the timer by rounding
114 * up to the nearest second.
115 */
116 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
117 if (autosuspend_delay >= 1000)
118 expires = round_jiffies(expires);
119 expires += !expires;
120 if (elapsed >= expires - last_busy)
121 expires = 0; /* Already expired. */
122
123 out:
124 return expires;
125}
126EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
127
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200128/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200129 * rpm_check_suspend_allowed - Test whether a device may be suspended.
130 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200131 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200132static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200133{
134 int retval = 0;
135
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200136 if (dev->power.runtime_error)
137 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200138 else if (dev->power.disable_depth > 0)
139 retval = -EACCES;
140 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200141 retval = -EAGAIN;
142 else if (!pm_children_suspended(dev))
143 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200144
145 /* Pending resume requests take precedence over suspends. */
146 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200147 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200148 || (dev->power.request_pending
149 && dev->power.request == RPM_REQ_RESUME))
150 retval = -EAGAIN;
151 else if (dev->power.runtime_status == RPM_SUSPENDED)
152 retval = 1;
153
154 return retval;
155}
156
Alan Stern1bfee5b2010-09-25 23:35:00 +0200157/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200158 * __rpm_callback - Run a given runtime PM callback for a given device.
159 * @cb: Runtime PM callback to run.
160 * @dev: Device to run the callback for.
161 */
162static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
163 __releases(&dev->power.lock) __acquires(&dev->power.lock)
164{
165 int retval;
166
167 if (dev->power.irq_safe)
168 spin_unlock(&dev->power.lock);
169 else
170 spin_unlock_irq(&dev->power.lock);
171
172 retval = cb(dev);
173
174 if (dev->power.irq_safe)
175 spin_lock(&dev->power.lock);
176 else
177 spin_lock_irq(&dev->power.lock);
178
179 return retval;
180}
181
182/**
Alan Stern140a6c92010-09-25 23:35:07 +0200183 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200184 * @dev: Device to notify the bus type about.
185 * @rpmflags: Flag bits.
186 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200187 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200188 * another idle notification has been started earlier, return immediately. If
189 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
190 * run the ->runtime_idle() callback directly.
191 *
192 * This function must be called under dev->power.lock with interrupts disabled.
193 */
Alan Stern140a6c92010-09-25 23:35:07 +0200194static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200195{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200196 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200197 int retval;
198
199 retval = rpm_check_suspend_allowed(dev);
200 if (retval < 0)
201 ; /* Conditions are wrong. */
202
203 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
204 else if (dev->power.runtime_status != RPM_ACTIVE)
205 retval = -EAGAIN;
206
207 /*
208 * Any pending request other than an idle notification takes
209 * precedence over us, except that the timer may be running.
210 */
211 else if (dev->power.request_pending &&
212 dev->power.request > RPM_REQ_IDLE)
213 retval = -EAGAIN;
214
215 /* Act as though RPM_NOWAIT is always set. */
216 else if (dev->power.idle_notification)
217 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200218 if (retval)
219 goto out;
220
Alan Stern1bfee5b2010-09-25 23:35:00 +0200221 /* Pending requests need to be canceled. */
222 dev->power.request = RPM_REQ_NONE;
223
Alan Stern7490e442010-09-25 23:35:15 +0200224 if (dev->power.no_callbacks) {
225 /* Assume ->runtime_idle() callback would have suspended. */
226 retval = rpm_suspend(dev, rpmflags);
227 goto out;
228 }
229
Alan Stern1bfee5b2010-09-25 23:35:00 +0200230 /* Carry out an asynchronous or a synchronous idle notification. */
231 if (rpmflags & RPM_ASYNC) {
232 dev->power.request = RPM_REQ_IDLE;
233 if (!dev->power.request_pending) {
234 dev->power.request_pending = true;
235 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200236 }
Alan Stern1bfee5b2010-09-25 23:35:00 +0200237 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200238 }
239
240 dev->power.idle_notification = true;
241
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200242 if (dev->pm_domain)
243 callback = dev->pm_domain->ops.runtime_idle;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200244 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200245 callback = dev->type->pm->runtime_idle;
246 else if (dev->class && dev->class->pm)
247 callback = dev->class->pm->runtime_idle;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100248 else if (dev->bus && dev->bus->pm)
249 callback = dev->bus->pm->runtime_idle;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200250 else
251 callback = NULL;
252
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200253 if (callback)
254 __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200255
256 dev->power.idle_notification = false;
257 wake_up_all(&dev->power.wait_queue);
258
259 out:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200260 return retval;
261}
262
263/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200264 * rpm_callback - Run a given runtime PM callback for a given device.
265 * @cb: Runtime PM callback to run.
266 * @dev: Device to run the callback for.
267 */
268static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200269{
270 int retval;
271
272 if (!cb)
273 return -ENOSYS;
274
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200275 retval = __rpm_callback(cb, dev);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200276
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200277 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200278 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200279}
280
281/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200282 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200283 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200284 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200285 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200286 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200287 * another suspend has been started earlier, either return immediately or wait
288 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
289 * pending idle notification. If the RPM_ASYNC flag is set then queue a
290 * suspend request; otherwise run the ->runtime_suspend() callback directly.
291 * If a deferred resume was requested while the callback was running then carry
292 * it out; otherwise send an idle notification for the device (if the suspend
293 * failed) or for its parent (if the suspend succeeded).
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200294 *
295 * This function must be called under dev->power.lock with interrupts disabled.
296 */
Alan Stern140a6c92010-09-25 23:35:07 +0200297static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200298 __releases(&dev->power.lock) __acquires(&dev->power.lock)
299{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200300 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200301 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200302 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200303
Alan Stern3f9af052010-09-25 23:34:54 +0200304 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200305
306 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200307 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308
Alan Stern1bfee5b2010-09-25 23:35:00 +0200309 if (retval < 0)
310 ; /* Conditions are wrong. */
311
312 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
313 else if (dev->power.runtime_status == RPM_RESUMING &&
314 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200315 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200316 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200317 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200318
Alan Stern15bcb912010-09-25 23:35:21 +0200319 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
320 if ((rpmflags & RPM_AUTO)
321 && dev->power.runtime_status != RPM_SUSPENDING) {
322 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
323
324 if (expires != 0) {
325 /* Pending requests need to be canceled. */
326 dev->power.request = RPM_REQ_NONE;
327
328 /*
329 * Optimization: If the timer is already running and is
330 * set to expire at or before the autosuspend delay,
331 * avoid the overhead of resetting it. Just let it
332 * expire; pm_suspend_timer_fn() will take care of the
333 * rest.
334 */
335 if (!(dev->power.timer_expires && time_before_eq(
336 dev->power.timer_expires, expires))) {
337 dev->power.timer_expires = expires;
338 mod_timer(&dev->power.suspend_timer, expires);
339 }
340 dev->power.timer_autosuspends = 1;
341 goto out;
342 }
343 }
344
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200345 /* Other scheduled or pending requests need to be canceled. */
346 pm_runtime_cancel_pending(dev);
347
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200348 if (dev->power.runtime_status == RPM_SUSPENDING) {
349 DEFINE_WAIT(wait);
350
Alan Stern1bfee5b2010-09-25 23:35:00 +0200351 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200352 retval = -EINPROGRESS;
353 goto out;
354 }
355
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200356 if (dev->power.irq_safe) {
357 spin_unlock(&dev->power.lock);
358
359 cpu_relax();
360
361 spin_lock(&dev->power.lock);
362 goto repeat;
363 }
364
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200365 /* Wait for the other suspend running in parallel with us. */
366 for (;;) {
367 prepare_to_wait(&dev->power.wait_queue, &wait,
368 TASK_UNINTERRUPTIBLE);
369 if (dev->power.runtime_status != RPM_SUSPENDING)
370 break;
371
372 spin_unlock_irq(&dev->power.lock);
373
374 schedule();
375
376 spin_lock_irq(&dev->power.lock);
377 }
378 finish_wait(&dev->power.wait_queue, &wait);
379 goto repeat;
380 }
381
Alan Stern7490e442010-09-25 23:35:15 +0200382 dev->power.deferred_resume = false;
383 if (dev->power.no_callbacks)
384 goto no_callback; /* Assume success. */
385
Alan Stern1bfee5b2010-09-25 23:35:00 +0200386 /* Carry out an asynchronous or a synchronous suspend. */
387 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb912010-09-25 23:35:21 +0200388 dev->power.request = (rpmflags & RPM_AUTO) ?
389 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200390 if (!dev->power.request_pending) {
391 dev->power.request_pending = true;
392 queue_work(pm_wq, &dev->power.work);
393 }
394 goto out;
395 }
396
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200397 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200398
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200399 if (dev->pm_domain)
400 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200401 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200402 callback = dev->type->pm->runtime_suspend;
403 else if (dev->class && dev->class->pm)
404 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100405 else if (dev->bus && dev->bus->pm)
406 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200407 else
408 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200409
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200410 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200411 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200412 __update_runtime_status(dev, RPM_ACTIVE);
ShuoX Liu2cffff12011-07-08 20:53:55 +0200413 dev->power.deferred_resume = false;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200414 if (retval == -EAGAIN || retval == -EBUSY)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200415 dev->power.runtime_error = 0;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200416 else
Alan Stern240c7332010-03-23 00:50:07 +0100417 pm_runtime_cancel_pending(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200418 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200419 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200420 __update_runtime_status(dev, RPM_SUSPENDED);
Alan Stern240c7332010-03-23 00:50:07 +0100421 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200422
423 if (dev->parent) {
424 parent = dev->parent;
425 atomic_add_unless(&parent->power.child_count, -1, 0);
426 }
427 }
428 wake_up_all(&dev->power.wait_queue);
429
430 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200431 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200432 retval = -EAGAIN;
433 goto out;
434 }
435
Alan Sternc3810c82011-01-25 20:50:07 +0100436 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100437 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100438 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200439
Alan Sternc3810c82011-01-25 20:50:07 +0100440 spin_lock(&parent->power.lock);
441 rpm_idle(parent, RPM_ASYNC);
442 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200443
Alan Sternc3810c82011-01-25 20:50:07 +0100444 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200445 }
446
447 out:
Alan Stern3f9af052010-09-25 23:34:54 +0200448 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200449
450 return retval;
451}
452
453/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200454 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200455 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200456 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200457 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200458 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200459 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300460 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200461 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
462 * parallel with this function, either tell the other process to resume after
463 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
464 * flag is set then queue a resume request; otherwise run the
465 * ->runtime_resume() callback directly. Queue an idle notification for the
466 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200467 *
468 * This function must be called under dev->power.lock with interrupts disabled.
469 */
Alan Stern140a6c92010-09-25 23:35:07 +0200470static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200471 __releases(&dev->power.lock) __acquires(&dev->power.lock)
472{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200473 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200474 struct device *parent = NULL;
475 int retval = 0;
476
Alan Stern3f9af052010-09-25 23:34:54 +0200477 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200478
479 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200480 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200481 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200482 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200483 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200484 if (retval)
485 goto out;
486
Alan Stern15bcb912010-09-25 23:35:21 +0200487 /*
488 * Other scheduled or pending requests need to be canceled. Small
489 * optimization: If an autosuspend timer is running, leave it running
490 * rather than cancelling it now only to restart it again in the near
491 * future.
492 */
493 dev->power.request = RPM_REQ_NONE;
494 if (!dev->power.timer_autosuspends)
495 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200496
497 if (dev->power.runtime_status == RPM_ACTIVE) {
498 retval = 1;
499 goto out;
500 }
501
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200502 if (dev->power.runtime_status == RPM_RESUMING
503 || dev->power.runtime_status == RPM_SUSPENDING) {
504 DEFINE_WAIT(wait);
505
Alan Stern1bfee5b2010-09-25 23:35:00 +0200506 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200507 if (dev->power.runtime_status == RPM_SUSPENDING)
508 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200509 else
510 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200511 goto out;
512 }
513
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200514 if (dev->power.irq_safe) {
515 spin_unlock(&dev->power.lock);
516
517 cpu_relax();
518
519 spin_lock(&dev->power.lock);
520 goto repeat;
521 }
522
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200523 /* Wait for the operation carried out in parallel with us. */
524 for (;;) {
525 prepare_to_wait(&dev->power.wait_queue, &wait,
526 TASK_UNINTERRUPTIBLE);
527 if (dev->power.runtime_status != RPM_RESUMING
528 && dev->power.runtime_status != RPM_SUSPENDING)
529 break;
530
531 spin_unlock_irq(&dev->power.lock);
532
533 schedule();
534
535 spin_lock_irq(&dev->power.lock);
536 }
537 finish_wait(&dev->power.wait_queue, &wait);
538 goto repeat;
539 }
540
Alan Stern7490e442010-09-25 23:35:15 +0200541 /*
542 * See if we can skip waking up the parent. This is safe only if
543 * power.no_callbacks is set, because otherwise we don't know whether
544 * the resume will actually succeed.
545 */
546 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200547 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200548 if (dev->parent->power.disable_depth > 0
549 || dev->parent->power.ignore_children
550 || dev->parent->power.runtime_status == RPM_ACTIVE) {
551 atomic_inc(&dev->parent->power.child_count);
552 spin_unlock(&dev->parent->power.lock);
553 goto no_callback; /* Assume success. */
554 }
555 spin_unlock(&dev->parent->power.lock);
556 }
557
Alan Stern1bfee5b2010-09-25 23:35:00 +0200558 /* Carry out an asynchronous or a synchronous resume. */
559 if (rpmflags & RPM_ASYNC) {
560 dev->power.request = RPM_REQ_RESUME;
561 if (!dev->power.request_pending) {
562 dev->power.request_pending = true;
563 queue_work(pm_wq, &dev->power.work);
564 }
565 retval = 0;
566 goto out;
567 }
568
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200569 if (!parent && dev->parent) {
570 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100571 * Increment the parent's usage counter and resume it if
572 * necessary. Not needed if dev is irq-safe; then the
573 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200574 */
575 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100576 if (dev->power.irq_safe)
577 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100578 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200579
580 pm_runtime_get_noresume(parent);
581
Alan Stern862f89b2009-11-25 01:06:37 +0100582 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200583 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200584 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200585 * is set to ignore children.
586 */
587 if (!parent->power.disable_depth
588 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200589 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200590 if (parent->power.runtime_status != RPM_ACTIVE)
591 retval = -EBUSY;
592 }
Alan Stern862f89b2009-11-25 01:06:37 +0100593 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200594
Alan Stern862f89b2009-11-25 01:06:37 +0100595 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200596 if (retval)
597 goto out;
598 goto repeat;
599 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100600 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200601
Alan Stern7490e442010-09-25 23:35:15 +0200602 if (dev->power.no_callbacks)
603 goto no_callback; /* Assume success. */
604
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200605 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200606
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200607 if (dev->pm_domain)
608 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200609 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200610 callback = dev->type->pm->runtime_resume;
611 else if (dev->class && dev->class->pm)
612 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100613 else if (dev->bus && dev->bus->pm)
614 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200615 else
616 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200617
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200618 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200619 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200620 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200621 pm_runtime_cancel_pending(dev);
622 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200623 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200624 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200625 if (parent)
626 atomic_inc(&parent->power.child_count);
627 }
628 wake_up_all(&dev->power.wait_queue);
629
630 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200631 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200632
633 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100634 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200635 spin_unlock_irq(&dev->power.lock);
636
637 pm_runtime_put(parent);
638
639 spin_lock_irq(&dev->power.lock);
640 }
641
Alan Stern3f9af052010-09-25 23:34:54 +0200642 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200643
644 return retval;
645}
646
647/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200648 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200649 * @work: Work structure used for scheduling the execution of this function.
650 *
651 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200652 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200653 */
654static void pm_runtime_work(struct work_struct *work)
655{
656 struct device *dev = container_of(work, struct device, power.work);
657 enum rpm_request req;
658
659 spin_lock_irq(&dev->power.lock);
660
661 if (!dev->power.request_pending)
662 goto out;
663
664 req = dev->power.request;
665 dev->power.request = RPM_REQ_NONE;
666 dev->power.request_pending = false;
667
668 switch (req) {
669 case RPM_REQ_NONE:
670 break;
671 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200672 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200673 break;
674 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200675 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200676 break;
Alan Stern15bcb912010-09-25 23:35:21 +0200677 case RPM_REQ_AUTOSUSPEND:
678 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
679 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200680 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200681 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200682 break;
683 }
684
685 out:
686 spin_unlock_irq(&dev->power.lock);
687}
688
689/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200690 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
691 * @data: Device pointer passed by pm_schedule_suspend().
692 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200693 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200694 */
695static void pm_suspend_timer_fn(unsigned long data)
696{
697 struct device *dev = (struct device *)data;
698 unsigned long flags;
699 unsigned long expires;
700
701 spin_lock_irqsave(&dev->power.lock, flags);
702
703 expires = dev->power.timer_expires;
704 /* If 'expire' is after 'jiffies' we've been called too early. */
705 if (expires > 0 && !time_after(expires, jiffies)) {
706 dev->power.timer_expires = 0;
Alan Stern15bcb912010-09-25 23:35:21 +0200707 rpm_suspend(dev, dev->power.timer_autosuspends ?
708 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200709 }
710
711 spin_unlock_irqrestore(&dev->power.lock, flags);
712}
713
714/**
715 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
716 * @dev: Device to suspend.
717 * @delay: Time to wait before submitting a suspend request, in milliseconds.
718 */
719int pm_schedule_suspend(struct device *dev, unsigned int delay)
720{
721 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200722 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200723
724 spin_lock_irqsave(&dev->power.lock, flags);
725
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200726 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200727 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200728 goto out;
729 }
730
Alan Stern1bfee5b2010-09-25 23:35:00 +0200731 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200732 if (retval)
733 goto out;
734
Alan Stern1bfee5b2010-09-25 23:35:00 +0200735 /* Other scheduled or pending requests need to be canceled. */
736 pm_runtime_cancel_pending(dev);
737
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200738 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200739 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb912010-09-25 23:35:21 +0200740 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200741 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
742
743 out:
744 spin_unlock_irqrestore(&dev->power.lock, flags);
745
746 return retval;
747}
748EXPORT_SYMBOL_GPL(pm_schedule_suspend);
749
750/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200751 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200752 * @dev: Device to send idle notification for.
753 * @rpmflags: Flag bits.
754 *
755 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
756 * return immediately if it is larger than zero. Then carry out an idle
757 * notification, either synchronous or asynchronous.
758 *
Colin Cross311aab72011-08-08 23:39:36 +0200759 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
760 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200761 */
Alan Stern140a6c92010-09-25 23:35:07 +0200762int __pm_runtime_idle(struct device *dev, int rpmflags)
763{
764 unsigned long flags;
765 int retval;
766
Colin Cross311aab72011-08-08 23:39:36 +0200767 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
768
Alan Stern140a6c92010-09-25 23:35:07 +0200769 if (rpmflags & RPM_GET_PUT) {
770 if (!atomic_dec_and_test(&dev->power.usage_count))
771 return 0;
772 }
773
774 spin_lock_irqsave(&dev->power.lock, flags);
775 retval = rpm_idle(dev, rpmflags);
776 spin_unlock_irqrestore(&dev->power.lock, flags);
777
778 return retval;
779}
780EXPORT_SYMBOL_GPL(__pm_runtime_idle);
781
782/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200783 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200784 * @dev: Device to suspend.
785 * @rpmflags: Flag bits.
786 *
Alan Stern15bcb912010-09-25 23:35:21 +0200787 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
788 * return immediately if it is larger than zero. Then carry out a suspend,
789 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200790 *
Colin Cross311aab72011-08-08 23:39:36 +0200791 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
792 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200793 */
794int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200795{
796 unsigned long flags;
797 int retval;
798
Colin Cross311aab72011-08-08 23:39:36 +0200799 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
800
Alan Stern15bcb912010-09-25 23:35:21 +0200801 if (rpmflags & RPM_GET_PUT) {
802 if (!atomic_dec_and_test(&dev->power.usage_count))
803 return 0;
804 }
805
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200806 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200807 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200808 spin_unlock_irqrestore(&dev->power.lock, flags);
809
810 return retval;
811}
Alan Stern140a6c92010-09-25 23:35:07 +0200812EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200813
814/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200815 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200816 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200817 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200818 *
Alan Stern140a6c92010-09-25 23:35:07 +0200819 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
820 * carry out a resume, either synchronous or asynchronous.
821 *
Colin Cross311aab72011-08-08 23:39:36 +0200822 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
823 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200824 */
Alan Stern140a6c92010-09-25 23:35:07 +0200825int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200826{
Alan Stern140a6c92010-09-25 23:35:07 +0200827 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100828 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200829
Colin Cross311aab72011-08-08 23:39:36 +0200830 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
831
Alan Stern140a6c92010-09-25 23:35:07 +0200832 if (rpmflags & RPM_GET_PUT)
833 atomic_inc(&dev->power.usage_count);
834
835 spin_lock_irqsave(&dev->power.lock, flags);
836 retval = rpm_resume(dev, rpmflags);
837 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200838
839 return retval;
840}
Alan Stern140a6c92010-09-25 23:35:07 +0200841EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200842
843/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200844 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200845 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200846 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200847 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200848 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200849 * different from zero, the status may be changed either to RPM_ACTIVE, or to
850 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
851 * However, if the device has a parent and the parent is not active, and the
852 * parent's power.ignore_children flag is unset, the device's status cannot be
853 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
854 *
855 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
856 * and the device parent's counter of unsuspended children is modified to
857 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
858 * notification request for the parent is submitted.
859 */
860int __pm_runtime_set_status(struct device *dev, unsigned int status)
861{
862 struct device *parent = dev->parent;
863 unsigned long flags;
864 bool notify_parent = false;
865 int error = 0;
866
867 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
868 return -EINVAL;
869
870 spin_lock_irqsave(&dev->power.lock, flags);
871
872 if (!dev->power.runtime_error && !dev->power.disable_depth) {
873 error = -EAGAIN;
874 goto out;
875 }
876
877 if (dev->power.runtime_status == status)
878 goto out_set;
879
880 if (status == RPM_SUSPENDED) {
881 /* It always is possible to set the status to 'suspended'. */
882 if (parent) {
883 atomic_add_unless(&parent->power.child_count, -1, 0);
884 notify_parent = !parent->power.ignore_children;
885 }
886 goto out_set;
887 }
888
889 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100890 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200891
892 /*
893 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200894 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200895 * 'power.ignore_children' flag unset.
896 */
897 if (!parent->power.disable_depth
898 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100899 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200900 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100901 else if (dev->power.runtime_status == RPM_SUSPENDED)
902 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200903
Alan Stern862f89b2009-11-25 01:06:37 +0100904 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200905
906 if (error)
907 goto out;
908 }
909
910 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200911 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200912 dev->power.runtime_error = 0;
913 out:
914 spin_unlock_irqrestore(&dev->power.lock, flags);
915
916 if (notify_parent)
917 pm_request_idle(parent);
918
919 return error;
920}
921EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
922
923/**
924 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
925 * @dev: Device to handle.
926 *
927 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200928 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200929 *
930 * Should be called under dev->power.lock with interrupts disabled.
931 */
932static void __pm_runtime_barrier(struct device *dev)
933{
934 pm_runtime_deactivate_timer(dev);
935
936 if (dev->power.request_pending) {
937 dev->power.request = RPM_REQ_NONE;
938 spin_unlock_irq(&dev->power.lock);
939
940 cancel_work_sync(&dev->power.work);
941
942 spin_lock_irq(&dev->power.lock);
943 dev->power.request_pending = false;
944 }
945
946 if (dev->power.runtime_status == RPM_SUSPENDING
947 || dev->power.runtime_status == RPM_RESUMING
948 || dev->power.idle_notification) {
949 DEFINE_WAIT(wait);
950
951 /* Suspend, wake-up or idle notification in progress. */
952 for (;;) {
953 prepare_to_wait(&dev->power.wait_queue, &wait,
954 TASK_UNINTERRUPTIBLE);
955 if (dev->power.runtime_status != RPM_SUSPENDING
956 && dev->power.runtime_status != RPM_RESUMING
957 && !dev->power.idle_notification)
958 break;
959 spin_unlock_irq(&dev->power.lock);
960
961 schedule();
962
963 spin_lock_irq(&dev->power.lock);
964 }
965 finish_wait(&dev->power.wait_queue, &wait);
966 }
967}
968
969/**
970 * pm_runtime_barrier - Flush pending requests and wait for completions.
971 * @dev: Device to handle.
972 *
973 * Prevent the device from being suspended by incrementing its usage counter and
974 * if there's a pending resume request for the device, wake the device up.
975 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200976 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200977 * progress to complete.
978 *
979 * Return value:
980 * 1, if there was a resume request pending and the device had to be woken up,
981 * 0, otherwise
982 */
983int pm_runtime_barrier(struct device *dev)
984{
985 int retval = 0;
986
987 pm_runtime_get_noresume(dev);
988 spin_lock_irq(&dev->power.lock);
989
990 if (dev->power.request_pending
991 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200992 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200993 retval = 1;
994 }
995
996 __pm_runtime_barrier(dev);
997
998 spin_unlock_irq(&dev->power.lock);
999 pm_runtime_put_noidle(dev);
1000
1001 return retval;
1002}
1003EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1004
1005/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001006 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001007 * @dev: Device to handle.
1008 * @check_resume: If set, check if there's a resume request for the device.
1009 *
1010 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001011 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001012 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001013 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001014 *
1015 * If @check_resume is set and there's a resume request pending when
1016 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001017 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001018 */
1019void __pm_runtime_disable(struct device *dev, bool check_resume)
1020{
1021 spin_lock_irq(&dev->power.lock);
1022
1023 if (dev->power.disable_depth > 0) {
1024 dev->power.disable_depth++;
1025 goto out;
1026 }
1027
1028 /*
1029 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001030 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001031 * shouldn't prevent the device from processing the I/O.
1032 */
1033 if (check_resume && dev->power.request_pending
1034 && dev->power.request == RPM_REQ_RESUME) {
1035 /*
1036 * Prevent suspends and idle notifications from being carried
1037 * out after we have woken up the device.
1038 */
1039 pm_runtime_get_noresume(dev);
1040
Alan Stern140a6c92010-09-25 23:35:07 +02001041 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001042
1043 pm_runtime_put_noidle(dev);
1044 }
1045
1046 if (!dev->power.disable_depth++)
1047 __pm_runtime_barrier(dev);
1048
1049 out:
1050 spin_unlock_irq(&dev->power.lock);
1051}
1052EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1053
1054/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001055 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001056 * @dev: Device to handle.
1057 */
1058void pm_runtime_enable(struct device *dev)
1059{
1060 unsigned long flags;
1061
1062 spin_lock_irqsave(&dev->power.lock, flags);
1063
1064 if (dev->power.disable_depth > 0)
1065 dev->power.disable_depth--;
1066 else
1067 dev_warn(dev, "Unbalanced %s!\n", __func__);
1068
1069 spin_unlock_irqrestore(&dev->power.lock, flags);
1070}
1071EXPORT_SYMBOL_GPL(pm_runtime_enable);
1072
1073/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001074 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001075 * @dev: Device to handle.
1076 *
1077 * Increase the device's usage count and clear its power.runtime_auto flag,
1078 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1079 * for it.
1080 */
1081void pm_runtime_forbid(struct device *dev)
1082{
1083 spin_lock_irq(&dev->power.lock);
1084 if (!dev->power.runtime_auto)
1085 goto out;
1086
1087 dev->power.runtime_auto = false;
1088 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001089 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001090
1091 out:
1092 spin_unlock_irq(&dev->power.lock);
1093}
1094EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1095
1096/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001097 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001098 * @dev: Device to handle.
1099 *
1100 * Decrease the device's usage count and set its power.runtime_auto flag.
1101 */
1102void pm_runtime_allow(struct device *dev)
1103{
1104 spin_lock_irq(&dev->power.lock);
1105 if (dev->power.runtime_auto)
1106 goto out;
1107
1108 dev->power.runtime_auto = true;
1109 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb912010-09-25 23:35:21 +02001110 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001111
1112 out:
1113 spin_unlock_irq(&dev->power.lock);
1114}
1115EXPORT_SYMBOL_GPL(pm_runtime_allow);
1116
1117/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001118 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001119 * @dev: Device to handle.
1120 *
1121 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001122 * device is power-managed through its parent and has no runtime PM
1123 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001124 */
1125void pm_runtime_no_callbacks(struct device *dev)
1126{
1127 spin_lock_irq(&dev->power.lock);
1128 dev->power.no_callbacks = 1;
1129 spin_unlock_irq(&dev->power.lock);
1130 if (device_is_registered(dev))
1131 rpm_sysfs_remove(dev);
1132}
1133EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1134
1135/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001136 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1137 * @dev: Device to handle
1138 *
1139 * Set the power.irq_safe flag, which tells the PM core that the
1140 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1141 * always be invoked with the spinlock held and interrupts disabled. It also
1142 * causes the parent's usage counter to be permanently incremented, preventing
1143 * the parent from runtime suspending -- otherwise an irq-safe child might have
1144 * to wait for a non-irq-safe parent.
1145 */
1146void pm_runtime_irq_safe(struct device *dev)
1147{
1148 if (dev->parent)
1149 pm_runtime_get_sync(dev->parent);
1150 spin_lock_irq(&dev->power.lock);
1151 dev->power.irq_safe = 1;
1152 spin_unlock_irq(&dev->power.lock);
1153}
1154EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1155
1156/**
Alan Stern15bcb912010-09-25 23:35:21 +02001157 * update_autosuspend - Handle a change to a device's autosuspend settings.
1158 * @dev: Device to handle.
1159 * @old_delay: The former autosuspend_delay value.
1160 * @old_use: The former use_autosuspend value.
1161 *
1162 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1163 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1164 *
1165 * This function must be called under dev->power.lock with interrupts disabled.
1166 */
1167static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1168{
1169 int delay = dev->power.autosuspend_delay;
1170
1171 /* Should runtime suspend be prevented now? */
1172 if (dev->power.use_autosuspend && delay < 0) {
1173
1174 /* If it used to be allowed then prevent it. */
1175 if (!old_use || old_delay >= 0) {
1176 atomic_inc(&dev->power.usage_count);
1177 rpm_resume(dev, 0);
1178 }
1179 }
1180
1181 /* Runtime suspend should be allowed now. */
1182 else {
1183
1184 /* If it used to be prevented then allow it. */
1185 if (old_use && old_delay < 0)
1186 atomic_dec(&dev->power.usage_count);
1187
1188 /* Maybe we can autosuspend now. */
1189 rpm_idle(dev, RPM_AUTO);
1190 }
1191}
1192
1193/**
1194 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1195 * @dev: Device to handle.
1196 * @delay: Value of the new delay in milliseconds.
1197 *
1198 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001199 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1200 * changes the other way, allow runtime suspends.
Alan Stern15bcb912010-09-25 23:35:21 +02001201 */
1202void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1203{
1204 int old_delay, old_use;
1205
1206 spin_lock_irq(&dev->power.lock);
1207 old_delay = dev->power.autosuspend_delay;
1208 old_use = dev->power.use_autosuspend;
1209 dev->power.autosuspend_delay = delay;
1210 update_autosuspend(dev, old_delay, old_use);
1211 spin_unlock_irq(&dev->power.lock);
1212}
1213EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1214
1215/**
1216 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1217 * @dev: Device to handle.
1218 * @use: New value for use_autosuspend.
1219 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001220 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb912010-09-25 23:35:21 +02001221 * suspends as needed.
1222 */
1223void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1224{
1225 int old_delay, old_use;
1226
1227 spin_lock_irq(&dev->power.lock);
1228 old_delay = dev->power.autosuspend_delay;
1229 old_use = dev->power.use_autosuspend;
1230 dev->power.use_autosuspend = use;
1231 update_autosuspend(dev, old_delay, old_use);
1232 spin_unlock_irq(&dev->power.lock);
1233}
1234EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1235
1236/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001237 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001238 * @dev: Device object to initialize.
1239 */
1240void pm_runtime_init(struct device *dev)
1241{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001242 dev->power.runtime_status = RPM_SUSPENDED;
1243 dev->power.idle_notification = false;
1244
1245 dev->power.disable_depth = 1;
1246 atomic_set(&dev->power.usage_count, 0);
1247
1248 dev->power.runtime_error = 0;
1249
1250 atomic_set(&dev->power.child_count, 0);
1251 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001252 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001253
1254 dev->power.request_pending = false;
1255 dev->power.request = RPM_REQ_NONE;
1256 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001257 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001258 INIT_WORK(&dev->power.work, pm_runtime_work);
1259
1260 dev->power.timer_expires = 0;
1261 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1262 (unsigned long)dev);
1263
1264 init_waitqueue_head(&dev->power.wait_queue);
1265}
1266
1267/**
1268 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1269 * @dev: Device object being removed from device hierarchy.
1270 */
1271void pm_runtime_remove(struct device *dev)
1272{
1273 __pm_runtime_disable(dev, false);
1274
1275 /* Change the status back to 'suspended' to match the initial status. */
1276 if (dev->power.runtime_status == RPM_ACTIVE)
1277 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001278 if (dev->power.irq_safe && dev->parent)
1279 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001280}