Skip to content

Commit 12a437c

Browse files
rghaddabnashif
authored andcommitted
pm: policy: change the policy event handling
Some events needs to be handled with a very low latency constraint. If the system is in deep sleep, exit latency from this low level state exceeds sometimes the maximum latency constraint of these events. Before suspending the system, select which events is happening sooner, kernel events or normal events. CPU will be up just before the next event occurs taking into account the exit latency of the current power state Change also the policy event API to take as argument absolute time in HW cycles instead of time in us Signed-off-by: Riadh Ghaddab <[email protected]>
1 parent 8233b70 commit 12a437c

File tree

4 files changed

+91
-37
lines changed

4 files changed

+91
-37
lines changed

include/zephyr/pm/policy.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,28 +188,30 @@ void pm_policy_latency_changed_unsubscribe(struct pm_policy_latency_subscription
188188
* will wake up the system at a known time in the future. By registering such
189189
* event, the policy manager will be able to decide whether certain power states
190190
* are worth entering or not.
191+
* CPU is woken up before the time passed in cycle to prevent the event handling
192+
* latency
191193
*
192194
* @note It is mandatory to unregister events once they have happened by using
193195
* pm_policy_event_unregister(). Not doing so is an API contract violation,
194196
* because the system would continue to consider them as valid events in the
195197
* *far* future, that is, after the cycle counter rollover.
196198
*
197199
* @param evt Event.
198-
* @param time_us When the event will occur, in microseconds from now.
200+
* @param cycle When the event will occur, in absolute time (cycles).
199201
*
200202
* @see pm_policy_event_unregister
201203
*/
202-
void pm_policy_event_register(struct pm_policy_event *evt, uint32_t time_us);
204+
void pm_policy_event_register(struct pm_policy_event *evt, uint32_t cycle);
203205

204206
/**
205207
* @brief Update an event.
206208
*
207209
* @param evt Event.
208-
* @param time_us When the event will occur, in microseconds from now.
210+
* @param cycle When the event will occur, in absolute time (cycles).
209211
*
210212
* @see pm_policy_event_register
211213
*/
212-
void pm_policy_event_update(struct pm_policy_event *evt, uint32_t time_us);
214+
void pm_policy_event_update(struct pm_policy_event *evt, uint32_t cycle);
213215

214216
/**
215217
* @brief Unregister an event.
@@ -246,6 +248,14 @@ void pm_policy_device_power_lock_get(const struct device *dev);
246248
*/
247249
void pm_policy_device_power_lock_put(const struct device *dev);
248250

251+
/**
252+
* @brief Returns the ticks until the next event
253+
*
254+
* If an event is registred, it will return the number of ticks until the next event as
255+
* a positive or zero value. Otherwise it returns -1
256+
*/
257+
int32_t pm_policy_next_event_ticks(void);
258+
249259
#else
250260
static inline void pm_policy_state_lock_get(enum pm_state state, uint8_t substate_id)
251261
{
@@ -287,18 +297,16 @@ static inline void pm_policy_latency_request_remove(
287297
ARG_UNUSED(req);
288298
}
289299

290-
static inline void pm_policy_event_register(struct pm_policy_event *evt,
291-
uint32_t time_us)
300+
static inline void pm_policy_event_register(struct pm_policy_event *evt, uint32_t cycle)
292301
{
293302
ARG_UNUSED(evt);
294-
ARG_UNUSED(time_us);
303+
ARG_UNUSED(cycle);
295304
}
296305

297-
static inline void pm_policy_event_update(struct pm_policy_event *evt,
298-
uint32_t time_us)
306+
static inline void pm_policy_event_update(struct pm_policy_event *evt, uint32_t cycle)
299307
{
300308
ARG_UNUSED(evt);
301-
ARG_UNUSED(time_us);
309+
ARG_UNUSED(cycle);
302310
}
303311

304312
static inline void pm_policy_event_unregister(struct pm_policy_event *evt)
@@ -316,6 +324,11 @@ static inline void pm_policy_device_power_lock_put(const struct device *dev)
316324
ARG_UNUSED(dev);
317325
}
318326

327+
static inline int32_t pm_policy_next_event_ticks(void)
328+
{
329+
return -1;
330+
}
331+
319332
#endif /* CONFIG_PM */
320333

321334
/**

subsys/pm/pm.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ static inline void pm_state_notify(bool entering_state)
6868
k_spin_unlock(&pm_notifier_lock, pm_notifier_key);
6969
}
7070

71+
static inline int32_t ticks_expiring_sooner(int32_t ticks1, int32_t ticks2)
72+
{
73+
/*
74+
* Ticks are relative numbers that defines the number of ticks
75+
* until the next event.
76+
* Its maximum value is K_TICKS_FOREVER ((uint32_t)-1) which is -1
77+
* when we cast it to (int32_t)
78+
* We need to find out which one is the closest
79+
*/
80+
81+
__ASSERT(ticks1 >= -1, "ticks1 has unexpected negative value");
82+
__ASSERT(ticks2 >= -1, "ticks2 has unexpected negative value");
83+
84+
if (ticks1 == K_TICKS_FOREVER) {
85+
return ticks2;
86+
}
87+
if (ticks2 == K_TICKS_FOREVER) {
88+
return ticks1;
89+
}
90+
/* At this step ticks1 and ticks2 are positive */
91+
return MIN(ticks1, ticks2);
92+
}
93+
7194
void pm_system_resume(void)
7295
{
7396
uint8_t id = _current_cpu->id;
@@ -117,12 +140,20 @@ bool pm_state_force(uint8_t cpu, const struct pm_state_info *info)
117140
return true;
118141
}
119142

