blob: aa23a64ea33bebd301cd3211b85193140761972c [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 Stern3f9af0512010-09-25 23:34:54 +0200287 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200288 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800289 * Check if the device's runtime PM status allows it to be suspended.
290 * Cancel a pending idle notification, autosuspend or suspend. If
291 * another suspend has been started earlier, either return immediately
292 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
293 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
294 * otherwise run the ->runtime_suspend() callback directly. If a deferred
295 * resume was requested while the callback was running then carry it out;
296 * otherwise send an idle notification for its parent (if the suspend
297 * succeeded and both ignore_children of parent->power and irq_safe of
298 * dev->power are not set).
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200299 *
300 * This function must be called under dev->power.lock with interrupts disabled.
301 */
Alan Stern140a6c92010-09-25 23:35:07 +0200302static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200303 __releases(&dev->power.lock) __acquires(&dev->power.lock)
304{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200305 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200306 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200307 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308
Ming Leic3dc2f12011-09-27 22:54:41 +0200309 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200310
311 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200312 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200313
Alan Stern1bfee5b2010-09-25 23:35:00 +0200314 if (retval < 0)
315 ; /* Conditions are wrong. */
316
317 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
318 else if (dev->power.runtime_status == RPM_RESUMING &&
319 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200320 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200321 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200322 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200323
Alan Stern15bcb91d2010-09-25 23:35:21 +0200324 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
325 if ((rpmflags & RPM_AUTO)
326 && dev->power.runtime_status != RPM_SUSPENDING) {
327 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
328
329 if (expires != 0) {
330 /* Pending requests need to be canceled. */
331 dev->power.request = RPM_REQ_NONE;
332
333 /*
334 * Optimization: If the timer is already running and is
335 * set to expire at or before the autosuspend delay,
336 * avoid the overhead of resetting it. Just let it
337 * expire; pm_suspend_timer_fn() will take care of the
338 * rest.
339 */
340 if (!(dev->power.timer_expires && time_before_eq(
341 dev->power.timer_expires, expires))) {
342 dev->power.timer_expires = expires;
343 mod_timer(&dev->power.suspend_timer, expires);
344 }
345 dev->power.timer_autosuspends = 1;
346 goto out;
347 }
348 }
349
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200350 /* Other scheduled or pending requests need to be canceled. */
351 pm_runtime_cancel_pending(dev);
352
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200353 if (dev->power.runtime_status == RPM_SUSPENDING) {
354 DEFINE_WAIT(wait);
355
Alan Stern1bfee5b2010-09-25 23:35:00 +0200356 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200357 retval = -EINPROGRESS;
358 goto out;
359 }
360
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200361 if (dev->power.irq_safe) {
362 spin_unlock(&dev->power.lock);
363
364 cpu_relax();
365
366 spin_lock(&dev->power.lock);
367 goto repeat;
368 }
369
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200370 /* Wait for the other suspend running in parallel with us. */
371 for (;;) {
372 prepare_to_wait(&dev->power.wait_queue, &wait,
373 TASK_UNINTERRUPTIBLE);
374 if (dev->power.runtime_status != RPM_SUSPENDING)
375 break;
376
377 spin_unlock_irq(&dev->power.lock);
378
379 schedule();
380
381 spin_lock_irq(&dev->power.lock);
382 }
383 finish_wait(&dev->power.wait_queue, &wait);
384 goto repeat;
385 }
386
Alan Stern7490e442010-09-25 23:35:15 +0200387 dev->power.deferred_resume = false;
388 if (dev->power.no_callbacks)
389 goto no_callback; /* Assume success. */
390
Alan Stern1bfee5b2010-09-25 23:35:00 +0200391 /* Carry out an asynchronous or a synchronous suspend. */
392 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200393 dev->power.request = (rpmflags & RPM_AUTO) ?
394 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200395 if (!dev->power.request_pending) {
396 dev->power.request_pending = true;
397 queue_work(pm_wq, &dev->power.work);
398 }
399 goto out;
400 }
401
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200402 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200403
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200404 if (dev->pm_domain)
405 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200406 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200407 callback = dev->type->pm->runtime_suspend;
408 else if (dev->class && dev->class->pm)
409 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100410 else if (dev->bus && dev->bus->pm)
411 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200412 else
413 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200414
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200415 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200416 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200417 __update_runtime_status(dev, RPM_ACTIVE);
ShuoX Liu2cffff12011-07-08 20:53:55 +0200418 dev->power.deferred_resume = false;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200419 if (retval == -EAGAIN || retval == -EBUSY)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200420 dev->power.runtime_error = 0;
Rafael J. Wysockif71648d2010-10-11 01:02:27 +0200421 else
Alan Stern240c7332010-03-23 00:50:07 +0100422 pm_runtime_cancel_pending(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200423 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200424 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200425 __update_runtime_status(dev, RPM_SUSPENDED);
Alan Stern240c7332010-03-23 00:50:07 +0100426 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200427
428 if (dev->parent) {
429 parent = dev->parent;
430 atomic_add_unless(&parent->power.child_count, -1, 0);
431 }
432 }
433 wake_up_all(&dev->power.wait_queue);
434
435 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200436 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200437 retval = -EAGAIN;
438 goto out;
439 }
440
Alan Sternc3810c82011-01-25 20:50:07 +0100441 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100442 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100443 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200444
Alan Sternc3810c82011-01-25 20:50:07 +0100445 spin_lock(&parent->power.lock);
446 rpm_idle(parent, RPM_ASYNC);
447 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200448
Alan Sternc3810c82011-01-25 20:50:07 +0100449 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200450 }
451
452 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200453 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200454
455 return retval;
456}
457
458/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200459 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200460 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200461 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200462 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200463 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200464 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300465 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200466 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
467 * parallel with this function, either tell the other process to resume after
468 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
469 * flag is set then queue a resume request; otherwise run the
470 * ->runtime_resume() callback directly. Queue an idle notification for the
471 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200472 *
473 * This function must be called under dev->power.lock with interrupts disabled.
474 */
Alan Stern140a6c92010-09-25 23:35:07 +0200475static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200476 __releases(&dev->power.lock) __acquires(&dev->power.lock)
477{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200478 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200479 struct device *parent = NULL;
480 int retval = 0;
481
Ming Leic3dc2f12011-09-27 22:54:41 +0200482 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200483
484 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200485 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200486 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200487 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200488 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200489 if (retval)
490 goto out;
491
Alan Stern15bcb91d2010-09-25 23:35:21 +0200492 /*
493 * Other scheduled or pending requests need to be canceled. Small
494 * optimization: If an autosuspend timer is running, leave it running
495 * rather than cancelling it now only to restart it again in the near
496 * future.
497 */
498 dev->power.request = RPM_REQ_NONE;
499 if (!dev->power.timer_autosuspends)
500 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200501
502 if (dev->power.runtime_status == RPM_ACTIVE) {
503 retval = 1;
504 goto out;
505 }
506
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200507 if (dev->power.runtime_status == RPM_RESUMING
508 || dev->power.runtime_status == RPM_SUSPENDING) {
509 DEFINE_WAIT(wait);
510
Alan Stern1bfee5b2010-09-25 23:35:00 +0200511 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200512 if (dev->power.runtime_status == RPM_SUSPENDING)
513 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200514 else
515 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200516 goto out;
517 }
518
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200519 if (dev->power.irq_safe) {
520 spin_unlock(&dev->power.lock);
521
522 cpu_relax();
523
524 spin_lock(&dev->power.lock);
525 goto repeat;
526 }
527
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200528 /* Wait for the operation carried out in parallel with us. */
529 for (;;) {
530 prepare_to_wait(&dev->power.wait_queue, &wait,
531 TASK_UNINTERRUPTIBLE);
532 if (dev->power.runtime_status != RPM_RESUMING
533 && dev->power.runtime_status != RPM_SUSPENDING)
534 break;
535
536 spin_unlock_irq(&dev->power.lock);
537
538 schedule();
539
540 spin_lock_irq(&dev->power.lock);
541 }
542 finish_wait(&dev->power.wait_queue, &wait);
543 goto repeat;
544 }
545
Alan Stern7490e442010-09-25 23:35:15 +0200546 /*
547 * See if we can skip waking up the parent. This is safe only if
548 * power.no_callbacks is set, because otherwise we don't know whether
549 * the resume will actually succeed.
550 */
551 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200552 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200553 if (dev->parent->power.disable_depth > 0
554 || dev->parent->power.ignore_children
555 || dev->parent->power.runtime_status == RPM_ACTIVE) {
556 atomic_inc(&dev->parent->power.child_count);
557 spin_unlock(&dev->parent->power.lock);
558 goto no_callback; /* Assume success. */
559 }
560 spin_unlock(&dev->parent->power.lock);
561 }
562
Alan Stern1bfee5b2010-09-25 23:35:00 +0200563 /* Carry out an asynchronous or a synchronous resume. */
564 if (rpmflags & RPM_ASYNC) {
565 dev->power.request = RPM_REQ_RESUME;
566 if (!dev->power.request_pending) {
567 dev->power.request_pending = true;
568 queue_work(pm_wq, &dev->power.work);
569 }
570 retval = 0;
571 goto out;
572 }
573
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200574 if (!parent && dev->parent) {
575 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100576 * Increment the parent's usage counter and resume it if
577 * necessary. Not needed if dev is irq-safe; then the
578 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200579 */
580 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100581 if (dev->power.irq_safe)
582 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100583 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200584
585 pm_runtime_get_noresume(parent);
586
Alan Stern862f89b2009-11-25 01:06:37 +0100587 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200588 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200589 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200590 * is set to ignore children.
591 */
592 if (!parent->power.disable_depth
593 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200594 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200595 if (parent->power.runtime_status != RPM_ACTIVE)
596 retval = -EBUSY;
597 }
Alan Stern862f89b2009-11-25 01:06:37 +0100598 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200599
Alan Stern862f89b2009-11-25 01:06:37 +0100600 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200601 if (retval)
602 goto out;
603 goto repeat;
604 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100605 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200606
Alan Stern7490e442010-09-25 23:35:15 +0200607 if (dev->power.no_callbacks)
608 goto no_callback; /* Assume success. */
609
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200610 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200611
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200612 if (dev->pm_domain)
613 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200614 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200615 callback = dev->type->pm->runtime_resume;
616 else if (dev->class && dev->class->pm)
617 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100618 else if (dev->bus && dev->bus->pm)
619 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200620 else
621 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200622
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200623 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200624 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200625 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200626 pm_runtime_cancel_pending(dev);
627 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200628 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200629 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200630 if (parent)
631 atomic_inc(&parent->power.child_count);
632 }
633 wake_up_all(&dev->power.wait_queue);
634
635 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200636 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200637
638 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100639 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200640 spin_unlock_irq(&dev->power.lock);
641
642 pm_runtime_put(parent);
643
644 spin_lock_irq(&dev->power.lock);
645 }
646
Ming Leic3dc2f12011-09-27 22:54:41 +0200647 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200648
649 return retval;
650}
651
652/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200653 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200654 * @work: Work structure used for scheduling the execution of this function.
655 *
656 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200657 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200658 */
659static void pm_runtime_work(struct work_struct *work)
660{
661 struct device *dev = container_of(work, struct device, power.work);
662 enum rpm_request req;
663
664 spin_lock_irq(&dev->power.lock);
665
666 if (!dev->power.request_pending)
667 goto out;
668
669 req = dev->power.request;
670 dev->power.request = RPM_REQ_NONE;
671 dev->power.request_pending = false;
672
673 switch (req) {
674 case RPM_REQ_NONE:
675 break;
676 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200677 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200678 break;
679 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200680 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200681 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200682 case RPM_REQ_AUTOSUSPEND:
683 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
684 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200685 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200686 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200687 break;
688 }
689
690 out:
691 spin_unlock_irq(&dev->power.lock);
692}
693
694/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200695 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
696 * @data: Device pointer passed by pm_schedule_suspend().
697 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200698 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200699 */
700static void pm_suspend_timer_fn(unsigned long data)
701{
702 struct device *dev = (struct device *)data;
703 unsigned long flags;
704 unsigned long expires;
705
706 spin_lock_irqsave(&dev->power.lock, flags);
707
708 expires = dev->power.timer_expires;
709 /* If 'expire' is after 'jiffies' we've been called too early. */
710 if (expires > 0 && !time_after(expires, jiffies)) {
711 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200712 rpm_suspend(dev, dev->power.timer_autosuspends ?
713 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200714 }
715
716 spin_unlock_irqrestore(&dev->power.lock, flags);
717}
718
719/**
720 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
721 * @dev: Device to suspend.
722 * @delay: Time to wait before submitting a suspend request, in milliseconds.
723 */
724int pm_schedule_suspend(struct device *dev, unsigned int delay)
725{
726 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200727 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200728
729 spin_lock_irqsave(&dev->power.lock, flags);
730
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200731 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200732 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200733 goto out;
734 }
735
Alan Stern1bfee5b2010-09-25 23:35:00 +0200736 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200737 if (retval)
738 goto out;
739
Alan Stern1bfee5b2010-09-25 23:35:00 +0200740 /* Other scheduled or pending requests need to be canceled. */
741 pm_runtime_cancel_pending(dev);
742
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200743 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200744 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200745 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200746 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
747
748 out:
749 spin_unlock_irqrestore(&dev->power.lock, flags);
750
751 return retval;
752}
753EXPORT_SYMBOL_GPL(pm_schedule_suspend);
754
755/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200756 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200757 * @dev: Device to send idle notification for.
758 * @rpmflags: Flag bits.
759 *
760 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
761 * return immediately if it is larger than zero. Then carry out an idle
762 * notification, either synchronous or asynchronous.
763 *
Colin Cross311aab72011-08-08 23:39:36 +0200764 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
765 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200766 */
Alan Stern140a6c92010-09-25 23:35:07 +0200767int __pm_runtime_idle(struct device *dev, int rpmflags)
768{
769 unsigned long flags;
770 int retval;
771
Colin Cross311aab72011-08-08 23:39:36 +0200772 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
773
Alan Stern140a6c92010-09-25 23:35:07 +0200774 if (rpmflags & RPM_GET_PUT) {
775 if (!atomic_dec_and_test(&dev->power.usage_count))
776 return 0;
777 }
778
779 spin_lock_irqsave(&dev->power.lock, flags);
780 retval = rpm_idle(dev, rpmflags);
781 spin_unlock_irqrestore(&dev->power.lock, flags);
782
783 return retval;
784}
785EXPORT_SYMBOL_GPL(__pm_runtime_idle);
786
787/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200788 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200789 * @dev: Device to suspend.
790 * @rpmflags: Flag bits.
791 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200792 * 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 a suspend,
794 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200795 *
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.
Alan Stern140a6c92010-09-25 23:35:07 +0200798 */
799int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200800{
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 Stern15bcb91d2010-09-25 23:35:21 +0200806 if (rpmflags & RPM_GET_PUT) {
807 if (!atomic_dec_and_test(&dev->power.usage_count))
808 return 0;
809 }
810
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200811 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200812 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200813 spin_unlock_irqrestore(&dev->power.lock, flags);
814
815 return retval;
816}
Alan Stern140a6c92010-09-25 23:35:07 +0200817EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200818
819/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200820 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200821 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200822 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200823 *
Alan Stern140a6c92010-09-25 23:35:07 +0200824 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
825 * carry out a resume, either synchronous or asynchronous.
826 *
Colin Cross311aab72011-08-08 23:39:36 +0200827 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
828 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200829 */
Alan Stern140a6c92010-09-25 23:35:07 +0200830int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200831{
Alan Stern140a6c92010-09-25 23:35:07 +0200832 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100833 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200834
Colin Cross311aab72011-08-08 23:39:36 +0200835 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
836
Alan Stern140a6c92010-09-25 23:35:07 +0200837 if (rpmflags & RPM_GET_PUT)
838 atomic_inc(&dev->power.usage_count);
839
840 spin_lock_irqsave(&dev->power.lock, flags);
841 retval = rpm_resume(dev, rpmflags);
842 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200843
844 return retval;
845}
Alan Stern140a6c92010-09-25 23:35:07 +0200846EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200847
848/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200849 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200850 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200851 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200852 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200853 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200854 * different from zero, the status may be changed either to RPM_ACTIVE, or to
855 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
856 * However, if the device has a parent and the parent is not active, and the
857 * parent's power.ignore_children flag is unset, the device's status cannot be
858 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
859 *
860 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
861 * and the device parent's counter of unsuspended children is modified to
862 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
863 * notification request for the parent is submitted.
864 */
865int __pm_runtime_set_status(struct device *dev, unsigned int status)
866{
867 struct device *parent = dev->parent;
868 unsigned long flags;
869 bool notify_parent = false;
870 int error = 0;
871
872 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
873 return -EINVAL;
874
875 spin_lock_irqsave(&dev->power.lock, flags);
876
877 if (!dev->power.runtime_error && !dev->power.disable_depth) {
878 error = -EAGAIN;
879 goto out;
880 }
881
882 if (dev->power.runtime_status == status)
883 goto out_set;
884
885 if (status == RPM_SUSPENDED) {
886 /* It always is possible to set the status to 'suspended'. */
887 if (parent) {
888 atomic_add_unless(&parent->power.child_count, -1, 0);
889 notify_parent = !parent->power.ignore_children;
890 }
891 goto out_set;
892 }
893
894 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100895 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200896
897 /*
898 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200899 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200900 * 'power.ignore_children' flag unset.
901 */
902 if (!parent->power.disable_depth
903 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100904 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200905 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100906 else if (dev->power.runtime_status == RPM_SUSPENDED)
907 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200908
Alan Stern862f89b2009-11-25 01:06:37 +0100909 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200910
911 if (error)
912 goto out;
913 }
914
915 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200916 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200917 dev->power.runtime_error = 0;
918 out:
919 spin_unlock_irqrestore(&dev->power.lock, flags);
920
921 if (notify_parent)
922 pm_request_idle(parent);
923
924 return error;
925}
926EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
927
928/**
929 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
930 * @dev: Device to handle.
931 *
932 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200933 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200934 *
935 * Should be called under dev->power.lock with interrupts disabled.
936 */
937static void __pm_runtime_barrier(struct device *dev)
938{
939 pm_runtime_deactivate_timer(dev);
940
941 if (dev->power.request_pending) {
942 dev->power.request = RPM_REQ_NONE;
943 spin_unlock_irq(&dev->power.lock);
944
945 cancel_work_sync(&dev->power.work);
946
947 spin_lock_irq(&dev->power.lock);
948 dev->power.request_pending = false;
949 }
950
951 if (dev->power.runtime_status == RPM_SUSPENDING
952 || dev->power.runtime_status == RPM_RESUMING
953 || dev->power.idle_notification) {
954 DEFINE_WAIT(wait);
955
956 /* Suspend, wake-up or idle notification in progress. */
957 for (;;) {
958 prepare_to_wait(&dev->power.wait_queue, &wait,
959 TASK_UNINTERRUPTIBLE);
960 if (dev->power.runtime_status != RPM_SUSPENDING
961 && dev->power.runtime_status != RPM_RESUMING
962 && !dev->power.idle_notification)
963 break;
964 spin_unlock_irq(&dev->power.lock);
965
966 schedule();
967
968 spin_lock_irq(&dev->power.lock);
969 }
970 finish_wait(&dev->power.wait_queue, &wait);
971 }
972}
973
974/**
975 * pm_runtime_barrier - Flush pending requests and wait for completions.
976 * @dev: Device to handle.
977 *
978 * Prevent the device from being suspended by incrementing its usage counter and
979 * if there's a pending resume request for the device, wake the device up.
980 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200981 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200982 * progress to complete.
983 *
984 * Return value:
985 * 1, if there was a resume request pending and the device had to be woken up,
986 * 0, otherwise
987 */
988int pm_runtime_barrier(struct device *dev)
989{
990 int retval = 0;
991
992 pm_runtime_get_noresume(dev);
993 spin_lock_irq(&dev->power.lock);
994
995 if (dev->power.request_pending
996 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200997 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200998 retval = 1;
999 }
1000
1001 __pm_runtime_barrier(dev);
1002
1003 spin_unlock_irq(&dev->power.lock);
1004 pm_runtime_put_noidle(dev);
1005
1006 return retval;
1007}
1008EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1009
1010/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001011 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001012 * @dev: Device to handle.
1013 * @check_resume: If set, check if there's a resume request for the device.
1014 *
1015 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001016 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001017 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001018 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001019 *
1020 * If @check_resume is set and there's a resume request pending when
1021 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001022 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001023 */
1024void __pm_runtime_disable(struct device *dev, bool check_resume)
1025{
1026 spin_lock_irq(&dev->power.lock);
1027
1028 if (dev->power.disable_depth > 0) {
1029 dev->power.disable_depth++;
1030 goto out;
1031 }
1032
1033 /*
1034 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001035 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001036 * shouldn't prevent the device from processing the I/O.
1037 */
1038 if (check_resume && dev->power.request_pending
1039 && dev->power.request == RPM_REQ_RESUME) {
1040 /*
1041 * Prevent suspends and idle notifications from being carried
1042 * out after we have woken up the device.
1043 */
1044 pm_runtime_get_noresume(dev);
1045
Alan Stern140a6c92010-09-25 23:35:07 +02001046 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001047
1048 pm_runtime_put_noidle(dev);
1049 }
1050
1051 if (!dev->power.disable_depth++)
1052 __pm_runtime_barrier(dev);
1053
1054 out:
1055 spin_unlock_irq(&dev->power.lock);
1056}
1057EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1058
1059/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001060 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001061 * @dev: Device to handle.
1062 */
1063void pm_runtime_enable(struct device *dev)
1064{
1065 unsigned long flags;
1066
1067 spin_lock_irqsave(&dev->power.lock, flags);
1068
1069 if (dev->power.disable_depth > 0)
1070 dev->power.disable_depth--;
1071 else
1072 dev_warn(dev, "Unbalanced %s!\n", __func__);
1073
1074 spin_unlock_irqrestore(&dev->power.lock, flags);
1075}
1076EXPORT_SYMBOL_GPL(pm_runtime_enable);
1077
1078/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001079 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001080 * @dev: Device to handle.
1081 *
1082 * Increase the device's usage count and clear its power.runtime_auto flag,
1083 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1084 * for it.
1085 */
1086void pm_runtime_forbid(struct device *dev)
1087{
1088 spin_lock_irq(&dev->power.lock);
1089 if (!dev->power.runtime_auto)
1090 goto out;
1091
1092 dev->power.runtime_auto = false;
1093 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001094 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001095
1096 out:
1097 spin_unlock_irq(&dev->power.lock);
1098}
1099EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1100
1101/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001102 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001103 * @dev: Device to handle.
1104 *
1105 * Decrease the device's usage count and set its power.runtime_auto flag.
1106 */
1107void pm_runtime_allow(struct device *dev)
1108{
1109 spin_lock_irq(&dev->power.lock);
1110 if (dev->power.runtime_auto)
1111 goto out;
1112
1113 dev->power.runtime_auto = true;
1114 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001115 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001116
1117 out:
1118 spin_unlock_irq(&dev->power.lock);
1119}
1120EXPORT_SYMBOL_GPL(pm_runtime_allow);
1121
1122/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001123 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001124 * @dev: Device to handle.
1125 *
1126 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001127 * device is power-managed through its parent and has no runtime PM
1128 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001129 */
1130void pm_runtime_no_callbacks(struct device *dev)
1131{
1132 spin_lock_irq(&dev->power.lock);
1133 dev->power.no_callbacks = 1;
1134 spin_unlock_irq(&dev->power.lock);
1135 if (device_is_registered(dev))
1136 rpm_sysfs_remove(dev);
1137}
1138EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1139
1140/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001141 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1142 * @dev: Device to handle
1143 *
1144 * Set the power.irq_safe flag, which tells the PM core that the
1145 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1146 * always be invoked with the spinlock held and interrupts disabled. It also
1147 * causes the parent's usage counter to be permanently incremented, preventing
1148 * the parent from runtime suspending -- otherwise an irq-safe child might have
1149 * to wait for a non-irq-safe parent.
1150 */
1151void pm_runtime_irq_safe(struct device *dev)
1152{
1153 if (dev->parent)
1154 pm_runtime_get_sync(dev->parent);
1155 spin_lock_irq(&dev->power.lock);
1156 dev->power.irq_safe = 1;
1157 spin_unlock_irq(&dev->power.lock);
1158}
1159EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1160
1161/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001162 * update_autosuspend - Handle a change to a device's autosuspend settings.
1163 * @dev: Device to handle.
1164 * @old_delay: The former autosuspend_delay value.
1165 * @old_use: The former use_autosuspend value.
1166 *
1167 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1168 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1169 *
1170 * This function must be called under dev->power.lock with interrupts disabled.
1171 */
1172static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1173{
1174 int delay = dev->power.autosuspend_delay;
1175
1176 /* Should runtime suspend be prevented now? */
1177 if (dev->power.use_autosuspend && delay < 0) {
1178
1179 /* If it used to be allowed then prevent it. */
1180 if (!old_use || old_delay >= 0) {
1181 atomic_inc(&dev->power.usage_count);
1182 rpm_resume(dev, 0);
1183 }
1184 }
1185
1186 /* Runtime suspend should be allowed now. */
1187 else {
1188
1189 /* If it used to be prevented then allow it. */
1190 if (old_use && old_delay < 0)
1191 atomic_dec(&dev->power.usage_count);
1192
1193 /* Maybe we can autosuspend now. */
1194 rpm_idle(dev, RPM_AUTO);
1195 }
1196}
1197
1198/**
1199 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1200 * @dev: Device to handle.
1201 * @delay: Value of the new delay in milliseconds.
1202 *
1203 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001204 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1205 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001206 */
1207void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1208{
1209 int old_delay, old_use;
1210
1211 spin_lock_irq(&dev->power.lock);
1212 old_delay = dev->power.autosuspend_delay;
1213 old_use = dev->power.use_autosuspend;
1214 dev->power.autosuspend_delay = delay;
1215 update_autosuspend(dev, old_delay, old_use);
1216 spin_unlock_irq(&dev->power.lock);
1217}
1218EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1219
1220/**
1221 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1222 * @dev: Device to handle.
1223 * @use: New value for use_autosuspend.
1224 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001225 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001226 * suspends as needed.
1227 */
1228void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1229{
1230 int old_delay, old_use;
1231
1232 spin_lock_irq(&dev->power.lock);
1233 old_delay = dev->power.autosuspend_delay;
1234 old_use = dev->power.use_autosuspend;
1235 dev->power.use_autosuspend = use;
1236 update_autosuspend(dev, old_delay, old_use);
1237 spin_unlock_irq(&dev->power.lock);
1238}
1239EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1240
1241/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001242 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001243 * @dev: Device object to initialize.
1244 */
1245void pm_runtime_init(struct device *dev)
1246{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001247 dev->power.runtime_status = RPM_SUSPENDED;
1248 dev->power.idle_notification = false;
1249
1250 dev->power.disable_depth = 1;
1251 atomic_set(&dev->power.usage_count, 0);
1252
1253 dev->power.runtime_error = 0;
1254
1255 atomic_set(&dev->power.child_count, 0);
1256 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001257 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001258
1259 dev->power.request_pending = false;
1260 dev->power.request = RPM_REQ_NONE;
1261 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001262 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001263 INIT_WORK(&dev->power.work, pm_runtime_work);
1264
1265 dev->power.timer_expires = 0;
1266 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1267 (unsigned long)dev);
1268
1269 init_waitqueue_head(&dev->power.wait_queue);
1270}
1271
1272/**
1273 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1274 * @dev: Device object being removed from device hierarchy.
1275 */
1276void pm_runtime_remove(struct device *dev)
1277{
1278 __pm_runtime_disable(dev, false);
1279
1280 /* Change the status back to 'suspended' to match the initial status. */
1281 if (dev->power.runtime_status == RPM_ACTIVE)
1282 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001283 if (dev->power.irq_safe && dev->parent)
1284 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001285}