blob: 0d4587b15c5599372e7b9f917fcea636404bf8d9 [file] [log] [blame]
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001/*
2 * drivers/base/power/runtime.c - Helper functions for device run-time PM
3 *
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 Stern15bcb91d2010-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. Wysocki5e928f72009-08-18 23:38:32 +0200138 else if (atomic_read(&dev->power.usage_count) > 0
Alan Stern1bfee5b2010-09-25 23:35:00 +0200139 || dev->power.disable_depth > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200140 retval = -EAGAIN;
141 else if (!pm_children_suspended(dev))
142 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200143
144 /* Pending resume requests take precedence over suspends. */
145 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200146 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200147 || (dev->power.request_pending
148 && dev->power.request == RPM_REQ_RESUME))
149 retval = -EAGAIN;
150 else if (dev->power.runtime_status == RPM_SUSPENDED)
151 retval = 1;
152
153 return retval;
154}
155
Alan Stern1bfee5b2010-09-25 23:35:00 +0200156/**
Alan Stern140a6c92010-09-25 23:35:07 +0200157 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200158 * @dev: Device to notify the bus type about.
159 * @rpmflags: Flag bits.
160 *
161 * Check if the device's run-time PM status allows it to be suspended. If
162 * another idle notification has been started earlier, return immediately. If
163 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
164 * run the ->runtime_idle() callback directly.
165 *
166 * This function must be called under dev->power.lock with interrupts disabled.
167 */
Alan Stern140a6c92010-09-25 23:35:07 +0200168static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200169{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200170 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200171 int retval;
172
173 retval = rpm_check_suspend_allowed(dev);
174 if (retval < 0)
175 ; /* Conditions are wrong. */
176
177 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
178 else if (dev->power.runtime_status != RPM_ACTIVE)
179 retval = -EAGAIN;
180
181 /*
182 * Any pending request other than an idle notification takes
183 * precedence over us, except that the timer may be running.
184 */
185 else if (dev->power.request_pending &&
186 dev->power.request > RPM_REQ_IDLE)
187 retval = -EAGAIN;
188
189 /* Act as though RPM_NOWAIT is always set. */
190 else if (dev->power.idle_notification)
191 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200192 if (retval)
193 goto out;
194
Alan Stern1bfee5b2010-09-25 23:35:00 +0200195 /* Pending requests need to be canceled. */
196 dev->power.request = RPM_REQ_NONE;
197
Alan Stern7490e442010-09-25 23:35:15 +0200198 if (dev->power.no_callbacks) {
199 /* Assume ->runtime_idle() callback would have suspended. */
200 retval = rpm_suspend(dev, rpmflags);
201 goto out;
202 }
203
Alan Stern1bfee5b2010-09-25 23:35:00 +0200204 /* Carry out an asynchronous or a synchronous idle notification. */
205 if (rpmflags & RPM_ASYNC) {
206 dev->power.request = RPM_REQ_IDLE;
207 if (!dev->power.request_pending) {
208 dev->power.request_pending = true;
209 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200210 }
Alan Stern1bfee5b2010-09-25 23:35:00 +0200211 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200212 }
213
214 dev->power.idle_notification = true;
215
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200216 if (dev->pwr_domain)
217 callback = dev->pwr_domain->ops.runtime_idle;
218 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200219 callback = dev->type->pm->runtime_idle;
220 else if (dev->class && dev->class->pm)
221 callback = dev->class->pm->runtime_idle;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100222 else if (dev->bus && dev->bus->pm)
223 callback = dev->bus->pm->runtime_idle;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200224 else
225 callback = NULL;
226
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200227 if (callback) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200228 spin_unlock_irq(&dev->power.lock);
229
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200230 callback(dev);
Rafael J. Wysockia6ab7aa2009-12-22 20:43:17 +0100231
232 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200233 }
234
235 dev->power.idle_notification = false;
236 wake_up_all(&dev->power.wait_queue);
237
238 out:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200239 return retval;
240}
241
242/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200243 * rpm_callback - Run a given runtime PM callback for a given device.
244 * @cb: Runtime PM callback to run.
245 * @dev: Device to run the callback for.
246 */
247static int rpm_callback(int (*cb)(struct device *), struct device *dev)
248 __releases(&dev->power.lock) __acquires(&dev->power.lock)
249{
250 int retval;
251
252 if (!cb)
253 return -ENOSYS;
254
Alan Sternc7b61de2010-12-01 00:14:42 +0100255 if (dev->power.irq_safe) {
256 retval = cb(dev);
257 } else {
258 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200259
Alan Sternc7b61de2010-12-01 00:14:42 +0100260 retval = cb(dev);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200261
Alan Sternc7b61de2010-12-01 00:14:42 +0100262 spin_lock_irq(&dev->power.lock);
263 }
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200264 dev->power.runtime_error = retval;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200265 return retval;
266}
267
268/**
Alan Stern140a6c92010-09-25 23:35:07 +0200269 * rpm_suspend - Carry out run-time suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200270 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200271 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200272 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200273 * Check if the device's run-time PM status allows it to be suspended. If
274 * another suspend has been started earlier, either return immediately or wait
275 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
276 * pending idle notification. If the RPM_ASYNC flag is set then queue a
277 * suspend request; otherwise run the ->runtime_suspend() callback directly.
278 * If a deferred resume was requested while the callback was running then carry
279 * it out; otherwise send an idle notification for the device (if the suspend
280 * failed) or for its parent (if the suspend succeeded).
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200281 *
282 * This function must be called under dev->power.lock with interrupts disabled.
283 */
Alan Stern140a6c92010-09-25 23:35:07 +0200284static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200285 __releases(&dev->power.lock) __acquires(&dev->power.lock)
286{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200287 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200288 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200289 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200290
Alan Stern3f9af052010-09-25 23:34:54 +0200291 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200292
293 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200294 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200295
Alan Stern1bfee5b2010-09-25 23:35:00 +0200296 if (retval < 0)
297 ; /* Conditions are wrong. */
298
299 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
300 else if (dev->power.runtime_status == RPM_RESUMING &&
301 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200302 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200303 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200304 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200305
Alan Stern15bcb91d2010-09-25 23:35:21 +0200306 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
307 if ((rpmflags & RPM_AUTO)
308 && dev->power.runtime_status != RPM_SUSPENDING) {
309 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
310
311 if (expires != 0) {
312 /* Pending requests need to be canceled. */
313 dev->power.request = RPM_REQ_NONE;
314
315 /*
316 * Optimization: If the timer is already running and is
317 * set to expire at or before the autosuspend delay,
318 * avoid the overhead of resetting it. Just let it
319 * expire; pm_suspend_timer_fn() will take care of the
320 * rest.
321 */
322 if (!(dev->power.timer_expires && time_before_eq(
323 dev->power.timer_expires, expires))) {
324 dev->power.timer_expires = expires;
325 mod_timer(&dev->power.suspend_timer, expires);
326 }
327 dev->power.timer_autosuspends = 1;
328 goto out;
329 }
330 }
331
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200332 /* Other scheduled or pending requests need to be canceled. */
333 pm_runtime_cancel_pending(dev);
334
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200335 if (dev->power.runtime_status == RPM_SUSPENDING) {
336 DEFINE_WAIT(wait);
337
Alan Stern1bfee5b2010-09-25 23:35:00 +0200338 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200339 retval = -EINPROGRESS;
340 goto out;
341 }
342
343 /* Wait for the other suspend running in parallel with us. */
344 for (;;) {
345 prepare_to_wait(&dev->power.wait_queue, &wait,
346 TASK_UNINTERRUPTIBLE);
347 if (dev->power.runtime_status != RPM_SUSPENDING)
348 break;
349
350 spin_unlock_irq(&dev->power.lock);
351
352 schedule();
353
354 spin_lock_irq(&dev->power.lock);
355 }
356 finish_wait(&dev->power.wait_queue, &wait);
357 goto repeat;
358 }
359
Alan Stern7490e442010-09-25 23:35:15 +0200360 dev->power.deferred_resume = false;
361 if (dev->power.no_callbacks)
362 goto no_callback; /* Assume success. */
363
Alan Stern1bfee5b2010-09-25 23:35:00 +0200364 /* Carry out an asynchronous or a synchronous suspend. */
365 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200366 dev->power.request = (rpmflags & RPM_AUTO) ?
367 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200368 if (!dev->power.request_pending) {
369 dev->power.request_pending = true;
370 queue_work(pm_wq, &dev->power.work);
371 }
372 goto out;
373 }
374
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200375 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200376
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200377 if (dev->pwr_domain)
378 callback = dev->pwr_domain->ops.runtime_suspend;
379 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200380 callback = dev->type->pm->runtime_suspend;
381 else if (dev->class && dev->class->pm)
382 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100383 else if (dev->bus && dev->bus->pm)
384 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200385 else
386 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200387
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200388 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200389 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200390 __update_runtime_status(dev, RPM_ACTIVE);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200391 dev->power.deferred_resume = 0;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200392 if (retval == -EAGAIN || retval == -EBUSY)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200393 dev->power.runtime_error = 0;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200394 else
Alan Stern240c7332010-03-23 00:50:07 +0100395 pm_runtime_cancel_pending(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200396 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200397 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200398 __update_runtime_status(dev, RPM_SUSPENDED);
Alan Stern240c7332010-03-23 00:50:07 +0100399 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200400
401 if (dev->parent) {
402 parent = dev->parent;
403 atomic_add_unless(&parent->power.child_count, -1, 0);
404 }
405 }
406 wake_up_all(&dev->power.wait_queue);
407
408 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200409 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200410 retval = -EAGAIN;
411 goto out;
412 }
413
Alan Sternc3810c82011-01-25 20:50:07 +0100414 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100415 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100416 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200417
Alan Sternc3810c82011-01-25 20:50:07 +0100418 spin_lock(&parent->power.lock);
419 rpm_idle(parent, RPM_ASYNC);
420 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200421
Alan Sternc3810c82011-01-25 20:50:07 +0100422 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200423 }
424
425 out:
Alan Stern3f9af052010-09-25 23:34:54 +0200426 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200427
428 return retval;
429}
430
431/**
Alan Stern140a6c92010-09-25 23:35:07 +0200432 * rpm_resume - Carry out run-time resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200433 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200434 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200435 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200436 * Check if the device's run-time PM status allows it to be resumed. Cancel
437 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300438 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200439 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
440 * parallel with this function, either tell the other process to resume after
441 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
442 * flag is set then queue a resume request; otherwise run the
443 * ->runtime_resume() callback directly. Queue an idle notification for the
444 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200445 *
446 * This function must be called under dev->power.lock with interrupts disabled.
447 */
Alan Stern140a6c92010-09-25 23:35:07 +0200448static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200449 __releases(&dev->power.lock) __acquires(&dev->power.lock)
450{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200451 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200452 struct device *parent = NULL;
453 int retval = 0;
454
Alan Stern3f9af052010-09-25 23:34:54 +0200455 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200456
457 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200458 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200459 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200460 else if (dev->power.disable_depth > 0)
461 retval = -EAGAIN;
462 if (retval)
463 goto out;
464
Alan Stern15bcb91d2010-09-25 23:35:21 +0200465 /*
466 * Other scheduled or pending requests need to be canceled. Small
467 * optimization: If an autosuspend timer is running, leave it running
468 * rather than cancelling it now only to restart it again in the near
469 * future.
470 */
471 dev->power.request = RPM_REQ_NONE;
472 if (!dev->power.timer_autosuspends)
473 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200474
475 if (dev->power.runtime_status == RPM_ACTIVE) {
476 retval = 1;
477 goto out;
478 }
479
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200480 if (dev->power.runtime_status == RPM_RESUMING
481 || dev->power.runtime_status == RPM_SUSPENDING) {
482 DEFINE_WAIT(wait);
483
Alan Stern1bfee5b2010-09-25 23:35:00 +0200484 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200485 if (dev->power.runtime_status == RPM_SUSPENDING)
486 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200487 else
488 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200489 goto out;
490 }
491
492 /* Wait for the operation carried out in parallel with us. */
493 for (;;) {
494 prepare_to_wait(&dev->power.wait_queue, &wait,
495 TASK_UNINTERRUPTIBLE);
496 if (dev->power.runtime_status != RPM_RESUMING
497 && dev->power.runtime_status != RPM_SUSPENDING)
498 break;
499
500 spin_unlock_irq(&dev->power.lock);
501
502 schedule();
503
504 spin_lock_irq(&dev->power.lock);
505 }
506 finish_wait(&dev->power.wait_queue, &wait);
507 goto repeat;
508 }
509
Alan Stern7490e442010-09-25 23:35:15 +0200510 /*
511 * See if we can skip waking up the parent. This is safe only if
512 * power.no_callbacks is set, because otherwise we don't know whether
513 * the resume will actually succeed.
514 */
515 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200516 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200517 if (dev->parent->power.disable_depth > 0
518 || dev->parent->power.ignore_children
519 || dev->parent->power.runtime_status == RPM_ACTIVE) {
520 atomic_inc(&dev->parent->power.child_count);
521 spin_unlock(&dev->parent->power.lock);
522 goto no_callback; /* Assume success. */
523 }
524 spin_unlock(&dev->parent->power.lock);
525 }
526
Alan Stern1bfee5b2010-09-25 23:35:00 +0200527 /* Carry out an asynchronous or a synchronous resume. */
528 if (rpmflags & RPM_ASYNC) {
529 dev->power.request = RPM_REQ_RESUME;
530 if (!dev->power.request_pending) {
531 dev->power.request_pending = true;
532 queue_work(pm_wq, &dev->power.work);
533 }
534 retval = 0;
535 goto out;
536 }
537
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200538 if (!parent && dev->parent) {
539 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100540 * Increment the parent's usage counter and resume it if
541 * necessary. Not needed if dev is irq-safe; then the
542 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200543 */
544 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100545 if (dev->power.irq_safe)
546 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100547 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200548
549 pm_runtime_get_noresume(parent);
550
Alan Stern862f89b2009-11-25 01:06:37 +0100551 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200552 /*
553 * We can resume if the parent's run-time PM is disabled or it
554 * is set to ignore children.
555 */
556 if (!parent->power.disable_depth
557 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200558 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200559 if (parent->power.runtime_status != RPM_ACTIVE)
560 retval = -EBUSY;
561 }
Alan Stern862f89b2009-11-25 01:06:37 +0100562 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200563
Alan Stern862f89b2009-11-25 01:06:37 +0100564 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200565 if (retval)
566 goto out;
567 goto repeat;
568 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100569 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200570
Alan Stern7490e442010-09-25 23:35:15 +0200571 if (dev->power.no_callbacks)
572 goto no_callback; /* Assume success. */
573
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200574 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200575
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100576 if (dev->pwr_domain)
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200577 callback = dev->pwr_domain->ops.runtime_resume;
578 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200579 callback = dev->type->pm->runtime_resume;
580 else if (dev->class && dev->class->pm)
581 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100582 else if (dev->bus && dev->bus->pm)
583 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200584 else
585 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200586
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200587 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200588 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200589 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200590 pm_runtime_cancel_pending(dev);
591 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200592 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200593 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200594 if (parent)
595 atomic_inc(&parent->power.child_count);
596 }
597 wake_up_all(&dev->power.wait_queue);
598
599 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200600 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200601
602 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100603 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200604 spin_unlock_irq(&dev->power.lock);
605
606 pm_runtime_put(parent);
607
608 spin_lock_irq(&dev->power.lock);
609 }
610
Alan Stern3f9af052010-09-25 23:34:54 +0200611 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200612
613 return retval;
614}
615
616/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200617 * pm_runtime_work - Universal run-time PM work function.
618 * @work: Work structure used for scheduling the execution of this function.
619 *
620 * Use @work to get the device object the work is to be done for, determine what
621 * is to be done and execute the appropriate run-time PM function.
622 */
623static void pm_runtime_work(struct work_struct *work)
624{
625 struct device *dev = container_of(work, struct device, power.work);
626 enum rpm_request req;
627
628 spin_lock_irq(&dev->power.lock);
629
630 if (!dev->power.request_pending)
631 goto out;
632
633 req = dev->power.request;
634 dev->power.request = RPM_REQ_NONE;
635 dev->power.request_pending = false;
636
637 switch (req) {
638 case RPM_REQ_NONE:
639 break;
640 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200641 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200642 break;
643 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200644 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200645 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200646 case RPM_REQ_AUTOSUSPEND:
647 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
648 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200649 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200650 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200651 break;
652 }
653
654 out:
655 spin_unlock_irq(&dev->power.lock);
656}
657
658/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200659 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
660 * @data: Device pointer passed by pm_schedule_suspend().
661 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200662 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200663 */
664static void pm_suspend_timer_fn(unsigned long data)
665{
666 struct device *dev = (struct device *)data;
667 unsigned long flags;
668 unsigned long expires;
669
670 spin_lock_irqsave(&dev->power.lock, flags);
671
672 expires = dev->power.timer_expires;
673 /* If 'expire' is after 'jiffies' we've been called too early. */
674 if (expires > 0 && !time_after(expires, jiffies)) {
675 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200676 rpm_suspend(dev, dev->power.timer_autosuspends ?
677 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200678 }
679
680 spin_unlock_irqrestore(&dev->power.lock, flags);
681}
682
683/**
684 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
685 * @dev: Device to suspend.
686 * @delay: Time to wait before submitting a suspend request, in milliseconds.
687 */
688int pm_schedule_suspend(struct device *dev, unsigned int delay)
689{
690 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200691 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200692
693 spin_lock_irqsave(&dev->power.lock, flags);
694
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200695 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200696 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200697 goto out;
698 }
699
Alan Stern1bfee5b2010-09-25 23:35:00 +0200700 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200701 if (retval)
702 goto out;
703
Alan Stern1bfee5b2010-09-25 23:35:00 +0200704 /* Other scheduled or pending requests need to be canceled. */
705 pm_runtime_cancel_pending(dev);
706
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200707 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200708 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200709 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200710 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
711
712 out:
713 spin_unlock_irqrestore(&dev->power.lock, flags);
714
715 return retval;
716}
717EXPORT_SYMBOL_GPL(pm_schedule_suspend);
718
719/**
Alan Stern140a6c92010-09-25 23:35:07 +0200720 * __pm_runtime_idle - Entry point for run-time idle operations.
721 * @dev: Device to send idle notification for.
722 * @rpmflags: Flag bits.
723 *
724 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
725 * return immediately if it is larger than zero. Then carry out an idle
726 * notification, either synchronous or asynchronous.
727 *
728 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200729 */
Alan Stern140a6c92010-09-25 23:35:07 +0200730int __pm_runtime_idle(struct device *dev, int rpmflags)
731{
732 unsigned long flags;
733 int retval;
734
735 if (rpmflags & RPM_GET_PUT) {
736 if (!atomic_dec_and_test(&dev->power.usage_count))
737 return 0;
738 }
739
740 spin_lock_irqsave(&dev->power.lock, flags);
741 retval = rpm_idle(dev, rpmflags);
742 spin_unlock_irqrestore(&dev->power.lock, flags);
743
744 return retval;
745}
746EXPORT_SYMBOL_GPL(__pm_runtime_idle);
747
748/**
749 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
750 * @dev: Device to suspend.
751 * @rpmflags: Flag bits.
752 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200753 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
754 * return immediately if it is larger than zero. Then carry out a suspend,
755 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200756 *
757 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
758 */
759int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200760{
761 unsigned long flags;
762 int retval;
763
Alan Stern15bcb91d2010-09-25 23:35:21 +0200764 if (rpmflags & RPM_GET_PUT) {
765 if (!atomic_dec_and_test(&dev->power.usage_count))
766 return 0;
767 }
768
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200769 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200770 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200771 spin_unlock_irqrestore(&dev->power.lock, flags);
772
773 return retval;
774}
Alan Stern140a6c92010-09-25 23:35:07 +0200775EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200776
777/**
Alan Stern140a6c92010-09-25 23:35:07 +0200778 * __pm_runtime_resume - Entry point for run-time resume operations.
779 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200780 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200781 *
Alan Stern140a6c92010-09-25 23:35:07 +0200782 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
783 * carry out a resume, either synchronous or asynchronous.
784 *
785 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200786 */
Alan Stern140a6c92010-09-25 23:35:07 +0200787int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200788{
Alan Stern140a6c92010-09-25 23:35:07 +0200789 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100790 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200791
Alan Stern140a6c92010-09-25 23:35:07 +0200792 if (rpmflags & RPM_GET_PUT)
793 atomic_inc(&dev->power.usage_count);
794
795 spin_lock_irqsave(&dev->power.lock, flags);
796 retval = rpm_resume(dev, rpmflags);
797 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200798
799 return retval;
800}
Alan Stern140a6c92010-09-25 23:35:07 +0200801EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200802
803/**
804 * __pm_runtime_set_status - Set run-time PM status of a device.
805 * @dev: Device to handle.
806 * @status: New run-time PM status of the device.
807 *
808 * If run-time PM of the device is disabled or its power.runtime_error field is
809 * different from zero, the status may be changed either to RPM_ACTIVE, or to
810 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
811 * However, if the device has a parent and the parent is not active, and the
812 * parent's power.ignore_children flag is unset, the device's status cannot be
813 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
814 *
815 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
816 * and the device parent's counter of unsuspended children is modified to
817 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
818 * notification request for the parent is submitted.
819 */
820int __pm_runtime_set_status(struct device *dev, unsigned int status)
821{
822 struct device *parent = dev->parent;
823 unsigned long flags;
824 bool notify_parent = false;
825 int error = 0;
826
827 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
828 return -EINVAL;
829
830 spin_lock_irqsave(&dev->power.lock, flags);
831
832 if (!dev->power.runtime_error && !dev->power.disable_depth) {
833 error = -EAGAIN;
834 goto out;
835 }
836
837 if (dev->power.runtime_status == status)
838 goto out_set;
839
840 if (status == RPM_SUSPENDED) {
841 /* It always is possible to set the status to 'suspended'. */
842 if (parent) {
843 atomic_add_unless(&parent->power.child_count, -1, 0);
844 notify_parent = !parent->power.ignore_children;
845 }
846 goto out_set;
847 }
848
849 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100850 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200851
852 /*
853 * It is invalid to put an active child under a parent that is
854 * not active, has run-time PM enabled and the
855 * 'power.ignore_children' flag unset.
856 */
857 if (!parent->power.disable_depth
858 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100859 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200860 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100861 else if (dev->power.runtime_status == RPM_SUSPENDED)
862 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200863
Alan Stern862f89b2009-11-25 01:06:37 +0100864 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200865
866 if (error)
867 goto out;
868 }
869
870 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200871 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200872 dev->power.runtime_error = 0;
873 out:
874 spin_unlock_irqrestore(&dev->power.lock, flags);
875
876 if (notify_parent)
877 pm_request_idle(parent);
878
879 return error;
880}
881EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
882
883/**
884 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
885 * @dev: Device to handle.
886 *
887 * Flush all pending requests for the device from pm_wq and wait for all
888 * run-time PM operations involving the device in progress to complete.
889 *
890 * Should be called under dev->power.lock with interrupts disabled.
891 */
892static void __pm_runtime_barrier(struct device *dev)
893{
894 pm_runtime_deactivate_timer(dev);
895
896 if (dev->power.request_pending) {
897 dev->power.request = RPM_REQ_NONE;
898 spin_unlock_irq(&dev->power.lock);
899
900 cancel_work_sync(&dev->power.work);
901
902 spin_lock_irq(&dev->power.lock);
903 dev->power.request_pending = false;
904 }
905
906 if (dev->power.runtime_status == RPM_SUSPENDING
907 || dev->power.runtime_status == RPM_RESUMING
908 || dev->power.idle_notification) {
909 DEFINE_WAIT(wait);
910
911 /* Suspend, wake-up or idle notification in progress. */
912 for (;;) {
913 prepare_to_wait(&dev->power.wait_queue, &wait,
914 TASK_UNINTERRUPTIBLE);
915 if (dev->power.runtime_status != RPM_SUSPENDING
916 && dev->power.runtime_status != RPM_RESUMING
917 && !dev->power.idle_notification)
918 break;
919 spin_unlock_irq(&dev->power.lock);
920
921 schedule();
922
923 spin_lock_irq(&dev->power.lock);
924 }
925 finish_wait(&dev->power.wait_queue, &wait);
926 }
927}
928
929/**
930 * pm_runtime_barrier - Flush pending requests and wait for completions.
931 * @dev: Device to handle.
932 *
933 * Prevent the device from being suspended by incrementing its usage counter and
934 * if there's a pending resume request for the device, wake the device up.
935 * Next, make sure that all pending requests for the device have been flushed
936 * from pm_wq and wait for all run-time PM operations involving the device in
937 * progress to complete.
938 *
939 * Return value:
940 * 1, if there was a resume request pending and the device had to be woken up,
941 * 0, otherwise
942 */
943int pm_runtime_barrier(struct device *dev)
944{
945 int retval = 0;
946
947 pm_runtime_get_noresume(dev);
948 spin_lock_irq(&dev->power.lock);
949
950 if (dev->power.request_pending
951 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200952 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200953 retval = 1;
954 }
955
956 __pm_runtime_barrier(dev);
957
958 spin_unlock_irq(&dev->power.lock);
959 pm_runtime_put_noidle(dev);
960
961 return retval;
962}
963EXPORT_SYMBOL_GPL(pm_runtime_barrier);
964
965/**
966 * __pm_runtime_disable - Disable run-time PM of a device.
967 * @dev: Device to handle.
968 * @check_resume: If set, check if there's a resume request for the device.
969 *
970 * Increment power.disable_depth for the device and if was zero previously,
971 * cancel all pending run-time PM requests for the device and wait for all
972 * operations in progress to complete. The device can be either active or
973 * suspended after its run-time PM has been disabled.
974 *
975 * If @check_resume is set and there's a resume request pending when
976 * __pm_runtime_disable() is called and power.disable_depth is zero, the
977 * function will wake up the device before disabling its run-time PM.
978 */
979void __pm_runtime_disable(struct device *dev, bool check_resume)
980{
981 spin_lock_irq(&dev->power.lock);
982
983 if (dev->power.disable_depth > 0) {
984 dev->power.disable_depth++;
985 goto out;
986 }
987
988 /*
989 * Wake up the device if there's a resume request pending, because that
990 * means there probably is some I/O to process and disabling run-time PM
991 * shouldn't prevent the device from processing the I/O.
992 */
993 if (check_resume && dev->power.request_pending
994 && dev->power.request == RPM_REQ_RESUME) {
995 /*
996 * Prevent suspends and idle notifications from being carried
997 * out after we have woken up the device.
998 */
999 pm_runtime_get_noresume(dev);
1000
Alan Stern140a6c92010-09-25 23:35:07 +02001001 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001002
1003 pm_runtime_put_noidle(dev);
1004 }
1005
1006 if (!dev->power.disable_depth++)
1007 __pm_runtime_barrier(dev);
1008
1009 out:
1010 spin_unlock_irq(&dev->power.lock);
1011}
1012EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1013
1014/**
1015 * pm_runtime_enable - Enable run-time PM of a device.
1016 * @dev: Device to handle.
1017 */
1018void pm_runtime_enable(struct device *dev)
1019{
1020 unsigned long flags;
1021
1022 spin_lock_irqsave(&dev->power.lock, flags);
1023
1024 if (dev->power.disable_depth > 0)
1025 dev->power.disable_depth--;
1026 else
1027 dev_warn(dev, "Unbalanced %s!\n", __func__);
1028
1029 spin_unlock_irqrestore(&dev->power.lock, flags);
1030}
1031EXPORT_SYMBOL_GPL(pm_runtime_enable);
1032
1033/**
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001034 * pm_runtime_forbid - Block run-time PM of a device.
1035 * @dev: Device to handle.
1036 *
1037 * Increase the device's usage count and clear its power.runtime_auto flag,
1038 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1039 * for it.
1040 */
1041void pm_runtime_forbid(struct device *dev)
1042{
1043 spin_lock_irq(&dev->power.lock);
1044 if (!dev->power.runtime_auto)
1045 goto out;
1046
1047 dev->power.runtime_auto = false;
1048 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001049 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001050
1051 out:
1052 spin_unlock_irq(&dev->power.lock);
1053}
1054EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1055
1056/**
1057 * pm_runtime_allow - Unblock run-time PM of a device.
1058 * @dev: Device to handle.
1059 *
1060 * Decrease the device's usage count and set its power.runtime_auto flag.
1061 */
1062void pm_runtime_allow(struct device *dev)
1063{
1064 spin_lock_irq(&dev->power.lock);
1065 if (dev->power.runtime_auto)
1066 goto out;
1067
1068 dev->power.runtime_auto = true;
1069 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001070 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001071
1072 out:
1073 spin_unlock_irq(&dev->power.lock);
1074}
1075EXPORT_SYMBOL_GPL(pm_runtime_allow);
1076
1077/**
Alan Stern7490e442010-09-25 23:35:15 +02001078 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1079 * @dev: Device to handle.
1080 *
1081 * Set the power.no_callbacks flag, which tells the PM core that this
1082 * device is power-managed through its parent and has no run-time PM
1083 * callbacks of its own. The run-time sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001084 */
1085void pm_runtime_no_callbacks(struct device *dev)
1086{
1087 spin_lock_irq(&dev->power.lock);
1088 dev->power.no_callbacks = 1;
1089 spin_unlock_irq(&dev->power.lock);
1090 if (device_is_registered(dev))
1091 rpm_sysfs_remove(dev);
1092}
1093EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1094
1095/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001096 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1097 * @dev: Device to handle
1098 *
1099 * Set the power.irq_safe flag, which tells the PM core that the
1100 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1101 * always be invoked with the spinlock held and interrupts disabled. It also
1102 * causes the parent's usage counter to be permanently incremented, preventing
1103 * the parent from runtime suspending -- otherwise an irq-safe child might have
1104 * to wait for a non-irq-safe parent.
1105 */
1106void pm_runtime_irq_safe(struct device *dev)
1107{
1108 if (dev->parent)
1109 pm_runtime_get_sync(dev->parent);
1110 spin_lock_irq(&dev->power.lock);
1111 dev->power.irq_safe = 1;
1112 spin_unlock_irq(&dev->power.lock);
1113}
1114EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1115
1116/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001117 * update_autosuspend - Handle a change to a device's autosuspend settings.
1118 * @dev: Device to handle.
1119 * @old_delay: The former autosuspend_delay value.
1120 * @old_use: The former use_autosuspend value.
1121 *
1122 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1123 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1124 *
1125 * This function must be called under dev->power.lock with interrupts disabled.
1126 */
1127static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1128{
1129 int delay = dev->power.autosuspend_delay;
1130
1131 /* Should runtime suspend be prevented now? */
1132 if (dev->power.use_autosuspend && delay < 0) {
1133
1134 /* If it used to be allowed then prevent it. */
1135 if (!old_use || old_delay >= 0) {
1136 atomic_inc(&dev->power.usage_count);
1137 rpm_resume(dev, 0);
1138 }
1139 }
1140
1141 /* Runtime suspend should be allowed now. */
1142 else {
1143
1144 /* If it used to be prevented then allow it. */
1145 if (old_use && old_delay < 0)
1146 atomic_dec(&dev->power.usage_count);
1147
1148 /* Maybe we can autosuspend now. */
1149 rpm_idle(dev, RPM_AUTO);
1150 }
1151}
1152
1153/**
1154 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1155 * @dev: Device to handle.
1156 * @delay: Value of the new delay in milliseconds.
1157 *
1158 * Set the device's power.autosuspend_delay value. If it changes to negative
1159 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1160 * changes the other way, allow run-time suspends.
1161 */
1162void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1163{
1164 int old_delay, old_use;
1165
1166 spin_lock_irq(&dev->power.lock);
1167 old_delay = dev->power.autosuspend_delay;
1168 old_use = dev->power.use_autosuspend;
1169 dev->power.autosuspend_delay = delay;
1170 update_autosuspend(dev, old_delay, old_use);
1171 spin_unlock_irq(&dev->power.lock);
1172}
1173EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1174
1175/**
1176 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1177 * @dev: Device to handle.
1178 * @use: New value for use_autosuspend.
1179 *
1180 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1181 * suspends as needed.
1182 */
1183void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1184{
1185 int old_delay, old_use;
1186
1187 spin_lock_irq(&dev->power.lock);
1188 old_delay = dev->power.autosuspend_delay;
1189 old_use = dev->power.use_autosuspend;
1190 dev->power.use_autosuspend = use;
1191 update_autosuspend(dev, old_delay, old_use);
1192 spin_unlock_irq(&dev->power.lock);
1193}
1194EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1195
1196/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001197 * pm_runtime_init - Initialize run-time PM fields in given device object.
1198 * @dev: Device object to initialize.
1199 */
1200void pm_runtime_init(struct device *dev)
1201{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001202 dev->power.runtime_status = RPM_SUSPENDED;
1203 dev->power.idle_notification = false;
1204
1205 dev->power.disable_depth = 1;
1206 atomic_set(&dev->power.usage_count, 0);
1207
1208 dev->power.runtime_error = 0;
1209
1210 atomic_set(&dev->power.child_count, 0);
1211 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001212 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001213
1214 dev->power.request_pending = false;
1215 dev->power.request = RPM_REQ_NONE;
1216 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001217 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001218 INIT_WORK(&dev->power.work, pm_runtime_work);
1219
1220 dev->power.timer_expires = 0;
1221 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1222 (unsigned long)dev);
1223
1224 init_waitqueue_head(&dev->power.wait_queue);
1225}
1226
1227/**
1228 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1229 * @dev: Device object being removed from device hierarchy.
1230 */
1231void pm_runtime_remove(struct device *dev)
1232{
1233 __pm_runtime_disable(dev, false);
1234
1235 /* Change the status back to 'suspended' to match the initial status. */
1236 if (dev->power.runtime_status == RPM_ACTIVE)
1237 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001238 if (dev->power.irq_safe && dev->parent)
1239 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001240}