blob: 6bb3aafa85ede9efe75d41abba500211fc9b262b [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;
Ming Lei857b36c2011-10-12 22:59:33 +0200294 * otherwise run the ->runtime_suspend() callback directly. When
295 * ->runtime_suspend succeeded, if a deferred resume was requested while
296 * the callback was running then carry it out, otherwise send an idle
297 * notification for its parent (if the suspend succeeded and both
298 * ignore_children of parent->power and irq_safe of 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);
Ming Lei857b36c2011-10-12 22:59:33 +0200423 wake_up_all(&dev->power.wait_queue);
424 goto out;
425 }
Alan Stern7490e442010-09-25 23:35:15 +0200426 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200427 __update_runtime_status(dev, RPM_SUSPENDED);
428 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200429
Ming Lei857b36c2011-10-12 22:59:33 +0200430 if (dev->parent) {
431 parent = dev->parent;
432 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200433 }
434 wake_up_all(&dev->power.wait_queue);
435
436 if (dev->power.deferred_resume) {
Alan Stern140a6c92010-09-25 23:35:07 +0200437 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200438 retval = -EAGAIN;
439 goto out;
440 }
441
Alan Sternc3810c82011-01-25 20:50:07 +0100442 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100443 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100444 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200445
Alan Sternc3810c82011-01-25 20:50:07 +0100446 spin_lock(&parent->power.lock);
447 rpm_idle(parent, RPM_ASYNC);
448 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200449
Alan Sternc3810c82011-01-25 20:50:07 +0100450 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200451 }
452
453 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200454 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200455
456 return retval;
457}
458
459/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200460 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200461 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200462 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200463 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200464 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200465 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300466 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200467 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
468 * parallel with this function, either tell the other process to resume after
469 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
470 * flag is set then queue a resume request; otherwise run the
471 * ->runtime_resume() callback directly. Queue an idle notification for the
472 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200473 *
474 * This function must be called under dev->power.lock with interrupts disabled.
475 */
Alan Stern140a6c92010-09-25 23:35:07 +0200476static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200477 __releases(&dev->power.lock) __acquires(&dev->power.lock)
478{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200479 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200480 struct device *parent = NULL;
481 int retval = 0;
482
Ming Leic3dc2f12011-09-27 22:54:41 +0200483 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200484
485 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200486 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200487 retval = -EINVAL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200488 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200489 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200490 if (retval)
491 goto out;
492
Alan Stern15bcb91d2010-09-25 23:35:21 +0200493 /*
494 * Other scheduled or pending requests need to be canceled. Small
495 * optimization: If an autosuspend timer is running, leave it running
496 * rather than cancelling it now only to restart it again in the near
497 * future.
498 */
499 dev->power.request = RPM_REQ_NONE;
500 if (!dev->power.timer_autosuspends)
501 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200502
503 if (dev->power.runtime_status == RPM_ACTIVE) {
504 retval = 1;
505 goto out;
506 }
507
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200508 if (dev->power.runtime_status == RPM_RESUMING
509 || dev->power.runtime_status == RPM_SUSPENDING) {
510 DEFINE_WAIT(wait);
511
Alan Stern1bfee5b2010-09-25 23:35:00 +0200512 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200513 if (dev->power.runtime_status == RPM_SUSPENDING)
514 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200515 else
516 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200517 goto out;
518 }
519
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200520 if (dev->power.irq_safe) {
521 spin_unlock(&dev->power.lock);
522
523 cpu_relax();
524
525 spin_lock(&dev->power.lock);
526 goto repeat;
527 }
528
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200529 /* Wait for the operation carried out in parallel with us. */
530 for (;;) {
531 prepare_to_wait(&dev->power.wait_queue, &wait,
532 TASK_UNINTERRUPTIBLE);
533 if (dev->power.runtime_status != RPM_RESUMING
534 && dev->power.runtime_status != RPM_SUSPENDING)
535 break;
536
537 spin_unlock_irq(&dev->power.lock);
538
539 schedule();
540
541 spin_lock_irq(&dev->power.lock);
542 }
543 finish_wait(&dev->power.wait_queue, &wait);
544 goto repeat;
545 }
546
Alan Stern7490e442010-09-25 23:35:15 +0200547 /*
548 * See if we can skip waking up the parent. This is safe only if
549 * power.no_callbacks is set, because otherwise we don't know whether
550 * the resume will actually succeed.
551 */
552 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200553 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200554 if (dev->parent->power.disable_depth > 0
555 || dev->parent->power.ignore_children
556 || dev->parent->power.runtime_status == RPM_ACTIVE) {
557 atomic_inc(&dev->parent->power.child_count);
558 spin_unlock(&dev->parent->power.lock);
559 goto no_callback; /* Assume success. */
560 }
561 spin_unlock(&dev->parent->power.lock);
562 }
563
Alan Stern1bfee5b2010-09-25 23:35:00 +0200564 /* Carry out an asynchronous or a synchronous resume. */
565 if (rpmflags & RPM_ASYNC) {
566 dev->power.request = RPM_REQ_RESUME;
567 if (!dev->power.request_pending) {
568 dev->power.request_pending = true;
569 queue_work(pm_wq, &dev->power.work);
570 }
571 retval = 0;
572 goto out;
573 }
574
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200575 if (!parent && dev->parent) {
576 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100577 * Increment the parent's usage counter and resume it if
578 * necessary. Not needed if dev is irq-safe; then the
579 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200580 */
581 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100582 if (dev->power.irq_safe)
583 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100584 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200585
586 pm_runtime_get_noresume(parent);
587
Alan Stern862f89b2009-11-25 01:06:37 +0100588 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200589 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200590 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200591 * is set to ignore children.
592 */
593 if (!parent->power.disable_depth
594 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200595 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200596 if (parent->power.runtime_status != RPM_ACTIVE)
597 retval = -EBUSY;
598 }
Alan Stern862f89b2009-11-25 01:06:37 +0100599 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200600
Alan Stern862f89b2009-11-25 01:06:37 +0100601 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602 if (retval)
603 goto out;
604 goto repeat;
605 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100606 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200607
Alan Stern7490e442010-09-25 23:35:15 +0200608 if (dev->power.no_callbacks)
609 goto no_callback; /* Assume success. */
610
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200611 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200612
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200613 if (dev->pm_domain)
614 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200615 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200616 callback = dev->type->pm->runtime_resume;
617 else if (dev->class && dev->class->pm)
618 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100619 else if (dev->bus && dev->bus->pm)
620 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200621 else
622 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200623
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200624 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200625 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200626 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200627 pm_runtime_cancel_pending(dev);
628 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200629 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200630 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200631 if (parent)
632 atomic_inc(&parent->power.child_count);
633 }
634 wake_up_all(&dev->power.wait_queue);
635
636 if (!retval)
Alan Stern140a6c92010-09-25 23:35:07 +0200637 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200638
639 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100640 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200641 spin_unlock_irq(&dev->power.lock);
642
643 pm_runtime_put(parent);
644
645 spin_lock_irq(&dev->power.lock);
646 }
647
Ming Leic3dc2f12011-09-27 22:54:41 +0200648 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200649
650 return retval;
651}
652
653/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200654 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200655 * @work: Work structure used for scheduling the execution of this function.
656 *
657 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200658 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200659 */
660static void pm_runtime_work(struct work_struct *work)
661{
662 struct device *dev = container_of(work, struct device, power.work);
663 enum rpm_request req;
664
665 spin_lock_irq(&dev->power.lock);
666
667 if (!dev->power.request_pending)
668 goto out;
669
670 req = dev->power.request;
671 dev->power.request = RPM_REQ_NONE;
672 dev->power.request_pending = false;
673
674 switch (req) {
675 case RPM_REQ_NONE:
676 break;
677 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200678 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200679 break;
680 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200681 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200682 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200683 case RPM_REQ_AUTOSUSPEND:
684 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
685 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200686 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200687 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200688 break;
689 }
690
691 out:
692 spin_unlock_irq(&dev->power.lock);
693}
694
695/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200696 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
697 * @data: Device pointer passed by pm_schedule_suspend().
698 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200699 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200700 */
701static void pm_suspend_timer_fn(unsigned long data)
702{
703 struct device *dev = (struct device *)data;
704 unsigned long flags;
705 unsigned long expires;
706
707 spin_lock_irqsave(&dev->power.lock, flags);
708
709 expires = dev->power.timer_expires;
710 /* If 'expire' is after 'jiffies' we've been called too early. */
711 if (expires > 0 && !time_after(expires, jiffies)) {
712 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200713 rpm_suspend(dev, dev->power.timer_autosuspends ?
714 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200715 }
716
717 spin_unlock_irqrestore(&dev->power.lock, flags);
718}
719
720/**
721 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
722 * @dev: Device to suspend.
723 * @delay: Time to wait before submitting a suspend request, in milliseconds.
724 */
725int pm_schedule_suspend(struct device *dev, unsigned int delay)
726{
727 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200728 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200729
730 spin_lock_irqsave(&dev->power.lock, flags);
731
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200732 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200733 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200734 goto out;
735 }
736
Alan Stern1bfee5b2010-09-25 23:35:00 +0200737 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200738 if (retval)
739 goto out;
740
Alan Stern1bfee5b2010-09-25 23:35:00 +0200741 /* Other scheduled or pending requests need to be canceled. */
742 pm_runtime_cancel_pending(dev);
743
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200744 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200745 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200746 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200747 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
748
749 out:
750 spin_unlock_irqrestore(&dev->power.lock, flags);
751
752 return retval;
753}
754EXPORT_SYMBOL_GPL(pm_schedule_suspend);
755
756/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200757 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200758 * @dev: Device to send idle notification for.
759 * @rpmflags: Flag bits.
760 *
761 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
762 * return immediately if it is larger than zero. Then carry out an idle
763 * notification, either synchronous or asynchronous.
764 *
Colin Cross311aab72011-08-08 23:39:36 +0200765 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
766 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200767 */
Alan Stern140a6c92010-09-25 23:35:07 +0200768int __pm_runtime_idle(struct device *dev, int rpmflags)
769{
770 unsigned long flags;
771 int retval;
772
Colin Cross311aab72011-08-08 23:39:36 +0200773 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
774
Alan Stern140a6c92010-09-25 23:35:07 +0200775 if (rpmflags & RPM_GET_PUT) {
776 if (!atomic_dec_and_test(&dev->power.usage_count))
777 return 0;
778 }
779
780 spin_lock_irqsave(&dev->power.lock, flags);
781 retval = rpm_idle(dev, rpmflags);
782 spin_unlock_irqrestore(&dev->power.lock, flags);
783
784 return retval;
785}
786EXPORT_SYMBOL_GPL(__pm_runtime_idle);
787
788/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200789 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200790 * @dev: Device to suspend.
791 * @rpmflags: Flag bits.
792 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200793 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
794 * return immediately if it is larger than zero. Then carry out a suspend,
795 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200796 *
Colin Cross311aab72011-08-08 23:39:36 +0200797 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
798 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200799 */
800int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200801{
802 unsigned long flags;
803 int retval;
804
Colin Cross311aab72011-08-08 23:39:36 +0200805 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
806
Alan Stern15bcb91d2010-09-25 23:35:21 +0200807 if (rpmflags & RPM_GET_PUT) {
808 if (!atomic_dec_and_test(&dev->power.usage_count))
809 return 0;
810 }
811
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200812 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200813 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200814 spin_unlock_irqrestore(&dev->power.lock, flags);
815
816 return retval;
817}
Alan Stern140a6c92010-09-25 23:35:07 +0200818EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200819
820/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200821 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200822 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200823 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200824 *
Alan Stern140a6c92010-09-25 23:35:07 +0200825 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
826 * carry out a resume, either synchronous or asynchronous.
827 *
Colin Cross311aab72011-08-08 23:39:36 +0200828 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
829 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200830 */
Alan Stern140a6c92010-09-25 23:35:07 +0200831int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200832{
Alan Stern140a6c92010-09-25 23:35:07 +0200833 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100834 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200835
Colin Cross311aab72011-08-08 23:39:36 +0200836 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
837
Alan Stern140a6c92010-09-25 23:35:07 +0200838 if (rpmflags & RPM_GET_PUT)
839 atomic_inc(&dev->power.usage_count);
840
841 spin_lock_irqsave(&dev->power.lock, flags);
842 retval = rpm_resume(dev, rpmflags);
843 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200844
845 return retval;
846}
Alan Stern140a6c92010-09-25 23:35:07 +0200847EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200848
849/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200850 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200851 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200852 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200853 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200854 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200855 * different from zero, the status may be changed either to RPM_ACTIVE, or to
856 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
857 * However, if the device has a parent and the parent is not active, and the
858 * parent's power.ignore_children flag is unset, the device's status cannot be
859 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
860 *
861 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
862 * and the device parent's counter of unsuspended children is modified to
863 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
864 * notification request for the parent is submitted.
865 */
866int __pm_runtime_set_status(struct device *dev, unsigned int status)
867{
868 struct device *parent = dev->parent;
869 unsigned long flags;
870 bool notify_parent = false;
871 int error = 0;
872
873 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
874 return -EINVAL;
875
876 spin_lock_irqsave(&dev->power.lock, flags);
877
878 if (!dev->power.runtime_error && !dev->power.disable_depth) {
879 error = -EAGAIN;
880 goto out;
881 }
882
883 if (dev->power.runtime_status == status)
884 goto out_set;
885
886 if (status == RPM_SUSPENDED) {
887 /* It always is possible to set the status to 'suspended'. */
888 if (parent) {
889 atomic_add_unless(&parent->power.child_count, -1, 0);
890 notify_parent = !parent->power.ignore_children;
891 }
892 goto out_set;
893 }
894
895 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +0100896 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200897
898 /*
899 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200900 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200901 * 'power.ignore_children' flag unset.
902 */
903 if (!parent->power.disable_depth
904 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100905 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200906 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +0100907 else if (dev->power.runtime_status == RPM_SUSPENDED)
908 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200909
Alan Stern862f89b2009-11-25 01:06:37 +0100910 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200911
912 if (error)
913 goto out;
914 }
915
916 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200917 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200918 dev->power.runtime_error = 0;
919 out:
920 spin_unlock_irqrestore(&dev->power.lock, flags);
921
922 if (notify_parent)
923 pm_request_idle(parent);
924
925 return error;
926}
927EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
928
929/**
930 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
931 * @dev: Device to handle.
932 *
933 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200934 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200935 *
936 * Should be called under dev->power.lock with interrupts disabled.
937 */
938static void __pm_runtime_barrier(struct device *dev)
939{
940 pm_runtime_deactivate_timer(dev);
941
942 if (dev->power.request_pending) {
943 dev->power.request = RPM_REQ_NONE;
944 spin_unlock_irq(&dev->power.lock);
945
946 cancel_work_sync(&dev->power.work);
947
948 spin_lock_irq(&dev->power.lock);
949 dev->power.request_pending = false;
950 }
951
952 if (dev->power.runtime_status == RPM_SUSPENDING
953 || dev->power.runtime_status == RPM_RESUMING
954 || dev->power.idle_notification) {
955 DEFINE_WAIT(wait);
956
957 /* Suspend, wake-up or idle notification in progress. */
958 for (;;) {
959 prepare_to_wait(&dev->power.wait_queue, &wait,
960 TASK_UNINTERRUPTIBLE);
961 if (dev->power.runtime_status != RPM_SUSPENDING
962 && dev->power.runtime_status != RPM_RESUMING
963 && !dev->power.idle_notification)
964 break;
965 spin_unlock_irq(&dev->power.lock);
966
967 schedule();
968
969 spin_lock_irq(&dev->power.lock);
970 }
971 finish_wait(&dev->power.wait_queue, &wait);
972 }
973}
974
975/**
976 * pm_runtime_barrier - Flush pending requests and wait for completions.
977 * @dev: Device to handle.
978 *
979 * Prevent the device from being suspended by incrementing its usage counter and
980 * if there's a pending resume request for the device, wake the device up.
981 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200982 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200983 * progress to complete.
984 *
985 * Return value:
986 * 1, if there was a resume request pending and the device had to be woken up,
987 * 0, otherwise
988 */
989int pm_runtime_barrier(struct device *dev)
990{
991 int retval = 0;
992
993 pm_runtime_get_noresume(dev);
994 spin_lock_irq(&dev->power.lock);
995
996 if (dev->power.request_pending
997 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +0200998 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200999 retval = 1;
1000 }
1001
1002 __pm_runtime_barrier(dev);
1003
1004 spin_unlock_irq(&dev->power.lock);
1005 pm_runtime_put_noidle(dev);
1006
1007 return retval;
1008}
1009EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1010
1011/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001012 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001013 * @dev: Device to handle.
1014 * @check_resume: If set, check if there's a resume request for the device.
1015 *
1016 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001017 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001018 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001019 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001020 *
1021 * If @check_resume is set and there's a resume request pending when
1022 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001023 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001024 */
1025void __pm_runtime_disable(struct device *dev, bool check_resume)
1026{
1027 spin_lock_irq(&dev->power.lock);
1028
1029 if (dev->power.disable_depth > 0) {
1030 dev->power.disable_depth++;
1031 goto out;
1032 }
1033
1034 /*
1035 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001036 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001037 * shouldn't prevent the device from processing the I/O.
1038 */
1039 if (check_resume && dev->power.request_pending
1040 && dev->power.request == RPM_REQ_RESUME) {
1041 /*
1042 * Prevent suspends and idle notifications from being carried
1043 * out after we have woken up the device.
1044 */
1045 pm_runtime_get_noresume(dev);
1046
Alan Stern140a6c92010-09-25 23:35:07 +02001047 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001048
1049 pm_runtime_put_noidle(dev);
1050 }
1051
1052 if (!dev->power.disable_depth++)
1053 __pm_runtime_barrier(dev);
1054
1055 out:
1056 spin_unlock_irq(&dev->power.lock);
1057}
1058EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1059
1060/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001061 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001062 * @dev: Device to handle.
1063 */
1064void pm_runtime_enable(struct device *dev)
1065{
1066 unsigned long flags;
1067
1068 spin_lock_irqsave(&dev->power.lock, flags);
1069
1070 if (dev->power.disable_depth > 0)
1071 dev->power.disable_depth--;
1072 else
1073 dev_warn(dev, "Unbalanced %s!\n", __func__);
1074
1075 spin_unlock_irqrestore(&dev->power.lock, flags);
1076}
1077EXPORT_SYMBOL_GPL(pm_runtime_enable);
1078
1079/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001080 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001081 * @dev: Device to handle.
1082 *
1083 * Increase the device's usage count and clear its power.runtime_auto flag,
1084 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1085 * for it.
1086 */
1087void pm_runtime_forbid(struct device *dev)
1088{
1089 spin_lock_irq(&dev->power.lock);
1090 if (!dev->power.runtime_auto)
1091 goto out;
1092
1093 dev->power.runtime_auto = false;
1094 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001095 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001096
1097 out:
1098 spin_unlock_irq(&dev->power.lock);
1099}
1100EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1101
1102/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001103 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001104 * @dev: Device to handle.
1105 *
1106 * Decrease the device's usage count and set its power.runtime_auto flag.
1107 */
1108void pm_runtime_allow(struct device *dev)
1109{
1110 spin_lock_irq(&dev->power.lock);
1111 if (dev->power.runtime_auto)
1112 goto out;
1113
1114 dev->power.runtime_auto = true;
1115 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001116 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001117
1118 out:
1119 spin_unlock_irq(&dev->power.lock);
1120}
1121EXPORT_SYMBOL_GPL(pm_runtime_allow);
1122
1123/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001124 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001125 * @dev: Device to handle.
1126 *
1127 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001128 * device is power-managed through its parent and has no runtime PM
1129 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001130 */
1131void pm_runtime_no_callbacks(struct device *dev)
1132{
1133 spin_lock_irq(&dev->power.lock);
1134 dev->power.no_callbacks = 1;
1135 spin_unlock_irq(&dev->power.lock);
1136 if (device_is_registered(dev))
1137 rpm_sysfs_remove(dev);
1138}
1139EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1140
1141/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001142 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1143 * @dev: Device to handle
1144 *
1145 * Set the power.irq_safe flag, which tells the PM core that the
1146 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1147 * always be invoked with the spinlock held and interrupts disabled. It also
1148 * causes the parent's usage counter to be permanently incremented, preventing
1149 * the parent from runtime suspending -- otherwise an irq-safe child might have
1150 * to wait for a non-irq-safe parent.
1151 */
1152void pm_runtime_irq_safe(struct device *dev)
1153{
1154 if (dev->parent)
1155 pm_runtime_get_sync(dev->parent);
1156 spin_lock_irq(&dev->power.lock);
1157 dev->power.irq_safe = 1;
1158 spin_unlock_irq(&dev->power.lock);
1159}
1160EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1161
1162/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001163 * update_autosuspend - Handle a change to a device's autosuspend settings.
1164 * @dev: Device to handle.
1165 * @old_delay: The former autosuspend_delay value.
1166 * @old_use: The former use_autosuspend value.
1167 *
1168 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1169 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1170 *
1171 * This function must be called under dev->power.lock with interrupts disabled.
1172 */
1173static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1174{
1175 int delay = dev->power.autosuspend_delay;
1176
1177 /* Should runtime suspend be prevented now? */
1178 if (dev->power.use_autosuspend && delay < 0) {
1179
1180 /* If it used to be allowed then prevent it. */
1181 if (!old_use || old_delay >= 0) {
1182 atomic_inc(&dev->power.usage_count);
1183 rpm_resume(dev, 0);
1184 }
1185 }
1186
1187 /* Runtime suspend should be allowed now. */
1188 else {
1189
1190 /* If it used to be prevented then allow it. */
1191 if (old_use && old_delay < 0)
1192 atomic_dec(&dev->power.usage_count);
1193
1194 /* Maybe we can autosuspend now. */
1195 rpm_idle(dev, RPM_AUTO);
1196 }
1197}
1198
1199/**
1200 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1201 * @dev: Device to handle.
1202 * @delay: Value of the new delay in milliseconds.
1203 *
1204 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001205 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1206 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001207 */
1208void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1209{
1210 int old_delay, old_use;
1211
1212 spin_lock_irq(&dev->power.lock);
1213 old_delay = dev->power.autosuspend_delay;
1214 old_use = dev->power.use_autosuspend;
1215 dev->power.autosuspend_delay = delay;
1216 update_autosuspend(dev, old_delay, old_use);
1217 spin_unlock_irq(&dev->power.lock);
1218}
1219EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1220
1221/**
1222 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1223 * @dev: Device to handle.
1224 * @use: New value for use_autosuspend.
1225 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001226 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001227 * suspends as needed.
1228 */
1229void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1230{
1231 int old_delay, old_use;
1232
1233 spin_lock_irq(&dev->power.lock);
1234 old_delay = dev->power.autosuspend_delay;
1235 old_use = dev->power.use_autosuspend;
1236 dev->power.use_autosuspend = use;
1237 update_autosuspend(dev, old_delay, old_use);
1238 spin_unlock_irq(&dev->power.lock);
1239}
1240EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1241
1242/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001243 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001244 * @dev: Device object to initialize.
1245 */
1246void pm_runtime_init(struct device *dev)
1247{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001248 dev->power.runtime_status = RPM_SUSPENDED;
1249 dev->power.idle_notification = false;
1250
1251 dev->power.disable_depth = 1;
1252 atomic_set(&dev->power.usage_count, 0);
1253
1254 dev->power.runtime_error = 0;
1255
1256 atomic_set(&dev->power.child_count, 0);
1257 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001258 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001259
1260 dev->power.request_pending = false;
1261 dev->power.request = RPM_REQ_NONE;
1262 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001263 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001264 INIT_WORK(&dev->power.work, pm_runtime_work);
1265
1266 dev->power.timer_expires = 0;
1267 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1268 (unsigned long)dev);
1269
1270 init_waitqueue_head(&dev->power.wait_queue);
1271}
1272
1273/**
1274 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1275 * @dev: Device object being removed from device hierarchy.
1276 */
1277void pm_runtime_remove(struct device *dev)
1278{
1279 __pm_runtime_disable(dev, false);
1280
1281 /* Change the status back to 'suspended' to match the initial status. */
1282 if (dev->power.runtime_status == RPM_ACTIVE)
1283 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001284 if (dev->power.irq_safe && dev->parent)
1285 pm_runtime_put_sync(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001286}