blob: 3148b10dc2e59b92dd5913e2b34c446d54ac69b7 [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>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040011#include <linux/export.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020012#include <linux/pm_runtime.h>
Ming Leic3dc2f12011-09-27 22:54:41 +020013#include <trace/events/rpm.h>
Alan Stern7490e442010-09-25 23:35:15 +020014#include "power.h"
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020015
Alan Stern140a6c92010-09-25 23:35:07 +020016static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020017static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020018
19/**
Alan Stern47693732010-09-25 23:34:46 +020020 * update_pm_runtime_accounting - Update the time accounting of power states
21 * @dev: Device to update the accounting for
22 *
23 * In order to be able to have time accounting of the various power states
24 * (as used by programs such as PowerTOP to show the effectiveness of runtime
25 * PM), we need to track the time spent in each state.
26 * update_pm_runtime_accounting must be called each time before the
27 * runtime_status field is updated, to account the time in the old state
28 * correctly.
29 */
30void update_pm_runtime_accounting(struct device *dev)
31{
32 unsigned long now = jiffies;
venu byravarasudef0c0a32011-11-03 10:12:14 +010033 unsigned long delta;
Alan Stern47693732010-09-25 23:34:46 +020034
35 delta = now - dev->power.accounting_timestamp;
36
Alan Stern47693732010-09-25 23:34:46 +020037 dev->power.accounting_timestamp = now;
38
39 if (dev->power.disable_depth > 0)
40 return;
41
42 if (dev->power.runtime_status == RPM_SUSPENDED)
43 dev->power.suspended_jiffies += delta;
44 else
45 dev->power.active_jiffies += delta;
46}
47
48static void __update_runtime_status(struct device *dev, enum rpm_status status)
49{
50 update_pm_runtime_accounting(dev);
51 dev->power.runtime_status = status;
52}
53
54/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020055 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
56 * @dev: Device to handle.
57 */
58static void pm_runtime_deactivate_timer(struct device *dev)
59{
60 if (dev->power.timer_expires > 0) {
61 del_timer(&dev->power.suspend_timer);
62 dev->power.timer_expires = 0;
63 }
64}
65
66/**
67 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
68 * @dev: Device to handle.
69 */
70static void pm_runtime_cancel_pending(struct device *dev)
71{
72 pm_runtime_deactivate_timer(dev);
73 /*
74 * In case there's a request pending, make sure its work function will
75 * return without doing anything.
76 */
77 dev->power.request = RPM_REQ_NONE;
78}
79
Alan Stern15bcb91d2010-09-25 23:35:21 +020080/*
81 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
82 * @dev: Device to handle.
83 *
84 * Compute the autosuspend-delay expiration time based on the device's
85 * power.last_busy time. If the delay has already expired or is disabled
86 * (negative) or the power.use_autosuspend flag isn't set, return 0.
87 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
88 *
89 * This function may be called either with or without dev->power.lock held.
90 * Either way it can be racy, since power.last_busy may be updated at any time.
91 */
92unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
93{
94 int autosuspend_delay;
95 long elapsed;
96 unsigned long last_busy;
97 unsigned long expires = 0;
98
99 if (!dev->power.use_autosuspend)
100 goto out;
101
102 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
103 if (autosuspend_delay < 0)
104 goto out;
105
106 last_busy = ACCESS_ONCE(dev->power.last_busy);
107 elapsed = jiffies - last_busy;
108 if (elapsed < 0)
109 goto out; /* jiffies has wrapped around. */
110
111 /*
112 * If the autosuspend_delay is >= 1 second, align the timer by rounding
113 * up to the nearest second.
114 */
115 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
116 if (autosuspend_delay >= 1000)
117 expires = round_jiffies(expires);
118 expires += !expires;
119 if (elapsed >= expires - last_busy)
120 expires = 0; /* Already expired. */
121
122 out:
123 return expires;
124}
125EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
126
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200127/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200128 * rpm_check_suspend_allowed - Test whether a device may be suspended.
129 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200130 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200131static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200132{
133 int retval = 0;
134
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200135 if (dev->power.runtime_error)
136 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200137 else if (dev->power.disable_depth > 0)
138 retval = -EACCES;
139 else if (atomic_read(&dev->power.usage_count) > 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;
Rafael J. Wysocki55d7ec42012-08-15 21:32:04 +0200150 else if (__dev_pm_qos_read_value(dev) < 0)
151 retval = -EPERM;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200152 else if (dev->power.runtime_status == RPM_SUSPENDED)
153 retval = 1;
154
155 return retval;
156}
157
Alan Stern1bfee5b2010-09-25 23:35:00 +0200158/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200159 * __rpm_callback - Run a given runtime PM callback for a given device.
160 * @cb: Runtime PM callback to run.
161 * @dev: Device to run the callback for.
162 */
163static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
164 __releases(&dev->power.lock) __acquires(&dev->power.lock)
165{
166 int retval;
167
168 if (dev->power.irq_safe)
169 spin_unlock(&dev->power.lock);
170 else
171 spin_unlock_irq(&dev->power.lock);
172
173 retval = cb(dev);
174
175 if (dev->power.irq_safe)
176 spin_lock(&dev->power.lock);
177 else
178 spin_lock_irq(&dev->power.lock);
179
180 return retval;
181}
182
183/**
Alan Stern140a6c92010-09-25 23:35:07 +0200184 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200185 * @dev: Device to notify the bus type about.
186 * @rpmflags: Flag bits.
187 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200188 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200189 * another idle notification has been started earlier, return immediately. If
190 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
191 * run the ->runtime_idle() callback directly.
192 *
193 * This function must be called under dev->power.lock with interrupts disabled.
194 */
Alan Stern140a6c92010-09-25 23:35:07 +0200195static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200196{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200197 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200198 int retval;
199
Ming Leic3dc2f12011-09-27 22:54:41 +0200200 trace_rpm_idle(dev, rpmflags);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200201 retval = rpm_check_suspend_allowed(dev);
202 if (retval < 0)
203 ; /* Conditions are wrong. */
204
205 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
206 else if (dev->power.runtime_status != RPM_ACTIVE)
207 retval = -EAGAIN;
208
209 /*
210 * Any pending request other than an idle notification takes
211 * precedence over us, except that the timer may be running.
212 */
213 else if (dev->power.request_pending &&
214 dev->power.request > RPM_REQ_IDLE)
215 retval = -EAGAIN;
216
217 /* Act as though RPM_NOWAIT is always set. */
218 else if (dev->power.idle_notification)
219 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200220 if (retval)
221 goto out;
222
Alan Stern1bfee5b2010-09-25 23:35:00 +0200223 /* Pending requests need to be canceled. */
224 dev->power.request = RPM_REQ_NONE;
225
Alan Stern7490e442010-09-25 23:35:15 +0200226 if (dev->power.no_callbacks) {
227 /* Assume ->runtime_idle() callback would have suspended. */
228 retval = rpm_suspend(dev, rpmflags);
229 goto out;
230 }
231
Alan Stern1bfee5b2010-09-25 23:35:00 +0200232 /* Carry out an asynchronous or a synchronous idle notification. */
233 if (rpmflags & RPM_ASYNC) {
234 dev->power.request = RPM_REQ_IDLE;
235 if (!dev->power.request_pending) {
236 dev->power.request_pending = true;
237 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200238 }
Alan Stern1bfee5b2010-09-25 23:35:00 +0200239 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200240 }
241
242 dev->power.idle_notification = true;
243
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200244 if (dev->pm_domain)
245 callback = dev->pm_domain->ops.runtime_idle;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200246 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200247 callback = dev->type->pm->runtime_idle;
248 else if (dev->class && dev->class->pm)
249 callback = dev->class->pm->runtime_idle;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100250 else if (dev->bus && dev->bus->pm)
251 callback = dev->bus->pm->runtime_idle;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200252 else
253 callback = NULL;
254
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100255 if (!callback && dev->driver && dev->driver->pm)
256 callback = dev->driver->pm->runtime_idle;
257
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200258 if (callback)
259 __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200260
261 dev->power.idle_notification = false;
262 wake_up_all(&dev->power.wait_queue);
263
264 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200265 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200266 return retval;
267}
268
269/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200270 * rpm_callback - Run a given runtime PM callback for a given device.
271 * @cb: Runtime PM callback to run.
272 * @dev: Device to run the callback for.
273 */
274static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200275{
276 int retval;
277
278 if (!cb)
279 return -ENOSYS;
280
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200281 retval = __rpm_callback(cb, dev);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200282
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200283 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200284 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200285}
286
287/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200288 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200289 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200290 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200291 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800292 * Check if the device's runtime PM status allows it to be suspended.
293 * Cancel a pending idle notification, autosuspend or suspend. If
294 * another suspend has been started earlier, either return immediately
295 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
296 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
Ming Lei857b36c2011-10-12 22:59:33 +0200297 * otherwise run the ->runtime_suspend() callback directly. When
298 * ->runtime_suspend succeeded, if a deferred resume was requested while
299 * the callback was running then carry it out, otherwise send an idle
300 * notification for its parent (if the suspend succeeded and both
301 * ignore_children of parent->power and irq_safe of dev->power are not set).
Alan Stern886486b2011-11-03 23:39:18 +0100302 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
303 * flag is set and the next autosuspend-delay expiration time is in the
304 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200305 *
306 * This function must be called under dev->power.lock with interrupts disabled.
307 */
Alan Stern140a6c92010-09-25 23:35:07 +0200308static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200309 __releases(&dev->power.lock) __acquires(&dev->power.lock)
310{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200311 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200312 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200313 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200314
Ming Leic3dc2f12011-09-27 22:54:41 +0200315 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200316
317 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200318 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200319
Alan Stern1bfee5b2010-09-25 23:35:00 +0200320 if (retval < 0)
321 ; /* Conditions are wrong. */
322
323 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
324 else if (dev->power.runtime_status == RPM_RESUMING &&
325 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200326 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200327 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200328 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200329
Alan Stern15bcb91d2010-09-25 23:35:21 +0200330 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
331 if ((rpmflags & RPM_AUTO)
332 && dev->power.runtime_status != RPM_SUSPENDING) {
333 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
334
335 if (expires != 0) {
336 /* Pending requests need to be canceled. */
337 dev->power.request = RPM_REQ_NONE;
338
339 /*
340 * Optimization: If the timer is already running and is
341 * set to expire at or before the autosuspend delay,
342 * avoid the overhead of resetting it. Just let it
343 * expire; pm_suspend_timer_fn() will take care of the
344 * rest.
345 */
346 if (!(dev->power.timer_expires && time_before_eq(
347 dev->power.timer_expires, expires))) {
348 dev->power.timer_expires = expires;
349 mod_timer(&dev->power.suspend_timer, expires);
350 }
351 dev->power.timer_autosuspends = 1;
352 goto out;
353 }
354 }
355
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200356 /* Other scheduled or pending requests need to be canceled. */
357 pm_runtime_cancel_pending(dev);
358
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200359 if (dev->power.runtime_status == RPM_SUSPENDING) {
360 DEFINE_WAIT(wait);
361
Alan Stern1bfee5b2010-09-25 23:35:00 +0200362 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200363 retval = -EINPROGRESS;
364 goto out;
365 }
366
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200367 if (dev->power.irq_safe) {
368 spin_unlock(&dev->power.lock);
369
370 cpu_relax();
371
372 spin_lock(&dev->power.lock);
373 goto repeat;
374 }
375
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200376 /* Wait for the other suspend running in parallel with us. */
377 for (;;) {
378 prepare_to_wait(&dev->power.wait_queue, &wait,
379 TASK_UNINTERRUPTIBLE);
380 if (dev->power.runtime_status != RPM_SUSPENDING)
381 break;
382
383 spin_unlock_irq(&dev->power.lock);
384
385 schedule();
386
387 spin_lock_irq(&dev->power.lock);
388 }
389 finish_wait(&dev->power.wait_queue, &wait);
390 goto repeat;
391 }
392
Alan Stern7490e442010-09-25 23:35:15 +0200393 if (dev->power.no_callbacks)
394 goto no_callback; /* Assume success. */
395
Alan Stern1bfee5b2010-09-25 23:35:00 +0200396 /* Carry out an asynchronous or a synchronous suspend. */
397 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200398 dev->power.request = (rpmflags & RPM_AUTO) ?
399 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200400 if (!dev->power.request_pending) {
401 dev->power.request_pending = true;
402 queue_work(pm_wq, &dev->power.work);
403 }
404 goto out;
405 }
406
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200407 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200408
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200409 if (dev->pm_domain)
410 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200411 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200412 callback = dev->type->pm->runtime_suspend;
413 else if (dev->class && dev->class->pm)
414 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100415 else if (dev->bus && dev->bus->pm)
416 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200417 else
418 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200419
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100420 if (!callback && dev->driver && dev->driver->pm)
421 callback = dev->driver->pm->runtime_suspend;
422
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200423 retval = rpm_callback(callback, dev);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100424 if (retval)
425 goto fail;
Alan Stern886486b2011-11-03 23:39:18 +0100426
Alan Stern7490e442010-09-25 23:35:15 +0200427 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200428 __update_runtime_status(dev, RPM_SUSPENDED);
429 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200430
Ming Lei857b36c2011-10-12 22:59:33 +0200431 if (dev->parent) {
432 parent = dev->parent;
433 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200434 }
435 wake_up_all(&dev->power.wait_queue);
436
437 if (dev->power.deferred_resume) {
Rafael J. Wysocki58a34de2012-08-15 21:31:55 +0200438 dev->power.deferred_resume = false;
Alan Stern140a6c92010-09-25 23:35:07 +0200439 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200440 retval = -EAGAIN;
441 goto out;
442 }
443
Alan Sternc3810c82011-01-25 20:50:07 +0100444 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100445 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100446 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200447
Alan Sternc3810c82011-01-25 20:50:07 +0100448 spin_lock(&parent->power.lock);
449 rpm_idle(parent, RPM_ASYNC);
450 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200451
Alan Sternc3810c82011-01-25 20:50:07 +0100452 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200453 }
454
455 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200456 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200457
458 return retval;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100459
460 fail:
461 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100462 dev->power.deferred_resume = false;
Alan Sternf2791d72012-03-26 22:46:52 +0200463 wake_up_all(&dev->power.wait_queue);
464
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100465 if (retval == -EAGAIN || retval == -EBUSY) {
466 dev->power.runtime_error = 0;
467
468 /*
469 * If the callback routine failed an autosuspend, and
470 * if the last_busy time has been updated so that there
471 * is a new autosuspend expiration time, automatically
472 * reschedule another autosuspend.
473 */
474 if ((rpmflags & RPM_AUTO) &&
475 pm_runtime_autosuspend_expiration(dev) != 0)
476 goto repeat;
477 } else {
478 pm_runtime_cancel_pending(dev);
479 }
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100480 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200481}
482
483/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200484 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200485 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200486 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200487 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200488 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200489 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300490 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200491 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
492 * parallel with this function, either tell the other process to resume after
493 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
494 * flag is set then queue a resume request; otherwise run the
495 * ->runtime_resume() callback directly. Queue an idle notification for the
496 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200497 *
498 * This function must be called under dev->power.lock with interrupts disabled.
499 */
Alan Stern140a6c92010-09-25 23:35:07 +0200500static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200501 __releases(&dev->power.lock) __acquires(&dev->power.lock)
502{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200503 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200504 struct device *parent = NULL;
505 int retval = 0;
506
Ming Leic3dc2f12011-09-27 22:54:41 +0200507 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200508
509 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200510 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200511 retval = -EINVAL;
Kevin Hilman6f3c77b2012-09-21 22:47:34 +0000512 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
513 && dev->power.runtime_status == RPM_ACTIVE)
514 retval = 1;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200515 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200516 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200517 if (retval)
518 goto out;
519
Alan Stern15bcb91d2010-09-25 23:35:21 +0200520 /*
521 * Other scheduled or pending requests need to be canceled. Small
522 * optimization: If an autosuspend timer is running, leave it running
523 * rather than cancelling it now only to restart it again in the near
524 * future.
525 */
526 dev->power.request = RPM_REQ_NONE;
527 if (!dev->power.timer_autosuspends)
528 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200529
530 if (dev->power.runtime_status == RPM_ACTIVE) {
531 retval = 1;
532 goto out;
533 }
534
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200535 if (dev->power.runtime_status == RPM_RESUMING
536 || dev->power.runtime_status == RPM_SUSPENDING) {
537 DEFINE_WAIT(wait);
538
Alan Stern1bfee5b2010-09-25 23:35:00 +0200539 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200540 if (dev->power.runtime_status == RPM_SUSPENDING)
541 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200542 else
543 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200544 goto out;
545 }
546
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200547 if (dev->power.irq_safe) {
548 spin_unlock(&dev->power.lock);
549
550 cpu_relax();
551
552 spin_lock(&dev->power.lock);
553 goto repeat;
554 }
555
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200556 /* Wait for the operation carried out in parallel with us. */
557 for (;;) {
558 prepare_to_wait(&dev->power.wait_queue, &wait,
559 TASK_UNINTERRUPTIBLE);
560 if (dev->power.runtime_status != RPM_RESUMING
561 && dev->power.runtime_status != RPM_SUSPENDING)
562 break;
563
564 spin_unlock_irq(&dev->power.lock);
565
566 schedule();
567
568 spin_lock_irq(&dev->power.lock);
569 }
570 finish_wait(&dev->power.wait_queue, &wait);
571 goto repeat;
572 }
573
Alan Stern7490e442010-09-25 23:35:15 +0200574 /*
575 * See if we can skip waking up the parent. This is safe only if
576 * power.no_callbacks is set, because otherwise we don't know whether
577 * the resume will actually succeed.
578 */
579 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200580 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200581 if (dev->parent->power.disable_depth > 0
582 || dev->parent->power.ignore_children
583 || dev->parent->power.runtime_status == RPM_ACTIVE) {
584 atomic_inc(&dev->parent->power.child_count);
585 spin_unlock(&dev->parent->power.lock);
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200586 retval = 1;
Alan Stern7490e442010-09-25 23:35:15 +0200587 goto no_callback; /* Assume success. */
588 }
589 spin_unlock(&dev->parent->power.lock);
590 }
591
Alan Stern1bfee5b2010-09-25 23:35:00 +0200592 /* Carry out an asynchronous or a synchronous resume. */
593 if (rpmflags & RPM_ASYNC) {
594 dev->power.request = RPM_REQ_RESUME;
595 if (!dev->power.request_pending) {
596 dev->power.request_pending = true;
597 queue_work(pm_wq, &dev->power.work);
598 }
599 retval = 0;
600 goto out;
601 }
602
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200603 if (!parent && dev->parent) {
604 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100605 * Increment the parent's usage counter and resume it if
606 * necessary. Not needed if dev is irq-safe; then the
607 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200608 */
609 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100610 if (dev->power.irq_safe)
611 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100612 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200613
614 pm_runtime_get_noresume(parent);
615
Alan Stern862f89b2009-11-25 01:06:37 +0100616 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200617 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200618 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200619 * is set to ignore children.
620 */
621 if (!parent->power.disable_depth
622 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200623 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200624 if (parent->power.runtime_status != RPM_ACTIVE)
625 retval = -EBUSY;
626 }
Alan Stern862f89b2009-11-25 01:06:37 +0100627 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200628
Alan Stern862f89b2009-11-25 01:06:37 +0100629 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200630 if (retval)
631 goto out;
632 goto repeat;
633 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100634 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200635
Alan Stern7490e442010-09-25 23:35:15 +0200636 if (dev->power.no_callbacks)
637 goto no_callback; /* Assume success. */
638
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200639 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200640
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200641 if (dev->pm_domain)
642 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200643 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200644 callback = dev->type->pm->runtime_resume;
645 else if (dev->class && dev->class->pm)
646 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100647 else if (dev->bus && dev->bus->pm)
648 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200649 else
650 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200651
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100652 if (!callback && dev->driver && dev->driver->pm)
653 callback = dev->driver->pm->runtime_resume;
654
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200655 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200656 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200657 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200658 pm_runtime_cancel_pending(dev);
659 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200660 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200661 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200662 if (parent)
663 atomic_inc(&parent->power.child_count);
664 }
665 wake_up_all(&dev->power.wait_queue);
666
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200667 if (retval >= 0)
Alan Stern140a6c92010-09-25 23:35:07 +0200668 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200669
670 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100671 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200672 spin_unlock_irq(&dev->power.lock);
673
674 pm_runtime_put(parent);
675
676 spin_lock_irq(&dev->power.lock);
677 }
678
Ming Leic3dc2f12011-09-27 22:54:41 +0200679 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200680
681 return retval;
682}
683
684/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200685 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200686 * @work: Work structure used for scheduling the execution of this function.
687 *
688 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200689 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200690 */
691static void pm_runtime_work(struct work_struct *work)
692{
693 struct device *dev = container_of(work, struct device, power.work);
694 enum rpm_request req;
695
696 spin_lock_irq(&dev->power.lock);
697
698 if (!dev->power.request_pending)
699 goto out;
700
701 req = dev->power.request;
702 dev->power.request = RPM_REQ_NONE;
703 dev->power.request_pending = false;
704
705 switch (req) {
706 case RPM_REQ_NONE:
707 break;
708 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200709 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200710 break;
711 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200712 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200713 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200714 case RPM_REQ_AUTOSUSPEND:
715 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
716 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200717 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200718 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200719 break;
720 }
721
722 out:
723 spin_unlock_irq(&dev->power.lock);
724}
725
726/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200727 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
728 * @data: Device pointer passed by pm_schedule_suspend().
729 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200730 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200731 */
732static void pm_suspend_timer_fn(unsigned long data)
733{
734 struct device *dev = (struct device *)data;
735 unsigned long flags;
736 unsigned long expires;
737
738 spin_lock_irqsave(&dev->power.lock, flags);
739
740 expires = dev->power.timer_expires;
741 /* If 'expire' is after 'jiffies' we've been called too early. */
742 if (expires > 0 && !time_after(expires, jiffies)) {
743 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200744 rpm_suspend(dev, dev->power.timer_autosuspends ?
745 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200746 }
747
748 spin_unlock_irqrestore(&dev->power.lock, flags);
749}
750
751/**
752 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
753 * @dev: Device to suspend.
754 * @delay: Time to wait before submitting a suspend request, in milliseconds.
755 */
756int pm_schedule_suspend(struct device *dev, unsigned int delay)
757{
758 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200759 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200760
761 spin_lock_irqsave(&dev->power.lock, flags);
762
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200763 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200764 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200765 goto out;
766 }
767
Alan Stern1bfee5b2010-09-25 23:35:00 +0200768 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200769 if (retval)
770 goto out;
771
Alan Stern1bfee5b2010-09-25 23:35:00 +0200772 /* Other scheduled or pending requests need to be canceled. */
773 pm_runtime_cancel_pending(dev);
774
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200775 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200776 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200777 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200778 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
779
780 out:
781 spin_unlock_irqrestore(&dev->power.lock, flags);
782
783 return retval;
784}
785EXPORT_SYMBOL_GPL(pm_schedule_suspend);
786
787/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200788 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200789 * @dev: Device to send idle notification for.
790 * @rpmflags: Flag bits.
791 *
792 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
793 * return immediately if it is larger than zero. Then carry out an idle
794 * notification, either synchronous or asynchronous.
795 *
Colin Cross311aab72011-08-08 23:39:36 +0200796 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
797 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200798 */
Alan Stern140a6c92010-09-25 23:35:07 +0200799int __pm_runtime_idle(struct device *dev, int rpmflags)
800{
801 unsigned long flags;
802 int retval;
803
Colin Cross311aab72011-08-08 23:39:36 +0200804 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
805
Alan Stern140a6c92010-09-25 23:35:07 +0200806 if (rpmflags & RPM_GET_PUT) {
807 if (!atomic_dec_and_test(&dev->power.usage_count))
808 return 0;
809 }
810
811 spin_lock_irqsave(&dev->power.lock, flags);
812 retval = rpm_idle(dev, rpmflags);
813 spin_unlock_irqrestore(&dev->power.lock, flags);
814
815 return retval;
816}
817EXPORT_SYMBOL_GPL(__pm_runtime_idle);
818
819/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200820 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200821 * @dev: Device to suspend.
822 * @rpmflags: Flag bits.
823 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200824 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
825 * return immediately if it is larger than zero. Then carry out a suspend,
826 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200827 *
Colin Cross311aab72011-08-08 23:39:36 +0200828 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
829 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200830 */
831int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200832{
833 unsigned long flags;
834 int retval;
835
Colin Cross311aab72011-08-08 23:39:36 +0200836 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
837
Alan Stern15bcb91d2010-09-25 23:35:21 +0200838 if (rpmflags & RPM_GET_PUT) {
839 if (!atomic_dec_and_test(&dev->power.usage_count))
840 return 0;
841 }
842
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200843 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200844 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200845 spin_unlock_irqrestore(&dev->power.lock, flags);
846
847 return retval;
848}
Alan Stern140a6c92010-09-25 23:35:07 +0200849EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200850
851/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200852 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200853 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200854 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200855 *
Alan Stern140a6c92010-09-25 23:35:07 +0200856 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
857 * carry out a resume, either synchronous or asynchronous.
858 *
Colin Cross311aab72011-08-08 23:39:36 +0200859 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
860 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200861 */
Alan Stern140a6c92010-09-25 23:35:07 +0200862int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200863{
Alan Stern140a6c92010-09-25 23:35:07 +0200864 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100865 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200866
Colin Cross311aab72011-08-08 23:39:36 +0200867 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
868
Alan Stern140a6c92010-09-25 23:35:07 +0200869 if (rpmflags & RPM_GET_PUT)
870 atomic_inc(&dev->power.usage_count);
871
872 spin_lock_irqsave(&dev->power.lock, flags);
873 retval = rpm_resume(dev, rpmflags);
874 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200875
876 return retval;
877}
Alan Stern140a6c92010-09-25 23:35:07 +0200878EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200879
880/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200881 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200882 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200883 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200884 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200885 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200886 * different from zero, the status may be changed either to RPM_ACTIVE, or to
887 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
888 * However, if the device has a parent and the parent is not active, and the
889 * parent's power.ignore_children flag is unset, the device's status cannot be
890 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
891 *
892 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
893 * and the device parent's counter of unsuspended children is modified to
894 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
895 * notification request for the parent is submitted.
896 */
897int __pm_runtime_set_status(struct device *dev, unsigned int status)
898{
899 struct device *parent = dev->parent;
900 unsigned long flags;
901 bool notify_parent = false;
902 int error = 0;
903
904 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
905 return -EINVAL;
906
907 spin_lock_irqsave(&dev->power.lock, flags);
908
909 if (!dev->power.runtime_error && !dev->power.disable_depth) {
910 error = -EAGAIN;
911 goto out;
912 }
913
914 if (dev->power.runtime_status == status)
915 goto out_set;
916
917 if (status == RPM_SUSPENDED) {
918 /* It always is possible to set the status to 'suspended'. */
919 if (parent) {
920 atomic_add_unless(&parent->power.child_count, -1, 0);
921 notify_parent = !parent->power.ignore_children;
922 }
923 goto out_set;
924 }
925
926 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100927 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200928
929 /*
930 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200931 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200932 * 'power.ignore_children' flag unset.
933 */
934 if (!parent->power.disable_depth
935 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100936 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200937 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100938 else if (dev->power.runtime_status == RPM_SUSPENDED)
939 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200940
Alan Stern862f89b2009-11-25 01:06:37 +0100941 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200942
943 if (error)
944 goto out;
945 }
946
947 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200948 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200949 dev->power.runtime_error = 0;
950 out:
951 spin_unlock_irqrestore(&dev->power.lock, flags);
952
953 if (notify_parent)
954 pm_request_idle(parent);
955
956 return error;
957}
958EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
959
960/**
961 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
962 * @dev: Device to handle.
963 *
964 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200965 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200966 *
967 * Should be called under dev->power.lock with interrupts disabled.
968 */
969static void __pm_runtime_barrier(struct device *dev)
970{
971 pm_runtime_deactivate_timer(dev);
972
973 if (dev->power.request_pending) {
974 dev->power.request = RPM_REQ_NONE;
975 spin_unlock_irq(&dev->power.lock);
976
977 cancel_work_sync(&dev->power.work);
978
979 spin_lock_irq(&dev->power.lock);
980 dev->power.request_pending = false;
981 }
982
983 if (dev->power.runtime_status == RPM_SUSPENDING
984 || dev->power.runtime_status == RPM_RESUMING
985 || dev->power.idle_notification) {
986 DEFINE_WAIT(wait);
987
988 /* Suspend, wake-up or idle notification in progress. */
989 for (;;) {
990 prepare_to_wait(&dev->power.wait_queue, &wait,
991 TASK_UNINTERRUPTIBLE);
992 if (dev->power.runtime_status != RPM_SUSPENDING
993 && dev->power.runtime_status != RPM_RESUMING
994 && !dev->power.idle_notification)
995 break;
996 spin_unlock_irq(&dev->power.lock);
997
998 schedule();
999
1000 spin_lock_irq(&dev->power.lock);
1001 }
1002 finish_wait(&dev->power.wait_queue, &wait);
1003 }
1004}
1005
1006/**
1007 * pm_runtime_barrier - Flush pending requests and wait for completions.
1008 * @dev: Device to handle.
1009 *
1010 * Prevent the device from being suspended by incrementing its usage counter and
1011 * if there's a pending resume request for the device, wake the device up.
1012 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001013 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001014 * progress to complete.
1015 *
1016 * Return value:
1017 * 1, if there was a resume request pending and the device had to be woken up,
1018 * 0, otherwise
1019 */
1020int pm_runtime_barrier(struct device *dev)
1021{
1022 int retval = 0;
1023
1024 pm_runtime_get_noresume(dev);
1025 spin_lock_irq(&dev->power.lock);
1026
1027 if (dev->power.request_pending
1028 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +02001029 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001030 retval = 1;
1031 }
1032
1033 __pm_runtime_barrier(dev);
1034
1035 spin_unlock_irq(&dev->power.lock);
1036 pm_runtime_put_noidle(dev);
1037
1038 return retval;
1039}
1040EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1041
1042/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001043 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001044 * @dev: Device to handle.
1045 * @check_resume: If set, check if there's a resume request for the device.
1046 *
1047 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001048 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001049 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001050 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001051 *
1052 * If @check_resume is set and there's a resume request pending when
1053 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001054 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001055 */
1056void __pm_runtime_disable(struct device *dev, bool check_resume)
1057{
1058 spin_lock_irq(&dev->power.lock);
1059
1060 if (dev->power.disable_depth > 0) {
1061 dev->power.disable_depth++;
1062 goto out;
1063 }
1064
1065 /*
1066 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001067 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001068 * shouldn't prevent the device from processing the I/O.
1069 */
1070 if (check_resume && dev->power.request_pending
1071 && dev->power.request == RPM_REQ_RESUME) {
1072 /*
1073 * Prevent suspends and idle notifications from being carried
1074 * out after we have woken up the device.
1075 */
1076 pm_runtime_get_noresume(dev);
1077
Alan Stern140a6c92010-09-25 23:35:07 +02001078 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001079
1080 pm_runtime_put_noidle(dev);
1081 }
1082
1083 if (!dev->power.disable_depth++)
1084 __pm_runtime_barrier(dev);
1085
1086 out:
1087 spin_unlock_irq(&dev->power.lock);
1088}
1089EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1090
1091/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001092 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001093 * @dev: Device to handle.
1094 */
1095void pm_runtime_enable(struct device *dev)
1096{
1097 unsigned long flags;
1098
1099 spin_lock_irqsave(&dev->power.lock, flags);
1100
1101 if (dev->power.disable_depth > 0)
1102 dev->power.disable_depth--;
1103 else
1104 dev_warn(dev, "Unbalanced %s!\n", __func__);
1105
1106 spin_unlock_irqrestore(&dev->power.lock, flags);
1107}
1108EXPORT_SYMBOL_GPL(pm_runtime_enable);
1109
1110/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001111 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001112 * @dev: Device to handle.
1113 *
1114 * Increase the device's usage count and clear its power.runtime_auto flag,
1115 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1116 * for it.
1117 */
1118void pm_runtime_forbid(struct device *dev)
1119{
1120 spin_lock_irq(&dev->power.lock);
1121 if (!dev->power.runtime_auto)
1122 goto out;
1123
1124 dev->power.runtime_auto = false;
1125 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001126 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001127
1128 out:
1129 spin_unlock_irq(&dev->power.lock);
1130}
1131EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1132
1133/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001134 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001135 * @dev: Device to handle.
1136 *
1137 * Decrease the device's usage count and set its power.runtime_auto flag.
1138 */
1139void pm_runtime_allow(struct device *dev)
1140{
1141 spin_lock_irq(&dev->power.lock);
1142 if (dev->power.runtime_auto)
1143 goto out;
1144
1145 dev->power.runtime_auto = true;
1146 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001147 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001148
1149 out:
1150 spin_unlock_irq(&dev->power.lock);
1151}
1152EXPORT_SYMBOL_GPL(pm_runtime_allow);
1153
1154/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001155 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001156 * @dev: Device to handle.
1157 *
1158 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001159 * device is power-managed through its parent and has no runtime PM
1160 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001161 */
1162void pm_runtime_no_callbacks(struct device *dev)
1163{
1164 spin_lock_irq(&dev->power.lock);
1165 dev->power.no_callbacks = 1;
1166 spin_unlock_irq(&dev->power.lock);
1167 if (device_is_registered(dev))
1168 rpm_sysfs_remove(dev);
1169}
1170EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1171
1172/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001173 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1174 * @dev: Device to handle
1175 *
1176 * Set the power.irq_safe flag, which tells the PM core that the
1177 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1178 * always be invoked with the spinlock held and interrupts disabled. It also
1179 * causes the parent's usage counter to be permanently incremented, preventing
1180 * the parent from runtime suspending -- otherwise an irq-safe child might have
1181 * to wait for a non-irq-safe parent.
1182 */
1183void pm_runtime_irq_safe(struct device *dev)
1184{
1185 if (dev->parent)
1186 pm_runtime_get_sync(dev->parent);
1187 spin_lock_irq(&dev->power.lock);
1188 dev->power.irq_safe = 1;
1189 spin_unlock_irq(&dev->power.lock);
1190}
1191EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1192
1193/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001194 * update_autosuspend - Handle a change to a device's autosuspend settings.
1195 * @dev: Device to handle.
1196 * @old_delay: The former autosuspend_delay value.
1197 * @old_use: The former use_autosuspend value.
1198 *
1199 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1200 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1201 *
1202 * This function must be called under dev->power.lock with interrupts disabled.
1203 */
1204static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1205{
1206 int delay = dev->power.autosuspend_delay;
1207
1208 /* Should runtime suspend be prevented now? */
1209 if (dev->power.use_autosuspend && delay < 0) {
1210
1211 /* If it used to be allowed then prevent it. */
1212 if (!old_use || old_delay >= 0) {
1213 atomic_inc(&dev->power.usage_count);
1214 rpm_resume(dev, 0);
1215 }
1216 }
1217
1218 /* Runtime suspend should be allowed now. */
1219 else {
1220
1221 /* If it used to be prevented then allow it. */
1222 if (old_use && old_delay < 0)
1223 atomic_dec(&dev->power.usage_count);
1224
1225 /* Maybe we can autosuspend now. */
1226 rpm_idle(dev, RPM_AUTO);
1227 }
1228}
1229
1230/**
1231 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1232 * @dev: Device to handle.
1233 * @delay: Value of the new delay in milliseconds.
1234 *
1235 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001236 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1237 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001238 */
1239void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1240{
1241 int old_delay, old_use;
1242
1243 spin_lock_irq(&dev->power.lock);
1244 old_delay = dev->power.autosuspend_delay;
1245 old_use = dev->power.use_autosuspend;
1246 dev->power.autosuspend_delay = delay;
1247 update_autosuspend(dev, old_delay, old_use);
1248 spin_unlock_irq(&dev->power.lock);
1249}
1250EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1251
1252/**
1253 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1254 * @dev: Device to handle.
1255 * @use: New value for use_autosuspend.
1256 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001257 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001258 * suspends as needed.
1259 */
1260void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1261{
1262 int old_delay, old_use;
1263
1264 spin_lock_irq(&dev->power.lock);
1265 old_delay = dev->power.autosuspend_delay;
1266 old_use = dev->power.use_autosuspend;
1267 dev->power.use_autosuspend = use;
1268 update_autosuspend(dev, old_delay, old_use);
1269 spin_unlock_irq(&dev->power.lock);
1270}
1271EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1272
1273/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001274 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001275 * @dev: Device object to initialize.
1276 */
1277void pm_runtime_init(struct device *dev)
1278{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001279 dev->power.runtime_status = RPM_SUSPENDED;
1280 dev->power.idle_notification = false;
1281
1282 dev->power.disable_depth = 1;
1283 atomic_set(&dev->power.usage_count, 0);
1284
1285 dev->power.runtime_error = 0;
1286
1287 atomic_set(&dev->power.child_count, 0);
1288 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001289 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001290
1291 dev->power.request_pending = false;
1292 dev->power.request = RPM_REQ_NONE;
1293 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001294 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001295 INIT_WORK(&dev->power.work, pm_runtime_work);
1296
1297 dev->power.timer_expires = 0;
1298 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1299 (unsigned long)dev);
1300
1301 init_waitqueue_head(&dev->power.wait_queue);
1302}
1303
1304/**
1305 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1306 * @dev: Device object being removed from device hierarchy.
1307 */
1308void pm_runtime_remove(struct device *dev)
1309{
1310 __pm_runtime_disable(dev, false);
1311
1312 /* Change the status back to 'suspended' to match the initial status. */
1313 if (dev->power.runtime_status == RPM_ACTIVE)
1314 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001315 if (dev->power.irq_safe && dev->parent)
1316 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001317}