Commit graph

58 commits

Author SHA1 Message Date
Tomasz Leman
750821547a pm: Return false if pm_state_force fails to set state
Ensure `pm_state_force` returns `false` when it fails to retrieve or set
the desired power state. This change improves error handling and ensures
the function's behavior aligns with its intended purpose.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
2025-05-15 14:09:46 +02:00
Keith Short
5615e466c9 pm: Cleanup PM stats
Convert PM stats code to use IS_ENABLED in place of #ifdefs.

Signed-off-by: Keith Short <keithshort@google.com>
2025-04-30 18:44:32 +02:00
Krzysztof Chruściński
216476caaa pm: Fix case when forced state is not set
pm_suspend is returning early if there are no states
available (due to locking or latency policy). However,
if state is forced it should not return but rather enter
forced power state.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-28 16:40:41 +02:00
Krzysztof Chruściński
5a313e141d pm: Add option to quickly detect power state availability
When all states are locked or latency requirement cannot be met
by any power state it is important to be able to quickly exit
suspend procedure because that usually means that application
requires high performance. Add function for detecting if any
power state is available.

Additionally, add function pm_policy_state_is_available for
checking if given state is available which means that it is not
locked and fulfills current latency requirement.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-21 07:45:07 +02:00
Krzysztof Chruściński
ecabcf5db5 pm: Use pointers for current and forced power states
Use power state pointers instead of copies which improves performance.

Align power_mgmt_multicore test which was creating pm states in
runtime.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-17 21:17:18 +02:00
Krzysztof Chruściński
226c6ae17c pm: Optimize pre-wakeup in suspend procedure
When core is suspended and power mode has non zero exit_latency_us
a system timeout will be rescheduled to a point in time that is
earlier by exit_latency_us than request. It is to accommodate for
lengthy resuming procedure which would cause requested timeout to
be significantly late. However, setting additional wake up point
has cost, it is one more redundant core wake up and that impacts
performance and power consumption.

Add Kconfig option to chose what conversion method is used. It has
the biggest impact on small exit_latency_us where conversion may
result in 0 ticks (no pre-wake up) or 1 tick (wake up).

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-08 19:26:57 +02:00
Krzysztof Chruściński
29d9405036 pm: Use fixed cpu id in single core case
When there are no multiple cores than fixing id to 0 saves few
cycles.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-08 08:57:57 +02:00
Flavio Ceolin
5e3f3ce3f5 pm: Remove deprecated function
z_pm_save_idle_exit was deprecated before 3.7, it is time to remove it.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2025-02-10 03:41:06 +01:00
Ryan McClelland
9f8120528d pm: fix warning for missing initializer
A warning is giving for missing initalizer for field `exit_latency_us`
of `struct pm_state_info`. This adds the additional init fields.

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
2024-12-09 01:40:18 +01:00
Riadh Ghaddab
12a437c4c9 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 <rghaddab@baylibre.com>
2024-09-09 13:56:04 -04:00
Flavio Ceolin
1c2e988021 pm: device: De-couple device pm from system pm
PM_DEVICE is not attached to system managed device power management.
It is a very common use case targets with device runtime power
management that don't want system device power management enabled.

We introduce a new symbol (PM_DEVICE_SYSTEM_MANAGED) to explicit
control whether or not system device power management should be
globally enabled.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-04 19:13:53 -04:00
Flavio Ceolin
c65be0cfb9 pm: Exclude device pm path when it is not needed
Remove device pm path when there is no is no power state in DT with
device pm enabled. This basically does the same thing that was done
by PM_DEVICE_RUNTIME_EXCLUSIVE.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-04 19:13:53 -04:00
Flavio Ceolin
2f99ff51cc pm: Disable device pm per power state
Make it possible to disble device power management individually per
power state.  This allows targets tuning which states should
(and which should not) trigger device power management.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-04 19:13:53 -04:00
Flavio Ceolin
4c5ee9b2db pm: system: Restore clock after sleep
sys_clock_idle_exits was being called *only* when the system
was resumed from interruption.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-27 02:10:03 -07:00
Flavio Ceolin
1860dd9153 pm: system: Resume devices in pm_system_resume
pm_system_resume() can be called directly from ISR and because of this
devices should be resumed before calling pm_state_exit_post_ops().

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-27 02:10:03 -07:00
Flavio Ceolin
f7437ac3b1 pm: Move z_pm_save_idle_exit to pm subsys
There is no need to this function be defined inside the kernel since
all places using it are protecting the call under ifdef PM guards.

This way we can also remove the ifdef condition inside the implementation.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-27 02:10:03 -07:00
Dong Wang
6a962fb50a pm: re-program timer HW only when the exit latency is not zero
Avoid re-programming timer unnecessarily.

