blob: 7a6fb5e34a0e4bbaf527eae45d1f0484ebdbc24a [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;
32 int delta;
33
34 delta = now - dev->power.accounting_timestamp;
35
36 if (delta < 0)
37 delta = 0;
38
39 dev->power.accounting_timestamp = now;
40
41 if (dev->power.disable_depth > 0)
42 return;
43
44 if (dev->power.runtime_status == RPM_SUSPENDED)
45 dev->power.suspended_jiffies += delta;
46 else
47 dev->power.active_jiffies += delta;
48}
49
50static void __update_runtime_status(struct device *dev, enum rpm_status status)
51{
52 update_pm_runtime_accounting(dev);
53 dev->power.runtime_status = status;
54}
55
56/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020057 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
58 * @dev: Device to handle.
59 */
60static void pm_runtime_deactivate_timer(struct device *dev)
61{
62 if (dev->power.timer_expires > 0) {
63 del_timer(&dev->power.suspend_timer);
64 dev->power.timer_expires = 0;
65 }
66}
67
68/**
69 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
70 * @dev: Device to handle.
71 */
72static void pm_runtime_cancel_pending(struct device *dev)
73{
74 pm_runtime_deactivate_timer(dev);
75 /*
76 * In case there's a request pending, make sure its work function will
77 * return without doing anything.
78 */
79 dev->power.request = RPM_REQ_NONE;
80}
81
Alan Stern15bcb91d2010-09-25 23:35:21 +020082/*
83 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
84 * @dev: Device to handle.
85 *
86 * Compute the autosuspend-delay expiration time based on the device's
87 * power.last_busy time. If the delay has already expired or is disabled
88 * (negative) or the power.use_autosuspend flag isn't set, return 0.
89 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
90 *
91 * This function may be called either with or without dev->power.lock held.
92 * Either way it can be racy, since power.last_busy may be updated at any time.
93 */
94unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
95{
96 int autosuspend_delay;
97 long elapsed;
98 unsigned long last_busy;
99 unsigned long expires = 0;
100
101 if (!dev->power.use_autosuspend)
102 goto out;
103
104 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
105 if (autosuspend_delay < 0)
106 goto out;
107
108 last_busy = ACCESS_ONCE(dev->power.last_busy);
109 elapsed = jiffies - last_busy;
110 if (elapsed < 0)
111 goto out; /* jiffies has wrapped around. */
112
113 /*
114 * If the autosuspend_delay is >= 1 second, align the timer by rounding
115 * up to the nearest second.
116 */
117 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
118 if (autosuspend_delay >= 1000)
119 expires = round_jiffies(expires);
120 expires += !expires;
121 if (elapsed >= expires - last_busy)
122 expires = 0; /* Already expired. */
123
124 out:
125 return expires;
126}
127EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
128
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200129/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200130 * rpm_check_suspend_allowed - Test whether a device may be suspended.
131 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200132 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200133static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200134{
135 int retval = 0;
136
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200137 if (dev->power.runtime_error)
138 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200139 else if (dev->power.disable_depth > 0)
140 retval = -EACCES;
141 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200142 retval = -EAGAIN;
143 else if (!pm_children_suspended(dev))
144 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200145
146 /* Pending resume requests take precedence over suspends. */
147 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200148 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200149 || (dev->power.request_pending
150 && dev->power.request == RPM_REQ_RESUME))
151 retval = -EAGAIN;
152 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. Wysockiad3c36a2011-09-27 21:54:52 +0200255 if (callback)
256 __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200257
258 dev->power.idle_notification = false;
259 wake_up_all(&dev->power.wait_queue);
260
261 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200262 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200263 return retval;
264}
265
266/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200267 * rpm_callback - Run a given runtime PM callback for a given device.
268 * @cb: Runtime PM callback to run.
269 * @dev: Device to run the callback for.
270 */
271static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200272{
273 int retval;
274
275 if (!cb)
276 return -ENOSYS;
277
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200278 retval = __rpm_callback(cb, dev);
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200279
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200280 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200281 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200282}
283
284/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200285 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200286 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200287 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200288 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200289 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200290 * another suspend has been started earlier, either return immediately or wait
291 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
292 * pending idle notification. If the RPM_ASYNC flag is set then queue a
293 * suspend request; otherwise run the ->runtime_suspend() callback directly.
294 * If a deferred resume was requested while the callback was running then carry
295 * it out; otherwise send an idle notification for the device (if the suspend
296 * failed) or for its parent (if the suspend succeeded).
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200297 *
298 * This function must be called under dev->power.lock with interrupts disabled.
299 */
Alan Stern140a6c92010-09-25 23:35:07 +0200300static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200301 __releases(&dev->power.lock) __acquires(&dev->power.lock)
302{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200303 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200304 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200305 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200306
Ming Leic3dc2f12011-09-27 22:54:41 +0200307 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308
309 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200310 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200311
Alan Stern1bfee5b2010-09-25 23:35:00 +0200312 if (retval < 0)
313 ; /* Conditions are wrong. */
314
315 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
316 else if (dev->power.runtime_status == RPM_RESUMING &&
317 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200318 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200319 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200320 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200321
Alan Stern15bcb91d2010-09-25 23:35:21 +0200322 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
323 if ((rpmflags & RPM_AUTO)
324 && dev->power.runtime_status != RPM_SUSPENDING) {
325 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
326
327 if (expires != 0) {
328 /* Pending requests need to be canceled. */
329 dev->power.request = RPM_REQ_NONE;
330
331 /*
332 * Optimization: If the timer is already running and is
333 * set to expire at or before the autosuspend delay,
334 * avoid the overhead of resetting it. Just let it
335 * expire; pm_suspend_timer_fn() will take care of the
336 * rest.
337 */
338 if (!(dev->power.timer_expires && time_before_eq(
339 dev->power.timer_expires, expires))) {
340 dev->power.timer_expires = expires;
341 mod_timer(&dev->power.suspend_timer, expires);
342 }
343 dev->power.timer_autosuspends = 1;
344 goto out;
345 }
346 }
347
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200348 /* Other scheduled or pending requests need to be canceled. */
349 pm_runtime_cancel_pending(dev);
350
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200351 if (dev->power.runtime_status == RPM_SUSPENDING) {
352 DEFINE_WAIT(wait);
353
Alan Stern1bfee5b2010-09-25 23:35:00 +0200354 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200355 retval = -EINPROGRESS;
356 goto out;
357 }
358
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200359 if (dev->power.irq_safe) {
360 spin_unlock(&dev->power.lock);
361
362 cpu_relax();
363
364 spin_lock(&dev->power.lock);
365 goto repeat;
366 }
367
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200368 /* Wait for the other suspend running in parallel with us. */
369 for (;;) {
370 prepare_to_wait(&dev->power.wait_queue, &wait,
371 TASK_UNINTERRUPTIBLE);
372 if (dev->power.runtime_status != RPM_SUSPENDING)
373 break;
374
375 spin_unlock_irq(&dev->power.lock);
376
377 schedule();
378
379 spin_lock_irq(&dev->power.lock);
380 }
381 finish_wait(&dev->power.wait_queue, &wait);
382 goto repeat;
383 }
384
Alan Stern7490e442010-09-25 23:35:15 +0200385 dev->power.deferred_resume = false;
386 if (dev->power.no_callbacks)
387 goto no_callback; /* Assume success. */
388
Alan Stern1bfee5b2010-09-25 23:35:00 +0200389 /* Carry out an asynchronous or a synchronous suspend. */
390 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200391 dev->power.request = (rpmflags & RPM_AUTO) ?
392 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200393 if (!dev->power.request_pending) {
394 dev->power.request_pending = true;
395 queue_work(pm_wq, &dev->power.work);
396 }
397 goto out;
398 }
399
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200400 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200401
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200402 if (dev->pm_domain)
403 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200404 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200405 callback = dev->type->pm->runtime_suspend;
406 else if (dev->class && dev->class->pm)
407 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100408 else if (dev->bus && dev->bus->pm)
409 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200410 else
411 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200412
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200413 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200414 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200415 __update_runtime_status(dev, RPM_ACTIVE);
ShuoX Liu2cffff12011-07-08 20:53:55 +0200416 dev->power.deferred_resume = false;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200417 if (retval == -EAGAIN || retval == -EBUSY)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200418 dev->power.runtime_error = 0;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200419 else
Alan Stern240c7332010-03-23 00:50:07 +0100420 pm_runtime_cancel_pending(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200421 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200422 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200423 __update_runtime_status(dev, RPM_SUSPENDED);
Alan Stern240c7332010-03-23 00:50:07 +0100424 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200425
426 if (dev->parent) {
427 parent = dev->parent;
428 atomic_add_unless(&parent->power.child_count, -1, 0);
429 }
430 }
431 wake_up_all(&dev->power.wait_queue);
432
433 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200434 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200435 retval = -EAGAIN;
436 goto out;
437 }
438
Alan Sternc3810c82011-01-25 20:50:07 +0100439 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100440 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100441 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200442
Alan Sternc3810c82011-01-25 20:50:07 +0100443 spin_lock(&parent->power.lock);
444 rpm_idle(parent, RPM_ASYNC);
445 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200446
Alan Sternc3810c82011-01-25 20:50:07 +0100447 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200448 }
449
450 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200451 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200452
453 return retval;
454}
455
456/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200457 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200458 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200459 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200460 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200461 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200462 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300463 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200464 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
465 * parallel with this function, either tell the other process to resume after
466 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
467 * flag is set then queue a resume request; otherwise run the
468 * ->runtime_resume() callback directly. Queue an idle notification for the
469 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200470 *
471 * This function must be called under dev->power.lock with interrupts disabled.
472 */
Alan Stern140a6c92010-09-25 23:35:07 +0200473static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200474 __releases(&dev->power.lock) __acquires(&dev->power.lock)
475{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200476 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200477 struct device *parent = NULL;
478 int retval = 0;
479
Ming Leic3dc2f12011-09-27 22:54:41 +0200480 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200481
482 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200483 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200484 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200485 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200486 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200487 if (retval)
488 goto out;
489
Alan Stern15bcb91d2010-09-25 23:35:21 +0200490 /*
491 * Other scheduled or pending requests need to be canceled. Small
492 * optimization: If an autosuspend timer is running, leave it running
493 * rather than cancelling it now only to restart it again in the near
494 * future.
495 */
496 dev->power.request = RPM_REQ_NONE;
497 if (!dev->power.timer_autosuspends)
498 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200499
500 if (dev->power.runtime_status == RPM_ACTIVE) {
501 retval = 1;
502 goto out;
503 }
504
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200505 if (dev->power.runtime_status == RPM_RESUMING
506 || dev->power.runtime_status == RPM_SUSPENDING) {
507 DEFINE_WAIT(wait);
508
Alan Stern1bfee5b2010-09-25 23:35:00 +0200509 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200510 if (dev->power.runtime_status == RPM_SUSPENDING)
511 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200512 else
513 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200514 goto out;
515 }
516
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200517 if (dev->power.irq_safe) {
518 spin_unlock(&dev->power.lock);
519
520 cpu_relax();
521
522 spin_lock(&dev->power.lock);
523 goto repeat;
524 }
525
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200526 /* Wait for the operation carried out in parallel with us. */
527 for (;;) {
528 prepare_to_wait(&dev->power.wait_queue, &wait,
529 TASK_UNINTERRUPTIBLE);
530 if (dev->power.runtime_status != RPM_RESUMING
531 && dev->power.runtime_status != RPM_SUSPENDING)
532 break;
533
534 spin_unlock_irq(&dev->power.lock);
535
536 schedule();
537
538 spin_lock_irq(&dev->power.lock);
539 }
540 finish_wait(&dev->power.wait_queue, &wait);
541 goto repeat;
542 }
543
Alan Stern7490e442010-09-25 23:35:15 +0200544 /*
545 * See if we can skip waking up the parent. This is safe only if
546 * power.no_callbacks is set, because otherwise we don't know whether
547 * the resume will actually succeed.
548 */
549 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200550 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200551 if (dev->parent->power.disable_depth > 0
552 || dev->parent->power.ignore_children
553 || dev->parent->power.runtime_status == RPM_ACTIVE) {
554 atomic_inc(&dev->parent->power.child_count);
555 spin_unlock(&dev->parent->power.lock);
556 goto no_callback; /* Assume success. */
557 }
558 spin_unlock(&dev->parent->power.lock);
559 }
560
Alan Stern1bfee5b2010-09-25 23:35:00 +0200561 /* Carry out an asynchronous or a synchronous resume. */
562 if (rpmflags & RPM_ASYNC) {
563 dev->power.request = RPM_REQ_RESUME;
564 if (!dev->power.request_pending) {
565 dev->power.request_pending = true;
566 queue_work(pm_wq, &dev->power.work);
567 }
568 retval = 0;
569 goto out;
570 }
571
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200572 if (!parent && dev->parent) {
573 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100574 * Increment the parent's usage counter and resume it if
575 * necessary. Not needed if dev is irq-safe; then the
576 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200577 */
578 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100579 if (dev->power.irq_safe)
580 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100581 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200582
583 pm_runtime_get_noresume(parent);
584
Alan Stern862f89b2009-11-25 01:06:37 +0100585 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200586 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200587 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200588 * is set to ignore children.
589 */
590 if (!parent->power.disable_depth
591 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200592 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200593 if (parent->power.runtime_status != RPM_ACTIVE)
594 retval = -EBUSY;
595 }
Alan Stern862f89b2009-11-25 01:06:37 +0100596 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200597
Alan Stern862f89b2009-11-25 01:06:37 +0100598 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200599 if (retval)
600 goto out;
601 goto repeat;
602 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100603 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200604
Alan Stern7490e442010-09-25 23:35:15 +0200605 if (dev->power.no_callbacks)
606 goto no_callback; /* Assume success. */
607
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200608 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200609
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200610 if (dev->pm_domain)
611 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200612 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200613 callback = dev->type->pm->runtime_resume;
614 else if (dev->class && dev->class->pm)
615 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100616 else if (dev->bus && dev->bus->pm)
617 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200618 else
619 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200620
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200621 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200622 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200623 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200624 pm_runtime_cancel_pending(dev);
625 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200626 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200627 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200628 if (parent)
629 atomic_inc(&parent->power.child_count);
630 }
631 wake_up_all(&dev->power.wait_queue);
632
633 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200634 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200635
636 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100637 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200638 spin_unlock_irq(&dev->power.lock);
639
640 pm_runtime_put(parent);
641
642 spin_lock_irq(&dev->power.lock);
643 }
644
Ming Leic3dc2f12011-09-27 22:54:41 +0200645 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200646
647 return retval;
648}
649
650/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200651 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200652 * @work: Work structure used for scheduling the execution of this function.
653 *
654 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200655 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200656 */
657static void pm_runtime_work(struct work_struct *work)
658{
659 struct device *dev = container_of(work, struct device, power.work);
660 enum rpm_request req;
661
662 spin_lock_irq(&dev->power.lock);
663
664 if (!dev->power.request_pending)
665 goto out;
666
667 req = dev->power.request;
668 dev->power.request = RPM_REQ_NONE;
669 dev->power.request_pending = false;
670
671 switch (req) {
672 case RPM_REQ_NONE:
673 break;
674 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200675 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200676 break;
677 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200678 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200679 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200680 case RPM_REQ_AUTOSUSPEND:
681 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
682 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200683 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200684 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200685 break;
686 }
687
688 out:
689 spin_unlock_irq(&dev->power.lock);
690}
691
692/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200693 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
694 * @data: Device pointer passed by pm_schedule_suspend().
695 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200696 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200697 */
698static void pm_suspend_timer_fn(unsigned long data)
699{
700 struct device *dev = (struct device *)data;
701 unsigned long flags;
702 unsigned long expires;
703
704 spin_lock_irqsave(&dev->power.lock, flags);
705
706 expires = dev->power.timer_expires;
707 /* If 'expire' is after 'jiffies' we've been called too early. */
708 if (expires > 0 && !time_after(expires, jiffies)) {
709 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200710 rpm_suspend(dev, dev->power.timer_autosuspends ?
711 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200712 }
713
714 spin_unlock_irqrestore(&dev->power.lock, flags);
715}
716
717/**
718 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
719 * @dev: Device to suspend.
720 * @delay: Time to wait before submitting a suspend request, in milliseconds.
721 */
722int pm_schedule_suspend(struct device *dev, unsigned int delay)
723{
724 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200725 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200726
727 spin_lock_irqsave(&dev->power.lock, flags);
728
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200729 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200730 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200731 goto out;
732 }
733
Alan Stern1bfee5b2010-09-25 23:35:00 +0200734 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200735 if (retval)
736 goto out;
737
Alan Stern1bfee5b2010-09-25 23:35:00 +0200738 /* Other scheduled or pending requests need to be canceled. */
739 pm_runtime_cancel_pending(dev);
740
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200741 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200742 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200743 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200744 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
745
746 out:
747 spin_unlock_irqrestore(&dev->power.lock, flags);
748
749 return retval;
750}
751EXPORT_SYMBOL_GPL(pm_schedule_suspend);
752
753/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200754 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200755 * @dev: Device to send idle notification for.
756 * @rpmflags: Flag bits.
757 *
758 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
759 * return immediately if it is larger than zero. Then carry out an idle
760 * notification, either synchronous or asynchronous.
761 *
Colin Cross311aab72011-08-08 23:39:36 +0200762 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
763 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200764 */
Alan Stern140a6c92010-09-25 23:35:07 +0200765int __pm_runtime_idle(struct device *dev, int rpmflags)
766{
767 unsigned long flags;
768 int retval;
769
Colin Cross311aab72011-08-08 23:39:36 +0200770 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
771
Alan Stern140a6c92010-09-25 23:35:07 +0200772 if (rpmflags & RPM_GET_PUT) {
773 if (!atomic_dec_and_test(&dev->power.usage_count))
774 return 0;
775 }
776
777 spin_lock_irqsave(&dev->power.lock, flags);
778 retval = rpm_idle(dev, rpmflags);
779 spin_unlock_irqrestore(&dev->power.lock, flags);
780
781 return retval;
782}
783EXPORT_SYMBOL_GPL(__pm_runtime_idle);
784
785/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200786 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200787 * @dev: Device to suspend.
788 * @rpmflags: Flag bits.
789 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200790 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
791 * return immediately if it is larger than zero. Then carry out a suspend,
792 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200793 *
Colin Cross311aab72011-08-08 23:39:36 +0200794 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
795 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200796 */
797int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200798{
799 unsigned long flags;
800 int retval;
801
Colin Cross311aab72011-08-08 23:39:36 +0200802 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
803
Alan Stern15bcb91d2010-09-25 23:35:21 +0200804 if (rpmflags & RPM_GET_PUT) {
805 if (!atomic_dec_and_test(&dev->power.usage_count))
806 return 0;
807 }
808
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200809 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200810 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200811 spin_unlock_irqrestore(&dev->power.lock, flags);
812
813 return retval;
814}
Alan Stern140a6c92010-09-25 23:35:07 +0200815EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200816
817/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200818 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200819 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200820 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200821 *
Alan Stern140a6c92010-09-25 23:35:07 +0200822 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
823 * carry out a resume, either synchronous or asynchronous.
824 *
Colin Cross311aab72011-08-08 23:39:36 +0200825 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
826 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200827 */
Alan Stern140a6c92010-09-25 23:35:07 +0200828int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200829{
Alan Stern140a6c92010-09-25 23:35:07 +0200830 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100831 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200832
Colin Cross311aab72011-08-08 23:39:36 +0200833 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
834
Alan Stern140a6c92010-09-25 23:35:07 +0200835 if (rpmflags & RPM_GET_PUT)
836 atomic_inc(&dev->power.usage_count);
837
838 spin_lock_irqsave(&dev->power.lock, flags);
839 retval = rpm_resume(dev, rpmflags);
840 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200841
842 return retval;
843}
Alan Stern140a6c92010-09-25 23:35:07 +0200844EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200845
846/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200847 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200848 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200849 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200850 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200851 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200852 * different from zero, the status may be changed either to RPM_ACTIVE, or to
853 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
854 * However, if the device has a parent and the parent is not active, and the
855 * parent's power.ignore_children flag is unset, the device's status cannot be
856 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
857 *
858 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
859 * and the device parent's counter of unsuspended children is modified to
860 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
861 * notification request for the parent is submitted.
862 */
863int __pm_runtime_set_status(struct device *dev, unsigned int status)
864{
865 struct device *parent = dev->parent;
866 unsigned long flags;
867 bool notify_parent = false;
868 int error = 0;
869
870 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
871 return -EINVAL;
872
873 spin_lock_irqsave(&dev->power.lock, flags);
874
875 if (!dev->power.runtime_error && !dev->power.disable_depth) {
876 error = -EAGAIN;
877 goto out;
878 }
879
880 if (dev->power.runtime_status == status)
881 goto out_set;
882
883 if (status == RPM_SUSPENDED) {
884 /* It always is possible to set the status to 'suspended'. */
885 if (parent) {
886 atomic_add_unless(&parent->power.child_count, -1, 0);
887 notify_parent = !parent->power.ignore_children;
888 }
889 goto out_set;
890 }
891
892 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100893 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200894
895 /*
896 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200897 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200898 * 'power.ignore_children' flag unset.
899 */
900 if (!parent->power.disable_depth
901 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100902 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200903 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100904 else if (dev->power.runtime_status == RPM_SUSPENDED)
905 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200906
Alan Stern862f89b2009-11-25 01:06:37 +0100907 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200908
909 if (error)
910 goto out;
911 }
912
913 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200914 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200915 dev->power.runtime_error = 0;
916 out:
917 spin_unlock_irqrestore(&dev->power.lock, flags);
918
919 if (notify_parent)
920 pm_request_idle(parent);
921
922 return error;
923}
924EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
925
926/**
927 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
928 * @dev: Device to handle.
929 *
930 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200931 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200932 *
933 * Should be called under dev->power.lock with interrupts disabled.
934 */
935static void __pm_runtime_barrier(struct device *dev)
936{
937 pm_runtime_deactivate_timer(dev);
938
939 if (dev->power.request_pending) {
940 dev->power.request = RPM_REQ_NONE;
941 spin_unlock_irq(&dev->power.lock);
942
943 cancel_work_sync(&dev->power.work);
944
945 spin_lock_irq(&dev->power.lock);
946 dev->power.request_pending = false;
947 }
948
949 if (dev->power.runtime_status == RPM_SUSPENDING
950 || dev->power.runtime_status == RPM_RESUMING
951 || dev->power.idle_notification) {
952 DEFINE_WAIT(wait);
953
954 /* Suspend, wake-up or idle notification in progress. */
955 for (;;) {
956 prepare_to_wait(&dev->power.wait_queue, &wait,
957 TASK_UNINTERRUPTIBLE);
958 if (dev->power.runtime_status != RPM_SUSPENDING
959 && dev->power.runtime_status != RPM_RESUMING
960 && !dev->power.idle_notification)
961 break;
962 spin_unlock_irq(&dev->power.lock);
963
964 schedule();
965
966 spin_lock_irq(&dev->power.lock);
967 }
968 finish_wait(&dev->power.wait_queue, &wait);
969 }
970}
971
972/**
973 * pm_runtime_barrier - Flush pending requests and wait for completions.
974 * @dev: Device to handle.
975 *
976 * Prevent the device from being suspended by incrementing its usage counter and
977 * if there's a pending resume request for the device, wake the device up.
978 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200979 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200980 * progress to complete.
981 *
982 * Return value:
983 * 1, if there was a resume request pending and the device had to be woken up,
984 * 0, otherwise
985 */
986int pm_runtime_barrier(struct device *dev)
987{
988 int retval = 0;
989
990 pm_runtime_get_noresume(dev);
991 spin_lock_irq(&dev->power.lock);
992
993 if (dev->power.request_pending
994 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200995 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200996 retval = 1;
997 }
998
999 __pm_runtime_barrier(dev);
1000
1001 spin_unlock_irq(&dev->power.lock);
1002 pm_runtime_put_noidle(dev);
1003
1004 return retval;
1005}
1006EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1007
1008/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001009 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001010 * @dev: Device to handle.
1011 * @check_resume: If set, check if there's a resume request for the device.
1012 *
1013 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001014 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001015 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001016 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001017 *
1018 * If @check_resume is set and there's a resume request pending when
1019 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001020 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001021 */
1022void __pm_runtime_disable(struct device *dev, bool check_resume)
1023{
1024 spin_lock_irq(&dev->power.lock);
1025
1026 if (dev->power.disable_depth > 0) {
1027 dev->power.disable_depth++;
1028 goto out;
1029 }
1030
1031 /*
1032 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001033 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001034 * shouldn't prevent the device from processing the I/O.
1035 */
1036 if (check_resume && dev->power.request_pending
1037 && dev->power.request == RPM_REQ_RESUME) {
1038 /*
1039 * Prevent suspends and idle notifications from being carried
1040 * out after we have woken up the device.
1041 */
1042 pm_runtime_get_noresume(dev);
1043
Alan Stern140a6c92010-09-25 23:35:07 +02001044 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001045
1046 pm_runtime_put_noidle(dev);
1047 }
1048
1049 if (!dev->power.disable_depth++)
1050 __pm_runtime_barrier(dev);
1051
1052 out:
1053 spin_unlock_irq(&dev->power.lock);
1054}
1055EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1056
1057/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001058 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001059 * @dev: Device to handle.
1060 */
1061void pm_runtime_enable(struct device *dev)
1062{
1063 unsigned long flags;
1064
1065 spin_lock_irqsave(&dev->power.lock, flags);
1066
1067 if (dev->power.disable_depth > 0)
1068 dev->power.disable_depth--;
1069 else
1070 dev_warn(dev, "Unbalanced %s!\n", __func__);
1071
1072 spin_unlock_irqrestore(&dev->power.lock, flags);
1073}
1074EXPORT_SYMBOL_GPL(pm_runtime_enable);
1075
1076/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001077 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001078 * @dev: Device to handle.
1079 *
1080 * Increase the device's usage count and clear its power.runtime_auto flag,
1081 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1082 * for it.
1083 */
1084void pm_runtime_forbid(struct device *dev)
1085{
1086 spin_lock_irq(&dev->power.lock);
1087 if (!dev->power.runtime_auto)
1088 goto out;
1089
1090 dev->power.runtime_auto = false;
1091 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001092 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001093
1094 out:
1095 spin_unlock_irq(&dev->power.lock);
1096}
1097EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1098
1099/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001100 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001101 * @dev: Device to handle.
1102 *
1103 * Decrease the device's usage count and set its power.runtime_auto flag.
1104 */
1105void pm_runtime_allow(struct device *dev)
1106{
1107 spin_lock_irq(&dev->power.lock);
1108 if (dev->power.runtime_auto)
1109 goto out;
1110
1111 dev->power.runtime_auto = true;
1112 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001113 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001114
1115 out:
1116 spin_unlock_irq(&dev->power.lock);
1117}
1118EXPORT_SYMBOL_GPL(pm_runtime_allow);
1119
1120/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001121 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001122 * @dev: Device to handle.
1123 *
1124 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001125 * device is power-managed through its parent and has no runtime PM
1126 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001127 */
1128void pm_runtime_no_callbacks(struct device *dev)
1129{
1130 spin_lock_irq(&dev->power.lock);
1131 dev->power.no_callbacks = 1;
1132 spin_unlock_irq(&dev->power.lock);
1133 if (device_is_registered(dev))
1134 rpm_sysfs_remove(dev);
1135}
1136EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1137
1138/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001139 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1140 * @dev: Device to handle
1141 *
1142 * Set the power.irq_safe flag, which tells the PM core that the
1143 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1144 * always be invoked with the spinlock held and interrupts disabled. It also
1145 * causes the parent's usage counter to be permanently incremented, preventing
1146 * the parent from runtime suspending -- otherwise an irq-safe child might have
1147 * to wait for a non-irq-safe parent.
1148 */
1149void pm_runtime_irq_safe(struct device *dev)
1150{
1151 if (dev->parent)
1152 pm_runtime_get_sync(dev->parent);
1153 spin_lock_irq(&dev->power.lock);
1154 dev->power.irq_safe = 1;
1155 spin_unlock_irq(&dev->power.lock);
1156}
1157EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1158
1159/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001160 * update_autosuspend - Handle a change to a device's autosuspend settings.
1161 * @dev: Device to handle.
1162 * @old_delay: The former autosuspend_delay value.
1163 * @old_use: The former use_autosuspend value.
1164 *
1165 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1166 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1167 *
1168 * This function must be called under dev->power.lock with interrupts disabled.
1169 */
1170static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1171{
1172 int delay = dev->power.autosuspend_delay;
1173
1174 /* Should runtime suspend be prevented now? */
1175 if (dev->power.use_autosuspend && delay < 0) {
1176
1177 /* If it used to be allowed then prevent it. */
1178 if (!old_use || old_delay >= 0) {
1179 atomic_inc(&dev->power.usage_count);
1180 rpm_resume(dev, 0);
1181 }
1182 }
1183
1184 /* Runtime suspend should be allowed now. */
1185 else {
1186
1187 /* If it used to be prevented then allow it. */
1188 if (old_use && old_delay < 0)
1189 atomic_dec(&dev->power.usage_count);
1190
1191 /* Maybe we can autosuspend now. */
1192 rpm_idle(dev, RPM_AUTO);
1193 }
1194}
1195
1196/**
1197 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1198 * @dev: Device to handle.
1199 * @delay: Value of the new delay in milliseconds.
1200 *
1201 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001202 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1203 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001204 */
1205void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1206{
1207 int old_delay, old_use;
1208
1209 spin_lock_irq(&dev->power.lock);
1210 old_delay = dev->power.autosuspend_delay;
1211 old_use = dev->power.use_autosuspend;
1212 dev->power.autosuspend_delay = delay;
1213 update_autosuspend(dev, old_delay, old_use);
1214 spin_unlock_irq(&dev->power.lock);
1215}
1216EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1217
1218/**
1219 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1220 * @dev: Device to handle.
1221 * @use: New value for use_autosuspend.
1222 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001223 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001224 * suspends as needed.
1225 */
1226void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1227{
1228 int old_delay, old_use;
1229
1230 spin_lock_irq(&dev->power.lock);
1231 old_delay = dev->power.autosuspend_delay;
1232 old_use = dev->power.use_autosuspend;
1233 dev->power.use_autosuspend = use;
1234 update_autosuspend(dev, old_delay, old_use);
1235 spin_unlock_irq(&dev->power.lock);
1236}
1237EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1238
1239/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001240 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001241 * @dev: Device object to initialize.
1242 */
1243void pm_runtime_init(struct device *dev)
1244{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001245 dev->power.runtime_status = RPM_SUSPENDED;
1246 dev->power.idle_notification = false;
1247
1248 dev->power.disable_depth = 1;
1249 atomic_set(&dev->power.usage_count, 0);
1250
1251 dev->power.runtime_error = 0;
1252
1253 atomic_set(&dev->power.child_count, 0);
1254 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001255 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001256
1257 dev->power.request_pending = false;
1258 dev->power.request = RPM_REQ_NONE;
1259 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001260 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001261 INIT_WORK(&dev->power.work, pm_runtime_work);
1262
1263 dev->power.timer_expires = 0;
1264 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1265 (unsigned long)dev);
1266
1267 init_waitqueue_head(&dev->power.wait_queue);
1268}
1269
1270/**
1271 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1272 * @dev: Device object being removed from device hierarchy.
1273 */
1274void pm_runtime_remove(struct device *dev)
1275{
1276 __pm_runtime_disable(dev, false);
1277
1278 /* Change the status back to 'suspended' to match the initial status. */
1279 if (dev->power.runtime_status == RPM_ACTIVE)
1280 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001281 if (dev->power.irq_safe && dev->parent)
1282 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001283}