blob: 184cf54fa01cfbde621a31047866e8c0f1246ade [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).
Alan Stern8dc9c792011-11-03 23:39:18 +0100281 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
282 * flag is set and the next autosuspend-delay expiration time is in the
283 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200284 *
285 * This function must be called under dev->power.lock with interrupts disabled.
286 */
Alan Stern140a6c92010-09-25 23:35:07 +0200287static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200288 __releases(&dev->power.lock) __acquires(&dev->power.lock)
289{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200290 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200291 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200292 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200293
Alan Stern3f9af052010-09-25 23:34:54 +0200294 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200295
296 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200297 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200298
Alan Stern1bfee5b2010-09-25 23:35:00 +0200299 if (retval < 0)
300 ; /* Conditions are wrong. */
301
302 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
303 else if (dev->power.runtime_status == RPM_RESUMING &&
304 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200305 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200306 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200307 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308
Alan Stern15bcb912010-09-25 23:35:21 +0200309 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
310 if ((rpmflags & RPM_AUTO)
311 && dev->power.runtime_status != RPM_SUSPENDING) {
312 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
313
314 if (expires != 0) {
315 /* Pending requests need to be canceled. */
316 dev->power.request = RPM_REQ_NONE;
317
318 /*
319 * Optimization: If the timer is already running and is
320 * set to expire at or before the autosuspend delay,
321 * avoid the overhead of resetting it. Just let it
322 * expire; pm_suspend_timer_fn() will take care of the
323 * rest.
324 */
325 if (!(dev->power.timer_expires && time_before_eq(
326 dev->power.timer_expires, expires))) {
327 dev->power.timer_expires = expires;
328 mod_timer(&dev->power.suspend_timer, expires);
329 }
330 dev->power.timer_autosuspends = 1;
331 goto out;
332 }
333 }
334
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200335 /* Other scheduled or pending requests need to be canceled. */
336 pm_runtime_cancel_pending(dev);
337
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200338 if (dev->power.runtime_status == RPM_SUSPENDING) {
339 DEFINE_WAIT(wait);
340
Alan Stern1bfee5b2010-09-25 23:35:00 +0200341 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200342 retval = -EINPROGRESS;
343 goto out;
344 }
345
346 /* Wait for the other suspend running in parallel with us. */
347 for (;;) {
348 prepare_to_wait(&dev->power.wait_queue, &wait,
349 TASK_UNINTERRUPTIBLE);
350 if (dev->power.runtime_status != RPM_SUSPENDING)
351 break;
352
353 spin_unlock_irq(&dev->power.lock);
354
355 schedule();
356
357 spin_lock_irq(&dev->power.lock);
358 }
359 finish_wait(&dev->power.wait_queue, &wait);
360 goto repeat;
361 }
362
Alan Stern7490e442010-09-25 23:35:15 +0200363 dev->power.deferred_resume = false;
364 if (dev->power.no_callbacks)
365 goto no_callback; /* Assume success. */
366
Alan Stern1bfee5b2010-09-25 23:35:00 +0200367 /* Carry out an asynchronous or a synchronous suspend. */
368 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb912010-09-25 23:35:21 +0200369 dev->power.request = (rpmflags & RPM_AUTO) ?
370 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200371 if (!dev->power.request_pending) {
372 dev->power.request_pending = true;
373 queue_work(pm_wq, &dev->power.work);
374 }
375 goto out;
376 }
377
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200378 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200379
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200380 if (dev->pwr_domain)
381 callback = dev->pwr_domain->ops.runtime_suspend;
382 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200383 callback = dev->type->pm->runtime_suspend;
384 else if (dev->class && dev->class->pm)
385 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100386 else if (dev->bus && dev->bus->pm)
387 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200388 else
389 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200390
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200391 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200392 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200393 __update_runtime_status(dev, RPM_ACTIVE);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200394 dev->power.deferred_resume = 0;
Alan Stern8dc9c792011-11-03 23:39:18 +0100395 if (retval == -EAGAIN || retval == -EBUSY) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200396 dev->power.runtime_error = 0;
Alan Stern8dc9c792011-11-03 23:39:18 +0100397
398 /*
399 * If the callback routine failed an autosuspend, and
400 * if the last_busy time has been updated so that there
401 * is a new autosuspend expiration time, automatically
402 * reschedule another autosuspend.
403 */
404 if ((rpmflags & RPM_AUTO) &&
405 pm_runtime_autosuspend_expiration(dev) != 0)
406 goto repeat;
407 } else {
Alan Stern240c7332010-03-23 00:50:07 +0100408 pm_runtime_cancel_pending(dev);
Alan Stern8dc9c792011-11-03 23:39:18 +0100409 }
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200410 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200411 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200412 __update_runtime_status(dev, RPM_SUSPENDED);
Alan Stern240c7332010-03-23 00:50:07 +0100413 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200414
415 if (dev->parent) {
416 parent = dev->parent;
417 atomic_add_unless(&parent->power.child_count, -1, 0);
418 }
419 }
420 wake_up_all(&dev->power.wait_queue);
421
422 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200423 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200424 retval = -EAGAIN;
425 goto out;
426 }
427
Alan Sternc3810c82011-01-25 20:50:07 +0100428 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100429 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100430 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200431
Alan Sternc3810c82011-01-25 20:50:07 +0100432 spin_lock(&parent->power.lock);
433 rpm_idle(parent, RPM_ASYNC);
434 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200435
Alan Sternc3810c82011-01-25 20:50:07 +0100436 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200437 }
438
439 out:
Alan Stern3f9af052010-09-25 23:34:54 +0200440 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200441
442 return retval;
443}
444
445/**
Alan Stern140a6c92010-09-25 23:35:07 +0200446 * rpm_resume - Carry out run-time resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200447 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200448 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200449 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200450 * Check if the device's run-time PM status allows it to be resumed. Cancel
451 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300452 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200453 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
454 * parallel with this function, either tell the other process to resume after
455 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
456 * flag is set then queue a resume request; otherwise run the
457 * ->runtime_resume() callback directly. Queue an idle notification for the
458 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200459 *
460 * This function must be called under dev->power.lock with interrupts disabled.
461 */
Alan Stern140a6c92010-09-25 23:35:07 +0200462static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200463 __releases(&dev->power.lock) __acquires(&dev->power.lock)
464{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200465 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200466 struct device *parent = NULL;
467 int retval = 0;
468
Alan Stern3f9af052010-09-25 23:34:54 +0200469 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200470
471 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200472 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200473 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200474 else if (dev->power.disable_depth > 0)
475 retval = -EAGAIN;
476 if (retval)
477 goto out;
478
Alan Stern15bcb912010-09-25 23:35:21 +0200479 /*
480 * Other scheduled or pending requests need to be canceled. Small
481 * optimization: If an autosuspend timer is running, leave it running
482 * rather than cancelling it now only to restart it again in the near
483 * future.
484 */
485 dev->power.request = RPM_REQ_NONE;
486 if (!dev->power.timer_autosuspends)
487 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200488
489 if (dev->power.runtime_status == RPM_ACTIVE) {
490 retval = 1;
491 goto out;
492 }
493
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200494 if (dev->power.runtime_status == RPM_RESUMING
495 || dev->power.runtime_status == RPM_SUSPENDING) {
496 DEFINE_WAIT(wait);
497
Alan Stern1bfee5b2010-09-25 23:35:00 +0200498 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200499 if (dev->power.runtime_status == RPM_SUSPENDING)
500 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200501 else
502 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200503 goto out;
504 }
505
506 /* Wait for the operation carried out in parallel with us. */
507 for (;;) {
508 prepare_to_wait(&dev->power.wait_queue, &wait,
509 TASK_UNINTERRUPTIBLE);
510 if (dev->power.runtime_status != RPM_RESUMING
511 && dev->power.runtime_status != RPM_SUSPENDING)
512 break;
513
514 spin_unlock_irq(&dev->power.lock);
515
516 schedule();
517
518 spin_lock_irq(&dev->power.lock);
519 }
520 finish_wait(&dev->power.wait_queue, &wait);
521 goto repeat;
522 }
523
Alan Stern7490e442010-09-25 23:35:15 +0200524 /*
525 * See if we can skip waking up the parent. This is safe only if
526 * power.no_callbacks is set, because otherwise we don't know whether
527 * the resume will actually succeed.
528 */
529 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200530 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200531 if (dev->parent->power.disable_depth > 0
532 || dev->parent->power.ignore_children
533 || dev->parent->power.runtime_status == RPM_ACTIVE) {
534 atomic_inc(&dev->parent->power.child_count);
535 spin_unlock(&dev->parent->power.lock);
536 goto no_callback; /* Assume success. */
537 }
538 spin_unlock(&dev->parent->power.lock);
539 }
540
Alan Stern1bfee5b2010-09-25 23:35:00 +0200541 /* Carry out an asynchronous or a synchronous resume. */
542 if (rpmflags & RPM_ASYNC) {
543 dev->power.request = RPM_REQ_RESUME;
544 if (!dev->power.request_pending) {
545 dev->power.request_pending = true;
546 queue_work(pm_wq, &dev->power.work);
547 }
548 retval = 0;
549 goto out;
550 }
551
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200552 if (!parent && dev->parent) {
553 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100554 * Increment the parent's usage counter and resume it if
555 * necessary. Not needed if dev is irq-safe; then the
556 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200557 */
558 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100559 if (dev->power.irq_safe)
560 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100561 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200562
563 pm_runtime_get_noresume(parent);
564
Alan Stern862f89b2009-11-25 01:06:37 +0100565 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200566 /*
567 * We can resume if the parent's run-time PM is disabled or it
568 * is set to ignore children.
569 */
570 if (!parent->power.disable_depth
571 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200572 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200573 if (parent->power.runtime_status != RPM_ACTIVE)
574 retval = -EBUSY;
575 }
Alan Stern862f89b2009-11-25 01:06:37 +0100576 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200577
Alan Stern862f89b2009-11-25 01:06:37 +0100578 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200579 if (retval)
580 goto out;
581 goto repeat;
582 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100583 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200584
Alan Stern7490e442010-09-25 23:35:15 +0200585 if (dev->power.no_callbacks)
586 goto no_callback; /* Assume success. */
587
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200588 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200589
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100590 if (dev->pwr_domain)
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200591 callback = dev->pwr_domain->ops.runtime_resume;
592 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200593 callback = dev->type->pm->runtime_resume;
594 else if (dev->class && dev->class->pm)
595 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100596 else if (dev->bus && dev->bus->pm)
597 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200598 else
599 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200600
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200601 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200603 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200604 pm_runtime_cancel_pending(dev);
605 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200606 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200607 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200608 if (parent)
609 atomic_inc(&parent->power.child_count);
610 }
611 wake_up_all(&dev->power.wait_queue);
612
613 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200614 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200615
616 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100617 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200618 spin_unlock_irq(&dev->power.lock);
619
620 pm_runtime_put(parent);
621
622 spin_lock_irq(&dev->power.lock);
623 }
624
Alan Stern3f9af052010-09-25 23:34:54 +0200625 dev_dbg(dev, "%s returns %d\n", __func__, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200626
627 return retval;
628}
629
630/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200631 * pm_runtime_work - Universal run-time PM work function.
632 * @work: Work structure used for scheduling the execution of this function.
633 *
634 * Use @work to get the device object the work is to be done for, determine what
635 * is to be done and execute the appropriate run-time PM function.
636 */
637static void pm_runtime_work(struct work_struct *work)
638{
639 struct device *dev = container_of(work, struct device, power.work);
640 enum rpm_request req;
641
642 spin_lock_irq(&dev->power.lock);
643
644 if (!dev->power.request_pending)
645 goto out;
646
647 req = dev->power.request;
648 dev->power.request = RPM_REQ_NONE;
649 dev->power.request_pending = false;
650
651 switch (req) {
652 case RPM_REQ_NONE:
653 break;
654 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200655 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200656 break;
657 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200658 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200659 break;
Alan Stern15bcb912010-09-25 23:35:21 +0200660 case RPM_REQ_AUTOSUSPEND:
661 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
662 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200663 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200664 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200665 break;
666 }
667
668 out:
669 spin_unlock_irq(&dev->power.lock);
670}
671
672/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200673 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
674 * @data: Device pointer passed by pm_schedule_suspend().
675 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200676 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200677 */
678static void pm_suspend_timer_fn(unsigned long data)
679{
680 struct device *dev = (struct device *)data;
681 unsigned long flags;
682 unsigned long expires;
683
684 spin_lock_irqsave(&dev->power.lock, flags);
685
686 expires = dev->power.timer_expires;
687 /* If 'expire' is after 'jiffies' we've been called too early. */
688 if (expires > 0 && !time_after(expires, jiffies)) {
689 dev->power.timer_expires = 0;
Alan Stern15bcb912010-09-25 23:35:21 +0200690 rpm_suspend(dev, dev->power.timer_autosuspends ?
691 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200692 }
693
694 spin_unlock_irqrestore(&dev->power.lock, flags);
695}
696
697/**
698 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
699 * @dev: Device to suspend.
700 * @delay: Time to wait before submitting a suspend request, in milliseconds.
701 */
702int pm_schedule_suspend(struct device *dev, unsigned int delay)
703{
704 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200705 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200706
707 spin_lock_irqsave(&dev->power.lock, flags);
708
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200709 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200710 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200711 goto out;
712 }
713
Alan Stern1bfee5b2010-09-25 23:35:00 +0200714 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200715 if (retval)
716 goto out;
717
Alan Stern1bfee5b2010-09-25 23:35:00 +0200718 /* Other scheduled or pending requests need to be canceled. */
719 pm_runtime_cancel_pending(dev);
720
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200721 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200722 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb912010-09-25 23:35:21 +0200723 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200724 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
725
726 out:
727 spin_unlock_irqrestore(&dev->power.lock, flags);
728
729 return retval;
730}
731EXPORT_SYMBOL_GPL(pm_schedule_suspend);
732
733/**
Alan Stern140a6c92010-09-25 23:35:07 +0200734 * __pm_runtime_idle - Entry point for run-time idle operations.
735 * @dev: Device to send idle notification for.
736 * @rpmflags: Flag bits.
737 *
738 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
739 * return immediately if it is larger than zero. Then carry out an idle
740 * notification, either synchronous or asynchronous.
741 *
742 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200743 */
Alan Stern140a6c92010-09-25 23:35:07 +0200744int __pm_runtime_idle(struct device *dev, int rpmflags)
745{
746 unsigned long flags;
747 int retval;
748
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500749 might_sleep_if(!(rpmflags & RPM_ASYNC));
750
Alan Stern140a6c92010-09-25 23:35:07 +0200751 if (rpmflags & RPM_GET_PUT) {
752 if (!atomic_dec_and_test(&dev->power.usage_count))
753 return 0;
754 }
755
756 spin_lock_irqsave(&dev->power.lock, flags);
757 retval = rpm_idle(dev, rpmflags);
758 spin_unlock_irqrestore(&dev->power.lock, flags);
759
760 return retval;
761}
762EXPORT_SYMBOL_GPL(__pm_runtime_idle);
763
764/**
765 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
766 * @dev: Device to suspend.
767 * @rpmflags: Flag bits.
768 *
Alan Stern15bcb912010-09-25 23:35:21 +0200769 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
770 * return immediately if it is larger than zero. Then carry out a suspend,
771 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200772 *
773 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
774 */
775int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200776{
777 unsigned long flags;
778 int retval;
779
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500780 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
781
Alan Stern15bcb912010-09-25 23:35:21 +0200782 if (rpmflags & RPM_GET_PUT) {
783 if (!atomic_dec_and_test(&dev->power.usage_count))
784 return 0;
785 }
786
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200787 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200788 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200789 spin_unlock_irqrestore(&dev->power.lock, flags);
790
791 return retval;
792}
Alan Stern140a6c92010-09-25 23:35:07 +0200793EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200794
795/**
Alan Stern140a6c92010-09-25 23:35:07 +0200796 * __pm_runtime_resume - Entry point for run-time resume operations.
797 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200798 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200799 *
Alan Stern140a6c92010-09-25 23:35:07 +0200800 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
801 * carry out a resume, either synchronous or asynchronous.
802 *
803 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200804 */
Alan Stern140a6c92010-09-25 23:35:07 +0200805int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200806{
Alan Stern140a6c92010-09-25 23:35:07 +0200807 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100808 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200809
Colin Crossb1d0d5f2011-08-10 11:41:59 -0500810 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
811
Alan Stern140a6c92010-09-25 23:35:07 +0200812 if (rpmflags & RPM_GET_PUT)
813 atomic_inc(&dev->power.usage_count);
814
815 spin_lock_irqsave(&dev->power.lock, flags);
816 retval = rpm_resume(dev, rpmflags);
817 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200818
819 return retval;
820}
Alan Stern140a6c92010-09-25 23:35:07 +0200821EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200822
823/**
824 * __pm_runtime_set_status - Set run-time PM status of a device.
825 * @dev: Device to handle.
826 * @status: New run-time PM status of the device.
827 *
828 * If run-time PM of the device is disabled or its power.runtime_error field is
829 * different from zero, the status may be changed either to RPM_ACTIVE, or to
830 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
831 * However, if the device has a parent and the parent is not active, and the
832 * parent's power.ignore_children flag is unset, the device's status cannot be
833 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
834 *
835 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
836 * and the device parent's counter of unsuspended children is modified to
837 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
838 * notification request for the parent is submitted.
839 */
840int __pm_runtime_set_status(struct device *dev, unsigned int status)
841{
842 struct device *parent = dev->parent;
843 unsigned long flags;
844 bool notify_parent = false;
845 int error = 0;
846
847 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
848 return -EINVAL;
849
850 spin_lock_irqsave(&dev->power.lock, flags);
851
852 if (!dev->power.runtime_error && !dev->power.disable_depth) {
853 error = -EAGAIN;
854 goto out;
855 }
856
857 if (dev->power.runtime_status == status)
858 goto out_set;
859
860 if (status == RPM_SUSPENDED) {
861 /* It always is possible to set the status to 'suspended'. */
862 if (parent) {
863 atomic_add_unless(&parent->power.child_count, -1, 0);
864 notify_parent = !parent->power.ignore_children;
865 }
866 goto out_set;
867 }
868
869 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100870 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200871
872 /*
873 * It is invalid to put an active child under a parent that is
874 * not active, has run-time PM enabled and the
875 * 'power.ignore_children' flag unset.
876 */
877 if (!parent->power.disable_depth
878 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100879 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200880 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100881 else if (dev->power.runtime_status == RPM_SUSPENDED)
882 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200883
Alan Stern862f89b2009-11-25 01:06:37 +0100884 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200885
886 if (error)
887 goto out;
888 }
889
890 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200891 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200892 dev->power.runtime_error = 0;
893 out:
894 spin_unlock_irqrestore(&dev->power.lock, flags);
895
896 if (notify_parent)
897 pm_request_idle(parent);
898
899 return error;
900}
901EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
902
903/**
904 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
905 * @dev: Device to handle.
906 *
907 * Flush all pending requests for the device from pm_wq and wait for all
908 * run-time PM operations involving the device in progress to complete.
909 *
910 * Should be called under dev->power.lock with interrupts disabled.
911 */
912static void __pm_runtime_barrier(struct device *dev)
913{
914 pm_runtime_deactivate_timer(dev);
915
916 if (dev->power.request_pending) {
917 dev->power.request = RPM_REQ_NONE;
918 spin_unlock_irq(&dev->power.lock);
919
920 cancel_work_sync(&dev->power.work);
921
922 spin_lock_irq(&dev->power.lock);
923 dev->power.request_pending = false;
924 }
925
926 if (dev->power.runtime_status == RPM_SUSPENDING
927 || dev->power.runtime_status == RPM_RESUMING
928 || dev->power.idle_notification) {
929 DEFINE_WAIT(wait);
930
931 /* Suspend, wake-up or idle notification in progress. */
932 for (;;) {
933 prepare_to_wait(&dev->power.wait_queue, &wait,
934 TASK_UNINTERRUPTIBLE);
935 if (dev->power.runtime_status != RPM_SUSPENDING
936 && dev->power.runtime_status != RPM_RESUMING
937 && !dev->power.idle_notification)
938 break;
939 spin_unlock_irq(&dev->power.lock);
940
941 schedule();
942
943 spin_lock_irq(&dev->power.lock);
944 }
945 finish_wait(&dev->power.wait_queue, &wait);
946 }
947}
948
949/**
950 * pm_runtime_barrier - Flush pending requests and wait for completions.
951 * @dev: Device to handle.
952 *
953 * Prevent the device from being suspended by incrementing its usage counter and
954 * if there's a pending resume request for the device, wake the device up.
955 * Next, make sure that all pending requests for the device have been flushed
956 * from pm_wq and wait for all run-time PM operations involving the device in
957 * progress to complete.
958 *
959 * Return value:
960 * 1, if there was a resume request pending and the device had to be woken up,
961 * 0, otherwise
962 */
963int pm_runtime_barrier(struct device *dev)
964{
965 int retval = 0;
966
967 pm_runtime_get_noresume(dev);
968 spin_lock_irq(&dev->power.lock);
969
970 if (dev->power.request_pending
971 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200972 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200973 retval = 1;
974 }
975
976 __pm_runtime_barrier(dev);
977
978 spin_unlock_irq(&dev->power.lock);
979 pm_runtime_put_noidle(dev);
980
981 return retval;
982}
983EXPORT_SYMBOL_GPL(pm_runtime_barrier);
984
985/**
986 * __pm_runtime_disable - Disable run-time PM of a device.
987 * @dev: Device to handle.
988 * @check_resume: If set, check if there's a resume request for the device.
989 *
990 * Increment power.disable_depth for the device and if was zero previously,
991 * cancel all pending run-time PM requests for the device and wait for all
992 * operations in progress to complete. The device can be either active or
993 * suspended after its run-time PM has been disabled.
994 *
995 * If @check_resume is set and there's a resume request pending when
996 * __pm_runtime_disable() is called and power.disable_depth is zero, the
997 * function will wake up the device before disabling its run-time PM.
998 */
999void __pm_runtime_disable(struct device *dev, bool check_resume)
1000{
Colin Crossb1d0d5f2011-08-10 11:41:59 -05001001 might_sleep();
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001002 spin_lock_irq(&dev->power.lock);
1003
1004 if (dev->power.disable_depth > 0) {
1005 dev->power.disable_depth++;
1006 goto out;
1007 }
1008
1009 /*
1010 * Wake up the device if there's a resume request pending, because that
1011 * means there probably is some I/O to process and disabling run-time PM
1012 * shouldn't prevent the device from processing the I/O.
1013 */
1014 if (check_resume && dev->power.request_pending
1015 && dev->power.request == RPM_REQ_RESUME) {
1016 /*
1017 * Prevent suspends and idle notifications from being carried
1018 * out after we have woken up the device.
1019 */
1020 pm_runtime_get_noresume(dev);
1021
Alan Stern140a6c92010-09-25 23:35:07 +02001022 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001023
1024 pm_runtime_put_noidle(dev);
1025 }
1026
1027 if (!dev->power.disable_depth++)
1028 __pm_runtime_barrier(dev);
1029
1030 out:
1031 spin_unlock_irq(&dev->power.lock);
1032}
1033EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1034
1035/**
1036 * pm_runtime_enable - Enable run-time PM of a device.
1037 * @dev: Device to handle.
1038 */
1039void pm_runtime_enable(struct device *dev)
1040{
1041 unsigned long flags;
1042
1043 spin_lock_irqsave(&dev->power.lock, flags);
1044
1045 if (dev->power.disable_depth > 0)
1046 dev->power.disable_depth--;
1047 else
1048 dev_warn(dev, "Unbalanced %s!\n", __func__);
1049
1050 spin_unlock_irqrestore(&dev->power.lock, flags);
1051}
1052EXPORT_SYMBOL_GPL(pm_runtime_enable);
1053
1054/**
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001055 * pm_runtime_forbid - Block run-time PM of a device.
1056 * @dev: Device to handle.
1057 *
1058 * Increase the device's usage count and clear its power.runtime_auto flag,
1059 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1060 * for it.
1061 */
1062void pm_runtime_forbid(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 = false;
1069 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001070 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001071
1072 out:
1073 spin_unlock_irq(&dev->power.lock);
1074}
1075EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1076
1077/**
1078 * pm_runtime_allow - Unblock run-time PM of a device.
1079 * @dev: Device to handle.
1080 *
1081 * Decrease the device's usage count and set its power.runtime_auto flag.
1082 */
1083void pm_runtime_allow(struct device *dev)
1084{
1085 spin_lock_irq(&dev->power.lock);
1086 if (dev->power.runtime_auto)
1087 goto out;
1088
1089 dev->power.runtime_auto = true;
1090 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb912010-09-25 23:35:21 +02001091 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001092
1093 out:
1094 spin_unlock_irq(&dev->power.lock);
1095}
1096EXPORT_SYMBOL_GPL(pm_runtime_allow);
1097
1098/**
Alan Stern7490e442010-09-25 23:35:15 +02001099 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1100 * @dev: Device to handle.
1101 *
1102 * Set the power.no_callbacks flag, which tells the PM core that this
1103 * device is power-managed through its parent and has no run-time PM
1104 * callbacks of its own. The run-time sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001105 */
1106void pm_runtime_no_callbacks(struct device *dev)
1107{
1108 spin_lock_irq(&dev->power.lock);
1109 dev->power.no_callbacks = 1;
1110 spin_unlock_irq(&dev->power.lock);
1111 if (device_is_registered(dev))
1112 rpm_sysfs_remove(dev);
1113}
1114EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1115
1116/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001117 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1118 * @dev: Device to handle
1119 *
1120 * Set the power.irq_safe flag, which tells the PM core that the
1121 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1122 * always be invoked with the spinlock held and interrupts disabled. It also
1123 * causes the parent's usage counter to be permanently incremented, preventing
1124 * the parent from runtime suspending -- otherwise an irq-safe child might have
1125 * to wait for a non-irq-safe parent.
1126 */
1127void pm_runtime_irq_safe(struct device *dev)
1128{
1129 if (dev->parent)
1130 pm_runtime_get_sync(dev->parent);
1131 spin_lock_irq(&dev->power.lock);
1132 dev->power.irq_safe = 1;
1133 spin_unlock_irq(&dev->power.lock);
1134}
1135EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1136
1137/**
Alan Stern15bcb912010-09-25 23:35:21 +02001138 * update_autosuspend - Handle a change to a device's autosuspend settings.
1139 * @dev: Device to handle.
1140 * @old_delay: The former autosuspend_delay value.
1141 * @old_use: The former use_autosuspend value.
1142 *
1143 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1144 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1145 *
1146 * This function must be called under dev->power.lock with interrupts disabled.
1147 */
1148static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1149{
1150 int delay = dev->power.autosuspend_delay;
1151
1152 /* Should runtime suspend be prevented now? */
1153 if (dev->power.use_autosuspend && delay < 0) {
1154
1155 /* If it used to be allowed then prevent it. */
1156 if (!old_use || old_delay >= 0) {
1157 atomic_inc(&dev->power.usage_count);
1158 rpm_resume(dev, 0);
1159 }
1160 }
1161
1162 /* Runtime suspend should be allowed now. */
1163 else {
1164
1165 /* If it used to be prevented then allow it. */
1166 if (old_use && old_delay < 0)
1167 atomic_dec(&dev->power.usage_count);
1168
1169 /* Maybe we can autosuspend now. */
1170 rpm_idle(dev, RPM_AUTO);
1171 }
1172}
1173
1174/**
1175 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1176 * @dev: Device to handle.
1177 * @delay: Value of the new delay in milliseconds.
1178 *
1179 * Set the device's power.autosuspend_delay value. If it changes to negative
1180 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1181 * changes the other way, allow run-time suspends.
1182 */
1183void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
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.autosuspend_delay = delay;
1191 update_autosuspend(dev, old_delay, old_use);
1192 spin_unlock_irq(&dev->power.lock);
1193}
1194EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1195
1196/**
1197 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1198 * @dev: Device to handle.
1199 * @use: New value for use_autosuspend.
1200 *
1201 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1202 * suspends as needed.
1203 */
1204void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1205{
1206 int old_delay, old_use;
1207
Colin Crossb1d0d5f2011-08-10 11:41:59 -05001208 might_sleep();
1209
Alan Stern15bcb912010-09-25 23:35:21 +02001210 spin_lock_irq(&dev->power.lock);
1211 old_delay = dev->power.autosuspend_delay;
1212 old_use = dev->power.use_autosuspend;
1213 dev->power.use_autosuspend = use;
1214 update_autosuspend(dev, old_delay, old_use);
1215 spin_unlock_irq(&dev->power.lock);
1216}
1217EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1218
1219/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001220 * pm_runtime_init - Initialize run-time PM fields in given device object.
1221 * @dev: Device object to initialize.
1222 */
1223void pm_runtime_init(struct device *dev)
1224{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001225 dev->power.runtime_status = RPM_SUSPENDED;
1226 dev->power.idle_notification = false;
1227
1228 dev->power.disable_depth = 1;
1229 atomic_set(&dev->power.usage_count, 0);
1230
1231 dev->power.runtime_error = 0;
1232
1233 atomic_set(&dev->power.child_count, 0);
1234 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001235 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001236
1237 dev->power.request_pending = false;
1238 dev->power.request = RPM_REQ_NONE;
1239 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001240 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001241 INIT_WORK(&dev->power.work, pm_runtime_work);
1242
1243 dev->power.timer_expires = 0;
1244 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1245 (unsigned long)dev);
1246
1247 init_waitqueue_head(&dev->power.wait_queue);
1248}
1249
1250/**
1251 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1252 * @dev: Device object being removed from device hierarchy.
1253 */
1254void pm_runtime_remove(struct device *dev)
1255{
1256 __pm_runtime_disable(dev, false);
1257
1258 /* Change the status back to 'suspended' to match the initial status. */
1259 if (dev->power.runtime_status == RPM_ACTIVE)
1260 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001261 if (dev->power.irq_safe && dev->parent)
1262 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001263}