@@ -43,10 +43,27 @@ static const struct {
4343static atomic_t lock_cnt [ARRAY_SIZE (substates )];
4444static atomic_t latency_mask = BIT_MASK (ARRAY_SIZE (substates ));
4545static atomic_t unlock_mask = BIT_MASK (ARRAY_SIZE (substates ));
46+ static atomic_t global_lock_cnt ;
4647static struct k_spinlock lock ;
4748
4849#endif
4950
51+ void pm_policy_state_all_lock_get (void )
52+ {
53+ #if DT_HAS_COMPAT_STATUS_OKAY (zephyr_power_state )
54+ (void )atomic_inc (& global_lock_cnt );
55+ #endif
56+ }
57+
58+ void pm_policy_state_all_lock_put (void )
59+ {
60+ #if DT_HAS_COMPAT_STATUS_OKAY (zephyr_power_state )
61+ __ASSERT (global_lock_cnt > 0 , "Unbalanced state lock get/put" );
62+ (void )atomic_dec (& global_lock_cnt );
63+ #endif
64+ }
65+
66+
5067void pm_policy_state_lock_get (enum pm_state state , uint8_t substate_id )
5168{
5269#if DT_HAS_COMPAT_STATUS_OKAY (zephyr_power_state )
@@ -106,7 +123,8 @@ bool pm_policy_state_lock_is_active(enum pm_state state, uint8_t substate_id)
106123 for (size_t i = 0 ; i < ARRAY_SIZE (substates ); i ++ ) {
107124 if (substates [i ].state == state &&
108125 (substates [i ].substate_id == substate_id || substate_id == PM_ALL_SUBSTATES )) {
109- return atomic_get (& lock_cnt [i ]) != 0 ;
126+ return (atomic_get (& lock_cnt [i ]) != 0 ) ||
127+ (atomic_get (& global_lock_cnt ) != 0 );
110128 }
111129 }
112130#endif
@@ -121,7 +139,8 @@ bool pm_policy_state_is_available(enum pm_state state, uint8_t substate_id)
121139 if (substates [i ].state == state &&
122140 (substates [i ].substate_id == substate_id || substate_id == PM_ALL_SUBSTATES )) {
123141 return (atomic_get (& lock_cnt [i ]) == 0 ) &&
124- (atomic_get (& latency_mask ) & BIT (i ));
142+ (atomic_get (& latency_mask ) & BIT (i )) &&
143+ (atomic_get (& global_lock_cnt ) == 0 );
125144 }
126145 }
127146#endif
@@ -135,7 +154,8 @@ bool pm_policy_state_any_active(void)
135154 /* Check if there is any power state that is not locked and not disabled due
136155 * to latency requirements.
137156 */
138- return atomic_get (& unlock_mask ) & atomic_get (& latency_mask );
157+ return atomic_get (& unlock_mask ) & atomic_get (& latency_mask ) &&
158+ (atomic_get (& global_lock_cnt ) == 0 );
139159#endif
140160 return true;
141161}
0 commit comments