Signed-off-by: Dong Wang <dong.d.wang@intel.com>
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-06 17:31:40 +01:00
Flavio Ceolin
3b895064af pm: Remove state lock/unlock API
This API is not widely used and it is actually broken since device
runtime power management is not checking it when suspending and
resuming.

On top of that, this API is very close to pm_device_busy* API,
close enough to consolidate in only one API.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-04-09 20:11:48 +02:00
Flavio Ceolin
e2058a9537 pm: Fix a corner case when policy returns NULL
When the policy returns NULL pm_system_suspend was assuming that
the current state in z_cpus_pm_state was ACTIVE, since that is the
state set after the core wakes and return to this function. The
problem is that in cases where the cpu does not preserve the context,
and returns to this function, z_cpus_pm_state has the value of the
last state used and the cpu use it again.

Fix it setting z_cpus_pm_state to ACTIVE every time the policy returns
NULL.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-03-20 07:38:56 -05:00
Flavio Ceolin
b9d4b9d9ab pm: Remove CURRENT_CPU macro
Just use _current_cpu that works with/without multicore.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-01-19 13:49:54 +01:00
Mahesh Mahadevan
6cb1ff6560 pm: Move settting timeout to after suspending devices
pm_suspend_devices() could return an error. Set timeout using
sys_clock_set_timeout() to after this error is handled so that
we have the accurate power state when calling the timeout function.
This is useful in cases where we wish to compensate the
system timer for certain power modes.

Signed-off-by: Mahesh Mahadevan <mahesh.mahadevan@nxp.com>
2023-12-18 09:29:49 +01:00
Anas Nashif
fcf50ed6e7 kernel: move timeout_q.h to kernel/include
This is a private kernel header with private kernel APIs, it should not
be exposed in the public zephyr include directory.

Once sample remains to be fixed (metairq_dispatch), which currently uses
private APIs from that header, it should not be the case.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-12 12:55:36 -04:00
Gerard Marull-Paretas
e1eedd1a9f pm: require pm_state_set/pm_exit_post_ops
Any system supporting PM must now implement
pm_state_set/pm_exit_post_ops. Before this change any platform could
enable CONFIG_PM=y, even though it did nothing, ie, no power savings at
all.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-07-20 10:33:00 +00:00
Flavio Ceolin
4f29930e4c pm: Fix cpus active count
Only set a cpu as active (on pm subsystem) when the cpu is effectively
initialized. We cannot assume on pm subsystem that all cpus were
initialized since when the option CONFIG_SMP_BOOT_DELAY is used cpus are
initialized on demand by the application.

Note that once cpus are properly initialized the subystem is able to track
their status.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-06-01 10:05:31 +02:00
Kumar Gala
eec299c130 pm: Use TYPE_SECTION macros for pm_device_slots
Clean up pm_device_slots to utilize TYPE_SECTION macros for handling
sections.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-19 17:15:29 +02:00
Andy Ross
d20ab8c609 subsys/pm: Use correct timeout API
The z_set_timeout_expiry() function is a wrapper provided in the
timeout subsystem that has some corrections for timelicing.  But the
suspend code doesn't care; by definition there are no active threads
that might be timeslicing at the point where the OS is suspending.
Use the lower-level timer driver API instead.

(z_set_timeout_expiry() is also about to disappear)

Signed-off-by: Andy Ross <andyross@google.com>
2023-03-09 09:21:12 +01:00
Mahesh Mahadevan
ae97eae8c2 pm: Error with handling z_cpus_active count
z_cpus_active count becomes incorrect when the state is
PM_STATE_RUNTIME_IDLE and CONFIG_PM_DEVICE is enabled.

Signed-off-by: Mahesh Mahadevan <mahesh.mahadevan@nxp.com>
2023-01-23 10:06:17 +00:00
Corey Wharton
21a7c2d7eb pm: don't suspend unready devices when entering low power states
The PM subsystem should not call the PM control callbacks on
uninitialized devices when entering low-power states.

Signed-off-by: Corey Wharton <xodus7@cwharton.com>
2022-12-19 09:46:17 +00:00
Kumar Gala
dd03af653d pm: Rework usage of CONFIG_MP_NUM_CPUS to CONFIG_MP_MAX_NUM_CPUS
Replace usage of CONFIG_MP_NUM_CPUS with CONFIG_MP_MAX_NUM_CPUS for
init and declaration as we phase out CONFIG_MP_NUM_CPUS usage.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2022-10-27 13:51:55 -04:00
Carlo Caione
1608c8adba pm: Introduce CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE
Sometimes we want to entirely decouple the system PM from the device PM,
leaving the devices to manage its own power states using the runtime PM.

This is currently not possible because the suspend / resume code path is
triggering the device PM hooks even when the runtime PM is enabled.

