blob: 124dbf60c9bfc8daed3907721eba9567b43d7e67 [file] [log] [blame]
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001/*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02002 * drivers/base/power/runtime.c - Helper functions for device runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02003 *
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Alan Stern1bfee5b2010-09-25 23:35:00 +02005 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02006 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/sched.h>
11#include <linux/pm_runtime.h>
Ming Leic3dc2f12011-09-27 22:54:41 +020012#include <trace/events/rpm.h>
Alan Stern7490e442010-09-25 23:35:15 +020013#include "power.h"
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020014
Alan Stern140a6c92010-09-25 23:35:07 +020015static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020016static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020017
18/**
Alan Stern47693732010-09-25 23:34:46 +020019 * update_pm_runtime_accounting - Update the time accounting of power states
20 * @dev: Device to update the accounting for
21 *
22 * In order to be able to have time accounting of the various power states
23 * (as used by programs such as PowerTOP to show the effectiveness of runtime
24 * PM), we need to track the time spent in each state.
25 * update_pm_runtime_accounting must be called each time before the
26 * runtime_status field is updated, to account the time in the old state
27 * correctly.
28 */
29void update_pm_runtime_accounting(struct device *dev)
30{
31 unsigned long now = jiffies;
venu byravarasudef0c0a2011-11-03 10:12:14 +010032 unsigned long delta;
Alan Stern47693732010-09-25 23:34:46 +020033
34 delta = now - dev->power.accounting_timestamp;
35
Alan Stern47693732010-09-25 23:34:46 +020036 dev->power.accounting_timestamp = now;
37
38 if (dev->power.disable_depth > 0)
39 return;
40
41 if (dev->power.runtime_status == RPM_SUSPENDED)
42 dev->power.suspended_jiffies += delta;
43 else
44 dev->power.active_jiffies += delta;
45}
46
47static void __update_runtime_status(struct device *dev, enum rpm_status status)
48{
49 update_pm_runtime_accounting(dev);
50 dev->power.runtime_status = status;
51}
52
53/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020054 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
55 * @dev: Device to handle.
56 */
57static void pm_runtime_deactivate_timer(struct device *dev)
58{
59 if (dev->power.timer_expires > 0) {
60 del_timer(&dev->power.suspend_timer);
61 dev->power.timer_expires = 0;
62 }
63}
64
65/**
66 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
67 * @dev: Device to handle.
68 */
69static void pm_runtime_cancel_pending(struct device *dev)
70{
71 pm_runtime_deactivate_timer(dev);
72 /*
73 * In case there's a request pending, make sure its work function will
74 * return without doing anything.
75 */
76 dev->power.request = RPM_REQ_NONE;
77}
78
Alan Stern15bcb912010-09-25 23:35:21 +020079/*
80 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
81 * @dev: Device to handle.
82 *
83 * Compute the autosuspend-delay expiration time based on the device's
84 * power.last_busy time. If the delay has already expired or is disabled
85 * (negative) or the power.use_autosuspend flag isn't set, return 0.
86 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
87 *
88 * This function may be called either with or without dev->power.lock held.
89 * Either way it can be racy, since power.last_busy may be updated at any time.
90 */
91unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
92{
93 int autosuspend_delay;
94 long elapsed;
95 unsigned long last_busy;
96 unsigned long expires = 0;
97
98 if (!dev->power.use_autosuspend)
99 goto out;
100
101 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
102 if (autosuspend_delay < 0)
103 goto out;
104
105 last_busy = ACCESS_ONCE(dev->power.last_busy);
106 elapsed = jiffies - last_busy;
107 if (elapsed < 0)
108 goto out; /* jiffies has wrapped around. */
109
110 /*
111 * If the autosuspend_delay is >= 1 second, align the timer by rounding
112 * up to the nearest second.
113 */
114 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
115 if (autosuspend_delay >= 1000)
116 expires = round_jiffies(expires);
117 expires += !expires;
118 if (elapsed >= expires - last_busy)
119 expires = 0; /* Already expired. */
120
121 out:
122 return expires;
123}
124EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
125
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200126/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200127 * rpm_check_suspend_allowed - Test whether a device may be suspended.
128 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200129 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200130static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200131{
132 int retval = 0;
133
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200134 if (dev->power.runtime_error)
135 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200136 else if (dev->power.disable_depth > 0)
137 retval = -EACCES;
138 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200139 retval = -EAGAIN;
140 else if (!pm_children_suspended(dev))
141 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200142
143 /* Pending resume requests take precedence over suspends. */
144 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200145 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200146 || (dev->power.request_pending
147 && dev->power.request == RPM_REQ_RESUME))
148 retval = -EAGAIN;
149 else if (dev->power.runtime_status == RPM_SUSPENDED)
150 retval = 1;
151
152 return retval;
153}
154
Alan Stern1bfee5b2010-09-25 23:35:00 +0200155/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200156 * __rpm_callback - Run a given runtime PM callback for a given device.
157 * @cb: Runtime PM callback to run.
158 * @dev: Device to run the callback for.
159 */
160static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
161 __releases(&dev->power.lock) __acquires(&dev->power.lock)
162{
163 int retval;
164
165 if (dev->power.irq_safe)
166 spin_unlock(&dev->power.lock);
167 else
168 spin_unlock_irq(&dev->power.lock);
169
170 retval = cb(dev);
171
172 if (dev->power.irq_safe)
173 spin_lock(&dev->power.lock);
174 else
175 spin_lock_irq(&dev->power.lock);
176
177 return retval;
178}
179
180/**
Alan Stern140a6c92010-09-25 23:35:07 +0200181 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200182 * @dev: Device to notify the bus type about.
183 * @rpmflags: Flag bits.
184 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200185 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200186 * another idle notification has been started earlier, return immediately. If
187 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
188 * run the ->runtime_idle() callback directly.
189 *
190 * This function must be called under dev->power.lock with interrupts disabled.
191 */
Alan Stern140a6c92010-09-25 23:35:07 +0200192static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200193{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200194 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200195 int retval;
196
Ming Leic3dc2f12011-09-27 22:54:41 +0200197 trace_rpm_idle(dev, rpmflags);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200198 retval = rpm_check_suspend_allowed(dev);
199 if (retval < 0)
200 ; /* Conditions are wrong. */
201
202 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
203 else if (dev->power.runtime_status != RPM_ACTIVE)
204 retval = -EAGAIN;
205
206 /*
207 * Any pending request other than an idle notification takes
208 * precedence over us, except that the timer may be running.
209 */
210 else if (dev->power.request_pending &&
211 dev->power.request > RPM_REQ_IDLE)
212 retval = -EAGAIN;
213
214 /* Act as though RPM_NOWAIT is always set. */
215 else if (dev->power.idle_notification)
216 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200217 if (retval)
218 goto out;
219
Alan Stern1bfee5b2010-09-25 23:35:00 +0200220 /* Pending requests need to be canceled. */
221 dev->power.request = RPM_REQ_NONE;
222
Alan Stern7490e442010-09-25 23:35:15 +0200223 if (dev->power.no_callbacks) {
224 /* Assume ->runtime_idle() callback would have suspended. */
225 retval = rpm_suspend(dev, rpmflags);
226 goto out;
227 }
228
Alan Stern1bfee5b2010-09-25 23:35:00 +0200229 /* Carry out an asynchronous or a synchronous idle notification. */
230 if (rpmflags & RPM_ASYNC) {
231 dev->power.request = RPM_REQ_IDLE;
232 if (!dev->power.request_pending) {
233 dev->power.request_pending = true;
234 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200235 }
Alan Stern1bfee5b2010-09-25 23:35:00 +0200236 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200237 }
238
239 dev->power.idle_notification = true;
240
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200241 if (dev->pm_domain)
242 callback = dev->pm_domain->ops.runtime_idle;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200243 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200244 callback = dev->type->pm->runtime_idle;
245 else if (dev->class && dev->class->pm)
246 callback = dev->class->pm->runtime_idle;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100247 else if (dev->bus && dev->bus->pm)
248 callback = dev->bus->pm->runtime_idle;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200249 else
250 callback = NULL;
251
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200252 if (callback)
253 __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200254
255 dev->power.idle_notification = false;
256 wake_up_all(&dev->power.wait_queue);
257
258 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200259 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200260 return retval;
261}
262
263/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200264 * rpm_callback - Run a given runtime PM callback for a given device.
265 * @cb: Runtime PM callback to run.
266 * @dev: Device to run the callback for.
267 */
268static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200269{
270 int retval;
271
272 if (!cb)
273 return -ENOSYS;
274
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200275 retval = __rpm_callback(cb, dev);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200276
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200277 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200278 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200279}
280
281/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200282 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200283 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200284 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200285 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800286 * Check if the device's runtime PM status allows it to be suspended.
287 * Cancel a pending idle notification, autosuspend or suspend. If
288 * another suspend has been started earlier, either return immediately
289 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
290 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
Ming Lei857b36c2011-10-12 22:59:33 +0200291 * otherwise run the ->runtime_suspend() callback directly. When
292 * ->runtime_suspend succeeded, if a deferred resume was requested while
293 * the callback was running then carry it out, otherwise send an idle
294 * notification for its parent (if the suspend succeeded and both
295 * ignore_children of parent->power and irq_safe of dev->power are not set).
Alan Stern886486b2011-11-03 23:39:18 +0100296 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
297 * flag is set and the next autosuspend-delay expiration time is in the
298 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200299 *
300 * This function must be called under dev->power.lock with interrupts disabled.
301 */
Alan Stern140a6c92010-09-25 23:35:07 +0200302static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200303 __releases(&dev->power.lock) __acquires(&dev->power.lock)
304{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200305 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200306 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200307 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308
Ming Leic3dc2f12011-09-27 22:54:41 +0200309 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200310
311 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200312 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200313
Alan Stern1bfee5b2010-09-25 23:35:00 +0200314 if (retval < 0)
315 ; /* Conditions are wrong. */
316
317 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
318 else if (dev->power.runtime_status == RPM_RESUMING &&
319 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200320 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200321 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200322 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200323
Alan Stern15bcb912010-09-25 23:35:21 +0200324 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
325 if ((rpmflags & RPM_AUTO)
326 && dev->power.runtime_status != RPM_SUSPENDING) {
327 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
328
329 if (expires != 0) {
330 /* Pending requests need to be canceled. */
331 dev->power.request = RPM_REQ_NONE;
332
333 /*
334 * Optimization: If the timer is already running and is
335 * set to expire at or before the autosuspend delay,
336 * avoid the overhead of resetting it. Just let it
337 * expire; pm_suspend_timer_fn() will take care of the
338 * rest.
339 */
340 if (!(dev->power.timer_expires && time_before_eq(
341 dev->power.timer_expires, expires))) {
342 dev->power.timer_expires = expires;
343 mod_timer(&dev->power.suspend_timer, expires);
344 }
345 dev->power.timer_autosuspends = 1;
346 goto out;
347 }
348 }
349
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200350 /* Other scheduled or pending requests need to be canceled. */
351 pm_runtime_cancel_pending(dev);
352
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200353 if (dev->power.runtime_status == RPM_SUSPENDING) {
354 DEFINE_WAIT(wait);
355
Alan Stern1bfee5b2010-09-25 23:35:00 +0200356 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200357 retval = -EINPROGRESS;
358 goto out;
359 }
360
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200361 if (dev->power.irq_safe) {
362 spin_unlock(&dev->power.lock);
363
364 cpu_relax();
365
366 spin_lock(&dev->power.lock);
367 goto repeat;
368 }
369
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200370 /* Wait for the other suspend running in parallel with us. */
371 for (;;) {
372 prepare_to_wait(&dev->power.wait_queue, &wait,
373 TASK_UNINTERRUPTIBLE);
374 if (dev->power.runtime_status != RPM_SUSPENDING)
375 break;
376
377 spin_unlock_irq(&dev->power.lock);
378
379 schedule();
380
381 spin_lock_irq(&dev->power.lock);
382 }
383 finish_wait(&dev->power.wait_queue, &wait);
384 goto repeat;
385 }
386
Alan Stern7490e442010-09-25 23:35:15 +0200387 dev->power.deferred_resume = false;
388 if (dev->power.no_callbacks)
389 goto no_callback; /* Assume success. */
390
Alan Stern1bfee5b2010-09-25 23:35:00 +0200391 /* Carry out an asynchronous or a synchronous suspend. */
392 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb912010-09-25 23:35:21 +0200393 dev->power.request = (rpmflags & RPM_AUTO) ?
394 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200395 if (!dev->power.request_pending) {
396 dev->power.request_pending = true;
397 queue_work(pm_wq, &dev->power.work);
398 }
399 goto out;
400 }
401
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200402 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200403
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200404 if (dev->pm_domain)
405 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200406 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200407 callback = dev->type->pm->runtime_suspend;
408 else if (dev->class && dev->class->pm)
409 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100410 else if (dev->bus && dev->bus->pm)
411 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200412 else
413 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200414
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200415 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200416 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200417 __update_runtime_status(dev, RPM_ACTIVE);
ShuoX Liu2cffff12011-07-08 20:53:55 +0200418 dev->power.deferred_resume = false;
Alan Stern886486b2011-11-03 23:39:18 +0100419 if (retval == -EAGAIN || retval == -EBUSY) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200420 dev->power.runtime_error = 0;
Alan Stern886486b2011-11-03 23:39:18 +0100421
422 /*
423 * If the callback routine failed an autosuspend, and
424 * if the last_busy time has been updated so that there
425 * is a new autosuspend expiration time, automatically
426 * reschedule another autosuspend.
427 */
428 if ((rpmflags & RPM_AUTO) &&
429 pm_runtime_autosuspend_expiration(dev) != 0)
430 goto repeat;
431 } else {
Alan Stern240c7332010-03-23 00:50:07 +0100432 pm_runtime_cancel_pending(dev);
Alan Stern886486b2011-11-03 23:39:18 +0100433 }
Ming Lei857b36c2011-10-12 22:59:33 +0200434 wake_up_all(&dev->power.wait_queue);
435 goto out;
436 }
Alan Stern7490e442010-09-25 23:35:15 +0200437 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200438 __update_runtime_status(dev, RPM_SUSPENDED);
439 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200440
Ming Lei857b36c2011-10-12 22:59:33 +0200441 if (dev->parent) {
442 parent = dev->parent;
443 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200444 }
445 wake_up_all(&dev->power.wait_queue);
446
447 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200448 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200449 retval = -EAGAIN;
450 goto out;
451 }
452
Alan Sternc3810c82011-01-25 20:50:07 +0100453 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100454 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100455 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200456
Alan Sternc3810c82011-01-25 20:50:07 +0100457 spin_lock(&parent->power.lock);
458 rpm_idle(parent, RPM_ASYNC);
459 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200460
Alan Sternc3810c82011-01-25 20:50:07 +0100461 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200462 }
463
464 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200465 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200466
467 return retval;
468}
469
470/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200471 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200472 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200473 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200474 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200475 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200476 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300477 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200478 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
479 * parallel with this function, either tell the other process to resume after
480 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
481 * flag is set then queue a resume request; otherwise run the
482 * ->runtime_resume() callback directly. Queue an idle notification for the
483 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200484 *
485 * This function must be called under dev->power.lock with interrupts disabled.
486 */
Alan Stern140a6c92010-09-25 23:35:07 +0200487static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200488 __releases(&dev->power.lock) __acquires(&dev->power.lock)
489{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200490 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200491 struct device *parent = NULL;
492 int retval = 0;
493
Ming Leic3dc2f12011-09-27 22:54:41 +0200494 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200495
496 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200497 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200498 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200499 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200500 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200501 if (retval)
502 goto out;
503
Alan Stern15bcb912010-09-25 23:35:21 +0200504 /*
505 * Other scheduled or pending requests need to be canceled. Small
506 * optimization: If an autosuspend timer is running, leave it running
507 * rather than cancelling it now only to restart it again in the near
508 * future.
509 */
510 dev->power.request = RPM_REQ_NONE;
511 if (!dev->power.timer_autosuspends)
512 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200513
514 if (dev->power.runtime_status == RPM_ACTIVE) {
515 retval = 1;
516 goto out;
517 }
518
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200519 if (dev->power.runtime_status == RPM_RESUMING
520 || dev->power.runtime_status == RPM_SUSPENDING) {
521 DEFINE_WAIT(wait);
522
Alan Stern1bfee5b2010-09-25 23:35:00 +0200523 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200524 if (dev->power.runtime_status == RPM_SUSPENDING)
525 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200526 else
527 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200528 goto out;
529 }
530
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200531 if (dev->power.irq_safe) {
532 spin_unlock(&dev->power.lock);
533
534 cpu_relax();
535
536 spin_lock(&dev->power.lock);
537 goto repeat;
538 }
539
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200540 /* Wait for the operation carried out in parallel with us. */
541 for (;;) {
542 prepare_to_wait(&dev->power.wait_queue, &wait,
543 TASK_UNINTERRUPTIBLE);
544 if (dev->power.runtime_status != RPM_RESUMING
545 && dev->power.runtime_status != RPM_SUSPENDING)
546 break;
547
548 spin_unlock_irq(&dev->power.lock);
549
550 schedule();
551
552 spin_lock_irq(&dev->power.lock);
553 }
554 finish_wait(&dev->power.wait_queue, &wait);
555 goto repeat;
556 }
557
Alan Stern7490e442010-09-25 23:35:15 +0200558 /*
559 * See if we can skip waking up the parent. This is safe only if
560 * power.no_callbacks is set, because otherwise we don't know whether
561 * the resume will actually succeed.
562 */
563 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200564 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200565 if (dev->parent->power.disable_depth > 0
566 || dev->parent->power.ignore_children
567 || dev->parent->power.runtime_status == RPM_ACTIVE) {
568 atomic_inc(&dev->parent->power.child_count);
569 spin_unlock(&dev->parent->power.lock);
570 goto no_callback; /* Assume success. */
571 }
572 spin_unlock(&dev->parent->power.lock);
573 }
574
Alan Stern1bfee5b2010-09-25 23:35:00 +0200575 /* Carry out an asynchronous or a synchronous resume. */
576 if (rpmflags & RPM_ASYNC) {
577 dev->power.request = RPM_REQ_RESUME;
578 if (!dev->power.request_pending) {
579 dev->power.request_pending = true;
580 queue_work(pm_wq, &dev->power.work);
581 }
582 retval = 0;
583 goto out;
584 }
585
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200586 if (!parent && dev->parent) {
587 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100588 * Increment the parent's usage counter and resume it if
589 * necessary. Not needed if dev is irq-safe; then the
590 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200591 */
592 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100593 if (dev->power.irq_safe)
594 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100595 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200596
597 pm_runtime_get_noresume(parent);
598
Alan Stern862f89b2009-11-25 01:06:37 +0100599 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200600 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200601 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602 * is set to ignore children.
603 */
604 if (!parent->power.disable_depth
605 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200606 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200607 if (parent->power.runtime_status != RPM_ACTIVE)
608 retval = -EBUSY;
609 }
Alan Stern862f89b2009-11-25 01:06:37 +0100610 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200611
Alan Stern862f89b2009-11-25 01:06:37 +0100612 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200613 if (retval)
614 goto out;
615 goto repeat;
616 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100617 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200618
Alan Stern7490e442010-09-25 23:35:15 +0200619 if (dev->power.no_callbacks)
620 goto no_callback; /* Assume success. */
621
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200622 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200623
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200624 if (dev->pm_domain)
625 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200626 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200627 callback = dev->type->pm->runtime_resume;
628 else if (dev->class && dev->class->pm)
629 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100630 else if (dev->bus && dev->bus->pm)
631 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200632 else
633 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200634
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200635 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200636 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200637 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200638 pm_runtime_cancel_pending(dev);
639 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200640 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200641 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200642 if (parent)
643 atomic_inc(&parent->power.child_count);
644 }
645 wake_up_all(&dev->power.wait_queue);
646
647 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200648 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200649
650 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100651 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200652 spin_unlock_irq(&dev->power.lock);
653
654 pm_runtime_put(parent);
655
656 spin_lock_irq(&dev->power.lock);
657 }
658
Ming Leic3dc2f12011-09-27 22:54:41 +0200659 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200660
661 return retval;
662}
663
664/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200665 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200666 * @work: Work structure used for scheduling the execution of this function.
667 *
668 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200669 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200670 */
671static void pm_runtime_work(struct work_struct *work)
672{
673 struct device *dev = container_of(work, struct device, power.work);
674 enum rpm_request req;
675
676 spin_lock_irq(&dev->power.lock);
677
678 if (!dev->power.request_pending)
679 goto out;
680
681 req = dev->power.request;
682 dev->power.request = RPM_REQ_NONE;
683 dev->power.request_pending = false;
684
685 switch (req) {
686 case RPM_REQ_NONE:
687 break;
688 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200689 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200690 break;
691 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200692 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200693 break;
Alan Stern15bcb912010-09-25 23:35:21 +0200694 case RPM_REQ_AUTOSUSPEND:
695 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
696 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200697 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200698 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200699 break;
700 }
701
702 out:
703 spin_unlock_irq(&dev->power.lock);
704}
705
706/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200707 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
708 * @data: Device pointer passed by pm_schedule_suspend().
709 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200710 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200711 */
712static void pm_suspend_timer_fn(unsigned long data)
713{
714 struct device *dev = (struct device *)data;
715 unsigned long flags;
716 unsigned long expires;
717
718 spin_lock_irqsave(&dev->power.lock, flags);
719
720 expires = dev->power.timer_expires;
721 /* If 'expire' is after 'jiffies' we've been called too early. */
722 if (expires > 0 && !time_after(expires, jiffies)) {
723 dev->power.timer_expires = 0;
Alan Stern15bcb912010-09-25 23:35:21 +0200724 rpm_suspend(dev, dev->power.timer_autosuspends ?
725 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200726 }
727
728 spin_unlock_irqrestore(&dev->power.lock, flags);
729}
730
731/**
732 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
733 * @dev: Device to suspend.
734 * @delay: Time to wait before submitting a suspend request, in milliseconds.
735 */
736int pm_schedule_suspend(struct device *dev, unsigned int delay)
737{
738 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200739 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200740
741 spin_lock_irqsave(&dev->power.lock, flags);
742
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200743 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200744 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200745 goto out;
746 }
747
Alan Stern1bfee5b2010-09-25 23:35:00 +0200748 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200749 if (retval)
750 goto out;
751
Alan Stern1bfee5b2010-09-25 23:35:00 +0200752 /* Other scheduled or pending requests need to be canceled. */
753 pm_runtime_cancel_pending(dev);
754
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200755 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200756 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb912010-09-25 23:35:21 +0200757 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200758 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
759
760 out:
761 spin_unlock_irqrestore(&dev->power.lock, flags);
762
763 return retval;
764}
765EXPORT_SYMBOL_GPL(pm_schedule_suspend);
766
767/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200768 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200769 * @dev: Device to send idle notification for.
770 * @rpmflags: Flag bits.
771 *
772 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
773 * return immediately if it is larger than zero. Then carry out an idle
774 * notification, either synchronous or asynchronous.
775 *
Colin Cross311aab72011-08-08 23:39:36 +0200776 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
777 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200778 */
Alan Stern140a6c92010-09-25 23:35:07 +0200779int __pm_runtime_idle(struct device *dev, int rpmflags)
780{
781 unsigned long flags;
782 int retval;
783
Colin Cross311aab72011-08-08 23:39:36 +0200784 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
785
Alan Stern140a6c92010-09-25 23:35:07 +0200786 if (rpmflags & RPM_GET_PUT) {
787 if (!atomic_dec_and_test(&dev->power.usage_count))
788 return 0;
789 }
790
791 spin_lock_irqsave(&dev->power.lock, flags);
792 retval = rpm_idle(dev, rpmflags);
793 spin_unlock_irqrestore(&dev->power.lock, flags);
794
795 return retval;
796}
797EXPORT_SYMBOL_GPL(__pm_runtime_idle);
798
799/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200800 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200801 * @dev: Device to suspend.
802 * @rpmflags: Flag bits.
803 *
Alan Stern15bcb912010-09-25 23:35:21 +0200804 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
805 * return immediately if it is larger than zero. Then carry out a suspend,
806 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200807 *
Colin Cross311aab72011-08-08 23:39:36 +0200808 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
809 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200810 */
811int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200812{
813 unsigned long flags;
814 int retval;
815
Colin Cross311aab72011-08-08 23:39:36 +0200816 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
817
Alan Stern15bcb912010-09-25 23:35:21 +0200818 if (rpmflags & RPM_GET_PUT) {
819 if (!atomic_dec_and_test(&dev->power.usage_count))
820 return 0;
821 }
822
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200823 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200824 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200825 spin_unlock_irqrestore(&dev->power.lock, flags);
826
827 return retval;
828}
Alan Stern140a6c92010-09-25 23:35:07 +0200829EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200830
831/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200832 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200833 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200834 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200835 *
Alan Stern140a6c92010-09-25 23:35:07 +0200836 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
837 * carry out a resume, either synchronous or asynchronous.
838 *
Colin Cross311aab72011-08-08 23:39:36 +0200839 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
840 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200841 */
Alan Stern140a6c92010-09-25 23:35:07 +0200842int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200843{
Alan Stern140a6c92010-09-25 23:35:07 +0200844 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100845 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200846
Colin Cross311aab72011-08-08 23:39:36 +0200847 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
848
Alan Stern140a6c92010-09-25 23:35:07 +0200849 if (rpmflags & RPM_GET_PUT)
850 atomic_inc(&dev->power.usage_count);
851
852 spin_lock_irqsave(&dev->power.lock, flags);
853 retval = rpm_resume(dev, rpmflags);
854 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200855
856 return retval;
857}
Alan Stern140a6c92010-09-25 23:35:07 +0200858EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200859
860/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200861 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200862 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200863 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200864 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200865 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200866 * different from zero, the status may be changed either to RPM_ACTIVE, or to
867 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
868 * However, if the device has a parent and the parent is not active, and the
869 * parent's power.ignore_children flag is unset, the device's status cannot be
870 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
871 *
872 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
873 * and the device parent's counter of unsuspended children is modified to
874 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
875 * notification request for the parent is submitted.
876 */
877int __pm_runtime_set_status(struct device *dev, unsigned int status)
878{
879 struct device *parent = dev->parent;
880 unsigned long flags;
881 bool notify_parent = false;
882 int error = 0;
883
884 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
885 return -EINVAL;
886
887 spin_lock_irqsave(&dev->power.lock, flags);
888
889 if (!dev->power.runtime_error && !dev->power.disable_depth) {
890 error = -EAGAIN;
891 goto out;
892 }
893
894 if (dev->power.runtime_status == status)
895 goto out_set;
896
897 if (status == RPM_SUSPENDED) {
898 /* It always is possible to set the status to 'suspended'. */
899 if (parent) {
900 atomic_add_unless(&parent->power.child_count, -1, 0);
901 notify_parent = !parent->power.ignore_children;
902 }
903 goto out_set;
904 }
905
906 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100907 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200908
909 /*
910 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200911 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200912 * 'power.ignore_children' flag unset.
913 */
914 if (!parent->power.disable_depth
915 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100916 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200917 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100918 else if (dev->power.runtime_status == RPM_SUSPENDED)
919 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200920
Alan Stern862f89b2009-11-25 01:06:37 +0100921 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200922
923 if (error)
924 goto out;
925 }
926
927 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200928 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200929 dev->power.runtime_error = 0;
930 out:
931 spin_unlock_irqrestore(&dev->power.lock, flags);
932
933 if (notify_parent)
934 pm_request_idle(parent);
935
936 return error;
937}
938EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
939
940/**
941 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
942 * @dev: Device to handle.
943 *
944 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200945 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200946 *
947 * Should be called under dev->power.lock with interrupts disabled.
948 */
949static void __pm_runtime_barrier(struct device *dev)
950{
951 pm_runtime_deactivate_timer(dev);
952
953 if (dev->power.request_pending) {
954 dev->power.request = RPM_REQ_NONE;
955 spin_unlock_irq(&dev->power.lock);
956
957 cancel_work_sync(&dev->power.work);
958
959 spin_lock_irq(&dev->power.lock);
960 dev->power.request_pending = false;
961 }
962
963 if (dev->power.runtime_status == RPM_SUSPENDING
964 || dev->power.runtime_status == RPM_RESUMING
965 || dev->power.idle_notification) {
966 DEFINE_WAIT(wait);
967
968 /* Suspend, wake-up or idle notification in progress. */
969 for (;;) {
970 prepare_to_wait(&dev->power.wait_queue, &wait,
971 TASK_UNINTERRUPTIBLE);
972 if (dev->power.runtime_status != RPM_SUSPENDING
973 && dev->power.runtime_status != RPM_RESUMING
974 && !dev->power.idle_notification)
975 break;
976 spin_unlock_irq(&dev->power.lock);
977
978 schedule();
979
980 spin_lock_irq(&dev->power.lock);
981 }
982 finish_wait(&dev->power.wait_queue, &wait);
983 }
984}
985
986/**
987 * pm_runtime_barrier - Flush pending requests and wait for completions.
988 * @dev: Device to handle.
989 *
990 * Prevent the device from being suspended by incrementing its usage counter and
991 * if there's a pending resume request for the device, wake the device up.
992 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200993 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200994 * progress to complete.
995 *
996 * Return value:
997 * 1, if there was a resume request pending and the device had to be woken up,
998 * 0, otherwise
999 */
1000int pm_runtime_barrier(struct device *dev)
1001{
1002 int retval = 0;
1003
1004 pm_runtime_get_noresume(dev);
1005 spin_lock_irq(&dev->power.lock);
1006
1007 if (dev->power.request_pending
1008 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +02001009 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001010 retval = 1;
1011 }
1012
1013 __pm_runtime_barrier(dev);
1014
1015 spin_unlock_irq(&dev->power.lock);
1016 pm_runtime_put_noidle(dev);
1017
1018 return retval;
1019}
1020EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1021
1022/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001023 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001024 * @dev: Device to handle.
1025 * @check_resume: If set, check if there's a resume request for the device.
1026 *
1027 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001028 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001029 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001030 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001031 *
1032 * If @check_resume is set and there's a resume request pending when
1033 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001034 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001035 */
1036void __pm_runtime_disable(struct device *dev, bool check_resume)
1037{
1038 spin_lock_irq(&dev->power.lock);
1039
1040 if (dev->power.disable_depth > 0) {
1041 dev->power.disable_depth++;
1042 goto out;
1043 }
1044
1045 /*
1046 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001047 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001048 * shouldn't prevent the device from processing the I/O.
1049 */
1050 if (check_resume && dev->power.request_pending
1051 && dev->power.request == RPM_REQ_RESUME) {
1052 /*
1053 * Prevent suspends and idle notifications from being carried
1054 * out after we have woken up the device.
1055 */
1056 pm_runtime_get_noresume(dev);
1057
Alan Stern140a6c92010-09-25 23:35:07 +02001058 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001059
1060 pm_runtime_put_noidle(dev);
1061 }
1062
1063 if (!dev->power.disable_depth++)
1064 __pm_runtime_barrier(dev);
1065
1066 out:
1067 spin_unlock_irq(&dev->power.lock);
1068}
1069EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1070
1071/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001072 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001073 * @dev: Device to handle.
1074 */
1075void pm_runtime_enable(struct device *dev)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&dev->power.lock, flags);
1080
1081 if (dev->power.disable_depth > 0)
1082 dev->power.disable_depth--;
1083 else
1084 dev_warn(dev, "Unbalanced %s!\n", __func__);
1085
1086 spin_unlock_irqrestore(&dev->power.lock, flags);
1087}
1088EXPORT_SYMBOL_GPL(pm_runtime_enable);
1089
1090/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001091 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001092 * @dev: Device to handle.
1093 *
1094 * Increase the device's usage count and clear its power.runtime_auto flag,
1095 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1096 * for it.
1097 */
1098void pm_runtime_forbid(struct device *dev)
1099{
1100 spin_lock_irq(&dev->power.lock);
1101 if (!dev->power.runtime_auto)
1102 goto out;
1103
1104 dev->power.runtime_auto = false;
1105 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001106 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001107
1108 out:
1109 spin_unlock_irq(&dev->power.lock);
1110}
1111EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1112
1113/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001114 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001115 * @dev: Device to handle.
1116 *
1117 * Decrease the device's usage count and set its power.runtime_auto flag.
1118 */
1119void pm_runtime_allow(struct device *dev)
1120{
1121 spin_lock_irq(&dev->power.lock);
1122 if (dev->power.runtime_auto)
1123 goto out;
1124
1125 dev->power.runtime_auto = true;
1126 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb912010-09-25 23:35:21 +02001127 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001128
1129 out:
1130 spin_unlock_irq(&dev->power.lock);
1131}
1132EXPORT_SYMBOL_GPL(pm_runtime_allow);
1133
1134/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001135 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001136 * @dev: Device to handle.
1137 *
1138 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001139 * device is power-managed through its parent and has no runtime PM
1140 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001141 */
1142void pm_runtime_no_callbacks(struct device *dev)
1143{
1144 spin_lock_irq(&dev->power.lock);
1145 dev->power.no_callbacks = 1;
1146 spin_unlock_irq(&dev->power.lock);
1147 if (device_is_registered(dev))
1148 rpm_sysfs_remove(dev);
1149}
1150EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1151
1152/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001153 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1154 * @dev: Device to handle
1155 *
1156 * Set the power.irq_safe flag, which tells the PM core that the
1157 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1158 * always be invoked with the spinlock held and interrupts disabled. It also
1159 * causes the parent's usage counter to be permanently incremented, preventing
1160 * the parent from runtime suspending -- otherwise an irq-safe child might have
1161 * to wait for a non-irq-safe parent.
1162 */
1163void pm_runtime_irq_safe(struct device *dev)
1164{
1165 if (dev->parent)
1166 pm_runtime_get_sync(dev->parent);
1167 spin_lock_irq(&dev->power.lock);
1168 dev->power.irq_safe = 1;
1169 spin_unlock_irq(&dev->power.lock);
1170}
1171EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1172
1173/**
Alan Stern15bcb912010-09-25 23:35:21 +02001174 * update_autosuspend - Handle a change to a device's autosuspend settings.
1175 * @dev: Device to handle.
1176 * @old_delay: The former autosuspend_delay value.
1177 * @old_use: The former use_autosuspend value.
1178 *
1179 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1180 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1181 *
1182 * This function must be called under dev->power.lock with interrupts disabled.
1183 */
1184static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1185{
1186 int delay = dev->power.autosuspend_delay;
1187
1188 /* Should runtime suspend be prevented now? */
1189 if (dev->power.use_autosuspend && delay < 0) {
1190
1191 /* If it used to be allowed then prevent it. */
1192 if (!old_use || old_delay >= 0) {
1193 atomic_inc(&dev->power.usage_count);
1194 rpm_resume(dev, 0);
1195 }
1196 }
1197
1198 /* Runtime suspend should be allowed now. */
1199 else {
1200
1201 /* If it used to be prevented then allow it. */
1202 if (old_use && old_delay < 0)
1203 atomic_dec(&dev->power.usage_count);
1204
1205 /* Maybe we can autosuspend now. */
1206 rpm_idle(dev, RPM_AUTO);
1207 }
1208}
1209
1210/**
1211 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1212 * @dev: Device to handle.
1213 * @delay: Value of the new delay in milliseconds.
1214 *
1215 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001216 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1217 * changes the other way, allow runtime suspends.
Alan Stern15bcb912010-09-25 23:35:21 +02001218 */
1219void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1220{
1221 int old_delay, old_use;
1222
1223 spin_lock_irq(&dev->power.lock);
1224 old_delay = dev->power.autosuspend_delay;
1225 old_use = dev->power.use_autosuspend;
1226 dev->power.autosuspend_delay = delay;
1227 update_autosuspend(dev, old_delay, old_use);
1228 spin_unlock_irq(&dev->power.lock);
1229}
1230EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1231
1232/**
1233 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1234 * @dev: Device to handle.
1235 * @use: New value for use_autosuspend.
1236 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001237 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb912010-09-25 23:35:21 +02001238 * suspends as needed.
1239 */
1240void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1241{
1242 int old_delay, old_use;
1243
1244 spin_lock_irq(&dev->power.lock);
1245 old_delay = dev->power.autosuspend_delay;
1246 old_use = dev->power.use_autosuspend;
1247 dev->power.use_autosuspend = use;
1248 update_autosuspend(dev, old_delay, old_use);
1249 spin_unlock_irq(&dev->power.lock);
1250}
1251EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1252
1253/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001254 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001255 * @dev: Device object to initialize.
1256 */
1257void pm_runtime_init(struct device *dev)
1258{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001259 dev->power.runtime_status = RPM_SUSPENDED;
1260 dev->power.idle_notification = false;
1261
1262 dev->power.disable_depth = 1;
1263 atomic_set(&dev->power.usage_count, 0);
1264
1265 dev->power.runtime_error = 0;
1266
1267 atomic_set(&dev->power.child_count, 0);
1268 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001269 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001270
1271 dev->power.request_pending = false;
1272 dev->power.request = RPM_REQ_NONE;
1273 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001274 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001275 INIT_WORK(&dev->power.work, pm_runtime_work);
1276
1277 dev->power.timer_expires = 0;
1278 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1279 (unsigned long)dev);
1280
1281 init_waitqueue_head(&dev->power.wait_queue);
1282}
1283
1284/**
1285 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1286 * @dev: Device object being removed from device hierarchy.
1287 */
1288void pm_runtime_remove(struct device *dev)
1289{
1290 __pm_runtime_disable(dev, false);
1291
1292 /* Change the status back to 'suspended' to match the initial status. */
1293 if (dev->power.runtime_status == RPM_ACTIVE)
1294 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001295 if (dev->power.irq_safe && dev->parent)
1296 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001297}