120-
bool pm_system_suspend(int32_t ticks)
143+
bool pm_system_suspend(int32_t kernel_ticks)
121144
{
122145
uint8_t id = _current_cpu->id;
123146
k_spinlock_key_t key;
147+
int32_t ticks, events_ticks;
124148

125-
SYS_PORT_TRACING_FUNC_ENTER(pm, system_suspend, ticks);
149+
SYS_PORT_TRACING_FUNC_ENTER(pm, system_suspend, kernel_ticks);
150+
151+
/*
152+
* CPU needs to be fully wake up before the event is triggered.
153+
* We need to find out first the ticks to the next event
154+
*/
155+
events_ticks = pm_policy_next_event_ticks();
156+
ticks = ticks_expiring_sooner(kernel_ticks, events_ticks);
126157

127158
key = k_spin_lock(&pm_forced_state_lock);
128159
if (z_cpus_pm_forced_state[id].state != PM_STATE_ACTIVE) {

subsys/pm/policy.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ static sys_slist_t latency_subs;
143143
static struct k_spinlock events_lock;
144144
/** List of events. */
145145
static sys_slist_t events_list;
146-
/** Next event, in absolute cycles (<0: none, [0, UINT32_MAX]: cycles) */
147-
static int64_t next_event_cyc = -1;
146+
/** Pointer to Next Event. */
147+
static struct pm_policy_event *next_event;
148148

149149
/** @brief Update maximum allowed latency. */
150150
static void update_max_latency(void)
@@ -182,6 +182,9 @@ static void update_next_event(uint32_t cyc)
182182
int64_t new_next_event_cyc = -1;
183183
struct pm_policy_event *evt;
184184

185+
/* unset the next event pointer */
186+
next_event = NULL;
187+
185188
SYS_SLIST_FOR_EACH_CONTAINER(&events_list, evt, node) {
186189
uint64_t cyc_evt = evt->value_cyc;
187190

@@ -199,18 +202,26 @@ static void update_next_event(uint32_t cyc)
199202
cyc_evt += (uint64_t)UINT32_MAX + 1U;
200203
}
201204

202-
if ((new_next_event_cyc < 0) ||
203-
(cyc_evt < new_next_event_cyc)) {
205+
if ((new_next_event_cyc < 0) || (cyc_evt < new_next_event_cyc)) {
204206
new_next_event_cyc = cyc_evt;
207+
next_event = evt;
205208
}
206209
}
210+
}
207211

208-
/* undo padding for events in the [0, cyc) range */
209-
if (new_next_event_cyc > UINT32_MAX) {
210-
new_next_event_cyc -= (uint64_t)UINT32_MAX + 1U;
212+
int32_t pm_policy_next_event_ticks(void)
213+
{
214+
int32_t cyc_evt = -1;
215+
216+
if ((next_event) && (next_event->value_cyc > 0)) {
217+
cyc_evt = next_event->value_cyc - k_cycle_get_32();
218+
cyc_evt = MAX(0, cyc_evt);
219+
BUILD_ASSERT(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= CONFIG_SYS_CLOCK_TICKS_PER_SEC,
220+
"HW Cycles per sec should be greater that ticks per sec");
221+
return k_cyc_to_ticks_floor32(cyc_evt);
211222
}
212223

213-
next_event_cyc = new_next_event_cyc;
224+
return -1;
214225
}
215226

216227
#ifdef CONFIG_PM_POLICY_DEFAULT
@@ -232,12 +243,12 @@ const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
232243

233244
num_cpu_states = pm_state_cpu_get_all(cpu, &cpu_states);
234245

235-
if (next_event_cyc >= 0) {
246+
if ((next_event) && (next_event->value_cyc >= 0)) {
236247
uint32_t cyc_curr = k_cycle_get_32();
237-
int64_t cyc_evt = next_event_cyc - cyc_curr;
248+
int64_t cyc_evt = next_event->value_cyc - cyc_curr;
238249

239250
/* event happening after cycle counter max value, pad */
240-
if (next_event_cyc <= cyc_curr) {
251+
if (next_event->value_cyc <= cyc_curr) {
241252
cyc_evt += UINT32_MAX;
242253
}
243254

@@ -392,13 +403,12 @@ void pm_policy_event_register(struct pm_policy_event *evt, uint32_t time_us)
392403
k_spin_unlock(&events_lock, key);
393404
}
394405

395-
void pm_policy_event_update(struct pm_policy_event *evt, uint32_t time_us)
406+
void pm_policy_event_update(struct pm_policy_event *evt, uint32_t cycle)
396407
{
397408
k_spinlock_key_t key = k_spin_lock(&events_lock);
398-
uint32_t cyc = k_cycle_get_32();
399409

400-
evt->value_cyc = cyc + k_us_to_cyc_ceil32(time_us);
401-
update_next_event(cyc);
410+
evt->value_cyc = cycle;
411+
update_next_event(k_cycle_get_32());
402412

403413
k_spin_unlock(&events_lock, key);
404414
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ ZTEST(policy_api, test_pm_policy_events)
312312
const struct pm_state_info *next;
313313
uint32_t now;
314314

315-
now = k_cyc_to_us_ceil32(k_cycle_get_32());
315+
now = k_cyc_to_ticks_ceil32(k_cycle_get_32());
316316

317317
/* events:
318318
* - 10ms from now (time < runtime idle latency)
@@ -323,21 +323,21 @@ ZTEST(policy_api, test_pm_policy_events)
323323
*
324324
* first event wins, so we must stay active
325325
*/
326-
pm_policy_event_register(&evt1, 10000);
327-
pm_policy_event_register(&evt2, 200000);
328-
next = pm_policy_next_state(0U, now + k_us_to_ticks_floor32(2000000));
326+
pm_policy_event_register(&evt1, k_ms_to_cyc_floor32(10) + k_cycle_get_32());
327+
pm_policy_event_register(&evt2, k_ms_to_cyc_floor32(200) + k_cycle_get_32());
328+
next = pm_policy_next_state(0U, now + k_sec_to_ticks_floor32(2));
329329
zassert_is_null(next);
330330

331331
/* remove first event so second event now wins, meaning we can now enter
332332
* runtime idle
333333
*/
334334
pm_policy_event_unregister(&evt1);
335-
next = pm_policy_next_state(0U, now + k_us_to_ticks_floor32(2000000));
335+
next = pm_policy_next_state(0U, now + k_sec_to_ticks_floor32(2));
336336
zassert_equal(next->state, PM_STATE_RUNTIME_IDLE);
337337

338338
/* remove second event, now we can enter deepest state */
339339
pm_policy_event_unregister(&evt2);
340-
next = pm_policy_next_state(0U, now + k_us_to_ticks_floor32(2000000));
340+
next = pm_policy_next_state(0U, now + k_sec_to_ticks_floor32(2));
341341
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM);
342342

343343
/* events:
@@ -348,15 +348,15 @@ ZTEST(policy_api, test_pm_policy_events)
348348
*
349349
* system wakeup wins, so we can go up to runtime idle.
350350
*/
351-
pm_policy_event_register(&evt1, 2000000);
352-
next = pm_policy_next_state(0U, now + k_us_to_ticks_floor32(200000));
351+
pm_policy_event_register(&evt1, k_sec_to_cyc_floor32(2) + k_cycle_get_32());
352+
next = pm_policy_next_state(0U, now + k_ms_to_ticks_floor32(200));
353353
zassert_equal(next->state, PM_STATE_RUNTIME_IDLE);
354354

355355
/* modify event to occur in 10ms, so it now wins system wakeup and
356356
* requires to stay awake
357357
*/
358-
pm_policy_event_update(&evt1, 10000);
359-
next = pm_policy_next_state(0U, now + k_us_to_ticks_floor32(200000));
358+
pm_policy_event_update(&evt1, k_ms_to_cyc_floor32(10) + k_cycle_get_32());
359+
next = pm_policy_next_state(0U, now + k_ms_to_ticks_floor32(200));
360360
zassert_is_null(next);
361361

362362
pm_policy_event_unregister(&evt1);

0 commit comments

Comments
 (0)