From 3624b51f24d4018f5f249964fc33bd58f6f8d846 Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Wed, 1 Dec 2021 15:44:57 -0800 Subject: [PATCH] pm: device_runtime: Use pm flags for runtime state Although we are declaring `pm->enable`as bitfield, it ends up using more memory due memory alignment. Since we already have an atomic variable for device flags, this commit adds a new flag to indicates whether or not device runtime is enabled. Doing it we are saving some extra bits and avoiding need to lock the mutex in several situations since we can atomically check if pm runtime is enabled on a given device. Signed-off-by: Flavio Ceolin --- include/pm/device.h | 4 ++-- subsys/pm/device_runtime.c | 23 +++++++---------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/include/pm/device.h b/include/pm/device.h index 8882d1f2edd..5884c32900f 100644 --- a/include/pm/device.h +++ b/include/pm/device.h @@ -37,6 +37,8 @@ enum pm_device_flag { PM_DEVICE_FLAG_WS_CAPABLE, /** Indicates if the device is being used as wakeup source. */ PM_DEVICE_FLAG_WS_ENABLED, + /** Indicates if device runtime is enabled */ + PM_DEVICE_FLAG_RUNTIME_ENABLED, }; /** @endcond */ @@ -99,8 +101,6 @@ struct pm_device { const struct device *dev; /** Lock to synchronize the get/put operations */ struct k_mutex lock; - /** Device pm enable flag */ - bool enable : 1; /** Device usage count */ uint32_t usage; /** Work object for asynchronous calls */ diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index 80023896f2c..bf2c6dc1fa3 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -41,7 +41,7 @@ static int runtime_suspend(const struct device *dev, bool async) (void)k_mutex_lock(&pm->lock, K_FOREVER); } - if (!pm->enable) { + if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) { ret = -ENOTSUP; goto unlock; } @@ -108,7 +108,7 @@ int pm_device_runtime_get(const struct device *dev) (void)k_mutex_lock(&pm->lock, K_FOREVER); } - if (!pm->enable) { + if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) { ret = -ENOTSUP; goto unlock; } @@ -176,7 +176,7 @@ void pm_device_runtime_enable(const struct device *dev) (void)k_mutex_lock(&pm->lock, K_FOREVER); } - if (pm->enable) { + if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) != 0U) { goto unlock; } @@ -187,7 +187,7 @@ void pm_device_runtime_enable(const struct device *dev) k_work_init_delayable(&pm->work, runtime_suspend_work); } - pm->enable = true; + atomic_set_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED); unlock: if (!k_is_pre_kernel()) { @@ -208,7 +208,7 @@ int pm_device_runtime_disable(const struct device *dev) (void)k_mutex_lock(&pm->lock, K_FOREVER); } - if (!pm->enable) { + if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) { goto unlock; } @@ -230,7 +230,7 @@ int pm_device_runtime_disable(const struct device *dev) pm->state = PM_DEVICE_STATE_ACTIVE; } - pm->enable = false; + atomic_clear_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED); unlock: if (!k_is_pre_kernel()) { @@ -244,16 +244,7 @@ unlock: bool pm_device_runtime_is_enabled(const struct device *dev) { - bool ret = false; struct pm_device *pm = dev->pm; - if (!k_is_pre_kernel()) { - (void)k_mutex_lock(&pm->lock, K_FOREVER); - ret = pm->enable; - (void)k_mutex_unlock(&pm->lock); - } else { - ret = pm->enable; - } - - return ret; + return atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED); }