Commit graph

335 commits

Author SHA1 Message Date
Benjamin Cabé
5f9d4385f3 pm: adopt SHELL_HELP
Adopt SHELL_HELP macro for pm_shell

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-06-13 10:42:16 -04:00
Declan Snyder
6bd6e50838 pm: Add API to check if a state disables a device
Add an API function that can be used by client code to check if a state
disables a device.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-05-30 19:40:11 +02:00
Declan Snyder
65ebecbf78 pm: refactor policy_device_ get/put
Refactor this code so it has less nesting and is able to share some code
between functions, by making a function to find the device constraints
object which can be called before doing operations using it.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-05-30 19:40:11 +02:00
Flavio Ceolin
437b56d22e pm: device_runtime: Make async optional
Async now uses its own work queue, which means it consumes more
resources. Since not all applications need the async API, we can make
it optional without any penalty for those applications.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2025-05-27 14:45:07 +02:00
Flavio Ceolin
64e38bab55 pm: device_runtime: Allow to use a dedicated wq
Device runtime is using the system workqueue to do operations
that are mostly blockers (suspend a device). This should not happen.

This commit adds an option to use dedicated queue for the device runtime
async operations.

The test for this API was assuming that the system workqueue
priority (which is cooperative) so we need to change the test priority
to be lower than the device runtime workqueue priority.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2025-05-27 14:45:07 +02:00
Zhaoxiang Jin
1b230f8741 pm: policy: default: Removed unused code
Removed unused code

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2025-05-21 12:35:08 +02:00
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
Krzysztof Chruściński
8ad44471b9 pm: Include disabled states in search inside pm_state_get
pm_state_get() is used to get a PM state that can be forced.
Disabled states (not included in automatic state selection) can
also be forced thus they should be included in search inside
pm_state_get().

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-05-05 21:56:55 +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
bb4fb12ca4 pm: policy: default: Optimize power state selection
Update power state selection. Previously, it was iterating over states
starting from the last one so the most common short sleep periods were
taking the longest time to select. Order is now swapped so that short
sleeps will get power state as quick as possible.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-21 07:45:07 +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
Jordan Yates
71a0f39568 pm: device: correct state in pm_device_driver_init
Set the value of `pm->state` as we move through the various stages of
`pm_device_driver_init`. This ensures hat if any of the code inside the
actions callbacks runs `pm_device_state_get` they get the correct state,
instead of always getting `PM_DEVICE_STATE_ACTIVE` (0, the value at
reset).

Signed-off-by: Jordan Yates <jordan@embeint.com>
2025-04-11 14:53:53 +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
Flavio Ceolin
b8ce176109 pm: policy: Fix possible undefined reference
When PM_POLICY_DEVICE_CONSTRAINTS is enabled, we create a list of
devices with the property “zephyr,disabling-power-states” and their
respective states to lock. However, if a device is declared but not
built (e.g., due to a Kconfig option), we have an undefined reference.

This PR changes the code to use the device name instead of the
reference. This increases lookup time (which could be cached at
runtime), and we still have an entry for a non-existent device.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2025-02-08 04:06:29 +01:00
Flavio Ceolin
5bc6446dcc doc: releases: 4.1: Document removed PM symbol
The deprecated PM_DEVICE_RUNTIME_EXCLUSIVE option is been removed
in this release.

Signed-off-by: Flavio Ceolin <flavio.ceolin@gmail.com>
2025-01-31 07:50:17 +01:00
Yishai Jaffe
1b4cef325b shell: use shell_device_get_binding
Use shell_device_get_binding() instead of device_get_binding() so that
we get the device based on its name and in addition by its label.

Signed-off-by: Yishai Jaffe <yishai1999@gmail.com>
2024-12-18 15:32:10 +01:00
Bjarki Arge Andreasen
59779ebe4b pm: policy: event: use uptime ticks
Update events to use uptime ticks, which is a monotonic clock which
in the same res as kernel ticks. This makes comparisons simple and
removes the complexity of dealing with wrapping counter values.

The wrapping is particularly problematic for events since this makes
it quite complex to track if an event has occured in the past, or
will occur in the future. This info is needed to know if an event
has actually been handled or not.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2024-12-09 03:55:52 +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
Krzysztof Chruściński
3b47ec6410 pm: device_runtime: Optimize pm_device_runtime_usage
There is no point in using lock or semaphore to read current
usage counter as it may change after unlocking or giving
back the semaphore. Value can only be trusted in the controlled
environment (e.g. test).

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2024-11-26 15:43:21 -05:00
Krzysztof Chruściński
581c55496d pm: device_runtime: Fix bitfields misuse
PM_DEVICE_FLAG_ISR_SAFE is an enum and it must be converted to
a bit mask before masking with flags.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2024-11-19 09:51:14 -05:00
Bjarki Arge Andreasen
14117b453d pm: policy: separate default policy and events
The default policy currently directly references the private
variable next_event from policy_events.c to then convert the cycle
of said event (if exists) to a kernel tick in the future, something
policy_events.c already implements and exposes through
pm_policy_next_event_ticks().

Additionally, the implementation of pm_policy_next_state() in
policy_default.c already gets the nearest kernel tick, wherein
the next event has already been accounted for in, see
implementation of pm_system_suspend().

