From 7812e54e8a2fa406d12162cd05509a598db9562c Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Thu, 1 Feb 2024 18:51:26 -0800 Subject: [PATCH] pm: device_runtime: Simplify runtime_enable Move the check of PM_DEVICE_FLAG_RUNTIME_ENABLED to the beginning of the function. With this we avoid taking/release the semaphore and also we no longer need check it in runtime_enable_sync(), because it was already checked in pm_device_runtime_enable(). Signed-off-by: Flavio Ceolin --- subsys/pm/device_runtime.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index 82aed3cd1985..8ad738cd2dc0 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -387,12 +387,6 @@ static int runtime_enable_sync(const struct device *dev) struct pm_device_isr *pm = dev->pm_isr; k_spinlock_key_t k = k_spin_lock(&pm->lock); - /* Because context is locked we can access flags directly. */ - if (pm->base.flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) { - ret = 0; - goto unlock; - } - if (pm->base.state == PM_DEVICE_STATE_ACTIVE) { ret = pm->base.action_cb(dev, PM_DEVICE_ACTION_SUSPEND); if (ret < 0) { @@ -423,6 +417,10 @@ int pm_device_runtime_enable(const struct device *dev) goto end; } + if (atomic_test_bit(&pm->base.flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) { + goto end; + } + if (pm_device_state_is_locked(dev)) { ret = -EPERM; goto end; @@ -437,10 +435,6 @@ int pm_device_runtime_enable(const struct device *dev) (void)k_sem_take(&pm->lock, K_FOREVER); } - if (atomic_test_bit(&pm->base.flags, PM_DEVICE_FLAG_RUNTIME_ENABLED)) { - goto unlock; - } - /* lazy init of PM fields */ if (pm->dev == NULL) { pm->dev = dev;