]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/base/power/runtime.c
Merge branch 'pm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/suspe...
[net-next-2.6.git] / drivers / base / power / runtime.c
CommitLineData
5e928f77
RW
1/*
2 * drivers/base/power/runtime.c - Helper functions for device run-time PM
3 *
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
1bfee5bc 5 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
5e928f77
RW
6 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/sched.h>
11#include <linux/pm_runtime.h>
7490e442 12#include "power.h"
5e928f77 13
140a6c94 14static int rpm_resume(struct device *dev, int rpmflags);
7490e442 15static int rpm_suspend(struct device *dev, int rpmflags);
5e928f77 16
4769373c
AS
17/**
18 * update_pm_runtime_accounting - Update the time accounting of power states
19 * @dev: Device to update the accounting for
20 *
21 * In order to be able to have time accounting of the various power states
22 * (as used by programs such as PowerTOP to show the effectiveness of runtime
23 * PM), we need to track the time spent in each state.
24 * update_pm_runtime_accounting must be called each time before the
25 * runtime_status field is updated, to account the time in the old state
26 * correctly.
27 */
28void update_pm_runtime_accounting(struct device *dev)
29{
30 unsigned long now = jiffies;
31 int delta;
32
33 delta = now - dev->power.accounting_timestamp;
34
35 if (delta < 0)
36 delta = 0;
37
38 dev->power.accounting_timestamp = now;
39
40 if (dev->power.disable_depth > 0)
41 return;
42
43 if (dev->power.runtime_status == RPM_SUSPENDED)
44 dev->power.suspended_jiffies += delta;
45 else
46 dev->power.active_jiffies += delta;
47}
48
49static void __update_runtime_status(struct device *dev, enum rpm_status status)
50{
51 update_pm_runtime_accounting(dev);
52 dev->power.runtime_status = status;
53}
54
5e928f77
RW
55/**
56 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
57 * @dev: Device to handle.
58 */
59static void pm_runtime_deactivate_timer(struct device *dev)
60{
61 if (dev->power.timer_expires > 0) {
62 del_timer(&dev->power.suspend_timer);
63 dev->power.timer_expires = 0;
64 }
65}
66
67/**
68 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
69 * @dev: Device to handle.
70 */
71static void pm_runtime_cancel_pending(struct device *dev)
72{
73 pm_runtime_deactivate_timer(dev);
74 /*
75 * In case there's a request pending, make sure its work function will
76 * return without doing anything.
77 */
78 dev->power.request = RPM_REQ_NONE;
79}
80
15bcb91d
AS
81/*
82 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
83 * @dev: Device to handle.
84 *
85 * Compute the autosuspend-delay expiration time based on the device's
86 * power.last_busy time. If the delay has already expired or is disabled
87 * (negative) or the power.use_autosuspend flag isn't set, return 0.
88 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
89 *
90 * This function may be called either with or without dev->power.lock held.
91 * Either way it can be racy, since power.last_busy may be updated at any time.
92 */
93unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
94{
95 int autosuspend_delay;
96 long elapsed;
97 unsigned long last_busy;
98 unsigned long expires = 0;
99
100 if (!dev->power.use_autosuspend)
101 goto out;
102
103 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
104 if (autosuspend_delay < 0)
105 goto out;
106
107 last_busy = ACCESS_ONCE(dev->power.last_busy);
108 elapsed = jiffies - last_busy;
109 if (elapsed < 0)
110 goto out; /* jiffies has wrapped around. */
111
112 /*
113 * If the autosuspend_delay is >= 1 second, align the timer by rounding
114 * up to the nearest second.
115 */
116 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
117 if (autosuspend_delay >= 1000)
118 expires = round_jiffies(expires);
119 expires += !expires;
120 if (elapsed >= expires - last_busy)
121 expires = 0; /* Already expired. */
122
123 out:
124 return expires;
125}
126EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
127
5e928f77 128/**
1bfee5bc
AS
129 * rpm_check_suspend_allowed - Test whether a device may be suspended.
130 * @dev: Device to test.
5e928f77 131 */
1bfee5bc 132static int rpm_check_suspend_allowed(struct device *dev)
5e928f77
RW
133{
134 int retval = 0;
135
5e928f77
RW
136 if (dev->power.runtime_error)
137 retval = -EINVAL;
5e928f77 138 else if (atomic_read(&dev->power.usage_count) > 0
1bfee5bc 139 || dev->power.disable_depth > 0)
5e928f77
RW
140 retval = -EAGAIN;
141 else if (!pm_children_suspended(dev))
142 retval = -EBUSY;
1bfee5bc
AS
143
144 /* Pending resume requests take precedence over suspends. */
145 else if ((dev->power.deferred_resume
78ca7c37 146 && dev->power.runtime_status == RPM_SUSPENDING)
1bfee5bc
AS
147 || (dev->power.request_pending
148 && dev->power.request == RPM_REQ_RESUME))
149 retval = -EAGAIN;
150 else if (dev->power.runtime_status == RPM_SUSPENDED)
151 retval = 1;
152
153 return retval;
154}
155
1bfee5bc 156/**
140a6c94 157 * rpm_idle - Notify device bus type if the device can be suspended.
1bfee5bc
AS
158 * @dev: Device to notify the bus type about.
159 * @rpmflags: Flag bits.
160 *
161 * Check if the device's run-time PM status allows it to be suspended. If
162 * another idle notification has been started earlier, return immediately. If
163 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
164 * run the ->runtime_idle() callback directly.
165 *
166 * This function must be called under dev->power.lock with interrupts disabled.
167 */
140a6c94 168static int rpm_idle(struct device *dev, int rpmflags)
1bfee5bc 169{
71c63122 170 int (*callback)(struct device *);
1bfee5bc
AS
171 int retval;
172
173 retval = rpm_check_suspend_allowed(dev);
174 if (retval < 0)
175 ; /* Conditions are wrong. */
176
177 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
178 else if (dev->power.runtime_status != RPM_ACTIVE)
179 retval = -EAGAIN;
180
181 /*
182 * Any pending request other than an idle notification takes
183 * precedence over us, except that the timer may be running.
184 */
185 else if (dev->power.request_pending &&
186 dev->power.request > RPM_REQ_IDLE)
187 retval = -EAGAIN;
188
189 /* Act as though RPM_NOWAIT is always set. */
190 else if (dev->power.idle_notification)
191 retval = -EINPROGRESS;
5e928f77
RW
192 if (retval)
193 goto out;
194
1bfee5bc
AS
195 /* Pending requests need to be canceled. */
196 dev->power.request = RPM_REQ_NONE;
197
7490e442
AS
198 if (dev->power.no_callbacks) {
199 /* Assume ->runtime_idle() callback would have suspended. */
200 retval = rpm_suspend(dev, rpmflags);
201 goto out;
202 }
203
1bfee5bc
AS
204 /* Carry out an asynchronous or a synchronous idle notification. */
205 if (rpmflags & RPM_ASYNC) {
206 dev->power.request = RPM_REQ_IDLE;
207 if (!dev->power.request_pending) {
208 dev->power.request_pending = true;
209 queue_work(pm_wq, &dev->power.work);
5e928f77 210 }
1bfee5bc 211 goto out;
5e928f77
RW
212 }
213
214 dev->power.idle_notification = true;
215
71c63122
RW
216 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_idle)
217 callback = dev->bus->pm->runtime_idle;
218 else if (dev->type && dev->type->pm && dev->type->pm->runtime_idle)
219 callback = dev->type->pm->runtime_idle;
220 else if (dev->class && dev->class->pm)
221 callback = dev->class->pm->runtime_idle;
222 else
223 callback = NULL;
a6ab7aa9 224
71c63122 225 if (callback) {
a6ab7aa9
RW
226 spin_unlock_irq(&dev->power.lock);
227
71c63122 228 callback(dev);
a6ab7aa9 229
5e928f77
RW
230 spin_lock_irq(&dev->power.lock);
231 }
232
233 dev->power.idle_notification = false;
234 wake_up_all(&dev->power.wait_queue);
235
236 out:
5e928f77
RW
237 return retval;
238}
239
71c63122
RW
240/**
241 * rpm_callback - Run a given runtime PM callback for a given device.
242 * @cb: Runtime PM callback to run.
243 * @dev: Device to run the callback for.
244 */
245static int rpm_callback(int (*cb)(struct device *), struct device *dev)
246 __releases(&dev->power.lock) __acquires(&dev->power.lock)
247{
248 int retval;
249
250 if (!cb)
251 return -ENOSYS;
252
253 spin_unlock_irq(&dev->power.lock);
254
255 retval = cb(dev);
256
257 spin_lock_irq(&dev->power.lock);
258 dev->power.runtime_error = retval;
259
260 return retval;
261}
262
5e928f77 263/**
140a6c94 264 * rpm_suspend - Carry out run-time suspend of given device.
5e928f77 265 * @dev: Device to suspend.
3f9af051 266 * @rpmflags: Flag bits.
5e928f77 267 *
1bfee5bc
AS
268 * Check if the device's run-time PM status allows it to be suspended. If
269 * another suspend has been started earlier, either return immediately or wait
270 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
271 * pending idle notification. If the RPM_ASYNC flag is set then queue a
272 * suspend request; otherwise run the ->runtime_suspend() callback directly.
273 * If a deferred resume was requested while the callback was running then carry
274 * it out; otherwise send an idle notification for the device (if the suspend
275 * failed) or for its parent (if the suspend succeeded).
5e928f77
RW
276 *
277 * This function must be called under dev->power.lock with interrupts disabled.
278 */
140a6c94 279static int rpm_suspend(struct device *dev, int rpmflags)
5e928f77
RW
280 __releases(&dev->power.lock) __acquires(&dev->power.lock)
281{
71c63122 282 int (*callback)(struct device *);
5e928f77 283 struct device *parent = NULL;
1bfee5bc 284 int retval;
5e928f77 285
3f9af051 286 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
5e928f77
RW
287
288 repeat:
1bfee5bc 289 retval = rpm_check_suspend_allowed(dev);
5e928f77 290
1bfee5bc
AS
291 if (retval < 0)
292 ; /* Conditions are wrong. */
293
294 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
295 else if (dev->power.runtime_status == RPM_RESUMING &&
296 !(rpmflags & RPM_ASYNC))
5e928f77 297 retval = -EAGAIN;
1bfee5bc 298 if (retval)
5e928f77 299 goto out;
5e928f77 300
15bcb91d
AS
301 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
302 if ((rpmflags & RPM_AUTO)
303 && dev->power.runtime_status != RPM_SUSPENDING) {
304 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
305
306 if (expires != 0) {
307 /* Pending requests need to be canceled. */
308 dev->power.request = RPM_REQ_NONE;
309
310 /*
311 * Optimization: If the timer is already running and is
312 * set to expire at or before the autosuspend delay,
313 * avoid the overhead of resetting it. Just let it
314 * expire; pm_suspend_timer_fn() will take care of the
315 * rest.
316 */
317 if (!(dev->power.timer_expires && time_before_eq(
318 dev->power.timer_expires, expires))) {
319 dev->power.timer_expires = expires;
320 mod_timer(&dev->power.suspend_timer, expires);
321 }
322 dev->power.timer_autosuspends = 1;
323 goto out;
324 }
325 }
326
5e928f77
RW
327 /* Other scheduled or pending requests need to be canceled. */
328 pm_runtime_cancel_pending(dev);
329
5e928f77
RW
330 if (dev->power.runtime_status == RPM_SUSPENDING) {
331 DEFINE_WAIT(wait);
332
1bfee5bc 333 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
5e928f77
RW
334 retval = -EINPROGRESS;
335 goto out;
336 }
337
338 /* Wait for the other suspend running in parallel with us. */
339 for (;;) {
340 prepare_to_wait(&dev->power.wait_queue, &wait,
341 TASK_UNINTERRUPTIBLE);
342 if (dev->power.runtime_status != RPM_SUSPENDING)
343 break;
344
345 spin_unlock_irq(&dev->power.lock);
346
347 schedule();
348
349 spin_lock_irq(&dev->power.lock);
350 }
351 finish_wait(&dev->power.wait_queue, &wait);
352 goto repeat;
353 }
354
7490e442
AS
355 dev->power.deferred_resume = false;
356 if (dev->power.no_callbacks)
357 goto no_callback; /* Assume success. */
358
1bfee5bc
AS
359 /* Carry out an asynchronous or a synchronous suspend. */
360 if (rpmflags & RPM_ASYNC) {
15bcb91d
AS
361 dev->power.request = (rpmflags & RPM_AUTO) ?
362 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
1bfee5bc
AS
363 if (!dev->power.request_pending) {
364 dev->power.request_pending = true;
365 queue_work(pm_wq, &dev->power.work);
366 }
367 goto out;
368 }
369
8d4b9d1b 370 __update_runtime_status(dev, RPM_SUSPENDING);
5e928f77 371
71c63122
RW
372 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_suspend)
373 callback = dev->bus->pm->runtime_suspend;
374 else if (dev->type && dev->type->pm && dev->type->pm->runtime_suspend)
375 callback = dev->type->pm->runtime_suspend;
376 else if (dev->class && dev->class->pm)
377 callback = dev->class->pm->runtime_suspend;
378 else
379 callback = NULL;
5e928f77 380
71c63122 381 retval = rpm_callback(callback, dev);
5e928f77 382 if (retval) {
8d4b9d1b 383 __update_runtime_status(dev, RPM_ACTIVE);
1bfee5bc 384 dev->power.deferred_resume = 0;
f71648d7 385 if (retval == -EAGAIN || retval == -EBUSY)
5e928f77 386 dev->power.runtime_error = 0;
f71648d7 387 else
240c7337 388 pm_runtime_cancel_pending(dev);
5e928f77 389 } else {
7490e442 390 no_callback:
8d4b9d1b 391 __update_runtime_status(dev, RPM_SUSPENDED);
240c7337 392 pm_runtime_deactivate_timer(dev);
5e928f77
RW
393
394 if (dev->parent) {
395 parent = dev->parent;
396 atomic_add_unless(&parent->power.child_count, -1, 0);
397 }
398 }
399 wake_up_all(&dev->power.wait_queue);
400
401 if (dev->power.deferred_resume) {
140a6c94 402 rpm_resume(dev, 0);
5e928f77
RW
403 retval = -EAGAIN;
404 goto out;
405 }
406
5e928f77
RW
407 if (parent && !parent->power.ignore_children) {
408 spin_unlock_irq(&dev->power.lock);
409
410 pm_request_idle(parent);
411
412 spin_lock_irq(&dev->power.lock);
413 }
414
415 out:
3f9af051 416 dev_dbg(dev, "%s returns %d\n", __func__, retval);
5e928f77
RW
417
418 return retval;
419}
420
421/**
140a6c94 422 * rpm_resume - Carry out run-time resume of given device.
5e928f77 423 * @dev: Device to resume.
3f9af051 424 * @rpmflags: Flag bits.
5e928f77 425 *
1bfee5bc
AS
426 * Check if the device's run-time PM status allows it to be resumed. Cancel
427 * any scheduled or pending requests. If another resume has been started
428 * earlier, either return imediately or wait for it to finish, depending on the
429 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
430 * parallel with this function, either tell the other process to resume after
431 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
432 * flag is set then queue a resume request; otherwise run the
433 * ->runtime_resume() callback directly. Queue an idle notification for the
434 * device if the resume succeeded.
5e928f77
RW
435 *
436 * This function must be called under dev->power.lock with interrupts disabled.
437 */
140a6c94 438static int rpm_resume(struct device *dev, int rpmflags)
5e928f77
RW
439 __releases(&dev->power.lock) __acquires(&dev->power.lock)
440{
71c63122 441 int (*callback)(struct device *);
5e928f77
RW
442 struct device *parent = NULL;
443 int retval = 0;
444
3f9af051 445 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
5e928f77
RW
446
447 repeat:
1bfee5bc 448 if (dev->power.runtime_error)
5e928f77 449 retval = -EINVAL;
1bfee5bc
AS
450 else if (dev->power.disable_depth > 0)
451 retval = -EAGAIN;
452 if (retval)
5e928f77 453 goto out;
5e928f77 454
15bcb91d
AS
455 /*
456 * Other scheduled or pending requests need to be canceled. Small
457 * optimization: If an autosuspend timer is running, leave it running
458 * rather than cancelling it now only to restart it again in the near
459 * future.
460 */
461 dev->power.request = RPM_REQ_NONE;
462 if (!dev->power.timer_autosuspends)
463 pm_runtime_deactivate_timer(dev);
5e928f77 464
1bfee5bc 465 if (dev->power.runtime_status == RPM_ACTIVE) {
5e928f77 466 retval = 1;
5e928f77 467 goto out;
1bfee5bc 468 }
5e928f77
RW
469
470 if (dev->power.runtime_status == RPM_RESUMING
471 || dev->power.runtime_status == RPM_SUSPENDING) {
472 DEFINE_WAIT(wait);
473
1bfee5bc 474 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
5e928f77
RW
475 if (dev->power.runtime_status == RPM_SUSPENDING)
476 dev->power.deferred_resume = true;
1bfee5bc
AS
477 else
478 retval = -EINPROGRESS;
5e928f77
RW
479 goto out;
480 }
481
482 /* Wait for the operation carried out in parallel with us. */
483 for (;;) {
484 prepare_to_wait(&dev->power.wait_queue, &wait,
485 TASK_UNINTERRUPTIBLE);
486 if (dev->power.runtime_status != RPM_RESUMING
487 && dev->power.runtime_status != RPM_SUSPENDING)
488 break;
489
490 spin_unlock_irq(&dev->power.lock);
491
492 schedule();
493
494 spin_lock_irq(&dev->power.lock);
495 }
496 finish_wait(&dev->power.wait_queue, &wait);
497 goto repeat;
498 }
499
7490e442
AS
500 /*
501 * See if we can skip waking up the parent. This is safe only if
502 * power.no_callbacks is set, because otherwise we don't know whether
503 * the resume will actually succeed.
504 */
505 if (dev->power.no_callbacks && !parent && dev->parent) {
d63be5f9 506 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
7490e442
AS
507 if (dev->parent->power.disable_depth > 0
508 || dev->parent->power.ignore_children
509 || dev->parent->power.runtime_status == RPM_ACTIVE) {
510 atomic_inc(&dev->parent->power.child_count);
511 spin_unlock(&dev->parent->power.lock);
512 goto no_callback; /* Assume success. */
513 }
514 spin_unlock(&dev->parent->power.lock);
515 }
516
1bfee5bc
AS
517 /* Carry out an asynchronous or a synchronous resume. */
518 if (rpmflags & RPM_ASYNC) {
519 dev->power.request = RPM_REQ_RESUME;
520 if (!dev->power.request_pending) {
521 dev->power.request_pending = true;
522 queue_work(pm_wq, &dev->power.work);
523 }
524 retval = 0;
525 goto out;
526 }
527
5e928f77
RW
528 if (!parent && dev->parent) {
529 /*
530 * Increment the parent's resume counter and resume it if
531 * necessary.
532 */
533 parent = dev->parent;
862f89b3 534 spin_unlock(&dev->power.lock);
5e928f77
RW
535
536 pm_runtime_get_noresume(parent);
537
862f89b3 538 spin_lock(&parent->power.lock);
5e928f77
RW
539 /*
540 * We can resume if the parent's run-time PM is disabled or it
541 * is set to ignore children.
542 */
543 if (!parent->power.disable_depth
544 && !parent->power.ignore_children) {
140a6c94 545 rpm_resume(parent, 0);
5e928f77
RW
546 if (parent->power.runtime_status != RPM_ACTIVE)
547 retval = -EBUSY;
548 }
862f89b3 549 spin_unlock(&parent->power.lock);
5e928f77 550
862f89b3 551 spin_lock(&dev->power.lock);
5e928f77
RW
552 if (retval)
553 goto out;
554 goto repeat;
555 }
556
7490e442
AS
557 if (dev->power.no_callbacks)
558 goto no_callback; /* Assume success. */
559
8d4b9d1b 560 __update_runtime_status(dev, RPM_RESUMING);
5e928f77 561
71c63122
RW
562 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_resume)
563 callback = dev->bus->pm->runtime_resume;
564 else if (dev->type && dev->type->pm && dev->type->pm->runtime_resume)
565 callback = dev->type->pm->runtime_resume;
566 else if (dev->class && dev->class->pm)
567 callback = dev->class->pm->runtime_resume;
568 else
569 callback = NULL;
5e928f77 570
71c63122 571 retval = rpm_callback(callback, dev);
5e928f77 572 if (retval) {
8d4b9d1b 573 __update_runtime_status(dev, RPM_SUSPENDED);
5e928f77
RW
574 pm_runtime_cancel_pending(dev);
575 } else {
7490e442 576 no_callback:
8d4b9d1b 577 __update_runtime_status(dev, RPM_ACTIVE);
5e928f77
RW
578 if (parent)
579 atomic_inc(&parent->power.child_count);
580 }
581 wake_up_all(&dev->power.wait_queue);
582
583 if (!retval)
140a6c94 584 rpm_idle(dev, RPM_ASYNC);
5e928f77
RW
585
586 out:
587 if (parent) {
588 spin_unlock_irq(&dev->power.lock);
589
590 pm_runtime_put(parent);
591
592 spin_lock_irq(&dev->power.lock);
593 }
594
3f9af051 595 dev_dbg(dev, "%s returns %d\n", __func__, retval);
5e928f77
RW
596
597 return retval;
598}
599
5e928f77
RW
600/**
601 * pm_runtime_work - Universal run-time PM work function.
602 * @work: Work structure used for scheduling the execution of this function.
603 *
604 * Use @work to get the device object the work is to be done for, determine what
605 * is to be done and execute the appropriate run-time PM function.
606 */
607static void pm_runtime_work(struct work_struct *work)
608{
609 struct device *dev = container_of(work, struct device, power.work);
610 enum rpm_request req;
611
612 spin_lock_irq(&dev->power.lock);
613
614 if (!dev->power.request_pending)
615 goto out;
616
617 req = dev->power.request;
618 dev->power.request = RPM_REQ_NONE;
619 dev->power.request_pending = false;
620
621 switch (req) {
622 case RPM_REQ_NONE:
623 break;
624 case RPM_REQ_IDLE:
140a6c94 625 rpm_idle(dev, RPM_NOWAIT);
5e928f77
RW
626 break;
627 case RPM_REQ_SUSPEND:
140a6c94 628 rpm_suspend(dev, RPM_NOWAIT);
5e928f77 629 break;
15bcb91d
AS
630 case RPM_REQ_AUTOSUSPEND:
631 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
632 break;
5e928f77 633 case RPM_REQ_RESUME:
140a6c94 634 rpm_resume(dev, RPM_NOWAIT);
5e928f77
RW
635 break;
636 }
637
638 out:
639 spin_unlock_irq(&dev->power.lock);
640}
641
5e928f77
RW
642/**
643 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
644 * @data: Device pointer passed by pm_schedule_suspend().
645 *
1bfee5bc 646 * Check if the time is right and queue a suspend request.
5e928f77
RW
647 */
648static void pm_suspend_timer_fn(unsigned long data)
649{
650 struct device *dev = (struct device *)data;
651 unsigned long flags;
652 unsigned long expires;
653
654 spin_lock_irqsave(&dev->power.lock, flags);
655
656 expires = dev->power.timer_expires;
657 /* If 'expire' is after 'jiffies' we've been called too early. */
658 if (expires > 0 && !time_after(expires, jiffies)) {
659 dev->power.timer_expires = 0;
15bcb91d
AS
660 rpm_suspend(dev, dev->power.timer_autosuspends ?
661 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
5e928f77
RW
662 }
663
664 spin_unlock_irqrestore(&dev->power.lock, flags);
665}
666
667/**
668 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
669 * @dev: Device to suspend.
670 * @delay: Time to wait before submitting a suspend request, in milliseconds.
671 */
672int pm_schedule_suspend(struct device *dev, unsigned int delay)
673{
674 unsigned long flags;
1bfee5bc 675 int retval;
5e928f77
RW
676
677 spin_lock_irqsave(&dev->power.lock, flags);
678
5e928f77 679 if (!delay) {
140a6c94 680 retval = rpm_suspend(dev, RPM_ASYNC);
5e928f77
RW
681 goto out;
682 }
683
1bfee5bc 684 retval = rpm_check_suspend_allowed(dev);
5e928f77
RW
685 if (retval)
686 goto out;
687
1bfee5bc
AS
688 /* Other scheduled or pending requests need to be canceled. */
689 pm_runtime_cancel_pending(dev);
690
5e928f77 691 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
1bfee5bc 692 dev->power.timer_expires += !dev->power.timer_expires;
15bcb91d 693 dev->power.timer_autosuspends = 0;
5e928f77
RW
694 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
695
696 out:
697 spin_unlock_irqrestore(&dev->power.lock, flags);
698
699 return retval;
700}
701EXPORT_SYMBOL_GPL(pm_schedule_suspend);
702
5e928f77 703/**
140a6c94
AS
704 * __pm_runtime_idle - Entry point for run-time idle operations.
705 * @dev: Device to send idle notification for.
706 * @rpmflags: Flag bits.
707 *
708 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
709 * return immediately if it is larger than zero. Then carry out an idle
710 * notification, either synchronous or asynchronous.
711 *
712 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
5e928f77 713 */
140a6c94 714int __pm_runtime_idle(struct device *dev, int rpmflags)
5e928f77
RW
715{
716 unsigned long flags;
717 int retval;
718
140a6c94
AS
719 if (rpmflags & RPM_GET_PUT) {
720 if (!atomic_dec_and_test(&dev->power.usage_count))
721 return 0;
722 }
723
5e928f77 724 spin_lock_irqsave(&dev->power.lock, flags);
140a6c94 725 retval = rpm_idle(dev, rpmflags);
5e928f77
RW
726 spin_unlock_irqrestore(&dev->power.lock, flags);
727
728 return retval;
729}
140a6c94 730EXPORT_SYMBOL_GPL(__pm_runtime_idle);
5e928f77
RW
731
732/**
140a6c94
AS
733 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
734 * @dev: Device to suspend.
3f9af051 735 * @rpmflags: Flag bits.
5e928f77 736 *
15bcb91d
AS
737 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
738 * return immediately if it is larger than zero. Then carry out a suspend,
739 * either synchronous or asynchronous.
140a6c94
AS
740 *
741 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
5e928f77 742 */
140a6c94 743int __pm_runtime_suspend(struct device *dev, int rpmflags)
5e928f77 744{
140a6c94 745 unsigned long flags;
1d531c14 746 int retval;
5e928f77 747
15bcb91d
AS
748 if (rpmflags & RPM_GET_PUT) {
749 if (!atomic_dec_and_test(&dev->power.usage_count))
750 return 0;
751 }
752
140a6c94
AS
753 spin_lock_irqsave(&dev->power.lock, flags);
754 retval = rpm_suspend(dev, rpmflags);
755 spin_unlock_irqrestore(&dev->power.lock, flags);
5e928f77
RW
756
757 return retval;
758}
140a6c94 759EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
5e928f77
RW
760
761/**
140a6c94
AS
762 * __pm_runtime_resume - Entry point for run-time resume operations.
763 * @dev: Device to resume.
3f9af051 764 * @rpmflags: Flag bits.
5e928f77 765 *
140a6c94
AS
766 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
767 * carry out a resume, either synchronous or asynchronous.
768 *
769 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
5e928f77 770 */
140a6c94 771int __pm_runtime_resume(struct device *dev, int rpmflags)
5e928f77 772{
140a6c94
AS
773 unsigned long flags;
774 int retval;
5e928f77 775
140a6c94
AS
776 if (rpmflags & RPM_GET_PUT)
777 atomic_inc(&dev->power.usage_count);
778
779 spin_lock_irqsave(&dev->power.lock, flags);
780 retval = rpm_resume(dev, rpmflags);
781 spin_unlock_irqrestore(&dev->power.lock, flags);
5e928f77
RW
782
783 return retval;
784}
140a6c94 785EXPORT_SYMBOL_GPL(__pm_runtime_resume);
5e928f77
RW
786
787/**
788 * __pm_runtime_set_status - Set run-time PM status of a device.
789 * @dev: Device to handle.
790 * @status: New run-time PM status of the device.
791 *
792 * If run-time PM of the device is disabled or its power.runtime_error field is
793 * different from zero, the status may be changed either to RPM_ACTIVE, or to
794 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
795 * However, if the device has a parent and the parent is not active, and the
796 * parent's power.ignore_children flag is unset, the device's status cannot be
797 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
798 *
799 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
800 * and the device parent's counter of unsuspended children is modified to
801 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
802 * notification request for the parent is submitted.
803 */
804int __pm_runtime_set_status(struct device *dev, unsigned int status)
805{
806 struct device *parent = dev->parent;
807 unsigned long flags;
808 bool notify_parent = false;
809 int error = 0;
810
811 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
812 return -EINVAL;
813
814 spin_lock_irqsave(&dev->power.lock, flags);
815
816 if (!dev->power.runtime_error && !dev->power.disable_depth) {
817 error = -EAGAIN;
818 goto out;
819 }
820
821 if (dev->power.runtime_status == status)
822 goto out_set;
823
824 if (status == RPM_SUSPENDED) {
825 /* It always is possible to set the status to 'suspended'. */
826 if (parent) {
827 atomic_add_unless(&parent->power.child_count, -1, 0);
828 notify_parent = !parent->power.ignore_children;
829 }
830 goto out_set;
831 }
832
833 if (parent) {
bab636b9 834 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
5e928f77
RW
835
836 /*
837 * It is invalid to put an active child under a parent that is
838 * not active, has run-time PM enabled and the
839 * 'power.ignore_children' flag unset.
840 */
841 if (!parent->power.disable_depth
842 && !parent->power.ignore_children
965c4ac0 843 && parent->power.runtime_status != RPM_ACTIVE)
5e928f77 844 error = -EBUSY;
965c4ac0
RW
845 else if (dev->power.runtime_status == RPM_SUSPENDED)
846 atomic_inc(&parent->power.child_count);
5e928f77 847
862f89b3 848 spin_unlock(&parent->power.lock);
5e928f77
RW
849
850 if (error)
851 goto out;
852 }
853
854 out_set:
8d4b9d1b 855 __update_runtime_status(dev, status);
5e928f77
RW
856 dev->power.runtime_error = 0;
857 out:
858 spin_unlock_irqrestore(&dev->power.lock, flags);
859
860 if (notify_parent)
861 pm_request_idle(parent);
862
863 return error;
864}
865EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
866
867/**
868 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
869 * @dev: Device to handle.
870 *
871 * Flush all pending requests for the device from pm_wq and wait for all
872 * run-time PM operations involving the device in progress to complete.
873 *
874 * Should be called under dev->power.lock with interrupts disabled.
875 */
876static void __pm_runtime_barrier(struct device *dev)
877{
878 pm_runtime_deactivate_timer(dev);
879
880 if (dev->power.request_pending) {
881 dev->power.request = RPM_REQ_NONE;
882 spin_unlock_irq(&dev->power.lock);
883
884 cancel_work_sync(&dev->power.work);
885
886 spin_lock_irq(&dev->power.lock);
887 dev->power.request_pending = false;
888 }
889
890 if (dev->power.runtime_status == RPM_SUSPENDING
891 || dev->power.runtime_status == RPM_RESUMING
892 || dev->power.idle_notification) {
893 DEFINE_WAIT(wait);
894
895 /* Suspend, wake-up or idle notification in progress. */
896 for (;;) {
897 prepare_to_wait(&dev->power.wait_queue, &wait,
898 TASK_UNINTERRUPTIBLE);
899 if (dev->power.runtime_status != RPM_SUSPENDING
900 && dev->power.runtime_status != RPM_RESUMING
901 && !dev->power.idle_notification)
902 break;
903 spin_unlock_irq(&dev->power.lock);
904
905 schedule();
906
907 spin_lock_irq(&dev->power.lock);
908 }
909 finish_wait(&dev->power.wait_queue, &wait);
910 }
911}
912
913/**
914 * pm_runtime_barrier - Flush pending requests and wait for completions.
915 * @dev: Device to handle.
916 *
917 * Prevent the device from being suspended by incrementing its usage counter and
918 * if there's a pending resume request for the device, wake the device up.
919 * Next, make sure that all pending requests for the device have been flushed
920 * from pm_wq and wait for all run-time PM operations involving the device in
921 * progress to complete.
922 *
923 * Return value:
924 * 1, if there was a resume request pending and the device had to be woken up,
925 * 0, otherwise
926 */
927int pm_runtime_barrier(struct device *dev)
928{
929 int retval = 0;
930
931 pm_runtime_get_noresume(dev);
932 spin_lock_irq(&dev->power.lock);
933
934 if (dev->power.request_pending
935 && dev->power.request == RPM_REQ_RESUME) {
140a6c94 936 rpm_resume(dev, 0);
5e928f77
RW
937 retval = 1;
938 }
939
940 __pm_runtime_barrier(dev);
941
942 spin_unlock_irq(&dev->power.lock);
943 pm_runtime_put_noidle(dev);
944
945 return retval;
946}
947EXPORT_SYMBOL_GPL(pm_runtime_barrier);
948
949/**
950 * __pm_runtime_disable - Disable run-time PM of a device.
951 * @dev: Device to handle.
952 * @check_resume: If set, check if there's a resume request for the device.
953 *
954 * Increment power.disable_depth for the device and if was zero previously,
955 * cancel all pending run-time PM requests for the device and wait for all
956 * operations in progress to complete. The device can be either active or
957 * suspended after its run-time PM has been disabled.
958 *
959 * If @check_resume is set and there's a resume request pending when
960 * __pm_runtime_disable() is called and power.disable_depth is zero, the
961 * function will wake up the device before disabling its run-time PM.
962 */
963void __pm_runtime_disable(struct device *dev, bool check_resume)
964{
965 spin_lock_irq(&dev->power.lock);
966
967 if (dev->power.disable_depth > 0) {
968 dev->power.disable_depth++;
969 goto out;
970 }
971
972 /*
973 * Wake up the device if there's a resume request pending, because that
974 * means there probably is some I/O to process and disabling run-time PM
975 * shouldn't prevent the device from processing the I/O.
976 */
977 if (check_resume && dev->power.request_pending
978 && dev->power.request == RPM_REQ_RESUME) {
979 /*
980 * Prevent suspends and idle notifications from being carried
981 * out after we have woken up the device.
982 */
983 pm_runtime_get_noresume(dev);
984
140a6c94 985 rpm_resume(dev, 0);
5e928f77
RW
986
987 pm_runtime_put_noidle(dev);
988 }
989
990 if (!dev->power.disable_depth++)
991 __pm_runtime_barrier(dev);
992
993 out:
994 spin_unlock_irq(&dev->power.lock);
995}
996EXPORT_SYMBOL_GPL(__pm_runtime_disable);
997
998/**
999 * pm_runtime_enable - Enable run-time PM of a device.
1000 * @dev: Device to handle.
1001 */
1002void pm_runtime_enable(struct device *dev)
1003{
1004 unsigned long flags;
1005
1006 spin_lock_irqsave(&dev->power.lock, flags);
1007
1008 if (dev->power.disable_depth > 0)
1009 dev->power.disable_depth--;
1010 else
1011 dev_warn(dev, "Unbalanced %s!\n", __func__);
1012
1013 spin_unlock_irqrestore(&dev->power.lock, flags);
1014}
1015EXPORT_SYMBOL_GPL(pm_runtime_enable);
1016
53823639
RW
1017/**
1018 * pm_runtime_forbid - Block run-time PM of a device.
1019 * @dev: Device to handle.
1020 *
1021 * Increase the device's usage count and clear its power.runtime_auto flag,
1022 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1023 * for it.
1024 */
1025void pm_runtime_forbid(struct device *dev)
1026{
1027 spin_lock_irq(&dev->power.lock);
1028 if (!dev->power.runtime_auto)
1029 goto out;
1030
1031 dev->power.runtime_auto = false;
1032 atomic_inc(&dev->power.usage_count);
140a6c94 1033 rpm_resume(dev, 0);
53823639
RW
1034
1035 out:
1036 spin_unlock_irq(&dev->power.lock);
1037}
1038EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1039
1040/**
1041 * pm_runtime_allow - Unblock run-time PM of a device.
1042 * @dev: Device to handle.
1043 *
1044 * Decrease the device's usage count and set its power.runtime_auto flag.
1045 */
1046void pm_runtime_allow(struct device *dev)
1047{
1048 spin_lock_irq(&dev->power.lock);
1049 if (dev->power.runtime_auto)
1050 goto out;
1051
1052 dev->power.runtime_auto = true;
1053 if (atomic_dec_and_test(&dev->power.usage_count))
15bcb91d 1054 rpm_idle(dev, RPM_AUTO);
53823639
RW
1055
1056 out:
1057 spin_unlock_irq(&dev->power.lock);
1058}
1059EXPORT_SYMBOL_GPL(pm_runtime_allow);
1060
7490e442
AS
1061/**
1062 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1063 * @dev: Device to handle.
1064 *
1065 * Set the power.no_callbacks flag, which tells the PM core that this
1066 * device is power-managed through its parent and has no run-time PM
1067 * callbacks of its own. The run-time sysfs attributes will be removed.
1068 *
1069 */
1070void pm_runtime_no_callbacks(struct device *dev)
1071{
1072 spin_lock_irq(&dev->power.lock);
1073 dev->power.no_callbacks = 1;
1074 spin_unlock_irq(&dev->power.lock);
1075 if (device_is_registered(dev))
1076 rpm_sysfs_remove(dev);
1077}
1078EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1079
15bcb91d
AS
1080/**
1081 * update_autosuspend - Handle a change to a device's autosuspend settings.
1082 * @dev: Device to handle.
1083 * @old_delay: The former autosuspend_delay value.
1084 * @old_use: The former use_autosuspend value.
1085 *
1086 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1087 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1088 *
1089 * This function must be called under dev->power.lock with interrupts disabled.
1090 */
1091static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1092{
1093 int delay = dev->power.autosuspend_delay;
1094
1095 /* Should runtime suspend be prevented now? */
1096 if (dev->power.use_autosuspend && delay < 0) {
1097
1098 /* If it used to be allowed then prevent it. */
1099 if (!old_use || old_delay >= 0) {
1100 atomic_inc(&dev->power.usage_count);
1101 rpm_resume(dev, 0);
1102 }
1103 }
1104
1105 /* Runtime suspend should be allowed now. */
1106 else {
1107
1108 /* If it used to be prevented then allow it. */
1109 if (old_use && old_delay < 0)
1110 atomic_dec(&dev->power.usage_count);
1111
1112 /* Maybe we can autosuspend now. */
1113 rpm_idle(dev, RPM_AUTO);
1114 }
1115}
1116
1117/**
1118 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1119 * @dev: Device to handle.
1120 * @delay: Value of the new delay in milliseconds.
1121 *
1122 * Set the device's power.autosuspend_delay value. If it changes to negative
1123 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1124 * changes the other way, allow run-time suspends.
1125 */
1126void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1127{
1128 int old_delay, old_use;
1129
1130 spin_lock_irq(&dev->power.lock);
1131 old_delay = dev->power.autosuspend_delay;
1132 old_use = dev->power.use_autosuspend;
1133 dev->power.autosuspend_delay = delay;
1134 update_autosuspend(dev, old_delay, old_use);
1135 spin_unlock_irq(&dev->power.lock);
1136}
1137EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1138
1139/**
1140 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1141 * @dev: Device to handle.
1142 * @use: New value for use_autosuspend.
1143 *
1144 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1145 * suspends as needed.
1146 */
1147void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1148{
1149 int old_delay, old_use;
1150
1151 spin_lock_irq(&dev->power.lock);
1152 old_delay = dev->power.autosuspend_delay;
1153 old_use = dev->power.use_autosuspend;
1154 dev->power.use_autosuspend = use;
1155 update_autosuspend(dev, old_delay, old_use);
1156 spin_unlock_irq(&dev->power.lock);
1157}
1158EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1159
5e928f77
RW
1160/**
1161 * pm_runtime_init - Initialize run-time PM fields in given device object.
1162 * @dev: Device object to initialize.
1163 */
1164void pm_runtime_init(struct device *dev)
1165{
5e928f77
RW
1166 dev->power.runtime_status = RPM_SUSPENDED;
1167 dev->power.idle_notification = false;
1168
1169 dev->power.disable_depth = 1;
1170 atomic_set(&dev->power.usage_count, 0);
1171
1172 dev->power.runtime_error = 0;
1173
1174 atomic_set(&dev->power.child_count, 0);
1175 pm_suspend_ignore_children(dev, false);
53823639 1176 dev->power.runtime_auto = true;
5e928f77
RW
1177
1178 dev->power.request_pending = false;
1179 dev->power.request = RPM_REQ_NONE;
1180 dev->power.deferred_resume = false;
8d4b9d1b 1181 dev->power.accounting_timestamp = jiffies;
5e928f77
RW
1182 INIT_WORK(&dev->power.work, pm_runtime_work);
1183
1184 dev->power.timer_expires = 0;
1185 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1186 (unsigned long)dev);
1187
1188 init_waitqueue_head(&dev->power.wait_queue);
1189}
1190
1191/**
1192 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1193 * @dev: Device object being removed from device hierarchy.
1194 */
1195void pm_runtime_remove(struct device *dev)
1196{
1197 __pm_runtime_disable(dev, false);
1198
1199 /* Change the status back to 'suspended' to match the initial status. */
1200 if (dev->power.runtime_status == RPM_ACTIVE)
1201 pm_runtime_set_suspended(dev);
1202}