Skip to content

Commit 3595c9d

Browse files
nordic-krchjhedberg
authored andcommitted
tests: pm: policy_api: Add test for locking all PM states
Add test case that validates behavior of pm_policy_state_all_lock_get and pm_policy_state_all_lock_put. Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
1 parent 336e89e commit 3595c9d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

include/zephyr/pm/policy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks);
112112
*
113113
* @see pm_policy_state_lock_put()
114114
*/
115-
116115
void pm_policy_state_lock_get(enum pm_state state, uint8_t substate_id);
117116

118117
/**
@@ -365,7 +364,6 @@ static inline void pm_policy_device_power_lock_put(const struct device *dev)
365364
{
366365
ARG_UNUSED(dev);
367366
}
368-
369367
#endif /* CONFIG_PM_POLICY_DEVICE_CONSTRAINTS */
370368

371369
#if defined(CONFIG_PM) || defined(CONFIG_PM_POLICY_LATENCY_STANDALONE) || defined(__DOXYGEN__)

subsys/pm/policy/policy_state_lock.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ static struct k_spinlock lock;
5050

5151
void pm_policy_state_all_lock_get(void)
5252
{
53+
#if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state)
5354
(void)atomic_inc(&global_lock_cnt);
55+
#endif
5456
}
5557

5658
void pm_policy_state_all_lock_put(void)
5759
{
60+
#if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state)
5861
__ASSERT(global_lock_cnt > 0, "Unbalanced state lock get/put");
5962
(void)atomic_dec(&global_lock_cnt);
63+
#endif
6064
}
6165

6266

tests/subsys/pm/policy_api/src/main.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ ZTEST(policy_api, test_pm_policy_next_state_default)
7171
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM);
7272
}
7373

74+
ZTEST(policy_api, test_pm_policy_state_all_lock)
75+
{
76+
/* initial state: PM_STATE_RUNTIME_IDLE allowed */
77+
zassert_false(pm_policy_state_lock_is_active(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
78+
zassert_true(pm_policy_state_is_available(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
79+
zassert_true(pm_policy_state_any_active());
80+
81+
/* Locking all states. */
82+
pm_policy_state_all_lock_get();
83+
pm_policy_state_all_lock_get();
84+
85+
/* States are locked. */
86+
zassert_true(pm_policy_state_lock_is_active(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
87+
zassert_false(pm_policy_state_is_available(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
88+
zassert_false(pm_policy_state_any_active());
89+
90+
pm_policy_state_all_lock_put();
91+
92+
/* States are still locked due to reference counter. */
93+
zassert_true(pm_policy_state_lock_is_active(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
94+
zassert_false(pm_policy_state_is_available(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
95+
zassert_false(pm_policy_state_any_active());
96+
97+
pm_policy_state_all_lock_put();
98+
99+
/* States are available again. */
100+
zassert_false(pm_policy_state_lock_is_active(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
101+
zassert_true(pm_policy_state_is_available(PM_STATE_RUNTIME_IDLE, PM_ALL_SUBSTATES));
102+
zassert_true(pm_policy_state_any_active());
103+
}
104+
74105
/**
75106
* @brief Test the behavior of pm_policy_next_state() when
76107
* states are allowed/disallowed and CONFIG_PM_POLICY_DEFAULT=y.

0 commit comments

Comments
 (0)