Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[linux-2.6.git] / drivers / base / power / runtime.c
blob268a35097578d94f78f4baef4d9cc8b3fa1c31cd
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 <trace/events/rpm.h>
14 #include "power.h"
16 static int rpm_resume(struct device *dev, int rpmflags);
17 static int rpm_suspend(struct device *dev, int rpmflags);
19 /**
20 * update_pm_runtime_accounting - Update the time accounting of power states
21 * @dev: Device to update the accounting for
23 * In order to be able to have time accounting of the various power states
24 * (as used by programs such as PowerTOP to show the effectiveness of runtime
25 * PM), we need to track the time spent in each state.
26 * update_pm_runtime_accounting must be called each time before the
27 * runtime_status field is updated, to account the time in the old state
28 * correctly.
30 void update_pm_runtime_accounting(struct device *dev)
32 unsigned long now = jiffies;
33 unsigned long delta;
35 delta = now - dev->power.accounting_timestamp;
37 dev->power.accounting_timestamp = now;
39 if (dev->power.disable_depth > 0)
40 return;
42 if (dev->power.runtime_status == RPM_SUSPENDED)
43 dev->power.suspended_jiffies += delta;
44 else
45 dev->power.active_jiffies += delta;
48 static void __update_runtime_status(struct device *dev, enum rpm_status status)
50 update_pm_runtime_accounting(dev);
51 dev->power.runtime_status = status;
54 /**
55 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
56 * @dev: Device to handle.
58 static void pm_runtime_deactivate_timer(struct device *dev)
60 if (dev->power.timer_expires > 0) {
61 del_timer(&dev->power.suspend_timer);
62 dev->power.timer_expires = 0;
66 /**
67 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
68 * @dev: Device to handle.
70 static void pm_runtime_cancel_pending(struct device *dev)
72 pm_runtime_deactivate_timer(dev);
74 * In case there's a request pending, make sure its work function will
75 * return without doing anything.
77 dev->power.request = RPM_REQ_NONE;
81 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
82 * @dev: Device to handle.
84 * Compute the autosuspend-delay expiration time based on the device's
85 * power.last_busy time. If the delay has already expired or is disabled
86 * (negative) or the power.use_autosuspend flag isn't set, return 0.
87 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
89 * This function may be called either with or without dev->power.lock held.
90 * Either way it can be racy, since power.last_busy may be updated at any time.
92 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
94 int autosuspend_delay;
95 long elapsed;
96 unsigned long last_busy;
97 unsigned long expires = 0;
99 if (!dev->power.use_autosuspend)
100 goto out;
102 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
103 if (autosuspend_delay < 0)
104 goto out;
106 last_busy = ACCESS_ONCE(dev->power.last_busy);
107 elapsed = jiffies - last_busy;
108 if (elapsed < 0)
109 goto out; /* jiffies has wrapped around. */
112 * If the autosuspend_delay is >= 1 second, align the timer by rounding
113 * up to the nearest second.
115 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
116 if (autosuspend_delay >= 1000)
117 expires = round_jiffies(expires);
118 expires += !expires;
119 if (elapsed >= expires - last_busy)
120 expires = 0; /* Already expired. */
122 out:
123 return expires;
125 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
127 static int dev_memalloc_noio(struct device *dev, void *data)
129 return dev->power.memalloc_noio;
133 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
134 * @dev: Device to handle.
135 * @enable: True for setting the flag and False for clearing the flag.
137 * Set the flag for all devices in the path from the device to the
138 * root device in the device tree if @enable is true, otherwise clear
139 * the flag for devices in the path whose siblings don't set the flag.
141 * The function should only be called by block device, or network
142 * device driver for solving the deadlock problem during runtime
143 * resume/suspend:
145 * If memory allocation with GFP_KERNEL is called inside runtime
146 * resume/suspend callback of any one of its ancestors(or the
147 * block device itself), the deadlock may be triggered inside the
148 * memory allocation since it might not complete until the block
149 * device becomes active and the involed page I/O finishes. The
150 * situation is pointed out first by Alan Stern. Network device
151 * are involved in iSCSI kind of situation.
153 * The lock of dev_hotplug_mutex is held in the function for handling
154 * hotplug race because pm_runtime_set_memalloc_noio() may be called
155 * in async probe().
157 * The function should be called between device_add() and device_del()
158 * on the affected device(block/network device).
160 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
162 static DEFINE_MUTEX(dev_hotplug_mutex);
164 mutex_lock(&dev_hotplug_mutex);
165 for (;;) {
166 bool enabled;
168 /* hold power lock since bitfield is not SMP-safe. */
169 spin_lock_irq(&dev->power.lock);
170 enabled = dev->power.memalloc_noio;
171 dev->power.memalloc_noio = enable;
172 spin_unlock_irq(&dev->power.lock);
175 * not need to enable ancestors any more if the device
176 * has been enabled.
178 if (enabled && enable)
179 break;
181 dev = dev->parent;
184 * clear flag of the parent device only if all the
185 * children don't set the flag because ancestor's
186 * flag was set by any one of the descendants.
188 if (!dev || (!enable &&
189 device_for_each_child(dev, NULL,
190 dev_memalloc_noio)))
191 break;
193 mutex_unlock(&dev_hotplug_mutex);
195 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
198 * rpm_check_suspend_allowed - Test whether a device may be suspended.
199 * @dev: Device to test.
201 static int rpm_check_suspend_allowed(struct device *dev)
203 int retval = 0;
205 if (dev->power.runtime_error)
206 retval = -EINVAL;
207 else if (dev->power.disable_depth > 0)
208 retval = -EACCES;
209 else if (atomic_read(&dev->power.usage_count) > 0)
210 retval = -EAGAIN;
211 else if (!pm_children_suspended(dev))
212 retval = -EBUSY;
214 /* Pending resume requests take precedence over suspends. */
215 else if ((dev->power.deferred_resume
216 && dev->power.runtime_status == RPM_SUSPENDING)
217 || (dev->power.request_pending
218 && dev->power.request == RPM_REQ_RESUME))
219 retval = -EAGAIN;
220 else if (__dev_pm_qos_read_value(dev) < 0)
221 retval = -EPERM;
222 else if (dev->power.runtime_status == RPM_SUSPENDED)
223 retval = 1;
225 return retval;
229 * __rpm_callback - Run a given runtime PM callback for a given device.
230 * @cb: Runtime PM callback to run.
231 * @dev: Device to run the callback for.
233 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
234 __releases(&dev->power.lock) __acquires(&dev->power.lock)
236 int retval;
238 if (dev->power.irq_safe)
239 spin_unlock(&dev->power.lock);
240 else
241 spin_unlock_irq(&dev->power.lock);
243 retval = cb(dev);
245 if (dev->power.irq_safe)
246 spin_lock(&dev->power.lock);
247 else
248 spin_lock_irq(&dev->power.lock);
250 return retval;
254 * rpm_idle - Notify device bus type if the device can be suspended.
255 * @dev: Device to notify the bus type about.
256 * @rpmflags: Flag bits.
258 * Check if the device's runtime PM status allows it to be suspended. If
259 * another idle notification has been started earlier, return immediately. If
260 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
261 * run the ->runtime_idle() callback directly.
263 * This function must be called under dev->power.lock with interrupts disabled.
265 static int rpm_idle(struct device *dev, int rpmflags)
267 int (*callback)(struct device *);
268 int retval;
270 trace_rpm_idle(dev, rpmflags);
271 retval = rpm_check_suspend_allowed(dev);
272 if (retval < 0)
273 ; /* Conditions are wrong. */
275 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
276 else if (dev->power.runtime_status != RPM_ACTIVE)
277 retval = -EAGAIN;
280 * Any pending request other than an idle notification takes
281 * precedence over us, except that the timer may be running.
283 else if (dev->power.request_pending &&
284 dev->power.request > RPM_REQ_IDLE)
285 retval = -EAGAIN;
287 /* Act as though RPM_NOWAIT is always set. */
288 else if (dev->power.idle_notification)
289 retval = -EINPROGRESS;
290 if (retval)
291 goto out;
293 /* Pending requests need to be canceled. */
294 dev->power.request = RPM_REQ_NONE;
296 if (dev->power.no_callbacks)
297 goto out;
299 /* Carry out an asynchronous or a synchronous idle notification. */
300 if (rpmflags & RPM_ASYNC) {
301 dev->power.request = RPM_REQ_IDLE;
302 if (!dev->power.request_pending) {
303 dev->power.request_pending = true;
304 queue_work(pm_wq, &dev->power.work);
306 trace_rpm_return_int(dev, _THIS_IP_, 0);
307 return 0;
310 dev->power.idle_notification = true;
312 if (dev->pm_domain)
313 callback = dev->pm_domain->ops.runtime_idle;
314 else if (dev->type && dev->type->pm)
315 callback = dev->type->pm->runtime_idle;
316 else if (dev->class && dev->class->pm)
317 callback = dev->class->pm->runtime_idle;
318 else if (dev->bus && dev->bus->pm)
319 callback = dev->bus->pm->runtime_idle;
320 else
321 callback = NULL;
323 if (!callback && dev->driver && dev->driver->pm)
324 callback = dev->driver->pm->runtime_idle;
326 if (callback)
327 retval = __rpm_callback(callback, dev);
329 dev->power.idle_notification = false;
330 wake_up_all(&dev->power.wait_queue);
332 out:
333 trace_rpm_return_int(dev, _THIS_IP_, retval);
334 return retval ? retval : rpm_suspend(dev, rpmflags);
338 * rpm_callback - Run a given runtime PM callback for a given device.
339 * @cb: Runtime PM callback to run.
340 * @dev: Device to run the callback for.
342 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
344 int retval;
346 if (!cb)
347 return -ENOSYS;
349 if (dev->power.memalloc_noio) {
350 unsigned int noio_flag;
353 * Deadlock might be caused if memory allocation with
354 * GFP_KERNEL happens inside runtime_suspend and
355 * runtime_resume callbacks of one block device's
356 * ancestor or the block device itself. Network
357 * device might be thought as part of iSCSI block
358 * device, so network device and its ancestor should
359 * be marked as memalloc_noio too.
361 noio_flag = memalloc_noio_save();
362 retval = __rpm_callback(cb, dev);
363 memalloc_noio_restore(noio_flag);
364 } else {
365 retval = __rpm_callback(cb, dev);
368 dev->power.runtime_error = retval;
369 return retval != -EACCES ? retval : -EIO;
373 * rpm_suspend - Carry out runtime suspend of given device.
374 * @dev: Device to suspend.
375 * @rpmflags: Flag bits.
377 * Check if the device's runtime PM status allows it to be suspended.
378 * Cancel a pending idle notification, autosuspend or suspend. If
379 * another suspend has been started earlier, either return immediately
380 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
381 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
382 * otherwise run the ->runtime_suspend() callback directly. When
383 * ->runtime_suspend succeeded, if a deferred resume was requested while
384 * the callback was running then carry it out, otherwise send an idle
385 * notification for its parent (if the suspend succeeded and both
386 * ignore_children of parent->power and irq_safe of dev->power are not set).
387 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
388 * flag is set and the next autosuspend-delay expiration time is in the
389 * future, schedule another autosuspend attempt.
391 * This function must be called under dev->power.lock with interrupts disabled.
393 static int rpm_suspend(struct device *dev, int rpmflags)
394 __releases(&dev->power.lock) __acquires(&dev->power.lock)
396 int (*callback)(struct device *);
397 struct device *parent = NULL;
398 int retval;
400 trace_rpm_suspend(dev, rpmflags);
402 repeat:
403 retval = rpm_check_suspend_allowed(dev);
405 if (retval < 0)
406 ; /* Conditions are wrong. */
408 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
409 else if (dev->power.runtime_status == RPM_RESUMING &&
410 !(rpmflags & RPM_ASYNC))
411 retval = -EAGAIN;
412 if (retval)
413 goto out;
415 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
416 if ((rpmflags & RPM_AUTO)
417 && dev->power.runtime_status != RPM_SUSPENDING) {
418 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
420 if (expires != 0) {
421 /* Pending requests need to be canceled. */
422 dev->power.request = RPM_REQ_NONE;
425 * Optimization: If the timer is already running and is
426 * set to expire at or before the autosuspend delay,
427 * avoid the overhead of resetting it. Just let it
428 * expire; pm_suspend_timer_fn() will take care of the
429 * rest.
431 if (!(dev->power.timer_expires && time_before_eq(
432 dev->power.timer_expires, expires))) {
433 dev->power.timer_expires = expires;
434 mod_timer(&dev->power.suspend_timer, expires);
436 dev->power.timer_autosuspends = 1;
437 goto out;
441 /* Other scheduled or pending requests need to be canceled. */
442 pm_runtime_cancel_pending(dev);
444 if (dev->power.runtime_status == RPM_SUSPENDING) {
445 DEFINE_WAIT(wait);
447 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
448 retval = -EINPROGRESS;
449 goto out;
452 if (dev->power.irq_safe) {
453 spin_unlock(&dev->power.lock);
455 cpu_relax();
457 spin_lock(&dev->power.lock);
458 goto repeat;
461 /* Wait for the other suspend running in parallel with us. */
462 for (;;) {
463 prepare_to_wait(&dev->power.wait_queue, &wait,
464 TASK_UNINTERRUPTIBLE);
465 if (dev->power.runtime_status != RPM_SUSPENDING)
466 break;
468 spin_unlock_irq(&dev->power.lock);
470 schedule();
472 spin_lock_irq(&dev->power.lock);
474 finish_wait(&dev->power.wait_queue, &wait);
475 goto repeat;
478 if (dev->power.no_callbacks)
479 goto no_callback; /* Assume success. */
481 /* Carry out an asynchronous or a synchronous suspend. */
482 if (rpmflags & RPM_ASYNC) {
483 dev->power.request = (rpmflags & RPM_AUTO) ?
484 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
485 if (!dev->power.request_pending) {
486 dev->power.request_pending = true;
487 queue_work(pm_wq, &dev->power.work);
489 goto out;
492 __update_runtime_status(dev, RPM_SUSPENDING);
494 if (dev->pm_domain)
495 callback = dev->pm_domain->ops.runtime_suspend;
496 else if (dev->type && dev->type->pm)
497 callback = dev->type->pm->runtime_suspend;
498 else if (dev->class && dev->class->pm)
499 callback = dev->class->pm->runtime_suspend;
500 else if (dev->bus && dev->bus->pm)
501 callback = dev->bus->pm->runtime_suspend;
502 else
503 callback = NULL;
505 if (!callback && dev->driver && dev->driver->pm)
506 callback = dev->driver->pm->runtime_suspend;
508 retval = rpm_callback(callback, dev);
509 if (retval)
510 goto fail;
512 no_callback:
513 __update_runtime_status(dev, RPM_SUSPENDED);
514 pm_runtime_deactivate_timer(dev);
516 if (dev->parent) {
517 parent = dev->parent;
518 atomic_add_unless(&parent->power.child_count, -1, 0);
520 wake_up_all(&dev->power.wait_queue);
522 if (dev->power.deferred_resume) {
523 dev->power.deferred_resume = false;
524 rpm_resume(dev, 0);
525 retval = -EAGAIN;
526 goto out;
529 /* Maybe the parent is now able to suspend. */
530 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
531 spin_unlock(&dev->power.lock);
533 spin_lock(&parent->power.lock);
534 rpm_idle(parent, RPM_ASYNC);
535 spin_unlock(&parent->power.lock);
537 spin_lock(&dev->power.lock);
540 out:
541 trace_rpm_return_int(dev, _THIS_IP_, retval);
543 return retval;
545 fail:
546 __update_runtime_status(dev, RPM_ACTIVE);
547 dev->power.deferred_resume = false;
548 wake_up_all(&dev->power.wait_queue);
550 if (retval == -EAGAIN || retval == -EBUSY) {
551 dev->power.runtime_error = 0;
554 * If the callback routine failed an autosuspend, and
555 * if the last_busy time has been updated so that there
556 * is a new autosuspend expiration time, automatically
557 * reschedule another autosuspend.
559 if ((rpmflags & RPM_AUTO) &&
560 pm_runtime_autosuspend_expiration(dev) != 0)
561 goto repeat;
562 } else {
563 pm_runtime_cancel_pending(dev);
565 goto out;
569 * rpm_resume - Carry out runtime resume of given device.
570 * @dev: Device to resume.
571 * @rpmflags: Flag bits.
573 * Check if the device's runtime PM status allows it to be resumed. Cancel
574 * any scheduled or pending requests. If another resume has been started
575 * earlier, either return immediately or wait for it to finish, depending on the
576 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
577 * parallel with this function, either tell the other process to resume after
578 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
579 * flag is set then queue a resume request; otherwise run the
580 * ->runtime_resume() callback directly. Queue an idle notification for the
581 * device if the resume succeeded.
583 * This function must be called under dev->power.lock with interrupts disabled.
585 static int rpm_resume(struct device *dev, int rpmflags)
586 __releases(&dev->power.lock) __acquires(&dev->power.lock)
588 int (*callback)(struct device *);
589 struct device *parent = NULL;
590 int retval = 0;
592 trace_rpm_resume(dev, rpmflags);
594 repeat:
595 if (dev->power.runtime_error)
596 retval = -EINVAL;
597 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
598 && dev->power.runtime_status == RPM_ACTIVE)
599 retval = 1;
600 else if (dev->power.disable_depth > 0)
601 retval = -EACCES;
602 if (retval)
603 goto out;
606 * Other scheduled or pending requests need to be canceled. Small
607 * optimization: If an autosuspend timer is running, leave it running
608 * rather than cancelling it now only to restart it again in the near
609 * future.
611 dev->power.request = RPM_REQ_NONE;
612 if (!dev->power.timer_autosuspends)
613 pm_runtime_deactivate_timer(dev);
615 if (dev->power.runtime_status == RPM_ACTIVE) {
616 retval = 1;
617 goto out;
620 if (dev->power.runtime_status == RPM_RESUMING
621 || dev->power.runtime_status == RPM_SUSPENDING) {
622 DEFINE_WAIT(wait);
624 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
625 if (dev->power.runtime_status == RPM_SUSPENDING)
626 dev->power.deferred_resume = true;
627 else
628 retval = -EINPROGRESS;
629 goto out;
632 if (dev->power.irq_safe) {
633 spin_unlock(&dev->power.lock);
635 cpu_relax();
637 spin_lock(&dev->power.lock);
638 goto repeat;
641 /* Wait for the operation carried out in parallel with us. */
642 for (;;) {
643 prepare_to_wait(&dev->power.wait_queue, &wait,
644 TASK_UNINTERRUPTIBLE);
645 if (dev->power.runtime_status != RPM_RESUMING
646 && dev->power.runtime_status != RPM_SUSPENDING)
647 break;
649 spin_unlock_irq(&dev->power.lock);
651 schedule();
653 spin_lock_irq(&dev->power.lock);
655 finish_wait(&dev->power.wait_queue, &wait);
656 goto repeat;
660 * See if we can skip waking up the parent. This is safe only if
661 * power.no_callbacks is set, because otherwise we don't know whether
662 * the resume will actually succeed.
664 if (dev->power.no_callbacks && !parent && dev->parent) {
665 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
666 if (dev->parent->power.disable_depth > 0
667 || dev->parent->power.ignore_children
668 || dev->parent->power.runtime_status == RPM_ACTIVE) {
669 atomic_inc(&dev->parent->power.child_count);
670 spin_unlock(&dev->parent->power.lock);
671 retval = 1;
672 goto no_callback; /* Assume success. */
674 spin_unlock(&dev->parent->power.lock);
677 /* Carry out an asynchronous or a synchronous resume. */
678 if (rpmflags & RPM_ASYNC) {
679 dev->power.request = RPM_REQ_RESUME;
680 if (!dev->power.request_pending) {
681 dev->power.request_pending = true;
682 queue_work(pm_wq, &dev->power.work);
684 retval = 0;
685 goto out;
688 if (!parent && dev->parent) {
690 * Increment the parent's usage counter and resume it if
691 * necessary. Not needed if dev is irq-safe; then the
692 * parent is permanently resumed.
694 parent = dev->parent;
695 if (dev->power.irq_safe)
696 goto skip_parent;
697 spin_unlock(&dev->power.lock);
699 pm_runtime_get_noresume(parent);
701 spin_lock(&parent->power.lock);
703 * We can resume if the parent's runtime PM is disabled or it
704 * is set to ignore children.
706 if (!parent->power.disable_depth
707 && !parent->power.ignore_children) {
708 rpm_resume(parent, 0);
709 if (parent->power.runtime_status != RPM_ACTIVE)
710 retval = -EBUSY;
712 spin_unlock(&parent->power.lock);
714 spin_lock(&dev->power.lock);
715 if (retval)
716 goto out;
717 goto repeat;
719 skip_parent:
721 if (dev->power.no_callbacks)
722 goto no_callback; /* Assume success. */
724 __update_runtime_status(dev, RPM_RESUMING);
726 if (dev->pm_domain)
727 callback = dev->pm_domain->ops.runtime_resume;
728 else if (dev->type && dev->type->pm)
729 callback = dev->type->pm->runtime_resume;
730 else if (dev->class && dev->class->pm)
731 callback = dev->class->pm->runtime_resume;
732 else if (dev->bus && dev->bus->pm)
733 callback = dev->bus->pm->runtime_resume;
734 else
735 callback = NULL;
737 if (!callback && dev->driver && dev->driver->pm)
738 callback = dev->driver->pm->runtime_resume;
740 retval = rpm_callback(callback, dev);
741 if (retval) {
742 __update_runtime_status(dev, RPM_SUSPENDED);
743 pm_runtime_cancel_pending(dev);
744 } else {
745 no_callback:
746 __update_runtime_status(dev, RPM_ACTIVE);
747 if (parent)
748 atomic_inc(&parent->power.child_count);
750 wake_up_all(&dev->power.wait_queue);
752 if (retval >= 0)
753 rpm_idle(dev, RPM_ASYNC);
755 out:
756 if (parent && !dev->power.irq_safe) {
757 spin_unlock_irq(&dev->power.lock);
759 pm_runtime_put(parent);
761 spin_lock_irq(&dev->power.lock);
764 trace_rpm_return_int(dev, _THIS_IP_, retval);
766 return retval;
770 * pm_runtime_work - Universal runtime PM work function.
771 * @work: Work structure used for scheduling the execution of this function.
773 * Use @work to get the device object the work is to be done for, determine what
774 * is to be done and execute the appropriate runtime PM function.
776 static void pm_runtime_work(struct work_struct *work)
778 struct device *dev = container_of(work, struct device, power.work);
779 enum rpm_request req;
781 spin_lock_irq(&dev->power.lock);
783 if (!dev->power.request_pending)
784 goto out;
786 req = dev->power.request;
787 dev->power.request = RPM_REQ_NONE;
788 dev->power.request_pending = false;
790 switch (req) {
791 case RPM_REQ_NONE:
792 break;
793 case RPM_REQ_IDLE:
794 rpm_idle(dev, RPM_NOWAIT);
795 break;
796 case RPM_REQ_SUSPEND:
797 rpm_suspend(dev, RPM_NOWAIT);
798 break;
799 case RPM_REQ_AUTOSUSPEND:
800 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
801 break;
802 case RPM_REQ_RESUME:
803 rpm_resume(dev, RPM_NOWAIT);
804 break;
807 out:
808 spin_unlock_irq(&dev->power.lock);
812 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
813 * @data: Device pointer passed by pm_schedule_suspend().
815 * Check if the time is right and queue a suspend request.
817 static void pm_suspend_timer_fn(unsigned long data)
819 struct device *dev = (struct device *)data;
820 unsigned long flags;
821 unsigned long expires;
823 spin_lock_irqsave(&dev->power.lock, flags);
825 expires = dev->power.timer_expires;
826 /* If 'expire' is after 'jiffies' we've been called too early. */
827 if (expires > 0 && !time_after(expires, jiffies)) {
828 dev->power.timer_expires = 0;
829 rpm_suspend(dev, dev->power.timer_autosuspends ?
830 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
833 spin_unlock_irqrestore(&dev->power.lock, flags);
837 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
838 * @dev: Device to suspend.
839 * @delay: Time to wait before submitting a suspend request, in milliseconds.
841 int pm_schedule_suspend(struct device *dev, unsigned int delay)
843 unsigned long flags;
844 int retval;
846 spin_lock_irqsave(&dev->power.lock, flags);
848 if (!delay) {
849 retval = rpm_suspend(dev, RPM_ASYNC);
850 goto out;
853 retval = rpm_check_suspend_allowed(dev);
854 if (retval)
855 goto out;
857 /* Other scheduled or pending requests need to be canceled. */
858 pm_runtime_cancel_pending(dev);
860 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
861 dev->power.timer_expires += !dev->power.timer_expires;
862 dev->power.timer_autosuspends = 0;
863 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
865 out:
866 spin_unlock_irqrestore(&dev->power.lock, flags);
868 return retval;
870 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
873 * __pm_runtime_idle - Entry point for runtime idle operations.
874 * @dev: Device to send idle notification for.
875 * @rpmflags: Flag bits.
877 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
878 * return immediately if it is larger than zero. Then carry out an idle
879 * notification, either synchronous or asynchronous.
881 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
882 * or if pm_runtime_irq_safe() has been called.
884 int __pm_runtime_idle(struct device *dev, int rpmflags)
886 unsigned long flags;
887 int retval;
889 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
891 if (rpmflags & RPM_GET_PUT) {
892 if (!atomic_dec_and_test(&dev->power.usage_count))
893 return 0;
896 spin_lock_irqsave(&dev->power.lock, flags);
897 retval = rpm_idle(dev, rpmflags);
898 spin_unlock_irqrestore(&dev->power.lock, flags);
900 return retval;
902 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
905 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
906 * @dev: Device to suspend.
907 * @rpmflags: Flag bits.
909 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
910 * return immediately if it is larger than zero. Then carry out a suspend,
911 * either synchronous or asynchronous.
913 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
914 * or if pm_runtime_irq_safe() has been called.
916 int __pm_runtime_suspend(struct device *dev, int rpmflags)
918 unsigned long flags;
919 int retval;
921 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
923 if (rpmflags & RPM_GET_PUT) {
924 if (!atomic_dec_and_test(&dev->power.usage_count))
925 return 0;
928 spin_lock_irqsave(&dev->power.lock, flags);
929 retval = rpm_suspend(dev, rpmflags);
930 spin_unlock_irqrestore(&dev->power.lock, flags);
932 return retval;
934 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
937 * __pm_runtime_resume - Entry point for runtime resume operations.
938 * @dev: Device to resume.
939 * @rpmflags: Flag bits.
941 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
942 * carry out a resume, either synchronous or asynchronous.
944 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
945 * or if pm_runtime_irq_safe() has been called.
947 int __pm_runtime_resume(struct device *dev, int rpmflags)
949 unsigned long flags;
950 int retval;
952 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
954 if (rpmflags & RPM_GET_PUT)
955 atomic_inc(&dev->power.usage_count);
957 spin_lock_irqsave(&dev->power.lock, flags);
958 retval = rpm_resume(dev, rpmflags);
959 spin_unlock_irqrestore(&dev->power.lock, flags);
961 return retval;
963 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
966 * __pm_runtime_set_status - Set runtime PM status of a device.
967 * @dev: Device to handle.
968 * @status: New runtime PM status of the device.
970 * If runtime PM of the device is disabled or its power.runtime_error field is
971 * different from zero, the status may be changed either to RPM_ACTIVE, or to
972 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
973 * However, if the device has a parent and the parent is not active, and the
974 * parent's power.ignore_children flag is unset, the device's status cannot be
975 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
977 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
978 * and the device parent's counter of unsuspended children is modified to
979 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
980 * notification request for the parent is submitted.
982 int __pm_runtime_set_status(struct device *dev, unsigned int status)
984 struct device *parent = dev->parent;
985 unsigned long flags;
986 bool notify_parent = false;
987 int error = 0;
989 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
990 return -EINVAL;
992 spin_lock_irqsave(&dev->power.lock, flags);
994 if (!dev->power.runtime_error && !dev->power.disable_depth) {
995 error = -EAGAIN;
996 goto out;
999 if (dev->power.runtime_status == status)
1000 goto out_set;
1002 if (status == RPM_SUSPENDED) {
1003 /* It always is possible to set the status to 'suspended'. */
1004 if (parent) {
1005 atomic_add_unless(&parent->power.child_count, -1, 0);
1006 notify_parent = !parent->power.ignore_children;
1008 goto out_set;
1011 if (parent) {
1012 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1015 * It is invalid to put an active child under a parent that is
1016 * not active, has runtime PM enabled and the
1017 * 'power.ignore_children' flag unset.
1019 if (!parent->power.disable_depth
1020 && !parent->power.ignore_children
1021 && parent->power.runtime_status != RPM_ACTIVE)
1022 error = -EBUSY;
1023 else if (dev->power.runtime_status == RPM_SUSPENDED)
1024 atomic_inc(&parent->power.child_count);
1026 spin_unlock(&parent->power.lock);
1028 if (error)
1029 goto out;
1032 out_set:
1033 __update_runtime_status(dev, status);
1034 dev->power.runtime_error = 0;
1035 out:
1036 spin_unlock_irqrestore(&dev->power.lock, flags);
1038 if (notify_parent)
1039 pm_request_idle(parent);
1041 return error;
1043 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1046 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1047 * @dev: Device to handle.
1049 * Flush all pending requests for the device from pm_wq and wait for all
1050 * runtime PM operations involving the device in progress to complete.
1052 * Should be called under dev->power.lock with interrupts disabled.
1054 static void __pm_runtime_barrier(struct device *dev)
1056 pm_runtime_deactivate_timer(dev);
1058 if (dev->power.request_pending) {
1059 dev->power.request = RPM_REQ_NONE;
1060 spin_unlock_irq(&dev->power.lock);
1062 cancel_work_sync(&dev->power.work);
1064 spin_lock_irq(&dev->power.lock);
1065 dev->power.request_pending = false;
1068 if (dev->power.runtime_status == RPM_SUSPENDING
1069 || dev->power.runtime_status == RPM_RESUMING
1070 || dev->power.idle_notification) {
1071 DEFINE_WAIT(wait);
1073 /* Suspend, wake-up or idle notification in progress. */
1074 for (;;) {
1075 prepare_to_wait(&dev->power.wait_queue, &wait,
1076 TASK_UNINTERRUPTIBLE);
1077 if (dev->power.runtime_status != RPM_SUSPENDING
1078 && dev->power.runtime_status != RPM_RESUMING
1079 && !dev->power.idle_notification)
1080 break;
1081 spin_unlock_irq(&dev->power.lock);
1083 schedule();
1085 spin_lock_irq(&dev->power.lock);
1087 finish_wait(&dev->power.wait_queue, &wait);
1092 * pm_runtime_barrier - Flush pending requests and wait for completions.
1093 * @dev: Device to handle.
1095 * Prevent the device from being suspended by incrementing its usage counter and
1096 * if there's a pending resume request for the device, wake the device up.
1097 * Next, make sure that all pending requests for the device have been flushed
1098 * from pm_wq and wait for all runtime PM operations involving the device in
1099 * progress to complete.
1101 * Return value:
1102 * 1, if there was a resume request pending and the device had to be woken up,
1103 * 0, otherwise
1105 int pm_runtime_barrier(struct device *dev)
1107 int retval = 0;
1109 pm_runtime_get_noresume(dev);
1110 spin_lock_irq(&dev->power.lock);
1112 if (dev->power.request_pending
1113 && dev->power.request == RPM_REQ_RESUME) {
1114 rpm_resume(dev, 0);
1115 retval = 1;
1118 __pm_runtime_barrier(dev);
1120 spin_unlock_irq(&dev->power.lock);
1121 pm_runtime_put_noidle(dev);
1123 return retval;
1125 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1128 * __pm_runtime_disable - Disable runtime PM of a device.
1129 * @dev: Device to handle.
1130 * @check_resume: If set, check if there's a resume request for the device.
1132 * Increment power.disable_depth for the device and if was zero previously,
1133 * cancel all pending runtime PM requests for the device and wait for all
1134 * operations in progress to complete. The device can be either active or
1135 * suspended after its runtime PM has been disabled.
1137 * If @check_resume is set and there's a resume request pending when
1138 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1139 * function will wake up the device before disabling its runtime PM.
1141 void __pm_runtime_disable(struct device *dev, bool check_resume)
1143 spin_lock_irq(&dev->power.lock);
1145 if (dev->power.disable_depth > 0) {
1146 dev->power.disable_depth++;
1147 goto out;
1151 * Wake up the device if there's a resume request pending, because that
1152 * means there probably is some I/O to process and disabling runtime PM
1153 * shouldn't prevent the device from processing the I/O.
1155 if (check_resume && dev->power.request_pending
1156 && dev->power.request == RPM_REQ_RESUME) {
1158 * Prevent suspends and idle notifications from being carried
1159 * out after we have woken up the device.
1161 pm_runtime_get_noresume(dev);
1163 rpm_resume(dev, 0);
1165 pm_runtime_put_noidle(dev);
1168 if (!dev->power.disable_depth++)
1169 __pm_runtime_barrier(dev);
1171 out:
1172 spin_unlock_irq(&dev->power.lock);
1174 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1177 * pm_runtime_enable - Enable runtime PM of a device.
1178 * @dev: Device to handle.
1180 void pm_runtime_enable(struct device *dev)
1182 unsigned long flags;
1184 spin_lock_irqsave(&dev->power.lock, flags);
1186 if (dev->power.disable_depth > 0)
1187 dev->power.disable_depth--;
1188 else
1189 dev_warn(dev, "Unbalanced %s!\n", __func__);
1191 spin_unlock_irqrestore(&dev->power.lock, flags);
1193 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1196 * pm_runtime_forbid - Block runtime PM of a device.
1197 * @dev: Device to handle.
1199 * Increase the device's usage count and clear its power.runtime_auto flag,
1200 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1201 * for it.
1203 void pm_runtime_forbid(struct device *dev)
1205 spin_lock_irq(&dev->power.lock);
1206 if (!dev->power.runtime_auto)
1207 goto out;
1209 dev->power.runtime_auto = false;
1210 atomic_inc(&dev->power.usage_count);
1211 rpm_resume(dev, 0);
1213 out:
1214 spin_unlock_irq(&dev->power.lock);
1216 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1219 * pm_runtime_allow - Unblock runtime PM of a device.
1220 * @dev: Device to handle.
1222 * Decrease the device's usage count and set its power.runtime_auto flag.
1224 void pm_runtime_allow(struct device *dev)
1226 spin_lock_irq(&dev->power.lock);
1227 if (dev->power.runtime_auto)
1228 goto out;
1230 dev->power.runtime_auto = true;
1231 if (atomic_dec_and_test(&dev->power.usage_count))
1232 rpm_idle(dev, RPM_AUTO);
1234 out:
1235 spin_unlock_irq(&dev->power.lock);
1237 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1240 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1241 * @dev: Device to handle.
1243 * Set the power.no_callbacks flag, which tells the PM core that this
1244 * device is power-managed through its parent and has no runtime PM
1245 * callbacks of its own. The runtime sysfs attributes will be removed.
1247 void pm_runtime_no_callbacks(struct device *dev)
1249 spin_lock_irq(&dev->power.lock);
1250 dev->power.no_callbacks = 1;
1251 spin_unlock_irq(&dev->power.lock);
1252 if (device_is_registered(dev))
1253 rpm_sysfs_remove(dev);
1255 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1258 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1259 * @dev: Device to handle
1261 * Set the power.irq_safe flag, which tells the PM core that the
1262 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1263 * always be invoked with the spinlock held and interrupts disabled. It also
1264 * causes the parent's usage counter to be permanently incremented, preventing
1265 * the parent from runtime suspending -- otherwise an irq-safe child might have
1266 * to wait for a non-irq-safe parent.
1268 void pm_runtime_irq_safe(struct device *dev)
1270 if (dev->parent)
1271 pm_runtime_get_sync(dev->parent);
1272 spin_lock_irq(&dev->power.lock);
1273 dev->power.irq_safe = 1;
1274 spin_unlock_irq(&dev->power.lock);
1276 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1279 * update_autosuspend - Handle a change to a device's autosuspend settings.
1280 * @dev: Device to handle.
1281 * @old_delay: The former autosuspend_delay value.
1282 * @old_use: The former use_autosuspend value.
1284 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1285 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1287 * This function must be called under dev->power.lock with interrupts disabled.
1289 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1291 int delay = dev->power.autosuspend_delay;
1293 /* Should runtime suspend be prevented now? */
1294 if (dev->power.use_autosuspend && delay < 0) {
1296 /* If it used to be allowed then prevent it. */
1297 if (!old_use || old_delay >= 0) {
1298 atomic_inc(&dev->power.usage_count);
1299 rpm_resume(dev, 0);
1303 /* Runtime suspend should be allowed now. */
1304 else {
1306 /* If it used to be prevented then allow it. */
1307 if (old_use && old_delay < 0)
1308 atomic_dec(&dev->power.usage_count);
1310 /* Maybe we can autosuspend now. */
1311 rpm_idle(dev, RPM_AUTO);
1316 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1317 * @dev: Device to handle.
1318 * @delay: Value of the new delay in milliseconds.
1320 * Set the device's power.autosuspend_delay value. If it changes to negative
1321 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1322 * changes the other way, allow runtime suspends.
1324 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1326 int old_delay, old_use;
1328 spin_lock_irq(&dev->power.lock);
1329 old_delay = dev->power.autosuspend_delay;
1330 old_use = dev->power.use_autosuspend;
1331 dev->power.autosuspend_delay = delay;
1332 update_autosuspend(dev, old_delay, old_use);
1333 spin_unlock_irq(&dev->power.lock);
1335 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1338 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1339 * @dev: Device to handle.
1340 * @use: New value for use_autosuspend.
1342 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1343 * suspends as needed.
1345 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1347 int old_delay, old_use;
1349 spin_lock_irq(&dev->power.lock);
1350 old_delay = dev->power.autosuspend_delay;
1351 old_use = dev->power.use_autosuspend;
1352 dev->power.use_autosuspend = use;
1353 update_autosuspend(dev, old_delay, old_use);
1354 spin_unlock_irq(&dev->power.lock);
1356 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1359 * pm_runtime_init - Initialize runtime PM fields in given device object.
1360 * @dev: Device object to initialize.
1362 void pm_runtime_init(struct device *dev)
1364 dev->power.runtime_status = RPM_SUSPENDED;
1365 dev->power.idle_notification = false;
1367 dev->power.disable_depth = 1;
1368 atomic_set(&dev->power.usage_count, 0);
1370 dev->power.runtime_error = 0;
1372 atomic_set(&dev->power.child_count, 0);
1373 pm_suspend_ignore_children(dev, false);
1374 dev->power.runtime_auto = true;
1376 dev->power.request_pending = false;
1377 dev->power.request = RPM_REQ_NONE;
1378 dev->power.deferred_resume = false;
1379 dev->power.accounting_timestamp = jiffies;
1380 INIT_WORK(&dev->power.work, pm_runtime_work);
1382 dev->power.timer_expires = 0;
1383 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1384 (unsigned long)dev);
1386 init_waitqueue_head(&dev->power.wait_queue);
1390 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1391 * @dev: Device object being removed from device hierarchy.
1393 void pm_runtime_remove(struct device *dev)
1395 __pm_runtime_disable(dev, false);
1397 /* Change the status back to 'suspended' to match the initial status. */
1398 if (dev->power.runtime_status == RPM_ACTIVE)
1399 pm_runtime_set_suspended(dev);
1400 if (dev->power.irq_safe && dev->parent)
1401 pm_runtime_put(dev->parent);