Lines Matching +full:power +full:-

1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/runtime.c - Helper functions for device runtime PM
18 #include "power.h"
27 if (dev->pm_domain) in __rpm_get_callback()
28 ops = &dev->pm_domain->ops; in __rpm_get_callback()
29 else if (dev->type && dev->type->pm) in __rpm_get_callback()
30 ops = dev->type->pm; in __rpm_get_callback()
31 else if (dev->class && dev->class->pm) in __rpm_get_callback()
32 ops = dev->class->pm; in __rpm_get_callback()
33 else if (dev->bus && dev->bus->pm) in __rpm_get_callback()
34 ops = dev->bus->pm; in __rpm_get_callback()
43 if (!cb && dev->driver && dev->driver->pm) in __rpm_get_callback()
44 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset); in __rpm_get_callback()
56 * update_pm_runtime_accounting - Update the time accounting of power states
59 * In order to be able to have time accounting of the various power states
70 if (dev->power.disable_depth > 0) in update_pm_runtime_accounting()
73 last = dev->power.accounting_timestamp; in update_pm_runtime_accounting()
76 dev->power.accounting_timestamp = now; in update_pm_runtime_accounting()
86 delta = now - last; in update_pm_runtime_accounting()
88 if (dev->power.runtime_status == RPM_SUSPENDED) in update_pm_runtime_accounting()
89 dev->power.suspended_time += delta; in update_pm_runtime_accounting()
91 dev->power.active_time += delta; in update_pm_runtime_accounting()
98 dev->power.runtime_status = status; in __update_runtime_status()
106 spin_lock_irqsave(&dev->power.lock, flags); in rpm_get_accounted_time()
109 time = suspended ? dev->power.suspended_time : dev->power.active_time; in rpm_get_accounted_time()
111 spin_unlock_irqrestore(&dev->power.lock, flags); in rpm_get_accounted_time()
128 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
133 if (dev->power.timer_expires > 0) { in pm_runtime_deactivate_timer()
134 hrtimer_try_to_cancel(&dev->power.suspend_timer); in pm_runtime_deactivate_timer()
135 dev->power.timer_expires = 0; in pm_runtime_deactivate_timer()
140 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
150 dev->power.request = RPM_REQ_NONE; in pm_runtime_cancel_pending()
154 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
157 * Compute the autosuspend-delay expiration time based on the device's
158 * power.last_busy time. If the delay has already expired or is disabled
159 * (negative) or the power.use_autosuspend flag isn't set, return 0.
162 * This function may be called either with or without dev->power.lock held.
163 * Either way it can be racy, since power.last_busy may be updated at any time.
170 if (!dev->power.use_autosuspend) in pm_runtime_autosuspend_expiration()
173 autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay); in pm_runtime_autosuspend_expiration()
177 expires = READ_ONCE(dev->power.last_busy); in pm_runtime_autosuspend_expiration()
188 return dev->power.memalloc_noio; in dev_memalloc_noio()
192 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
227 /* hold power lock since bitfield is not SMP-safe. */ in pm_runtime_set_memalloc_noio()
228 spin_lock_irq(&dev->power.lock); in pm_runtime_set_memalloc_noio()
229 enabled = dev->power.memalloc_noio; in pm_runtime_set_memalloc_noio()
230 dev->power.memalloc_noio = enable; in pm_runtime_set_memalloc_noio()
231 spin_unlock_irq(&dev->power.lock); in pm_runtime_set_memalloc_noio()
240 dev = dev->parent; in pm_runtime_set_memalloc_noio()
256 * rpm_check_suspend_allowed - Test whether a device may be suspended.
263 if (dev->power.runtime_error) in rpm_check_suspend_allowed()
264 retval = -EINVAL; in rpm_check_suspend_allowed()
265 else if (dev->power.disable_depth > 0) in rpm_check_suspend_allowed()
266 retval = -EACCES; in rpm_check_suspend_allowed()
267 else if (atomic_read(&dev->power.usage_count)) in rpm_check_suspend_allowed()
268 retval = -EAGAIN; in rpm_check_suspend_allowed()
269 else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count)) in rpm_check_suspend_allowed()
270 retval = -EBUSY; in rpm_check_suspend_allowed()
273 else if ((dev->power.deferred_resume && in rpm_check_suspend_allowed()
274 dev->power.runtime_status == RPM_SUSPENDING) || in rpm_check_suspend_allowed()
275 (dev->power.request_pending && dev->power.request == RPM_REQ_RESUME)) in rpm_check_suspend_allowed()
276 retval = -EAGAIN; in rpm_check_suspend_allowed()
278 retval = -EPERM; in rpm_check_suspend_allowed()
279 else if (dev->power.runtime_status == RPM_SUSPENDED) in rpm_check_suspend_allowed()
289 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, in rpm_get_suppliers()
293 if (!(link->flags & DL_FLAG_PM_RUNTIME)) in rpm_get_suppliers()
296 retval = pm_runtime_get_sync(link->supplier); in rpm_get_suppliers()
298 if (retval < 0 && retval != -EACCES) { in rpm_get_suppliers()
299 pm_runtime_put_noidle(link->supplier); in rpm_get_suppliers()
302 refcount_inc(&link->rpm_active); in rpm_get_suppliers()
308 * pm_runtime_release_supplier - Drop references to device link's supplier.
315 struct device *supplier = link->supplier; in pm_runtime_release_supplier()
318 * The additional power.usage_count check is a safety net in case in pm_runtime_release_supplier()
323 while (refcount_dec_not_one(&link->rpm_active) && in pm_runtime_release_supplier()
324 atomic_read(&supplier->power.usage_count) > 0) in pm_runtime_release_supplier()
332 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, in __rpm_put_suppliers()
336 pm_request_idle(link->supplier); in __rpm_put_suppliers()
350 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, in rpm_suspend_suppliers()
352 pm_request_idle(link->supplier); in rpm_suspend_suppliers()
358 * __rpm_callback - Run a given runtime PM callback for a given device.
363 __releases(&dev->power.lock) __acquires(&dev->power.lock) in __rpm_callback()
366 bool use_links = dev->power.links_count > 0; in __rpm_callback()
368 if (dev->power.irq_safe) { in __rpm_callback()
369 spin_unlock(&dev->power.lock); in __rpm_callback()
371 spin_unlock_irq(&dev->power.lock); in __rpm_callback()
380 if (use_links && dev->power.runtime_status == RPM_RESUMING) { in __rpm_callback()
396 if (dev->power.irq_safe) { in __rpm_callback()
397 spin_lock(&dev->power.lock); in __rpm_callback()
407 ((dev->power.runtime_status == RPM_SUSPENDING && !retval) || in __rpm_callback()
408 (dev->power.runtime_status == RPM_RESUMING && retval))) { in __rpm_callback()
417 spin_lock_irq(&dev->power.lock); in __rpm_callback()
424 * rpm_callback - Run a given runtime PM callback for a given device.
432 if (dev->power.memalloc_noio) { in rpm_callback()
451 dev->power.runtime_error = retval; in rpm_callback()
452 return retval != -EACCES ? retval : -EIO; in rpm_callback()
456 * rpm_idle - Notify device bus type if the device can be suspended.
462 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
463 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
466 * This function must be called under dev->power.lock with interrupts disabled.
479 else if (dev->power.runtime_status != RPM_ACTIVE) in rpm_idle()
480 retval = -EAGAIN; in rpm_idle()
486 else if (dev->power.request_pending && in rpm_idle()
487 dev->power.request > RPM_REQ_IDLE) in rpm_idle()
488 retval = -EAGAIN; in rpm_idle()
491 else if (dev->power.idle_notification) in rpm_idle()
492 retval = -EINPROGRESS; in rpm_idle()
498 dev->power.request = RPM_REQ_NONE; in rpm_idle()
503 if (!callback || dev->power.no_callbacks) in rpm_idle()
508 dev->power.request = RPM_REQ_IDLE; in rpm_idle()
509 if (!dev->power.request_pending) { in rpm_idle()
510 dev->power.request_pending = true; in rpm_idle()
511 queue_work(pm_wq, &dev->power.work); in rpm_idle()
517 dev->power.idle_notification = true; in rpm_idle()
519 if (dev->power.irq_safe) in rpm_idle()
520 spin_unlock(&dev->power.lock); in rpm_idle()
522 spin_unlock_irq(&dev->power.lock); in rpm_idle()
526 if (dev->power.irq_safe) in rpm_idle()
527 spin_lock(&dev->power.lock); in rpm_idle()
529 spin_lock_irq(&dev->power.lock); in rpm_idle()
531 dev->power.idle_notification = false; in rpm_idle()
532 wake_up_all(&dev->power.wait_queue); in rpm_idle()
540 * rpm_suspend - Carry out runtime suspend of given device.
549 * otherwise run the ->runtime_suspend() callback directly. When
550 * ->runtime_suspend succeeded, if a deferred resume was requested while
553 * ignore_children of parent->power and irq_safe of dev->power are not set).
554 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
555 * flag is set and the next autosuspend-delay expiration time is in the
558 * This function must be called under dev->power.lock with interrupts disabled.
561 __releases(&dev->power.lock) __acquires(&dev->power.lock) in rpm_suspend()
575 if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC)) in rpm_suspend()
576 retval = -EAGAIN; in rpm_suspend()
582 if ((rpmflags & RPM_AUTO) && dev->power.runtime_status != RPM_SUSPENDING) { in rpm_suspend()
587 dev->power.request = RPM_REQ_NONE; in rpm_suspend()
596 if (!(dev->power.timer_expires && in rpm_suspend()
597 dev->power.timer_expires <= expires)) { in rpm_suspend()
602 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) * in rpm_suspend()
605 dev->power.timer_expires = expires; in rpm_suspend()
606 hrtimer_start_range_ns(&dev->power.suspend_timer, in rpm_suspend()
611 dev->power.timer_autosuspends = 1; in rpm_suspend()
619 if (dev->power.runtime_status == RPM_SUSPENDING) { in rpm_suspend()
623 retval = -EINPROGRESS; in rpm_suspend()
627 if (dev->power.irq_safe) { in rpm_suspend()
628 spin_unlock(&dev->power.lock); in rpm_suspend()
632 spin_lock(&dev->power.lock); in rpm_suspend()
638 prepare_to_wait(&dev->power.wait_queue, &wait, in rpm_suspend()
640 if (dev->power.runtime_status != RPM_SUSPENDING) in rpm_suspend()
643 spin_unlock_irq(&dev->power.lock); in rpm_suspend()
647 spin_lock_irq(&dev->power.lock); in rpm_suspend()
649 finish_wait(&dev->power.wait_queue, &wait); in rpm_suspend()
653 if (dev->power.no_callbacks) in rpm_suspend()
658 dev->power.request = (rpmflags & RPM_AUTO) ? in rpm_suspend()
660 if (!dev->power.request_pending) { in rpm_suspend()
661 dev->power.request_pending = true; in rpm_suspend()
662 queue_work(pm_wq, &dev->power.work); in rpm_suspend()
682 if (dev->parent) { in rpm_suspend()
683 parent = dev->parent; in rpm_suspend()
684 atomic_add_unless(&parent->power.child_count, -1, 0); in rpm_suspend()
686 wake_up_all(&dev->power.wait_queue); in rpm_suspend()
688 if (dev->power.deferred_resume) { in rpm_suspend()
689 dev->power.deferred_resume = false; in rpm_suspend()
691 retval = -EAGAIN; in rpm_suspend()
695 if (dev->power.irq_safe) in rpm_suspend()
699 if (parent && !parent->power.ignore_children) { in rpm_suspend()
700 spin_unlock(&dev->power.lock); in rpm_suspend()
702 spin_lock(&parent->power.lock); in rpm_suspend()
704 spin_unlock(&parent->power.lock); in rpm_suspend()
706 spin_lock(&dev->power.lock); in rpm_suspend()
709 if (dev->power.links_count > 0) { in rpm_suspend()
710 spin_unlock_irq(&dev->power.lock); in rpm_suspend()
714 spin_lock_irq(&dev->power.lock); in rpm_suspend()
725 dev->power.deferred_resume = false; in rpm_suspend()
726 wake_up_all(&dev->power.wait_queue); in rpm_suspend()
728 if (retval == -EAGAIN || retval == -EBUSY) { in rpm_suspend()
729 dev->power.runtime_error = 0; in rpm_suspend()
747 * rpm_resume - Carry out runtime resume of given device.
758 * ->runtime_resume() callback directly. Queue an idle notification for the
761 * This function must be called under dev->power.lock with interrupts disabled.
764 __releases(&dev->power.lock) __acquires(&dev->power.lock) in rpm_resume()
773 if (dev->power.runtime_error) { in rpm_resume()
774 retval = -EINVAL; in rpm_resume()
775 } else if (dev->power.disable_depth > 0) { in rpm_resume()
776 if (dev->power.runtime_status == RPM_ACTIVE && in rpm_resume()
777 dev->power.last_status == RPM_ACTIVE) in rpm_resume()
780 retval = -EACCES; in rpm_resume()
791 dev->power.request = RPM_REQ_NONE; in rpm_resume()
792 if (!dev->power.timer_autosuspends) in rpm_resume()
795 if (dev->power.runtime_status == RPM_ACTIVE) { in rpm_resume()
800 if (dev->power.runtime_status == RPM_RESUMING || in rpm_resume()
801 dev->power.runtime_status == RPM_SUSPENDING) { in rpm_resume()
805 if (dev->power.runtime_status == RPM_SUSPENDING) { in rpm_resume()
806 dev->power.deferred_resume = true; in rpm_resume()
808 retval = -EINPROGRESS; in rpm_resume()
810 retval = -EINPROGRESS; in rpm_resume()
815 if (dev->power.irq_safe) { in rpm_resume()
816 spin_unlock(&dev->power.lock); in rpm_resume()
820 spin_lock(&dev->power.lock); in rpm_resume()
826 prepare_to_wait(&dev->power.wait_queue, &wait, in rpm_resume()
828 if (dev->power.runtime_status != RPM_RESUMING && in rpm_resume()
829 dev->power.runtime_status != RPM_SUSPENDING) in rpm_resume()
832 spin_unlock_irq(&dev->power.lock); in rpm_resume()
836 spin_lock_irq(&dev->power.lock); in rpm_resume()
838 finish_wait(&dev->power.wait_queue, &wait); in rpm_resume()
844 * power.no_callbacks is set, because otherwise we don't know whether in rpm_resume()
847 if (dev->power.no_callbacks && !parent && dev->parent) { in rpm_resume()
848 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); in rpm_resume()
849 if (dev->parent->power.disable_depth > 0 || in rpm_resume()
850 dev->parent->power.ignore_children || in rpm_resume()
851 dev->parent->power.runtime_status == RPM_ACTIVE) { in rpm_resume()
852 atomic_inc(&dev->parent->power.child_count); in rpm_resume()
853 spin_unlock(&dev->parent->power.lock); in rpm_resume()
857 spin_unlock(&dev->parent->power.lock); in rpm_resume()
862 dev->power.request = RPM_REQ_RESUME; in rpm_resume()
863 if (!dev->power.request_pending) { in rpm_resume()
864 dev->power.request_pending = true; in rpm_resume()
865 queue_work(pm_wq, &dev->power.work); in rpm_resume()
871 if (!parent && dev->parent) { in rpm_resume()
874 * necessary. Not needed if dev is irq-safe; then the in rpm_resume()
877 parent = dev->parent; in rpm_resume()
878 if (dev->power.irq_safe) in rpm_resume()
881 spin_unlock(&dev->power.lock); in rpm_resume()
885 spin_lock(&parent->power.lock); in rpm_resume()
890 if (!parent->power.disable_depth && in rpm_resume()
891 !parent->power.ignore_children) { in rpm_resume()
893 if (parent->power.runtime_status != RPM_ACTIVE) in rpm_resume()
894 retval = -EBUSY; in rpm_resume()
896 spin_unlock(&parent->power.lock); in rpm_resume()
898 spin_lock(&dev->power.lock); in rpm_resume()
906 if (dev->power.no_callbacks) in rpm_resume()
924 atomic_inc(&parent->power.child_count); in rpm_resume()
926 wake_up_all(&dev->power.wait_queue); in rpm_resume()
932 if (parent && !dev->power.irq_safe) { in rpm_resume()
933 spin_unlock_irq(&dev->power.lock); in rpm_resume()
937 spin_lock_irq(&dev->power.lock); in rpm_resume()
946 * pm_runtime_work - Universal runtime PM work function.
954 struct device *dev = container_of(work, struct device, power.work); in pm_runtime_work()
957 spin_lock_irq(&dev->power.lock); in pm_runtime_work()
959 if (!dev->power.request_pending) in pm_runtime_work()
962 req = dev->power.request; in pm_runtime_work()
963 dev->power.request = RPM_REQ_NONE; in pm_runtime_work()
964 dev->power.request_pending = false; in pm_runtime_work()
984 spin_unlock_irq(&dev->power.lock); in pm_runtime_work()
988 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
995 struct device *dev = container_of(timer, struct device, power.suspend_timer); in pm_suspend_timer_fn()
999 spin_lock_irqsave(&dev->power.lock, flags); in pm_suspend_timer_fn()
1001 expires = dev->power.timer_expires; in pm_suspend_timer_fn()
1007 dev->power.timer_expires = 0; in pm_suspend_timer_fn()
1008 rpm_suspend(dev, dev->power.timer_autosuspends ? in pm_suspend_timer_fn()
1012 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_suspend_timer_fn()
1018 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1028 spin_lock_irqsave(&dev->power.lock, flags); in pm_schedule_suspend()
1043 dev->power.timer_expires = expires; in pm_schedule_suspend()
1044 dev->power.timer_autosuspends = 0; in pm_schedule_suspend()
1045 hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS); in pm_schedule_suspend()
1048 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_schedule_suspend()
1058 ret = atomic_sub_return(1, &dev->power.usage_count); in rpm_drop_usage_count()
1068 atomic_inc(&dev->power.usage_count); in rpm_drop_usage_count()
1070 return -EINVAL; in rpm_drop_usage_count()
1074 * __pm_runtime_idle - Entry point for runtime idle operations.
1101 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); in __pm_runtime_idle()
1103 spin_lock_irqsave(&dev->power.lock, flags); in __pm_runtime_idle()
1105 spin_unlock_irqrestore(&dev->power.lock, flags); in __pm_runtime_idle()
1112 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1139 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); in __pm_runtime_suspend()
1141 spin_lock_irqsave(&dev->power.lock, flags); in __pm_runtime_suspend()
1143 spin_unlock_irqrestore(&dev->power.lock, flags); in __pm_runtime_suspend()
1150 * __pm_runtime_resume - Entry point for runtime resume operations.
1165 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe && in __pm_runtime_resume()
1166 dev->power.runtime_status != RPM_ACTIVE); in __pm_runtime_resume()
1169 atomic_inc(&dev->power.usage_count); in __pm_runtime_resume()
1171 spin_lock_irqsave(&dev->power.lock, flags); in __pm_runtime_resume()
1173 spin_unlock_irqrestore(&dev->power.lock, flags); in __pm_runtime_resume()
1180 * pm_runtime_get_conditional - Conditionally bump up device usage counter.
1184 * Return -EINVAL if runtime PM is disabled for @dev.
1206 spin_lock_irqsave(&dev->power.lock, flags); in pm_runtime_get_conditional()
1207 if (dev->power.disable_depth > 0) { in pm_runtime_get_conditional()
1208 retval = -EINVAL; in pm_runtime_get_conditional()
1209 } else if (dev->power.runtime_status != RPM_ACTIVE) { in pm_runtime_get_conditional()
1213 atomic_inc(&dev->power.usage_count); in pm_runtime_get_conditional()
1215 retval = atomic_inc_not_zero(&dev->power.usage_count); in pm_runtime_get_conditional()
1218 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_runtime_get_conditional()
1224 * pm_runtime_get_if_active - Bump up runtime PM usage counter if the device is
1230 * state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the
1240 * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter.
1246 * 0 is returned. -EINVAL is returned if runtime PM is disabled for the device,
1256 * __pm_runtime_set_status - Set runtime PM status of a device.
1260 * If runtime PM of the device is disabled or its power.runtime_error field is
1264 * parent's power.ignore_children flag is unset, the device's status cannot be
1265 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1267 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1281 struct device *parent = dev->parent; in __pm_runtime_set_status()
1287 return -EINVAL; in __pm_runtime_set_status()
1289 spin_lock_irqsave(&dev->power.lock, flags); in __pm_runtime_set_status()
1292 * Prevent PM-runtime from being enabled for the device or return an in __pm_runtime_set_status()
1295 if (dev->power.runtime_error || dev->power.disable_depth) in __pm_runtime_set_status()
1296 dev->power.disable_depth++; in __pm_runtime_set_status()
1298 error = -EAGAIN; in __pm_runtime_set_status()
1300 spin_unlock_irqrestore(&dev->power.lock, flags); in __pm_runtime_set_status()
1321 spin_lock_irqsave(&dev->power.lock, flags); in __pm_runtime_set_status()
1323 if (dev->power.runtime_status == status || !parent) in __pm_runtime_set_status()
1327 atomic_add_unless(&parent->power.child_count, -1, 0); in __pm_runtime_set_status()
1328 notify_parent = !parent->power.ignore_children; in __pm_runtime_set_status()
1330 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); in __pm_runtime_set_status()
1335 * 'power.ignore_children' flag unset. in __pm_runtime_set_status()
1337 if (!parent->power.disable_depth && in __pm_runtime_set_status()
1338 !parent->power.ignore_children && in __pm_runtime_set_status()
1339 parent->power.runtime_status != RPM_ACTIVE) { in __pm_runtime_set_status()
1343 error = -EBUSY; in __pm_runtime_set_status()
1344 } else if (dev->power.runtime_status == RPM_SUSPENDED) { in __pm_runtime_set_status()
1345 atomic_inc(&parent->power.child_count); in __pm_runtime_set_status()
1348 spin_unlock(&parent->power.lock); in __pm_runtime_set_status()
1359 dev->power.runtime_error = 0; in __pm_runtime_set_status()
1362 spin_unlock_irqrestore(&dev->power.lock, flags); in __pm_runtime_set_status()
1382 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1388 * Should be called under dev->power.lock with interrupts disabled.
1394 if (dev->power.request_pending) { in __pm_runtime_barrier()
1395 dev->power.request = RPM_REQ_NONE; in __pm_runtime_barrier()
1396 spin_unlock_irq(&dev->power.lock); in __pm_runtime_barrier()
1398 cancel_work_sync(&dev->power.work); in __pm_runtime_barrier()
1400 spin_lock_irq(&dev->power.lock); in __pm_runtime_barrier()
1401 dev->power.request_pending = false; in __pm_runtime_barrier()
1404 if (dev->power.runtime_status == RPM_SUSPENDING || in __pm_runtime_barrier()
1405 dev->power.runtime_status == RPM_RESUMING || in __pm_runtime_barrier()
1406 dev->power.idle_notification) { in __pm_runtime_barrier()
1409 /* Suspend, wake-up or idle notification in progress. */ in __pm_runtime_barrier()
1411 prepare_to_wait(&dev->power.wait_queue, &wait, in __pm_runtime_barrier()
1413 if (dev->power.runtime_status != RPM_SUSPENDING in __pm_runtime_barrier()
1414 && dev->power.runtime_status != RPM_RESUMING in __pm_runtime_barrier()
1415 && !dev->power.idle_notification) in __pm_runtime_barrier()
1417 spin_unlock_irq(&dev->power.lock); in __pm_runtime_barrier()
1421 spin_lock_irq(&dev->power.lock); in __pm_runtime_barrier()
1423 finish_wait(&dev->power.wait_queue, &wait); in __pm_runtime_barrier()
1428 * pm_runtime_barrier - Flush pending requests and wait for completions.
1446 spin_lock_irq(&dev->power.lock); in pm_runtime_barrier()
1448 if (dev->power.request_pending in pm_runtime_barrier()
1449 && dev->power.request == RPM_REQ_RESUME) { in pm_runtime_barrier()
1456 spin_unlock_irq(&dev->power.lock); in pm_runtime_barrier()
1464 * __pm_runtime_disable - Disable runtime PM of a device.
1468 * Increment power.disable_depth for the device and if it was zero previously,
1474 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1479 spin_lock_irq(&dev->power.lock); in __pm_runtime_disable()
1481 if (dev->power.disable_depth > 0) { in __pm_runtime_disable()
1482 dev->power.disable_depth++; in __pm_runtime_disable()
1491 if (check_resume && dev->power.request_pending && in __pm_runtime_disable()
1492 dev->power.request == RPM_REQ_RESUME) { in __pm_runtime_disable()
1504 /* Update time accounting before disabling PM-runtime. */ in __pm_runtime_disable()
1507 if (!dev->power.disable_depth++) { in __pm_runtime_disable()
1509 dev->power.last_status = dev->power.runtime_status; in __pm_runtime_disable()
1513 spin_unlock_irq(&dev->power.lock); in __pm_runtime_disable()
1518 * pm_runtime_enable - Enable runtime PM of a device.
1525 spin_lock_irqsave(&dev->power.lock, flags); in pm_runtime_enable()
1527 if (!dev->power.disable_depth) { in pm_runtime_enable()
1532 if (--dev->power.disable_depth > 0) in pm_runtime_enable()
1535 dev->power.last_status = RPM_INVALID; in pm_runtime_enable()
1536 dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); in pm_runtime_enable()
1538 if (dev->power.runtime_status == RPM_SUSPENDED && in pm_runtime_enable()
1539 !dev->power.ignore_children && in pm_runtime_enable()
1540 atomic_read(&dev->power.child_count) > 0) in pm_runtime_enable()
1544 spin_unlock_irqrestore(&dev->power.lock, flags); in pm_runtime_enable()
1555 * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable.
1571 * pm_runtime_forbid - Block runtime PM of a device.
1574 * Increase the device's usage count and clear its power.runtime_auto flag,
1580 spin_lock_irq(&dev->power.lock); in pm_runtime_forbid()
1581 if (!dev->power.runtime_auto) in pm_runtime_forbid()
1584 dev->power.runtime_auto = false; in pm_runtime_forbid()
1585 atomic_inc(&dev->power.usage_count); in pm_runtime_forbid()
1589 spin_unlock_irq(&dev->power.lock); in pm_runtime_forbid()
1594 * pm_runtime_allow - Unblock runtime PM of a device.
1597 * Decrease the device's usage count and set its power.runtime_auto flag.
1603 spin_lock_irq(&dev->power.lock); in pm_runtime_allow()
1604 if (dev->power.runtime_auto) in pm_runtime_allow()
1607 dev->power.runtime_auto = true; in pm_runtime_allow()
1615 spin_unlock_irq(&dev->power.lock); in pm_runtime_allow()
1620 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1623 * Set the power.no_callbacks flag, which tells the PM core that this
1624 * device is power-managed through its parent and has no runtime PM
1629 spin_lock_irq(&dev->power.lock); in pm_runtime_no_callbacks()
1630 dev->power.no_callbacks = 1; in pm_runtime_no_callbacks()
1631 spin_unlock_irq(&dev->power.lock); in pm_runtime_no_callbacks()
1638 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1641 * Set the power.irq_safe flag, which tells the PM core that the
1642 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1645 * the parent from runtime suspending -- otherwise an irq-safe child might have
1646 * to wait for a non-irq-safe parent.
1650 if (dev->parent) in pm_runtime_irq_safe()
1651 pm_runtime_get_sync(dev->parent); in pm_runtime_irq_safe()
1653 spin_lock_irq(&dev->power.lock); in pm_runtime_irq_safe()
1654 dev->power.irq_safe = 1; in pm_runtime_irq_safe()
1655 spin_unlock_irq(&dev->power.lock); in pm_runtime_irq_safe()
1660 * update_autosuspend - Handle a change to a device's autosuspend settings.
1668 * This function must be called under dev->power.lock with interrupts disabled.
1672 int delay = dev->power.autosuspend_delay; in update_autosuspend()
1675 if (dev->power.use_autosuspend && delay < 0) { in update_autosuspend()
1679 atomic_inc(&dev->power.usage_count); in update_autosuspend()
1691 atomic_dec(&dev->power.usage_count); in update_autosuspend()
1699 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1703 * Set the device's power.autosuspend_delay value. If it changes to negative
1704 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1711 spin_lock_irq(&dev->power.lock); in pm_runtime_set_autosuspend_delay()
1712 old_delay = dev->power.autosuspend_delay; in pm_runtime_set_autosuspend_delay()
1713 old_use = dev->power.use_autosuspend; in pm_runtime_set_autosuspend_delay()
1714 dev->power.autosuspend_delay = delay; in pm_runtime_set_autosuspend_delay()
1716 spin_unlock_irq(&dev->power.lock); in pm_runtime_set_autosuspend_delay()
1721 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1725 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1732 spin_lock_irq(&dev->power.lock); in __pm_runtime_use_autosuspend()
1733 old_delay = dev->power.autosuspend_delay; in __pm_runtime_use_autosuspend()
1734 old_use = dev->power.use_autosuspend; in __pm_runtime_use_autosuspend()
1735 dev->power.use_autosuspend = use; in __pm_runtime_use_autosuspend()
1737 spin_unlock_irq(&dev->power.lock); in __pm_runtime_use_autosuspend()
1742 * pm_runtime_init - Initialize runtime PM fields in given device object.
1747 dev->power.runtime_status = RPM_SUSPENDED; in pm_runtime_init()
1748 dev->power.last_status = RPM_INVALID; in pm_runtime_init()
1749 dev->power.idle_notification = false; in pm_runtime_init()
1751 dev->power.disable_depth = 1; in pm_runtime_init()
1752 atomic_set(&dev->power.usage_count, 0); in pm_runtime_init()
1754 dev->power.runtime_error = 0; in pm_runtime_init()
1756 atomic_set(&dev->power.child_count, 0); in pm_runtime_init()
1758 dev->power.runtime_auto = true; in pm_runtime_init()
1760 dev->power.request_pending = false; in pm_runtime_init()
1761 dev->power.request = RPM_REQ_NONE; in pm_runtime_init()
1762 dev->power.deferred_resume = false; in pm_runtime_init()
1763 dev->power.needs_force_resume = 0; in pm_runtime_init()
1764 INIT_WORK(&dev->power.work, pm_runtime_work); in pm_runtime_init()
1766 dev->power.timer_expires = 0; in pm_runtime_init()
1767 hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); in pm_runtime_init()
1768 dev->power.suspend_timer.function = pm_suspend_timer_fn; in pm_runtime_init()
1770 init_waitqueue_head(&dev->power.wait_queue); in pm_runtime_init()
1774 * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1775 * @dev: Device object to re-initialize.
1780 if (dev->power.runtime_status == RPM_ACTIVE) in pm_runtime_reinit()
1782 if (dev->power.irq_safe) { in pm_runtime_reinit()
1783 spin_lock_irq(&dev->power.lock); in pm_runtime_reinit()
1784 dev->power.irq_safe = 0; in pm_runtime_reinit()
1785 spin_unlock_irq(&dev->power.lock); in pm_runtime_reinit()
1786 if (dev->parent) in pm_runtime_reinit()
1787 pm_runtime_put(dev->parent); in pm_runtime_reinit()
1793 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1803 * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1813 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, in pm_runtime_get_suppliers()
1815 if (link->flags & DL_FLAG_PM_RUNTIME) { in pm_runtime_get_suppliers()
1816 link->supplier_preactivated = true; in pm_runtime_get_suppliers()
1817 pm_runtime_get_sync(link->supplier); in pm_runtime_get_suppliers()
1824 * pm_runtime_put_suppliers - Drop references to supplier devices.
1834 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, in pm_runtime_put_suppliers()
1836 if (link->supplier_preactivated) { in pm_runtime_put_suppliers()
1837 link->supplier_preactivated = false; in pm_runtime_put_suppliers()
1838 pm_runtime_put(link->supplier); in pm_runtime_put_suppliers()
1846 spin_lock_irq(&dev->power.lock); in pm_runtime_new_link()
1847 dev->power.links_count++; in pm_runtime_new_link()
1848 spin_unlock_irq(&dev->power.lock); in pm_runtime_new_link()
1853 spin_lock_irq(&dev->power.lock); in pm_runtime_drop_link_count()
1854 WARN_ON(dev->power.links_count == 0); in pm_runtime_drop_link_count()
1855 dev->power.links_count--; in pm_runtime_drop_link_count()
1856 spin_unlock_irq(&dev->power.lock); in pm_runtime_drop_link_count()
1860 * pm_runtime_drop_link - Prepare for device link removal.
1869 if (!(link->flags & DL_FLAG_PM_RUNTIME)) in pm_runtime_drop_link()
1872 pm_runtime_drop_link_count(link->consumer); in pm_runtime_drop_link()
1874 pm_request_idle(link->supplier); in pm_runtime_drop_link()
1879 return atomic_read(&dev->power.usage_count) <= 1 && in pm_runtime_need_not_resume()
1880 (atomic_read(&dev->power.child_count) == 0 || in pm_runtime_need_not_resume()
1881 dev->power.ignore_children); in pm_runtime_need_not_resume()
1885 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1889 * if it is active, invoke its ->runtime_suspend callback to suspend it and
1892 * the system-wide transition under way, decrement its parent's children counter
1897 * sure the device is put into low power state and it should only be used during
1898 * system-wide PM transitions to sleep states. It assumes that the analogous
1902 * state where this function has called the ->runtime_suspend callback but the
1924 * If the device can stay in suspend after the system-wide transition in pm_runtime_force_suspend()
1933 dev->power.needs_force_resume = 1; in pm_runtime_force_suspend()
1946 * pm_runtime_force_resume - Force a device into resume state if needed.
1950 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1951 * those actions and bring the device into full power, if it is expected to be
1962 if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume) in pm_runtime_force_resume()
1983 dev->power.needs_force_resume = 0; in pm_runtime_force_resume()