Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / base / power / runtime.c
blob2c590fa3565692ed2193b7ddee5aff4791dd98a9
1 /*
2 * drivers/base/power/runtime.c - Helper functions for device runtime PM
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
7 * This file is released under the GPLv2.
8 */
10 #include <linux/sched.h>
11 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include "power.h"
15 static int rpm_resume(struct device *dev, int rpmflags);
16 static int rpm_suspend(struct device *dev, int rpmflags);
18 /**
19 * update_pm_runtime_accounting - Update the time accounting of power states
20 * @dev: Device to update the accounting for
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.
29 void update_pm_runtime_accounting(struct device *dev)
31 unsigned long now = jiffies;
32 int delta;
34 delta = now - dev->power.accounting_timestamp;
36 if (delta < 0)
37 delta = 0;
39 dev->power.accounting_timestamp = now;
41 if (dev->power.disable_depth > 0)
42 return;
44 if (dev->power.runtime_status == RPM_SUSPENDED)
45 dev->power.suspended_jiffies += delta;
46 else
47 dev->power.active_jiffies += delta;
50 static void __update_runtime_status(struct device *dev, enum rpm_status status)
52 update_pm_runtime_accounting(dev);
53 dev->power.runtime_status = status;
56 /**
57 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
58 * @dev: Device to handle.
60 static void pm_runtime_deactivate_timer(struct device *dev)
62 if (dev->power.timer_expires > 0) {
63 del_timer(&dev->power.suspend_timer);
64 dev->power.timer_expires = 0;
68 /**
69 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
70 * @dev: Device to handle.
72 static void pm_runtime_cancel_pending(struct device *dev)
74 pm_runtime_deactivate_timer(dev);
76 * In case there's a request pending, make sure its work function will
77 * return without doing anything.
79 dev->power.request = RPM_REQ_NONE;
83 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
84 * @dev: Device to handle.
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).
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.
94 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
96 int autosuspend_delay;
97 long elapsed;
98 unsigned long last_busy;
99 unsigned long expires = 0;
101 if (!dev->power.use_autosuspend)
102 goto out;
104 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
105 if (autosuspend_delay < 0)
106 goto out;
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. */
114 * If the autosuspend_delay is >= 1 second, align the timer by rounding
115 * up to the nearest second.
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. */
124 out:
125 return expires;
127 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
130 * rpm_check_suspend_allowed - Test whether a device may be suspended.
131 * @dev: Device to test.
133 static int rpm_check_suspend_allowed(struct device *dev)
135 int retval = 0;
137 if (dev->power.runtime_error)
138 retval = -EINVAL;
139 else if (dev->power.disable_depth > 0)
140 retval = -EACCES;
141 else if (atomic_read(&dev->power.usage_count) > 0)
142 retval = -EAGAIN;
143 else if (!pm_children_suspended(dev))
144 retval = -EBUSY;
146 /* Pending resume requests take precedence over suspends. */
147 else if ((dev->power.deferred_resume
148 && dev->power.runtime_status == RPM_SUSPENDING)
149 || (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;
155 return retval;
159 * rpm_idle - Notify device bus type if the device can be suspended.
160 * @dev: Device to notify the bus type about.
161 * @rpmflags: Flag bits.
163 * Check if the device's runtime PM status allows it to be suspended. If
164 * another idle notification has been started earlier, return immediately. If
165 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
166 * run the ->runtime_idle() callback directly.
168 * This function must be called under dev->power.lock with interrupts disabled.
170 static int rpm_idle(struct device *dev, int rpmflags)
172 int (*callback)(struct device *);
173 int retval;
175 retval = rpm_check_suspend_allowed(dev);
176 if (retval < 0)
177 ; /* Conditions are wrong. */
179 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
180 else if (dev->power.runtime_status != RPM_ACTIVE)
181 retval = -EAGAIN;
184 * Any pending request other than an idle notification takes
185 * precedence over us, except that the timer may be running.
187 else if (dev->power.request_pending &&
188 dev->power.request > RPM_REQ_IDLE)
189 retval = -EAGAIN;
191 /* Act as though RPM_NOWAIT is always set. */
192 else if (dev->power.idle_notification)
193 retval = -EINPROGRESS;
194 if (retval)
195 goto out;
197 /* Pending requests need to be canceled. */
198 dev->power.request = RPM_REQ_NONE;
200 if (dev->power.no_callbacks) {
201 /* Assume ->runtime_idle() callback would have suspended. */
202 retval = rpm_suspend(dev, rpmflags);
203 goto out;
206 /* Carry out an asynchronous or a synchronous idle notification. */
207 if (rpmflags & RPM_ASYNC) {
208 dev->power.request = RPM_REQ_IDLE;
209 if (!dev->power.request_pending) {
210 dev->power.request_pending = true;
211 queue_work(pm_wq, &dev->power.work);
213 goto out;
216 dev->power.idle_notification = true;
218 if (dev->pm_domain)
219 callback = dev->pm_domain->ops.runtime_idle;
220 else if (dev->type && dev->type->pm)
221 callback = dev->type->pm->runtime_idle;
222 else if (dev->class && dev->class->pm)
223 callback = dev->class->pm->runtime_idle;
224 else if (dev->bus && dev->bus->pm)
225 callback = dev->bus->pm->runtime_idle;
226 else
227 callback = NULL;
229 if (callback) {
230 if (dev->power.irq_safe)
231 spin_unlock(&dev->power.lock);
232 else
233 spin_unlock_irq(&dev->power.lock);
235 callback(dev);
237 if (dev->power.irq_safe)
238 spin_lock(&dev->power.lock);
239 else
240 spin_lock_irq(&dev->power.lock);
243 dev->power.idle_notification = false;
244 wake_up_all(&dev->power.wait_queue);
246 out:
247 return retval;
251 * rpm_callback - Run a given runtime PM callback for a given device.
252 * @cb: Runtime PM callback to run.
253 * @dev: Device to run the callback for.
255 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
256 __releases(&dev->power.lock) __acquires(&dev->power.lock)
258 int retval;
260 if (!cb)
261 return -ENOSYS;
263 if (dev->power.irq_safe) {
264 retval = cb(dev);
265 } else {
266 spin_unlock_irq(&dev->power.lock);
268 retval = cb(dev);
270 spin_lock_irq(&dev->power.lock);
272 dev->power.runtime_error = retval;
273 return retval != -EACCES ? retval : -EIO;
277 * rpm_suspend - Carry out runtime suspend of given device.
278 * @dev: Device to suspend.
279 * @rpmflags: Flag bits.
281 * Check if the device's runtime PM status allows it to be suspended. If
282 * another suspend has been started earlier, either return immediately or wait
283 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
284 * pending idle notification. If the RPM_ASYNC flag is set then queue a
285 * suspend request; otherwise run the ->runtime_suspend() callback directly.
286 * If a deferred resume was requested while the callback was running then carry
287 * it out; otherwise send an idle notification for the device (if the suspend
288 * failed) or for its parent (if the suspend succeeded).
290 * This function must be called under dev->power.lock with interrupts disabled.
292 static int rpm_suspend(struct device *dev, int rpmflags)
293 __releases(&dev->power.lock) __acquires(&dev->power.lock)
295 int (*callback)(struct device *);
296 struct device *parent = NULL;
297 int retval;
299 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
301 repeat:
302 retval = rpm_check_suspend_allowed(dev);
304 if (retval < 0)
305 ; /* Conditions are wrong. */
307 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
308 else if (dev->power.runtime_status == RPM_RESUMING &&
309 !(rpmflags & RPM_ASYNC))
310 retval = -EAGAIN;
311 if (retval)
312 goto out;
314 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
315 if ((rpmflags & RPM_AUTO)
316 && dev->power.runtime_status != RPM_SUSPENDING) {
317 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
319 if (expires != 0) {
320 /* Pending requests need to be canceled. */
321 dev->power.request = RPM_REQ_NONE;
324 * Optimization: If the timer is already running and is
325 * set to expire at or before the autosuspend delay,
326 * avoid the overhead of resetting it. Just let it
327 * expire; pm_suspend_timer_fn() will take care of the
328 * rest.
330 if (!(dev->power.timer_expires && time_before_eq(
331 dev->power.timer_expires, expires))) {
332 dev->power.timer_expires = expires;
333 mod_timer(&dev->power.suspend_timer, expires);
335 dev->power.timer_autosuspends = 1;
336 goto out;
340 /* Other scheduled or pending requests need to be canceled. */
341 pm_runtime_cancel_pending(dev);
343 if (dev->power.runtime_status == RPM_SUSPENDING) {
344 DEFINE_WAIT(wait);
346 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
347 retval = -EINPROGRESS;
348 goto out;
351 /* Wait for the other suspend running in parallel with us. */
352 for (;;) {
353 prepare_to_wait(&dev->power.wait_queue, &wait,
354 TASK_UNINTERRUPTIBLE);
355 if (dev->power.runtime_status != RPM_SUSPENDING)
356 break;
358 spin_unlock_irq(&dev->power.lock);
360 schedule();
362 spin_lock_irq(&dev->power.lock);
364 finish_wait(&dev->power.wait_queue, &wait);
365 goto repeat;
368 dev->power.deferred_resume = false;
369 if (dev->power.no_callbacks)
370 goto no_callback; /* Assume success. */
372 /* Carry out an asynchronous or a synchronous suspend. */
373 if (rpmflags & RPM_ASYNC) {
374 dev->power.request = (rpmflags & RPM_AUTO) ?
375 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
376 if (!dev->power.request_pending) {
377 dev->power.request_pending = true;
378 queue_work(pm_wq, &dev->power.work);
380 goto out;
383 __update_runtime_status(dev, RPM_SUSPENDING);
385 if (dev->pm_domain)
386 callback = dev->pm_domain->ops.runtime_suspend;
387 else if (dev->type && dev->type->pm)
388 callback = dev->type->pm->runtime_suspend;
389 else if (dev->class && dev->class->pm)
390 callback = dev->class->pm->runtime_suspend;
391 else if (dev->bus && dev->bus->pm)
392 callback = dev->bus->pm->runtime_suspend;
393 else
394 callback = NULL;
396 retval = rpm_callback(callback, dev);
397 if (retval) {
398 __update_runtime_status(dev, RPM_ACTIVE);
399 dev->power.deferred_resume = false;
400 if (retval == -EAGAIN || retval == -EBUSY)
401 dev->power.runtime_error = 0;
402 else
403 pm_runtime_cancel_pending(dev);
404 } else {
405 no_callback:
406 __update_runtime_status(dev, RPM_SUSPENDED);
407 pm_runtime_deactivate_timer(dev);
409 if (dev->parent) {
410 parent = dev->parent;
411 atomic_add_unless(&parent->power.child_count, -1, 0);
414 wake_up_all(&dev->power.wait_queue);
416 if (dev->power.deferred_resume) {
417 rpm_resume(dev, 0);
418 retval = -EAGAIN;
419 goto out;
422 /* Maybe the parent is now able to suspend. */
423 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
424 spin_unlock(&dev->power.lock);
426 spin_lock(&parent->power.lock);
427 rpm_idle(parent, RPM_ASYNC);
428 spin_unlock(&parent->power.lock);
430 spin_lock(&dev->power.lock);
433 out:
434 dev_dbg(dev, "%s returns %d\n", __func__, retval);
436 return retval;
440 * rpm_resume - Carry out runtime resume of given device.
441 * @dev: Device to resume.
442 * @rpmflags: Flag bits.
444 * Check if the device's runtime PM status allows it to be resumed. Cancel
445 * any scheduled or pending requests. If another resume has been started
446 * earlier, either return immediately or wait for it to finish, depending on the
447 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
448 * parallel with this function, either tell the other process to resume after
449 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
450 * flag is set then queue a resume request; otherwise run the
451 * ->runtime_resume() callback directly. Queue an idle notification for the
452 * device if the resume succeeded.
454 * This function must be called under dev->power.lock with interrupts disabled.
456 static int rpm_resume(struct device *dev, int rpmflags)
457 __releases(&dev->power.lock) __acquires(&dev->power.lock)
459 int (*callback)(struct device *);
460 struct device *parent = NULL;
461 int retval = 0;
463 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
465 repeat:
466 if (dev->power.runtime_error)
467 retval = -EINVAL;
468 else if (dev->power.disable_depth > 0)
469 retval = -EACCES;
470 if (retval)
471 goto out;
474 * Other scheduled or pending requests need to be canceled. Small
475 * optimization: If an autosuspend timer is running, leave it running
476 * rather than cancelling it now only to restart it again in the near
477 * future.
479 dev->power.request = RPM_REQ_NONE;
480 if (!dev->power.timer_autosuspends)
481 pm_runtime_deactivate_timer(dev);
483 if (dev->power.runtime_status == RPM_ACTIVE) {
484 retval = 1;
485 goto out;
488 if (dev->power.runtime_status == RPM_RESUMING
489 || dev->power.runtime_status == RPM_SUSPENDING) {
490 DEFINE_WAIT(wait);
492 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
493 if (dev->power.runtime_status == RPM_SUSPENDING)
494 dev->power.deferred_resume = true;
495 else
496 retval = -EINPROGRESS;
497 goto out;
500 /* Wait for the operation carried out in parallel with us. */
501 for (;;) {
502 prepare_to_wait(&dev->power.wait_queue, &wait,
503 TASK_UNINTERRUPTIBLE);
504 if (dev->power.runtime_status != RPM_RESUMING
505 && dev->power.runtime_status != RPM_SUSPENDING)
506 break;
508 spin_unlock_irq(&dev->power.lock);
510 schedule();
512 spin_lock_irq(&dev->power.lock);
514 finish_wait(&dev->power.wait_queue, &wait);
515 goto repeat;
519 * See if we can skip waking up the parent. This is safe only if
520 * power.no_callbacks is set, because otherwise we don't know whether
521 * the resume will actually succeed.
523 if (dev->power.no_callbacks && !parent && dev->parent) {
524 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
525 if (dev->parent->power.disable_depth > 0
526 || dev->parent->power.ignore_children
527 || dev->parent->power.runtime_status == RPM_ACTIVE) {
528 atomic_inc(&dev->parent->power.child_count);
529 spin_unlock(&dev->parent->power.lock);
530 goto no_callback; /* Assume success. */
532 spin_unlock(&dev->parent->power.lock);
535 /* Carry out an asynchronous or a synchronous resume. */
536 if (rpmflags & RPM_ASYNC) {
537 dev->power.request = RPM_REQ_RESUME;
538 if (!dev->power.request_pending) {
539 dev->power.request_pending = true;
540 queue_work(pm_wq, &dev->power.work);
542 retval = 0;
543 goto out;
546 if (!parent && dev->parent) {
548 * Increment the parent's usage counter and resume it if
549 * necessary. Not needed if dev is irq-safe; then the
550 * parent is permanently resumed.
552 parent = dev->parent;
553 if (dev->power.irq_safe)
554 goto skip_parent;
555 spin_unlock(&dev->power.lock);
557 pm_runtime_get_noresume(parent);
559 spin_lock(&parent->power.lock);
561 * We can resume if the parent's runtime PM is disabled or it
562 * is set to ignore children.
564 if (!parent->power.disable_depth
565 && !parent->power.ignore_children) {
566 rpm_resume(parent, 0);
567 if (parent->power.runtime_status != RPM_ACTIVE)
568 retval = -EBUSY;
570 spin_unlock(&parent->power.lock);
572 spin_lock(&dev->power.lock);
573 if (retval)
574 goto out;
575 goto repeat;
577 skip_parent:
579 if (dev->power.no_callbacks)
580 goto no_callback; /* Assume success. */
582 __update_runtime_status(dev, RPM_RESUMING);
584 if (dev->pm_domain)
585 callback = dev->pm_domain->ops.runtime_resume;
586 else if (dev->type && dev->type->pm)
587 callback = dev->type->pm->runtime_resume;
588 else if (dev->class && dev->class->pm)
589 callback = dev->class->pm->runtime_resume;
590 else if (dev->bus && dev->bus->pm)
591 callback = dev->bus->pm->runtime_resume;
592 else
593 callback = NULL;
595 retval = rpm_callback(callback, dev);
596 if (retval) {
597 __update_runtime_status(dev, RPM_SUSPENDED);
598 pm_runtime_cancel_pending(dev);
599 } else {
600 no_callback:
601 __update_runtime_status(dev, RPM_ACTIVE);
602 if (parent)
603 atomic_inc(&parent->power.child_count);
605 wake_up_all(&dev->power.wait_queue);
607 if (!retval)
608 rpm_idle(dev, RPM_ASYNC);
610 out:
611 if (parent && !dev->power.irq_safe) {
612 spin_unlock_irq(&dev->power.lock);
614 pm_runtime_put(parent);
616 spin_lock_irq(&dev->power.lock);
619 dev_dbg(dev, "%s returns %d\n", __func__, retval);
621 return retval;
625 * pm_runtime_work - Universal runtime PM work function.
626 * @work: Work structure used for scheduling the execution of this function.
628 * Use @work to get the device object the work is to be done for, determine what
629 * is to be done and execute the appropriate runtime PM function.
631 static void pm_runtime_work(struct work_struct *work)
633 struct device *dev = container_of(work, struct device, power.work);
634 enum rpm_request req;
636 spin_lock_irq(&dev->power.lock);
638 if (!dev->power.request_pending)
639 goto out;
641 req = dev->power.request;
642 dev->power.request = RPM_REQ_NONE;
643 dev->power.request_pending = false;
645 switch (req) {
646 case RPM_REQ_NONE:
647 break;
648 case RPM_REQ_IDLE:
649 rpm_idle(dev, RPM_NOWAIT);
650 break;
651 case RPM_REQ_SUSPEND:
652 rpm_suspend(dev, RPM_NOWAIT);
653 break;
654 case RPM_REQ_AUTOSUSPEND:
655 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
656 break;
657 case RPM_REQ_RESUME:
658 rpm_resume(dev, RPM_NOWAIT);
659 break;
662 out:
663 spin_unlock_irq(&dev->power.lock);
667 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
668 * @data: Device pointer passed by pm_schedule_suspend().
670 * Check if the time is right and queue a suspend request.
672 static void pm_suspend_timer_fn(unsigned long data)
674 struct device *dev = (struct device *)data;
675 unsigned long flags;
676 unsigned long expires;
678 spin_lock_irqsave(&dev->power.lock, flags);
680 expires = dev->power.timer_expires;
681 /* If 'expire' is after 'jiffies' we've been called too early. */
682 if (expires > 0 && !time_after(expires, jiffies)) {
683 dev->power.timer_expires = 0;
684 rpm_suspend(dev, dev->power.timer_autosuspends ?
685 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
688 spin_unlock_irqrestore(&dev->power.lock, flags);
692 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
693 * @dev: Device to suspend.
694 * @delay: Time to wait before submitting a suspend request, in milliseconds.
696 int pm_schedule_suspend(struct device *dev, unsigned int delay)
698 unsigned long flags;
699 int retval;
701 spin_lock_irqsave(&dev->power.lock, flags);
703 if (!delay) {
704 retval = rpm_suspend(dev, RPM_ASYNC);
705 goto out;
708 retval = rpm_check_suspend_allowed(dev);
709 if (retval)
710 goto out;
712 /* Other scheduled or pending requests need to be canceled. */
713 pm_runtime_cancel_pending(dev);
715 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
716 dev->power.timer_expires += !dev->power.timer_expires;
717 dev->power.timer_autosuspends = 0;
718 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
720 out:
721 spin_unlock_irqrestore(&dev->power.lock, flags);
723 return retval;
725 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
728 * __pm_runtime_idle - Entry point for runtime idle operations.
729 * @dev: Device to send idle notification for.
730 * @rpmflags: Flag bits.
732 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
733 * return immediately if it is larger than zero. Then carry out an idle
734 * notification, either synchronous or asynchronous.
736 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
737 * or if pm_runtime_irq_safe() has been called.
739 int __pm_runtime_idle(struct device *dev, int rpmflags)
741 unsigned long flags;
742 int retval;
744 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
746 if (rpmflags & RPM_GET_PUT) {
747 if (!atomic_dec_and_test(&dev->power.usage_count))
748 return 0;
751 spin_lock_irqsave(&dev->power.lock, flags);
752 retval = rpm_idle(dev, rpmflags);
753 spin_unlock_irqrestore(&dev->power.lock, flags);
755 return retval;
757 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
760 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
761 * @dev: Device to suspend.
762 * @rpmflags: Flag bits.
764 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
765 * return immediately if it is larger than zero. Then carry out a suspend,
766 * either synchronous or asynchronous.
768 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
769 * or if pm_runtime_irq_safe() has been called.
771 int __pm_runtime_suspend(struct device *dev, int rpmflags)
773 unsigned long flags;
774 int retval;
776 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
778 if (rpmflags & RPM_GET_PUT) {
779 if (!atomic_dec_and_test(&dev->power.usage_count))
780 return 0;
783 spin_lock_irqsave(&dev->power.lock, flags);
784 retval = rpm_suspend(dev, rpmflags);
785 spin_unlock_irqrestore(&dev->power.lock, flags);
787 return retval;
789 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
792 * __pm_runtime_resume - Entry point for runtime resume operations.
793 * @dev: Device to resume.
794 * @rpmflags: Flag bits.
796 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
797 * carry out a resume, either synchronous or asynchronous.
799 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
800 * or if pm_runtime_irq_safe() has been called.
802 int __pm_runtime_resume(struct device *dev, int rpmflags)
804 unsigned long flags;
805 int retval;
807 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
809 if (rpmflags & RPM_GET_PUT)
810 atomic_inc(&dev->power.usage_count);
812 spin_lock_irqsave(&dev->power.lock, flags);
813 retval = rpm_resume(dev, rpmflags);
814 spin_unlock_irqrestore(&dev->power.lock, flags);
816 return retval;
818 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
821 * __pm_runtime_set_status - Set runtime PM status of a device.
822 * @dev: Device to handle.
823 * @status: New runtime PM status of the device.
825 * If runtime PM of the device is disabled or its power.runtime_error field is
826 * different from zero, the status may be changed either to RPM_ACTIVE, or to
827 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
828 * However, if the device has a parent and the parent is not active, and the
829 * parent's power.ignore_children flag is unset, the device's status cannot be
830 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
832 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
833 * and the device parent's counter of unsuspended children is modified to
834 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
835 * notification request for the parent is submitted.
837 int __pm_runtime_set_status(struct device *dev, unsigned int status)
839 struct device *parent = dev->parent;
840 unsigned long flags;
841 bool notify_parent = false;
842 int error = 0;
844 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
845 return -EINVAL;
847 spin_lock_irqsave(&dev->power.lock, flags);
849 if (!dev->power.runtime_error && !dev->power.disable_depth) {
850 error = -EAGAIN;
851 goto out;
854 if (dev->power.runtime_status == status)
855 goto out_set;
857 if (status == RPM_SUSPENDED) {
858 /* It always is possible to set the status to 'suspended'. */
859 if (parent) {
860 atomic_add_unless(&parent->power.child_count, -1, 0);
861 notify_parent = !parent->power.ignore_children;
863 goto out_set;
866 if (parent) {
867 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
870 * It is invalid to put an active child under a parent that is
871 * not active, has runtime PM enabled and the
872 * 'power.ignore_children' flag unset.
874 if (!parent->power.disable_depth
875 && !parent->power.ignore_children
876 && parent->power.runtime_status != RPM_ACTIVE)
877 error = -EBUSY;
878 else if (dev->power.runtime_status == RPM_SUSPENDED)
879 atomic_inc(&parent->power.child_count);
881 spin_unlock(&parent->power.lock);
883 if (error)
884 goto out;
887 out_set:
888 __update_runtime_status(dev, status);
889 dev->power.runtime_error = 0;
890 out:
891 spin_unlock_irqrestore(&dev->power.lock, flags);
893 if (notify_parent)
894 pm_request_idle(parent);
896 return error;
898 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
901 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
902 * @dev: Device to handle.
904 * Flush all pending requests for the device from pm_wq and wait for all
905 * runtime PM operations involving the device in progress to complete.
907 * Should be called under dev->power.lock with interrupts disabled.
909 static void __pm_runtime_barrier(struct device *dev)
911 pm_runtime_deactivate_timer(dev);
913 if (dev->power.request_pending) {
914 dev->power.request = RPM_REQ_NONE;
915 spin_unlock_irq(&dev->power.lock);
917 cancel_work_sync(&dev->power.work);
919 spin_lock_irq(&dev->power.lock);
920 dev->power.request_pending = false;
923 if (dev->power.runtime_status == RPM_SUSPENDING
924 || dev->power.runtime_status == RPM_RESUMING
925 || dev->power.idle_notification) {
926 DEFINE_WAIT(wait);
928 /* Suspend, wake-up or idle notification in progress. */
929 for (;;) {
930 prepare_to_wait(&dev->power.wait_queue, &wait,
931 TASK_UNINTERRUPTIBLE);
932 if (dev->power.runtime_status != RPM_SUSPENDING
933 && dev->power.runtime_status != RPM_RESUMING
934 && !dev->power.idle_notification)
935 break;
936 spin_unlock_irq(&dev->power.lock);
938 schedule();
940 spin_lock_irq(&dev->power.lock);
942 finish_wait(&dev->power.wait_queue, &wait);
947 * pm_runtime_barrier - Flush pending requests and wait for completions.
948 * @dev: Device to handle.
950 * Prevent the device from being suspended by incrementing its usage counter and
951 * if there's a pending resume request for the device, wake the device up.
952 * Next, make sure that all pending requests for the device have been flushed
953 * from pm_wq and wait for all runtime PM operations involving the device in
954 * progress to complete.
956 * Return value:
957 * 1, if there was a resume request pending and the device had to be woken up,
958 * 0, otherwise
960 int pm_runtime_barrier(struct device *dev)
962 int retval = 0;
964 pm_runtime_get_noresume(dev);
965 spin_lock_irq(&dev->power.lock);
967 if (dev->power.request_pending
968 && dev->power.request == RPM_REQ_RESUME) {
969 rpm_resume(dev, 0);
970 retval = 1;
973 __pm_runtime_barrier(dev);
975 spin_unlock_irq(&dev->power.lock);
976 pm_runtime_put_noidle(dev);
978 return retval;
980 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
983 * __pm_runtime_disable - Disable runtime PM of a device.
984 * @dev: Device to handle.
985 * @check_resume: If set, check if there's a resume request for the device.
987 * Increment power.disable_depth for the device and if was zero previously,
988 * cancel all pending runtime PM requests for the device and wait for all
989 * operations in progress to complete. The device can be either active or
990 * suspended after its runtime PM has been disabled.
992 * If @check_resume is set and there's a resume request pending when
993 * __pm_runtime_disable() is called and power.disable_depth is zero, the
994 * function will wake up the device before disabling its runtime PM.
996 void __pm_runtime_disable(struct device *dev, bool check_resume)
998 spin_lock_irq(&dev->power.lock);
1000 if (dev->power.disable_depth > 0) {
1001 dev->power.disable_depth++;
1002 goto out;
1006 * Wake up the device if there's a resume request pending, because that
1007 * means there probably is some I/O to process and disabling runtime PM
1008 * shouldn't prevent the device from processing the I/O.
1010 if (check_resume && dev->power.request_pending
1011 && dev->power.request == RPM_REQ_RESUME) {
1013 * Prevent suspends and idle notifications from being carried
1014 * out after we have woken up the device.
1016 pm_runtime_get_noresume(dev);
1018 rpm_resume(dev, 0);
1020 pm_runtime_put_noidle(dev);
1023 if (!dev->power.disable_depth++)
1024 __pm_runtime_barrier(dev);
1026 out:
1027 spin_unlock_irq(&dev->power.lock);
1029 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1032 * pm_runtime_enable - Enable runtime PM of a device.
1033 * @dev: Device to handle.
1035 void pm_runtime_enable(struct device *dev)
1037 unsigned long flags;
1039 spin_lock_irqsave(&dev->power.lock, flags);
1041 if (dev->power.disable_depth > 0)
1042 dev->power.disable_depth--;
1043 else
1044 dev_warn(dev, "Unbalanced %s!\n", __func__);
1046 spin_unlock_irqrestore(&dev->power.lock, flags);
1048 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1051 * pm_runtime_forbid - Block runtime PM of a device.
1052 * @dev: Device to handle.
1054 * Increase the device's usage count and clear its power.runtime_auto flag,
1055 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1056 * for it.
1058 void pm_runtime_forbid(struct device *dev)
1060 spin_lock_irq(&dev->power.lock);
1061 if (!dev->power.runtime_auto)
1062 goto out;
1064 dev->power.runtime_auto = false;
1065 atomic_inc(&dev->power.usage_count);
1066 rpm_resume(dev, 0);
1068 out:
1069 spin_unlock_irq(&dev->power.lock);
1071 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1074 * pm_runtime_allow - Unblock runtime PM of a device.
1075 * @dev: Device to handle.
1077 * Decrease the device's usage count and set its power.runtime_auto flag.
1079 void pm_runtime_allow(struct device *dev)
1081 spin_lock_irq(&dev->power.lock);
1082 if (dev->power.runtime_auto)
1083 goto out;
1085 dev->power.runtime_auto = true;
1086 if (atomic_dec_and_test(&dev->power.usage_count))
1087 rpm_idle(dev, RPM_AUTO);
1089 out:
1090 spin_unlock_irq(&dev->power.lock);
1092 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1095 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1096 * @dev: Device to handle.
1098 * Set the power.no_callbacks flag, which tells the PM core that this
1099 * device is power-managed through its parent and has no runtime PM
1100 * callbacks of its own. The runtime sysfs attributes will be removed.
1102 void pm_runtime_no_callbacks(struct device *dev)
1104 spin_lock_irq(&dev->power.lock);
1105 dev->power.no_callbacks = 1;
1106 spin_unlock_irq(&dev->power.lock);
1107 if (device_is_registered(dev))
1108 rpm_sysfs_remove(dev);
1110 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1113 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1114 * @dev: Device to handle
1116 * Set the power.irq_safe flag, which tells the PM core that the
1117 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1118 * always be invoked with the spinlock held and interrupts disabled. It also
1119 * causes the parent's usage counter to be permanently incremented, preventing
1120 * the parent from runtime suspending -- otherwise an irq-safe child might have
1121 * to wait for a non-irq-safe parent.
1123 void pm_runtime_irq_safe(struct device *dev)
1125 if (dev->parent)
1126 pm_runtime_get_sync(dev->parent);
1127 spin_lock_irq(&dev->power.lock);
1128 dev->power.irq_safe = 1;
1129 spin_unlock_irq(&dev->power.lock);
1131 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1134 * update_autosuspend - Handle a change to a device's autosuspend settings.
1135 * @dev: Device to handle.
1136 * @old_delay: The former autosuspend_delay value.
1137 * @old_use: The former use_autosuspend value.
1139 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1140 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1142 * This function must be called under dev->power.lock with interrupts disabled.
1144 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1146 int delay = dev->power.autosuspend_delay;
1148 /* Should runtime suspend be prevented now? */
1149 if (dev->power.use_autosuspend && delay < 0) {
1151 /* If it used to be allowed then prevent it. */
1152 if (!old_use || old_delay >= 0) {
1153 atomic_inc(&dev->power.usage_count);
1154 rpm_resume(dev, 0);
1158 /* Runtime suspend should be allowed now. */
1159 else {
1161 /* If it used to be prevented then allow it. */
1162 if (old_use && old_delay < 0)
1163 atomic_dec(&dev->power.usage_count);
1165 /* Maybe we can autosuspend now. */
1166 rpm_idle(dev, RPM_AUTO);
1171 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1172 * @dev: Device to handle.
1173 * @delay: Value of the new delay in milliseconds.
1175 * Set the device's power.autosuspend_delay value. If it changes to negative
1176 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1177 * changes the other way, allow runtime suspends.
1179 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1181 int old_delay, old_use;
1183 spin_lock_irq(&dev->power.lock);
1184 old_delay = dev->power.autosuspend_delay;
1185 old_use = dev->power.use_autosuspend;
1186 dev->power.autosuspend_delay = delay;
1187 update_autosuspend(dev, old_delay, old_use);
1188 spin_unlock_irq(&dev->power.lock);
1190 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1193 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1194 * @dev: Device to handle.
1195 * @use: New value for use_autosuspend.
1197 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1198 * suspends as needed.
1200 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1202 int old_delay, old_use;
1204 spin_lock_irq(&dev->power.lock);
1205 old_delay = dev->power.autosuspend_delay;
1206 old_use = dev->power.use_autosuspend;
1207 dev->power.use_autosuspend = use;
1208 update_autosuspend(dev, old_delay, old_use);
1209 spin_unlock_irq(&dev->power.lock);
1211 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1214 * pm_runtime_init - Initialize runtime PM fields in given device object.
1215 * @dev: Device object to initialize.
1217 void pm_runtime_init(struct device *dev)
1219 dev->power.runtime_status = RPM_SUSPENDED;
1220 dev->power.idle_notification = false;
1222 dev->power.disable_depth = 1;
1223 atomic_set(&dev->power.usage_count, 0);
1225 dev->power.runtime_error = 0;
1227 atomic_set(&dev->power.child_count, 0);
1228 pm_suspend_ignore_children(dev, false);
1229 dev->power.runtime_auto = true;
1231 dev->power.request_pending = false;
1232 dev->power.request = RPM_REQ_NONE;
1233 dev->power.deferred_resume = false;
1234 dev->power.accounting_timestamp = jiffies;
1235 INIT_WORK(&dev->power.work, pm_runtime_work);
1237 dev->power.timer_expires = 0;
1238 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1239 (unsigned long)dev);
1241 init_waitqueue_head(&dev->power.wait_queue);
1245 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1246 * @dev: Device object being removed from device hierarchy.
1248 void pm_runtime_remove(struct device *dev)
1250 __pm_runtime_disable(dev, false);
1252 /* Change the status back to 'suspended' to match the initial status. */
1253 if (dev->power.runtime_status == RPM_ACTIVE)
1254 pm_runtime_set_suspended(dev);
1255 if (dev->power.irq_safe && dev->parent)
1256 pm_runtime_put_sync(dev->parent);