GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / base / power / runtime.c
blobb78c401ffa7380f67f29dbddf1fecf1396746e85
1 /*
2 * drivers/base/power/runtime.c - Helper functions for device run-time PM
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6 * This file is released under the GPLv2.
7 */
9 #include <linux/sched.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/jiffies.h>
13 static int __pm_runtime_resume(struct device *dev, bool from_wq);
14 static int __pm_request_idle(struct device *dev);
15 static int __pm_request_resume(struct device *dev);
17 /**
18 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
19 * @dev: Device to handle.
21 static void pm_runtime_deactivate_timer(struct device *dev)
23 if (dev->power.timer_expires > 0) {
24 del_timer(&dev->power.suspend_timer);
25 dev->power.timer_expires = 0;
29 /**
30 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
31 * @dev: Device to handle.
33 static void pm_runtime_cancel_pending(struct device *dev)
35 pm_runtime_deactivate_timer(dev);
37 * In case there's a request pending, make sure its work function will
38 * return without doing anything.
40 dev->power.request = RPM_REQ_NONE;
43 /**
44 * __pm_runtime_idle - Notify device bus type if the device can be suspended.
45 * @dev: Device to notify the bus type about.
47 * This function must be called under dev->power.lock with interrupts disabled.
49 static int __pm_runtime_idle(struct device *dev)
50 __releases(&dev->power.lock) __acquires(&dev->power.lock)
52 int retval = 0;
54 if (dev->power.runtime_error)
55 retval = -EINVAL;
56 else if (dev->power.idle_notification)
57 retval = -EINPROGRESS;
58 else if (atomic_read(&dev->power.usage_count) > 0
59 || dev->power.disable_depth > 0
60 || dev->power.runtime_status != RPM_ACTIVE)
61 retval = -EAGAIN;
62 else if (!pm_children_suspended(dev))
63 retval = -EBUSY;
64 if (retval)
65 goto out;
67 if (dev->power.request_pending) {
69 * If an idle notification request is pending, cancel it. Any
70 * other pending request takes precedence over us.
72 if (dev->power.request == RPM_REQ_IDLE) {
73 dev->power.request = RPM_REQ_NONE;
74 } else if (dev->power.request != RPM_REQ_NONE) {
75 retval = -EAGAIN;
76 goto out;
80 dev->power.idle_notification = true;
82 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_idle) {
83 spin_unlock_irq(&dev->power.lock);
85 dev->bus->pm->runtime_idle(dev);
87 spin_lock_irq(&dev->power.lock);
88 } else if (dev->type && dev->type->pm && dev->type->pm->runtime_idle) {
89 spin_unlock_irq(&dev->power.lock);
91 dev->type->pm->runtime_idle(dev);
93 spin_lock_irq(&dev->power.lock);
94 } else if (dev->class && dev->class->pm
95 && dev->class->pm->runtime_idle) {
96 spin_unlock_irq(&dev->power.lock);
98 dev->class->pm->runtime_idle(dev);
100 spin_lock_irq(&dev->power.lock);
103 dev->power.idle_notification = false;
104 wake_up_all(&dev->power.wait_queue);
106 out:
107 return retval;
111 * pm_runtime_idle - Notify device bus type if the device can be suspended.
112 * @dev: Device to notify the bus type about.
114 int pm_runtime_idle(struct device *dev)
116 int retval;
118 spin_lock_irq(&dev->power.lock);
119 retval = __pm_runtime_idle(dev);
120 spin_unlock_irq(&dev->power.lock);
122 return retval;
124 EXPORT_SYMBOL_GPL(pm_runtime_idle);
128 * update_pm_runtime_accounting - Update the time accounting of power states
129 * @dev: Device to update the accounting for
131 * In order to be able to have time accounting of the various power states
132 * (as used by programs such as PowerTOP to show the effectiveness of runtime
133 * PM), we need to track the time spent in each state.
134 * update_pm_runtime_accounting must be called each time before the
135 * runtime_status field is updated, to account the time in the old state
136 * correctly.
138 void update_pm_runtime_accounting(struct device *dev)
140 unsigned long now = jiffies;
141 int delta;
143 delta = now - dev->power.accounting_timestamp;
145 if (delta < 0)
146 delta = 0;
148 dev->power.accounting_timestamp = now;
150 if (dev->power.disable_depth > 0)
151 return;
153 if (dev->power.runtime_status == RPM_SUSPENDED)
154 dev->power.suspended_jiffies += delta;
155 else
156 dev->power.active_jiffies += delta;
159 static void __update_runtime_status(struct device *dev, enum rpm_status status)
161 update_pm_runtime_accounting(dev);
162 dev->power.runtime_status = status;
166 * __pm_runtime_suspend - Carry out run-time suspend of given device.
167 * @dev: Device to suspend.
168 * @from_wq: If set, the function has been called via pm_wq.
170 * Check if the device can be suspended and run the ->runtime_suspend() callback
171 * provided by its bus type. If another suspend has been started earlier, wait
172 * for it to finish. If an idle notification or suspend request is pending or
173 * scheduled, cancel it.
175 * This function must be called under dev->power.lock with interrupts disabled.
177 int __pm_runtime_suspend(struct device *dev, bool from_wq)
178 __releases(&dev->power.lock) __acquires(&dev->power.lock)
180 struct device *parent = NULL;
181 bool notify = false;
182 int retval = 0;
184 dev_dbg(dev, "__pm_runtime_suspend()%s!\n",
185 from_wq ? " from workqueue" : "");
187 repeat:
188 if (dev->power.runtime_error) {
189 retval = -EINVAL;
190 goto out;
193 /* Pending resume requests take precedence over us. */
194 if (dev->power.request_pending
195 && dev->power.request == RPM_REQ_RESUME) {
196 retval = -EAGAIN;
197 goto out;
200 /* Other scheduled or pending requests need to be canceled. */
201 pm_runtime_cancel_pending(dev);
203 if (dev->power.runtime_status == RPM_SUSPENDED)
204 retval = 1;
205 else if (dev->power.runtime_status == RPM_RESUMING
206 || dev->power.disable_depth > 0
207 || atomic_read(&dev->power.usage_count) > 0)
208 retval = -EAGAIN;
209 else if (!pm_children_suspended(dev))
210 retval = -EBUSY;
211 if (retval)
212 goto out;
214 if (dev->power.runtime_status == RPM_SUSPENDING) {
215 DEFINE_WAIT(wait);
217 if (from_wq) {
218 retval = -EINPROGRESS;
219 goto out;
222 /* Wait for the other suspend running in parallel with us. */
223 for (;;) {
224 prepare_to_wait(&dev->power.wait_queue, &wait,
225 TASK_UNINTERRUPTIBLE);
226 if (dev->power.runtime_status != RPM_SUSPENDING)
227 break;
229 spin_unlock_irq(&dev->power.lock);
231 schedule();
233 spin_lock_irq(&dev->power.lock);
235 finish_wait(&dev->power.wait_queue, &wait);
236 goto repeat;
239 __update_runtime_status(dev, RPM_SUSPENDING);
240 dev->power.deferred_resume = false;
242 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_suspend) {
243 spin_unlock_irq(&dev->power.lock);
245 retval = dev->bus->pm->runtime_suspend(dev);
247 spin_lock_irq(&dev->power.lock);
248 dev->power.runtime_error = retval;
249 } else if (dev->type && dev->type->pm
250 && dev->type->pm->runtime_suspend) {
251 spin_unlock_irq(&dev->power.lock);
253 retval = dev->type->pm->runtime_suspend(dev);
255 spin_lock_irq(&dev->power.lock);
256 dev->power.runtime_error = retval;
257 } else if (dev->class && dev->class->pm
258 && dev->class->pm->runtime_suspend) {
259 spin_unlock_irq(&dev->power.lock);
261 retval = dev->class->pm->runtime_suspend(dev);
263 spin_lock_irq(&dev->power.lock);
264 dev->power.runtime_error = retval;
265 } else {
266 retval = -ENOSYS;
269 if (retval) {
270 __update_runtime_status(dev, RPM_ACTIVE);
271 if (retval == -EAGAIN || retval == -EBUSY) {
272 if (dev->power.timer_expires == 0)
273 notify = true;
274 dev->power.runtime_error = 0;
275 } else {
276 pm_runtime_cancel_pending(dev);
278 } else {
279 __update_runtime_status(dev, RPM_SUSPENDED);
280 pm_runtime_deactivate_timer(dev);
282 if (dev->parent) {
283 parent = dev->parent;
284 atomic_add_unless(&parent->power.child_count, -1, 0);
287 wake_up_all(&dev->power.wait_queue);
289 if (dev->power.deferred_resume) {
290 __pm_runtime_resume(dev, false);
291 retval = -EAGAIN;
292 goto out;
295 if (notify)
296 __pm_runtime_idle(dev);
298 if (parent && !parent->power.ignore_children) {
299 spin_unlock_irq(&dev->power.lock);
301 pm_request_idle(parent);
303 spin_lock_irq(&dev->power.lock);
306 out:
307 dev_dbg(dev, "__pm_runtime_suspend() returns %d!\n", retval);
309 return retval;
313 * pm_runtime_suspend - Carry out run-time suspend of given device.
314 * @dev: Device to suspend.
316 int pm_runtime_suspend(struct device *dev)
318 int retval;
320 spin_lock_irq(&dev->power.lock);
321 retval = __pm_runtime_suspend(dev, false);
322 spin_unlock_irq(&dev->power.lock);
324 return retval;
326 EXPORT_SYMBOL_GPL(pm_runtime_suspend);
329 * __pm_runtime_resume - Carry out run-time resume of given device.
330 * @dev: Device to resume.
331 * @from_wq: If set, the function has been called via pm_wq.
333 * Check if the device can be woken up and run the ->runtime_resume() callback
334 * provided by its bus type. If another resume has been started earlier, wait
335 * for it to finish. If there's a suspend running in parallel with this
336 * function, wait for it to finish and resume the device. Cancel any scheduled
337 * or pending requests.
339 * This function must be called under dev->power.lock with interrupts disabled.
341 int __pm_runtime_resume(struct device *dev, bool from_wq)
342 __releases(&dev->power.lock) __acquires(&dev->power.lock)
344 struct device *parent = NULL;
345 int retval = 0;
347 dev_dbg(dev, "__pm_runtime_resume()%s!\n",
348 from_wq ? " from workqueue" : "");
350 repeat:
351 if (dev->power.runtime_error) {
352 retval = -EINVAL;
353 goto out;
356 pm_runtime_cancel_pending(dev);
358 if (dev->power.runtime_status == RPM_ACTIVE)
359 retval = 1;
360 else if (dev->power.disable_depth > 0)
361 retval = -EAGAIN;
362 if (retval)
363 goto out;
365 if (dev->power.runtime_status == RPM_RESUMING
366 || dev->power.runtime_status == RPM_SUSPENDING) {
367 DEFINE_WAIT(wait);
369 if (from_wq) {
370 if (dev->power.runtime_status == RPM_SUSPENDING)
371 dev->power.deferred_resume = true;
372 retval = -EINPROGRESS;
373 goto out;
376 /* Wait for the operation carried out in parallel with us. */
377 for (;;) {
378 prepare_to_wait(&dev->power.wait_queue, &wait,
379 TASK_UNINTERRUPTIBLE);
380 if (dev->power.runtime_status != RPM_RESUMING
381 && dev->power.runtime_status != RPM_SUSPENDING)
382 break;
384 spin_unlock_irq(&dev->power.lock);
386 schedule();
388 spin_lock_irq(&dev->power.lock);
390 finish_wait(&dev->power.wait_queue, &wait);
391 goto repeat;
394 if (!parent && dev->parent) {
396 * Increment the parent's resume counter and resume it if
397 * necessary.
399 parent = dev->parent;
400 spin_unlock(&dev->power.lock);
402 pm_runtime_get_noresume(parent);
404 spin_lock(&parent->power.lock);
406 * We can resume if the parent's run-time PM is disabled or it
407 * is set to ignore children.
409 if (!parent->power.disable_depth
410 && !parent->power.ignore_children) {
411 __pm_runtime_resume(parent, false);
412 if (parent->power.runtime_status != RPM_ACTIVE)
413 retval = -EBUSY;
415 spin_unlock(&parent->power.lock);
417 spin_lock(&dev->power.lock);
418 if (retval)
419 goto out;
420 goto repeat;
423 __update_runtime_status(dev, RPM_RESUMING);
425 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_resume) {
426 spin_unlock_irq(&dev->power.lock);
428 retval = dev->bus->pm->runtime_resume(dev);
430 spin_lock_irq(&dev->power.lock);
431 dev->power.runtime_error = retval;
432 } else if (dev->type && dev->type->pm
433 && dev->type->pm->runtime_resume) {
434 spin_unlock_irq(&dev->power.lock);
436 retval = dev->type->pm->runtime_resume(dev);
438 spin_lock_irq(&dev->power.lock);
439 dev->power.runtime_error = retval;
440 } else if (dev->class && dev->class->pm
441 && dev->class->pm->runtime_resume) {
442 spin_unlock_irq(&dev->power.lock);
444 retval = dev->class->pm->runtime_resume(dev);
446 spin_lock_irq(&dev->power.lock);
447 dev->power.runtime_error = retval;
448 } else {
449 retval = -ENOSYS;
452 if (retval) {
453 __update_runtime_status(dev, RPM_SUSPENDED);
454 pm_runtime_cancel_pending(dev);
455 } else {
456 __update_runtime_status(dev, RPM_ACTIVE);
457 if (parent)
458 atomic_inc(&parent->power.child_count);
460 wake_up_all(&dev->power.wait_queue);
462 if (!retval)
463 __pm_request_idle(dev);
465 out:
466 if (parent) {
467 spin_unlock_irq(&dev->power.lock);
469 pm_runtime_put(parent);
471 spin_lock_irq(&dev->power.lock);
474 dev_dbg(dev, "__pm_runtime_resume() returns %d!\n", retval);
476 return retval;
480 * pm_runtime_resume - Carry out run-time resume of given device.
481 * @dev: Device to suspend.
483 int pm_runtime_resume(struct device *dev)
485 int retval;
487 spin_lock_irq(&dev->power.lock);
488 retval = __pm_runtime_resume(dev, false);
489 spin_unlock_irq(&dev->power.lock);
491 return retval;
493 EXPORT_SYMBOL_GPL(pm_runtime_resume);
496 * pm_runtime_work - Universal run-time PM work function.
497 * @work: Work structure used for scheduling the execution of this function.
499 * Use @work to get the device object the work is to be done for, determine what
500 * is to be done and execute the appropriate run-time PM function.
502 static void pm_runtime_work(struct work_struct *work)
504 struct device *dev = container_of(work, struct device, power.work);
505 enum rpm_request req;
507 spin_lock_irq(&dev->power.lock);
509 if (!dev->power.request_pending)
510 goto out;
512 req = dev->power.request;
513 dev->power.request = RPM_REQ_NONE;
514 dev->power.request_pending = false;
516 switch (req) {
517 case RPM_REQ_NONE:
518 break;
519 case RPM_REQ_IDLE:
520 __pm_runtime_idle(dev);
521 break;
522 case RPM_REQ_SUSPEND:
523 __pm_runtime_suspend(dev, true);
524 break;
525 case RPM_REQ_RESUME:
526 __pm_runtime_resume(dev, true);
527 break;
530 out:
531 spin_unlock_irq(&dev->power.lock);
535 * __pm_request_idle - Submit an idle notification request for given device.
536 * @dev: Device to handle.
538 * Check if the device's run-time PM status is correct for suspending the device
539 * and queue up a request to run __pm_runtime_idle() for it.
541 * This function must be called under dev->power.lock with interrupts disabled.
543 static int __pm_request_idle(struct device *dev)
545 int retval = 0;
547 if (dev->power.runtime_error)
548 retval = -EINVAL;
549 else if (atomic_read(&dev->power.usage_count) > 0
550 || dev->power.disable_depth > 0
551 || dev->power.runtime_status == RPM_SUSPENDED
552 || dev->power.runtime_status == RPM_SUSPENDING)
553 retval = -EAGAIN;
554 else if (!pm_children_suspended(dev))
555 retval = -EBUSY;
556 if (retval)
557 return retval;
559 if (dev->power.request_pending) {
560 /* Any requests other then RPM_REQ_IDLE take precedence. */
561 if (dev->power.request == RPM_REQ_NONE)
562 dev->power.request = RPM_REQ_IDLE;
563 else if (dev->power.request != RPM_REQ_IDLE)
564 retval = -EAGAIN;
565 return retval;
568 dev->power.request = RPM_REQ_IDLE;
569 dev->power.request_pending = true;
570 queue_work(pm_wq, &dev->power.work);
572 return retval;
576 * pm_request_idle - Submit an idle notification request for given device.
577 * @dev: Device to handle.
579 int pm_request_idle(struct device *dev)
581 unsigned long flags;
582 int retval;
584 spin_lock_irqsave(&dev->power.lock, flags);
585 retval = __pm_request_idle(dev);
586 spin_unlock_irqrestore(&dev->power.lock, flags);
588 return retval;
590 EXPORT_SYMBOL_GPL(pm_request_idle);
593 * __pm_request_suspend - Submit a suspend request for given device.
594 * @dev: Device to suspend.
596 * This function must be called under dev->power.lock with interrupts disabled.
598 static int __pm_request_suspend(struct device *dev)
600 int retval = 0;
602 if (dev->power.runtime_error)
603 return -EINVAL;
605 if (dev->power.runtime_status == RPM_SUSPENDED)
606 retval = 1;
607 else if (atomic_read(&dev->power.usage_count) > 0
608 || dev->power.disable_depth > 0)
609 retval = -EAGAIN;
610 else if (dev->power.runtime_status == RPM_SUSPENDING)
611 retval = -EINPROGRESS;
612 else if (!pm_children_suspended(dev))
613 retval = -EBUSY;
614 if (retval < 0)
615 return retval;
617 pm_runtime_deactivate_timer(dev);
619 if (dev->power.request_pending) {
621 * Pending resume requests take precedence over us, but we can
622 * overtake any other pending request.
624 if (dev->power.request == RPM_REQ_RESUME)
625 retval = -EAGAIN;
626 else if (dev->power.request != RPM_REQ_SUSPEND)
627 dev->power.request = retval ?
628 RPM_REQ_NONE : RPM_REQ_SUSPEND;
629 return retval;
630 } else if (retval) {
631 return retval;
634 dev->power.request = RPM_REQ_SUSPEND;
635 dev->power.request_pending = true;
636 queue_work(pm_wq, &dev->power.work);
638 return 0;
642 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
643 * @data: Device pointer passed by pm_schedule_suspend().
645 * Check if the time is right and execute __pm_request_suspend() in that case.
647 static void pm_suspend_timer_fn(unsigned long data)
649 struct device *dev = (struct device *)data;
650 unsigned long flags;
651 unsigned long expires;
653 spin_lock_irqsave(&dev->power.lock, flags);
655 expires = dev->power.timer_expires;
656 /* If 'expire' is after 'jiffies' we've been called too early. */
657 if (expires > 0 && !time_after(expires, jiffies)) {
658 dev->power.timer_expires = 0;
659 __pm_request_suspend(dev);
662 spin_unlock_irqrestore(&dev->power.lock, flags);
666 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
667 * @dev: Device to suspend.
668 * @delay: Time to wait before submitting a suspend request, in milliseconds.
670 int pm_schedule_suspend(struct device *dev, unsigned int delay)
672 unsigned long flags;
673 int retval = 0;
675 spin_lock_irqsave(&dev->power.lock, flags);
677 if (dev->power.runtime_error) {
678 retval = -EINVAL;
679 goto out;
682 if (!delay) {
683 retval = __pm_request_suspend(dev);
684 goto out;
687 pm_runtime_deactivate_timer(dev);
689 if (dev->power.request_pending) {
691 * Pending resume requests take precedence over us, but any
692 * other pending requests have to be canceled.
694 if (dev->power.request == RPM_REQ_RESUME) {
695 retval = -EAGAIN;
696 goto out;
698 dev->power.request = RPM_REQ_NONE;
701 if (dev->power.runtime_status == RPM_SUSPENDED)
702 retval = 1;
703 else if (atomic_read(&dev->power.usage_count) > 0
704 || dev->power.disable_depth > 0)
705 retval = -EAGAIN;
706 else if (!pm_children_suspended(dev))
707 retval = -EBUSY;
708 if (retval)
709 goto out;
711 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
712 if (!dev->power.timer_expires)
713 dev->power.timer_expires = 1;
714 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
716 out:
717 spin_unlock_irqrestore(&dev->power.lock, flags);
719 return retval;
721 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
724 * pm_request_resume - Submit a resume request for given device.
725 * @dev: Device to resume.
727 * This function must be called under dev->power.lock with interrupts disabled.
729 static int __pm_request_resume(struct device *dev)
731 int retval = 0;
733 if (dev->power.runtime_error)
734 return -EINVAL;
736 if (dev->power.runtime_status == RPM_ACTIVE)
737 retval = 1;
738 else if (dev->power.runtime_status == RPM_RESUMING)
739 retval = -EINPROGRESS;
740 else if (dev->power.disable_depth > 0)
741 retval = -EAGAIN;
742 if (retval < 0)
743 return retval;
745 pm_runtime_deactivate_timer(dev);
747 if (dev->power.runtime_status == RPM_SUSPENDING) {
748 dev->power.deferred_resume = true;
749 return retval;
751 if (dev->power.request_pending) {
752 /* If non-resume request is pending, we can overtake it. */
753 dev->power.request = retval ? RPM_REQ_NONE : RPM_REQ_RESUME;
754 return retval;
756 if (retval)
757 return retval;
759 dev->power.request = RPM_REQ_RESUME;
760 dev->power.request_pending = true;
761 queue_work(pm_wq, &dev->power.work);
763 return retval;
767 * pm_request_resume - Submit a resume request for given device.
768 * @dev: Device to resume.
770 int pm_request_resume(struct device *dev)
772 unsigned long flags;
773 int retval;
775 spin_lock_irqsave(&dev->power.lock, flags);
776 retval = __pm_request_resume(dev);
777 spin_unlock_irqrestore(&dev->power.lock, flags);
779 return retval;
781 EXPORT_SYMBOL_GPL(pm_request_resume);
784 * __pm_runtime_get - Reference count a device and wake it up, if necessary.
785 * @dev: Device to handle.
786 * @sync: If set and the device is suspended, resume it synchronously.
788 * Increment the usage count of the device and resume it or submit a resume
789 * request for it, depending on the value of @sync.
791 int __pm_runtime_get(struct device *dev, bool sync)
793 int retval;
795 atomic_inc(&dev->power.usage_count);
796 retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);
798 return retval;
800 EXPORT_SYMBOL_GPL(__pm_runtime_get);
803 * __pm_runtime_put - Decrement the device's usage counter and notify its bus.
804 * @dev: Device to handle.
805 * @sync: If the device's bus type is to be notified, do that synchronously.
807 * Decrement the usage count of the device and if it reaches zero, carry out a
808 * synchronous idle notification or submit an idle notification request for it,
809 * depending on the value of @sync.
811 int __pm_runtime_put(struct device *dev, bool sync)
813 int retval = 0;
815 if (atomic_dec_and_test(&dev->power.usage_count))
816 retval = sync ? pm_runtime_idle(dev) : pm_request_idle(dev);
818 return retval;
820 EXPORT_SYMBOL_GPL(__pm_runtime_put);
823 * __pm_runtime_set_status - Set run-time PM status of a device.
824 * @dev: Device to handle.
825 * @status: New run-time PM status of the device.
827 * If run-time PM of the device is disabled or its power.runtime_error field is
828 * different from zero, the status may be changed either to RPM_ACTIVE, or to
829 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
830 * However, if the device has a parent and the parent is not active, and the
831 * parent's power.ignore_children flag is unset, the device's status cannot be
832 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
834 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
835 * and the device parent's counter of unsuspended children is modified to
836 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
837 * notification request for the parent is submitted.
839 int __pm_runtime_set_status(struct device *dev, unsigned int status)
841 struct device *parent = dev->parent;
842 unsigned long flags;
843 bool notify_parent = false;
844 int error = 0;
846 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
847 return -EINVAL;
849 spin_lock_irqsave(&dev->power.lock, flags);
851 if (!dev->power.runtime_error && !dev->power.disable_depth) {
852 error = -EAGAIN;
853 goto out;
856 if (dev->power.runtime_status == status)
857 goto out_set;
859 if (status == RPM_SUSPENDED) {
860 /* It always is possible to set the status to 'suspended'. */
861 if (parent) {
862 atomic_add_unless(&parent->power.child_count, -1, 0);
863 notify_parent = !parent->power.ignore_children;
865 goto out_set;
868 if (parent) {
869 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
872 * It is invalid to put an active child under a parent that is
873 * not active, has run-time PM enabled and the
874 * 'power.ignore_children' flag unset.
876 if (!parent->power.disable_depth
877 && !parent->power.ignore_children
878 && parent->power.runtime_status != RPM_ACTIVE)
879 error = -EBUSY;
880 else if (dev->power.runtime_status == RPM_SUSPENDED)
881 atomic_inc(&parent->power.child_count);
883 spin_unlock(&parent->power.lock);
885 if (error)
886 goto out;
889 out_set:
890 __update_runtime_status(dev, status);
891 dev->power.runtime_error = 0;
892 out:
893 spin_unlock_irqrestore(&dev->power.lock, flags);
895 if (notify_parent)
896 pm_request_idle(parent);
898 return error;
900 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
903 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
904 * @dev: Device to handle.
906 * Flush all pending requests for the device from pm_wq and wait for all
907 * run-time PM operations involving the device in progress to complete.
909 * Should be called under dev->power.lock with interrupts disabled.
911 static void __pm_runtime_barrier(struct device *dev)
913 pm_runtime_deactivate_timer(dev);
915 if (dev->power.request_pending) {
916 dev->power.request = RPM_REQ_NONE;
917 spin_unlock_irq(&dev->power.lock);
919 cancel_work_sync(&dev->power.work);
921 spin_lock_irq(&dev->power.lock);
922 dev->power.request_pending = false;
925 if (dev->power.runtime_status == RPM_SUSPENDING
926 || dev->power.runtime_status == RPM_RESUMING
927 || dev->power.idle_notification) {
928 DEFINE_WAIT(wait);
930 /* Suspend, wake-up or idle notification in progress. */
931 for (;;) {
932 prepare_to_wait(&dev->power.wait_queue, &wait,
933 TASK_UNINTERRUPTIBLE);
934 if (dev->power.runtime_status != RPM_SUSPENDING
935 && dev->power.runtime_status != RPM_RESUMING
936 && !dev->power.idle_notification)
937 break;
938 spin_unlock_irq(&dev->power.lock);
940 schedule();
942 spin_lock_irq(&dev->power.lock);
944 finish_wait(&dev->power.wait_queue, &wait);
949 * pm_runtime_barrier - Flush pending requests and wait for completions.
950 * @dev: Device to handle.
952 * Prevent the device from being suspended by incrementing its usage counter and
953 * if there's a pending resume request for the device, wake the device up.
954 * Next, make sure that all pending requests for the device have been flushed
955 * from pm_wq and wait for all run-time PM operations involving the device in
956 * progress to complete.
958 * Return value:
959 * 1, if there was a resume request pending and the device had to be woken up,
960 * 0, otherwise
962 int pm_runtime_barrier(struct device *dev)
964 int retval = 0;
966 pm_runtime_get_noresume(dev);
967 spin_lock_irq(&dev->power.lock);
969 if (dev->power.request_pending
970 && dev->power.request == RPM_REQ_RESUME) {
971 __pm_runtime_resume(dev, false);
972 retval = 1;
975 __pm_runtime_barrier(dev);
977 spin_unlock_irq(&dev->power.lock);
978 pm_runtime_put_noidle(dev);
980 return retval;
982 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
985 * __pm_runtime_disable - Disable run-time PM of a device.
986 * @dev: Device to handle.
987 * @check_resume: If set, check if there's a resume request for the device.
989 * Increment power.disable_depth for the device and if was zero previously,
990 * cancel all pending run-time PM requests for the device and wait for all
991 * operations in progress to complete. The device can be either active or
992 * suspended after its run-time PM has been disabled.
994 * If @check_resume is set and there's a resume request pending when
995 * __pm_runtime_disable() is called and power.disable_depth is zero, the
996 * function will wake up the device before disabling its run-time PM.
998 void __pm_runtime_disable(struct device *dev, bool check_resume)
1000 spin_lock_irq(&dev->power.lock);
1002 if (dev->power.disable_depth > 0) {
1003 dev->power.disable_depth++;
1004 goto out;
1008 * Wake up the device if there's a resume request pending, because that
1009 * means there probably is some I/O to process and disabling run-time PM
1010 * shouldn't prevent the device from processing the I/O.
1012 if (check_resume && dev->power.request_pending
1013 && dev->power.request == RPM_REQ_RESUME) {
1015 * Prevent suspends and idle notifications from being carried
1016 * out after we have woken up the device.
1018 pm_runtime_get_noresume(dev);
1020 __pm_runtime_resume(dev, false);
1022 pm_runtime_put_noidle(dev);
1025 if (!dev->power.disable_depth++)
1026 __pm_runtime_barrier(dev);
1028 out:
1029 spin_unlock_irq(&dev->power.lock);
1031 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1034 * pm_runtime_enable - Enable run-time PM of a device.
1035 * @dev: Device to handle.
1037 void pm_runtime_enable(struct device *dev)
1039 unsigned long flags;
1041 spin_lock_irqsave(&dev->power.lock, flags);
1043 if (dev->power.disable_depth > 0)
1044 dev->power.disable_depth--;
1045 else
1046 dev_warn(dev, "Unbalanced %s!\n", __func__);
1048 spin_unlock_irqrestore(&dev->power.lock, flags);
1050 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1053 * pm_runtime_forbid - Block run-time PM of a device.
1054 * @dev: Device to handle.
1056 * Increase the device's usage count and clear its power.runtime_auto flag,
1057 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1058 * for it.
1060 void pm_runtime_forbid(struct device *dev)
1062 spin_lock_irq(&dev->power.lock);
1063 if (!dev->power.runtime_auto)
1064 goto out;
1066 dev->power.runtime_auto = false;
1067 atomic_inc(&dev->power.usage_count);
1068 __pm_runtime_resume(dev, false);
1070 out:
1071 spin_unlock_irq(&dev->power.lock);
1073 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1076 * pm_runtime_allow - Unblock run-time PM of a device.
1077 * @dev: Device to handle.
1079 * Decrease the device's usage count and set its power.runtime_auto flag.
1081 void pm_runtime_allow(struct device *dev)
1083 spin_lock_irq(&dev->power.lock);
1084 if (dev->power.runtime_auto)
1085 goto out;
1087 dev->power.runtime_auto = true;
1088 if (atomic_dec_and_test(&dev->power.usage_count))
1089 __pm_runtime_idle(dev);
1091 out:
1092 spin_unlock_irq(&dev->power.lock);
1094 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1097 * pm_runtime_init - Initialize run-time PM fields in given device object.
1098 * @dev: Device object to initialize.
1100 void pm_runtime_init(struct device *dev)
1102 spin_lock_init(&dev->power.lock);
1104 dev->power.runtime_status = RPM_SUSPENDED;
1105 dev->power.idle_notification = false;
1107 dev->power.disable_depth = 1;
1108 atomic_set(&dev->power.usage_count, 0);
1110 dev->power.runtime_error = 0;
1112 atomic_set(&dev->power.child_count, 0);
1113 pm_suspend_ignore_children(dev, false);
1114 dev->power.runtime_auto = true;
1116 dev->power.request_pending = false;
1117 dev->power.request = RPM_REQ_NONE;
1118 dev->power.deferred_resume = false;
1119 dev->power.accounting_timestamp = jiffies;
1120 INIT_WORK(&dev->power.work, pm_runtime_work);
1122 dev->power.timer_expires = 0;
1123 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1124 (unsigned long)dev);
1126 init_waitqueue_head(&dev->power.wait_queue);
1130 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1131 * @dev: Device object being removed from device hierarchy.
1133 void pm_runtime_remove(struct device *dev)
1135 __pm_runtime_disable(dev, false);
1137 /* Change the status back to 'suspended' to match the initial status. */
1138 if (dev->power.runtime_status == RPM_ACTIVE)
1139 pm_runtime_set_suspended(dev);