This commit removes the redundant and layer violating computation
if the tick of the next event from policy_default.c and updates
the test test_pm_policy_events to not use default policy to
determine if pm_policy_next_event_ticks() is correct.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2024-11-16 15:56:02 -05:00
Bjarki Arge Andreasen
145d04101d pm: policy: fix pm_policy_event_register arg
The pm_policy_event_register() API takes absolute cycles as the
second arg, like pm_policy_event_update(), but the arg is renamed
time_us and treated as a relative time in us rather than abs
cycles.

Fix implementation of pm_policy_event_register() to treat cycles
like pm_policy_event_update() and API docs suggest.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2024-11-05 10:54:41 -06:00
Gerard Marull-Paretas
578008cd47 pm: policy: allow using latency API in standalone mode
This option allows using the pm_policy_latency* APIs to gather latency
requirements on systems that do not support PM (e.g. systems whithout
CPU idle states). Because the API has a subscription mechanism, it can
be useful to perform system-level adjustments based on latency
requirements gathered from multiple system components.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2024-10-24 17:50:44 +02:00
Gerard Marull-Paretas
6f4bb118a8 pm: policy: split policy APIs implementations
policy.c has grown organically, it contained many independent pieces of
code. This patch splits each logical unit into its own C file, making it
easier to browse the code.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2024-10-24 17:50:44 +02:00
Yong Cong Sin
52a202309b zephyr: bulk update to DT_NODE_HAS_STATUS_OKAY
Change instances of:

DT_NODE_HAS_STATUS(<node_id>, okay)

to

DT_NODE_HAS_STATUS_OKAY(<node_id>)

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-10-03 17:06:52 +01:00
Gerard Marull-Paretas
740eba1341 pm: device: allow optional support of TURN_ON action
Some devices, e.g. SoC level devices like I2C peripheral, can never be
powerd off as they are always energized. Such devices can only go from an
active state or to a low power state (suspended). Allow them to simply
return -ENOTSUP when called with TURN_ON (or TURN_OFF).

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2024-09-30 17:11:20 +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
Adam Kondraciuk
be523ad396 arch: arm: cortex_m: pm_s2ram: Minor typo fix
Fix typo in `PM_S2RAM_CUSTOM_MARKING` description.

Signed-off-by: Adam Kondraciuk <adam.kondraciuk@nordicsemi.no>
2024-09-06 11:29:06 -04:00
Fabio Baltieri
f3a6454f93 pm: refactor pm_device_driver_init
Refactor pm_device_driver_init code to keep the normal execution path
inline and the early exit branches at a single indentation, this is
commonly done throughout the code base.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2024-08-02 03:31:17 -04:00
Flavio Ceolin
f9de58efb9 pm: Do not select deprecated symbol
PM_DEVICE_RUNTIME_EXCLUSIVE was deprecated and its behavior
is achived with PM_DEVICE_SYSTEM_MANAGED=n.

Fixes #76037

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-07-22 03:28:54 -04:00
Flavio Ceolin
e1f20f36a0 pm: policy: Move device power state constraints to policy
Move information about device power state constraints from device
to policy.

It slows down the constraints lookup since we now have to find the
constraints for a device in a global array, but it saves resources
because we don't need to add a reference to constraints in all devices
instances.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-28 12:22:28 -04:00
Jordan Yates
07870934e3 everywhere: replace double words
Treewide search and replace on a range of double word combinations:
    * `the the`
    * `to to`
    * `if if`
    * `that that`
    * `on on`
    * `is is`
    * `from from`

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-22 05:40:22 -04:00
Alberto Escolar Piedras
1949f3205e subsys/pm/Kconfig: Fix typo
Fix a trivial typo in Kconfig

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-06-18 20:02:00 -04:00
Flavio Ceolin
836c2ccfd4 pm: device_system_managed: Fix functions return
Fix functions return when CONFIG_PM_DEVICE_SYSTEM_MANAGED is
not enabled.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-18 19:56:39 -04:00
Flavio Ceolin
73b755d817 pm: Add a symbol for device power state constraints
Add a symbol to enable device power state constraints this
saves resources when this feature is not needed.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-07 19:06:23 -04:00
Flavio Ceolin
02a14d75fc pm: Declare pm state constraints for a device
Declare power state constraints for a device in devicetree.
It allows a map between device instances and power states that disable
their power. This information is used by a new API
(pm_policy_device_power_lock_put/get) that automically set/release
pm state constraints.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-06-07 19:06:23 -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
94af630b22 pm: Deprecate PM_DEVICE_RUNTIME_EXCLUSIVE
That is option has shown confusing on it is attempt to prevent
system pm doing device power management. Lets address this
problem properly.

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
Flavio Ceolin
6569ae1798 pm: device_runtime: Fix usage API
Fix pm_device_runtime_usage() API for isr safe devices.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2024-05-22 15:08:39 -05:00
Krzysztof Chruściński
24082d582f arch: arm: cortex_m: pm_s2ram: Add option for custom marking
s2ram procedure used RAM magic word for marking suspend-to-RAM. This
method may not work in some cases, e.g. when global reset does not
reset RAM content. In that case resuming from s2ram is detected when
global reset occurred.

RAM magic word method is the default but with
CONFIG_PM_S2RAM_CUSTOM_MARKING a custom implementation can be provided.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2024-05-17 14:33:47 +02: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