blob: 577f4fd1648037592de188ce9dab16704ce4c620 [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 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. 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 Stern15bcb912010-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 Stern15bcb912010-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 Stern15bcb912010-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 Stern15bcb912010-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 Stern15bcb912010-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 Stern15bcb912010-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
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500735 might_sleep_if(!(rpmflags & RPM_ASYNC));
736
Alan Stern140a6c92010-09-25 23:35:07 +0200737 if (rpmflags & RPM_GET_PUT) {
738 if (!atomic_dec_and_test(&dev->power.usage_count))
739 return 0;
740 }
741
742 spin_lock_irqsave(&dev->power.lock, flags);
743 retval = rpm_idle(dev, rpmflags);
744 spin_unlock_irqrestore(&dev->power.lock, flags);
745
746 return retval;
747}
748EXPORT_SYMBOL_GPL(__pm_runtime_idle);
749
750/**
751 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
752 * @dev: Device to suspend.
753 * @rpmflags: Flag bits.
754 *
Alan Stern15bcb912010-09-25 23:35:21 +0200755 * 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 a suspend,
757 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200758 *
759 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
760 */
761int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200762{
763 unsigned long flags;
764 int retval;
765
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500766 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
767
Alan Stern15bcb912010-09-25 23:35:21 +0200768 if (rpmflags & RPM_GET_PUT) {
769 if (!atomic_dec_and_test(&dev->power.usage_count))
770 return 0;
771 }
772
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200773 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200774 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200775 spin_unlock_irqrestore(&dev->power.lock, flags);
776
777 return retval;
778}
Alan Stern140a6c92010-09-25 23:35:07 +0200779EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200780
781/**
Alan Stern140a6c92010-09-25 23:35:07 +0200782 * __pm_runtime_resume - Entry point for run-time resume operations.
783 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200784 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200785 *
Alan Stern140a6c92010-09-25 23:35:07 +0200786 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
787 * carry out a resume, either synchronous or asynchronous.
788 *
789 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200790 */
Alan Stern140a6c92010-09-25 23:35:07 +0200791int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200792{
Alan Stern140a6c92010-09-25 23:35:07 +0200793 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100794 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200795
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500796 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
797
Alan Stern140a6c92010-09-25 23:35:07 +0200798 if (rpmflags & RPM_GET_PUT)
799 atomic_inc(&dev->power.usage_count);
800
801 spin_lock_irqsave(&dev->power.lock, flags);
802 retval = rpm_resume(dev, rpmflags);
803 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200804
805 return retval;
806}
Alan Stern140a6c92010-09-25 23:35:07 +0200807EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200808
809/**
810 * __pm_runtime_set_status - Set run-time PM status of a device.
811 * @dev: Device to handle.
812 * @status: New run-time PM status of the device.
813 *
814 * If run-time PM of the device is disabled or its power.runtime_error field is
815 * different from zero, the status may be changed either to RPM_ACTIVE, or to
816 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
817 * However, if the device has a parent and the parent is not active, and the
818 * parent's power.ignore_children flag is unset, the device's status cannot be
819 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
820 *
821 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
822 * and the device parent's counter of unsuspended children is modified to
823 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
824 * notification request for the parent is submitted.
825 */
826int __pm_runtime_set_status(struct device *dev, unsigned int status)
827{
828 struct device *parent = dev->parent;
829 unsigned long flags;
830 bool notify_parent = false;
831 int error = 0;
832
833 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
834 return -EINVAL;
835
836 spin_lock_irqsave(&dev->power.lock, flags);
837
838 if (!dev->power.runtime_error && !dev->power.disable_depth) {
839 error = -EAGAIN;
840 goto out;
841 }
842
843 if (dev->power.runtime_status == status)
844 goto out_set;
845
846 if (status == RPM_SUSPENDED) {
847 /* It always is possible to set the status to 'suspended'. */
848 if (parent) {
849 atomic_add_unless(&parent->power.child_count, -1, 0);
850 notify_parent = !parent->power.ignore_children;
851 }
852 goto out_set;
853 }
854
855 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100856 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200857
858 /*
859 * It is invalid to put an active child under a parent that is
860 * not active, has run-time PM enabled and the
861 * 'power.ignore_children' flag unset.
862 */
863 if (!parent->power.disable_depth
864 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100865 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200866 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100867 else if (dev->power.runtime_status == RPM_SUSPENDED)
868 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200869
Alan Stern862f89b2009-11-25 01:06:37 +0100870 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200871
872 if (error)
873 goto out;
874 }
875
876 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200877 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200878 dev->power.runtime_error = 0;
879 out:
880 spin_unlock_irqrestore(&dev->power.lock, flags);
881
882 if (notify_parent)
883 pm_request_idle(parent);
884
885 return error;
886}
887EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
888
889/**
890 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
891 * @dev: Device to handle.
892 *
893 * Flush all pending requests for the device from pm_wq and wait for all
894 * run-time PM operations involving the device in progress to complete.
895 *
896 * Should be called under dev->power.lock with interrupts disabled.
897 */
898static void __pm_runtime_barrier(struct device *dev)
899{
900 pm_runtime_deactivate_timer(dev);
901
902 if (dev->power.request_pending) {
903 dev->power.request = RPM_REQ_NONE;
904 spin_unlock_irq(&dev->power.lock);
905
906 cancel_work_sync(&dev->power.work);
907
908 spin_lock_irq(&dev->power.lock);
909 dev->power.request_pending = false;
910 }
911
912 if (dev->power.runtime_status == RPM_SUSPENDING
913 || dev->power.runtime_status == RPM_RESUMING
914 || dev->power.idle_notification) {
915 DEFINE_WAIT(wait);
916
917 /* Suspend, wake-up or idle notification in progress. */
918 for (;;) {
919 prepare_to_wait(&dev->power.wait_queue, &wait,
920 TASK_UNINTERRUPTIBLE);
921 if (dev->power.runtime_status != RPM_SUSPENDING
922 && dev->power.runtime_status != RPM_RESUMING
923 && !dev->power.idle_notification)
924 break;
925 spin_unlock_irq(&dev->power.lock);
926
927 schedule();
928
929 spin_lock_irq(&dev->power.lock);
930 }
931 finish_wait(&dev->power.wait_queue, &wait);
932 }
933}
934
935/**
936 * pm_runtime_barrier - Flush pending requests and wait for completions.
937 * @dev: Device to handle.
938 *
939 * Prevent the device from being suspended by incrementing its usage counter and
940 * if there's a pending resume request for the device, wake the device up.
941 * Next, make sure that all pending requests for the device have been flushed
942 * from pm_wq and wait for all run-time PM operations involving the device in
943 * progress to complete.
944 *
945 * Return value:
946 * 1, if there was a resume request pending and the device had to be woken up,
947 * 0, otherwise
948 */
949int pm_runtime_barrier(struct device *dev)
950{
951 int retval = 0;
952
953 pm_runtime_get_noresume(dev);
954 spin_lock_irq(&dev->power.lock);
955
956 if (dev->power.request_pending
957 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200958 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200959 retval = 1;
960 }
961
962 __pm_runtime_barrier(dev);
963
964 spin_unlock_irq(&dev->power.lock);
965 pm_runtime_put_noidle(dev);
966
967 return retval;
968}
969EXPORT_SYMBOL_GPL(pm_runtime_barrier);
970
971/**
972 * __pm_runtime_disable - Disable run-time PM of a device.
973 * @dev: Device to handle.
974 * @check_resume: If set, check if there's a resume request for the device.
975 *
976 * Increment power.disable_depth for the device and if was zero previously,
977 * cancel all pending run-time PM requests for the device and wait for all
978 * operations in progress to complete. The device can be either active or
979 * suspended after its run-time PM has been disabled.
980 *
981 * If @check_resume is set and there's a resume request pending when
982 * __pm_runtime_disable() is called and power.disable_depth is zero, the
983 * function will wake up the device before disabling its run-time PM.
984 */
985void __pm_runtime_disable(struct device *dev, bool check_resume)
986{
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500987 might_sleep();
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200988 spin_lock_irq(&dev->power.lock);
989
990 if (dev->power.disable_depth > 0) {
991 dev->power.disable_depth++;
992 goto out;
993 }
994
995 /*
996 * Wake up the device if there's a resume request pending, because that
997 * means there probably is some I/O to process and disabling run-time PM
998 * shouldn't prevent the device from processing the I/O.
999 */
1000 if (check_resume && dev->power.request_pending
1001 && dev->power.request == RPM_REQ_RESUME) {
1002 /*
1003 * Prevent suspends and idle notifications from being carried
1004 * out after we have woken up the device.
1005 */
1006 pm_runtime_get_noresume(dev);
1007
Alan Stern140a6c92010-09-25 23:35:07 +02001008 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001009
1010 pm_runtime_put_noidle(dev);
1011 }
1012
1013 if (!dev->power.disable_depth++)
1014 __pm_runtime_barrier(dev);
1015
1016 out:
1017 spin_unlock_irq(&dev->power.lock);
1018}
1019EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1020
1021/**
1022 * pm_runtime_enable - Enable run-time PM of a device.
1023 * @dev: Device to handle.
1024 */
1025void pm_runtime_enable(struct device *dev)
1026{
1027 unsigned long flags;
1028
1029 spin_lock_irqsave(&dev->power.lock, flags);
1030
1031 if (dev->power.disable_depth > 0)
1032 dev->power.disable_depth--;
1033 else
1034 dev_warn(dev, "Unbalanced %s!\n", __func__);
1035
1036 spin_unlock_irqrestore(&dev->power.lock, flags);
1037}
1038EXPORT_SYMBOL_GPL(pm_runtime_enable);
1039
1040/**
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001041 * pm_runtime_forbid - Block run-time PM of a device.
1042 * @dev: Device to handle.
1043 *
1044 * Increase the device's usage count and clear its power.runtime_auto flag,
1045 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1046 * for it.
1047 */
1048void pm_runtime_forbid(struct device *dev)
1049{
1050 spin_lock_irq(&dev->power.lock);
1051 if (!dev->power.runtime_auto)
1052 goto out;
1053
1054 dev->power.runtime_auto = false;
1055 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001056 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001057
1058 out:
1059 spin_unlock_irq(&dev->power.lock);
1060}
1061EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1062
1063/**
1064 * pm_runtime_allow - Unblock run-time PM of a device.
1065 * @dev: Device to handle.
1066 *
1067 * Decrease the device's usage count and set its power.runtime_auto flag.
1068 */
1069void pm_runtime_allow(struct device *dev)
1070{
1071 spin_lock_irq(&dev->power.lock);
1072 if (dev->power.runtime_auto)
1073 goto out;
1074
1075 dev->power.runtime_auto = true;
1076 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb912010-09-25 23:35:21 +02001077 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001078
1079 out:
1080 spin_unlock_irq(&dev->power.lock);
1081}
1082EXPORT_SYMBOL_GPL(pm_runtime_allow);
1083
1084/**
Alan Stern7490e442010-09-25 23:35:15 +02001085 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1086 * @dev: Device to handle.
1087 *
1088 * Set the power.no_callbacks flag, which tells the PM core that this
1089 * device is power-managed through its parent and has no run-time PM
1090 * callbacks of its own. The run-time sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001091 */
1092void pm_runtime_no_callbacks(struct device *dev)
1093{
1094 spin_lock_irq(&dev->power.lock);
1095 dev->power.no_callbacks = 1;
1096 spin_unlock_irq(&dev->power.lock);
1097 if (device_is_registered(dev))
1098 rpm_sysfs_remove(dev);
1099}
1100EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1101
1102/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001103 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1104 * @dev: Device to handle
1105 *
1106 * Set the power.irq_safe flag, which tells the PM core that the
1107 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1108 * always be invoked with the spinlock held and interrupts disabled. It also
1109 * causes the parent's usage counter to be permanently incremented, preventing
1110 * the parent from runtime suspending -- otherwise an irq-safe child might have
1111 * to wait for a non-irq-safe parent.
1112 */
1113void pm_runtime_irq_safe(struct device *dev)
1114{
1115 if (dev->parent)
1116 pm_runtime_get_sync(dev->parent);
1117 spin_lock_irq(&dev->power.lock);
1118 dev->power.irq_safe = 1;
1119 spin_unlock_irq(&dev->power.lock);
1120}
1121EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1122
1123/**
Alan Stern15bcb912010-09-25 23:35:21 +02001124 * update_autosuspend - Handle a change to a device's autosuspend settings.
1125 * @dev: Device to handle.
1126 * @old_delay: The former autosuspend_delay value.
1127 * @old_use: The former use_autosuspend value.
1128 *
1129 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1130 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1131 *
1132 * This function must be called under dev->power.lock with interrupts disabled.
1133 */
1134static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1135{
1136 int delay = dev->power.autosuspend_delay;
1137
1138 /* Should runtime suspend be prevented now? */
1139 if (dev->power.use_autosuspend && delay < 0) {
1140
1141 /* If it used to be allowed then prevent it. */
1142 if (!old_use || old_delay >= 0) {
1143 atomic_inc(&dev->power.usage_count);
1144 rpm_resume(dev, 0);
1145 }
1146 }
1147
1148 /* Runtime suspend should be allowed now. */
1149 else {
1150
1151 /* If it used to be prevented then allow it. */
1152 if (old_use && old_delay < 0)
1153 atomic_dec(&dev->power.usage_count);
1154
1155 /* Maybe we can autosuspend now. */
1156 rpm_idle(dev, RPM_AUTO);
1157 }
1158}
1159
1160/**
1161 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1162 * @dev: Device to handle.
1163 * @delay: Value of the new delay in milliseconds.
1164 *
1165 * Set the device's power.autosuspend_delay value. If it changes to negative
1166 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1167 * changes the other way, allow run-time suspends.
1168 */
1169void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1170{
1171 int old_delay, old_use;
1172
1173 spin_lock_irq(&dev->power.lock);
1174 old_delay = dev->power.autosuspend_delay;
1175 old_use = dev->power.use_autosuspend;
1176 dev->power.autosuspend_delay = delay;
1177 update_autosuspend(dev, old_delay, old_use);
1178 spin_unlock_irq(&dev->power.lock);
1179}
1180EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1181
1182/**
1183 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1184 * @dev: Device to handle.
1185 * @use: New value for use_autosuspend.
1186 *
1187 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1188 * suspends as needed.
1189 */
1190void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1191{
1192 int old_delay, old_use;
1193
Colin Crossb1d0d5f2011-08-10 11:41:59 -05001194 might_sleep();
1195
Alan Stern15bcb912010-09-25 23:35:21 +02001196 spin_lock_irq(&dev->power.lock);
1197 old_delay = dev->power.autosuspend_delay;
1198 old_use = dev->power.use_autosuspend;
1199 dev->power.use_autosuspend = use;
1200 update_autosuspend(dev, old_delay, old_use);
1201 spin_unlock_irq(&dev->power.lock);
1202}
1203EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1204
1205/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001206 * pm_runtime_init - Initialize run-time PM fields in given device object.
1207 * @dev: Device object to initialize.
1208 */
1209void pm_runtime_init(struct device *dev)
1210{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001211 dev->power.runtime_status = RPM_SUSPENDED;
1212 dev->power.idle_notification = false;
1213
1214 dev->power.disable_depth = 1;
1215 atomic_set(&dev->power.usage_count, 0);
1216
1217 dev->power.runtime_error = 0;
1218
1219 atomic_set(&dev->power.child_count, 0);
1220 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001221 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001222
1223 dev->power.request_pending = false;
1224 dev->power.request = RPM_REQ_NONE;
1225 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001226 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001227 INIT_WORK(&dev->power.work, pm_runtime_work);
1228
1229 dev->power.timer_expires = 0;
1230 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1231 (unsigned long)dev);
1232
1233 init_waitqueue_head(&dev->power.wait_queue);
1234}
1235
1236/**
1237 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1238 * @dev: Device object being removed from device hierarchy.
1239 */
1240void pm_runtime_remove(struct device *dev)
1241{
1242 __pm_runtime_disable(dev, false);
1243
1244 /* Change the status back to 'suspended' to match the initial status. */
1245 if (dev->power.runtime_status == RPM_ACTIVE)
1246 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001247 if (dev->power.irq_safe && dev->parent)
1248 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001249}