Introduce a new PM_DEVICE_RUNTIME_EXCLUSIVE symbol to allow the platform
to skip the device PM triggers on suspend / resume.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-06-22 12:24:43 +02:00
Flavio Ceolin
f232db4630 pm: Avoid unnecessary check in SMP
_current_cpu is a macro that expands to an assert that checks if the
current context can be migrated to a another cpu. Since this
pm_system_suspend() is called from the idle thread (each cpu has its
own) and  with locked this check is redundant. Just use
arch_curr_cpu() to avoid it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-06-16 19:53:20 -04:00
Flavio Ceolin
2cd49ca0fe pm: Fix possible assertion when resuming
pm_system_resume() can be called from the idle thread with
interruptions unmasked. In this situation, _current_cpu will call
z_smp_cpu_mobile() that will return true and cause an assert.

In this function we don't need to check if the current context can be
preempted, we just need the information about which cpu is executing
it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-06-16 19:53:20 -04:00
Gerard Marull-Paretas
8cb33df108 pm: remove redundant <zephyr/zephyr.h> includes
Files including <zephyr/kernel.h> do not have to include
<zephyr/zephyr.h>, a shim to <zephyr/kernel.h>.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-06-15 09:13:11 +02:00
Flavio Ceolin
bbb8c91afa pm: Fix possible race condition in multicore
There is a race condition in a multicore system that happens when the
idle thread in a CPU checks if the state was forced, if not it will
call the policy manager. If a secondary core forces a state after that
this point the value returned by the policy will be rewritten.

Another case is, if a state is forced while a CPU is sleeping,
when this CPU resumes, the forced bit is cleared and the forced state
is never be used.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-05-13 08:48:29 -07:00
Flavio Ceolin
2b02843ca3 pm: Remove unnecessary goto
In pm_system_suspend there is a goto label that is supposed to be used
to do cleanup before exit the function but it is not doing anything
right now. Just remove it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-05-13 08:48:29 -07:00
Gerard Marull-Paretas
5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00
Krzysztof Chruscinski
47ae656cc1 all: Deprecate UTIL_LISTIFY and replace with LISTIFY
UTIL_LISTIFY is deprecated. Replacing it with LISTIFY.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-08 11:03:30 +01:00
Flavio Ceolin
78454e5e93 pm: Rename pm_power_state_exit_post_ops
Aligning with the rest of PM API, replace pm_power_state_exit_post_ops
with pm_state_exit_post_ops.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Flavio Ceolin
f74c38e061 pm: Rename pm_power_state_next_get
Aligning with the rest of PM API, replace pm_power_state_next_get with
pm_state_next_get.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Flavio Ceolin
6142fcb8ba pm: Rename pm_power_state_force
Aligning with the rest of PM API, replace pm_power_state_force with
pm_state_force.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Flavio Ceolin
89fee61b43 pm: Rename pm_power_state_set
Aligning with the rest of PM API, replace pm_power_state_set with
pm_state_set.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Carlo Caione
c5894df5ec pm: Change parameters of pm_power_state_{set,exit_post_ops}
Just pass state and substate_id instead of the whole info structure
pointer.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Carlo Caione
8143f206dd pm: Rename exit_pos_ops to pm_exit_pos_ops
Because exit_pos_ops is the only function without the prefix.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Carlo Caione
11f1dd2370 pm: Reference pm_state_info only by pointer
It's unnecessary to move the pm_state_info around by value, just use a
pointer.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Flavio Ceolin
350014d399 pm: Clear the forced flag in the error path
Now that the forced flag is cleared with the kernel locked in the
suspend path, we need to clear it out when the suspend process fails.

This is being done before jump to the end label because in the
successful path the flag is already cleared.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-01 14:41:05 -05:00
Flavio Ceolin
2e9589bd44 pm: Just check the forced flag
When suspending we don't need to set the forced flag but just check if
it is set.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-01 14:41:05 -05:00
Flavio Ceolin
fdf7465851 pm: Fix multithread issue with force state
The "state forced" flag has to be cleared with the scheduler locked,
otherwise the idle thread can be scheduled out without clear it and
the next call to force a state will fail.

Fixes #41911

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-01 14:41:05 -05:00
Flavio Ceolin
06bdd1001c pm: Better name for cpus state variable
A better name for array tracking cpus pm state.
s/z_power_states/z_cpus_pm_state/g

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-01-19 14:22:08 -05:00
Flavio Ceolin
baf50b5cdb pm: Proper initialize cpu power states
Do not make assumptions about the enum value for ACTIVE state and
explicitly set it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-01-19 14:22:08 -05:00
Gerard Marull-Paretas
696caa0524 pm: policy: return a reference to the next state
Return a constant reference to the next state instead of a copy of
struct pm_state_info. When the next state should be active, just return
NULL. Struct copying should be in general avoided, specially in code
paths executed frequently as is this one.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-01-11 10:46:20 +01:00