Commit graph

379 commits

Author SHA1 Message Date
Holt Sun
15af50d202 pm: keep irq restore ownership in idle
Keep the original architecture IRQ key owned by idle across a

successful system PM transition.

Add architecture hooks and the PM_STATE_SET_IRQ_LOCKED migration

contract for SoCs that keep PM hooks from unmasking interrupts.

Signed-off-by: Holt Sun <holt.sun@nxp.com>
2026-05-21 17:02:03 -04:00
Petr Hlineny
77ddb3f36c pm: device_runtime: include device name in "Unbalanced suspend" warning
When `pm_device_runtime_put*()` is called on a device whose usage
count is already zero, `runtime_suspend()` emits "Unbalanced suspend"
at warning level. The message does not include the device name,
so on a system with many runtime-managed devices the warning gives
no hint which call site to fix.

Add `dev->name` to the log message, matching the style of the
neighbouring debug logs in the file.

Signed-off-by: Petr Hlineny <development@hlineny.cz>
2026-05-14 00:48:19 +02:00
Nicolas Pitre
1c9955a060 pm: shell: hold sys_clock_lock() around timer driver calls
sys_clock_set_timeout() and sys_clock_elapsed() are now documented
as requiring the system clock lock to be held by the caller. Wrap
the calls in subsys/pm/pm.c, soc/nxp/rw/power.c and the kernel
shell thread list command with sys_clock_lock()/sys_clock_unlock().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-04-07 11:40:49 -05:00
Raffael Rostagno
8047d8f408 pm: device: Suppress error message when device is busy
Power managed devices should be able to return -EBUSY at
pm_action() (driver) calls without generating error log messages.
Suppress such error messages to allow the driver to decide at
sleep time if it still is unable to suspend, without generating
log noise.

Signed-off-by: Raffael Rostagno <raffael.rostagno@espressif.com>
2026-03-26 14:02:36 -04:00
Yongxu Wang
62f53cccde pm: device: replace magic number with named constant
Introduce enum pm_device_deps_region to replace the magic number
used when iterating through device dependency array regions in
power_domain_add_or_remove().

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
2026-03-14 05:59:45 +01:00
Yongxu Wang
11dc0bde67 pm: device_system_managed: add warning log for resume failures
Add error logging in pm_resume_devices() to report when a device
fails to resume. This improves debuggability by making resume
failures visible in the system log.

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
2026-03-13 14:45:17 +01:00
Jiafei Pan
4773cb1aa0 pm: state: only define variable for enabled CPU Core
Only define variable for enabled CPU Core to fix the following building
warning in case of any CPU Core is not enabled but with "cpu-power-states"
dts node property:

zephyr/subsys/pm/state.c:15:43: warning: 'pmstates_DT_N_S_cpus_S_cpu_0'
defined but not used [-Wunused-const-variable=]

Signed-off-by: Jiafei Pan <Jiafei.Pan@nxp.com>
2026-03-13 14:45:02 +01:00
Yongxu Wang
1a27ec9a18 pm: policy: fix duplicated space in policy
Fix duplicated space in policy_state_lock file

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
2026-03-10 12:37:59 +01:00
Zhaoxiang Jin
224f5f74c4 pm: device: Refine device driver initialization
Only set state to ACTIVE after RESUME succeeds, if it fails,
keep SUSPENDED STATE and attach an error code.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-06 11:36:10 +00:00
Zhaoxiang Jin
de97ddb0e8 pm: device: Refactor power domain add/remove logic
It turns out that power_domain_add_or_remove() when add==true only
looks for DEVICE_HANDLE_NULL empty slots to write to, and never checks
"whether this dev_handle is already in the supported handles or not".
The result is: repeated adds may insert duplicate entries; or when
there are no empty slots, even though it's "already in the domain",
it returns -ENOSPC (which conflicts with the documented semantics).

This commit adds a check: when add==true and rv[i] == dev_handle is
found, it returns -EALREADY and avoids duplicate insertion.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-05 10:02:30 +01:00
Zhaoxiang Jin
2e9f09e12d pm: policy: change 'greater than or equal' to 'greater than'
The comment definition for pm_policy_latency_request_add
states: states that exceed the given latency are unavailable.
However, the implementation in pm_policy_latency_update_locked
does not use the greater-than operator, so according to the
semantics, it should be changed to > to be consistent.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-04 11:42:11 +01:00
Zhaoxiang Jin
9834dac28a pm: policy: add NULL callback support for latency subscriptions
The comment for the API pm_policy_latency_changed_subscribe
states: "NULL to disable", but the function implementation
directly saves and subscribes to this node, and subsequently
calls it directly without null checking when changes occur.
This actually poses a risk of calling a null function pointer.

This commit fixes this issue to make the function implementation
consistent with the API description.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-02 11:01:47 +01:00
Zhaoxiang Jin
2865d5ce0d pm: policy: replace DT_PROP_LEN with ARRAY_SIZE
Use ARRAY_SIZE macro to determine the size of the pm_constraints
array instead of DT_PROP_LEN. Because a practical issue is that
'zephyr, disabling-power-states' may contain multiple power states,
but one of the power states is in a disabled status. In this case,
the size obtained by DT_PROP_LEN is larger than the actual size.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-02 10:58:56 +01:00
Zhaoxiang Jin
af746bc840 pm: policy: Improve the event implementation
1. Updated event-related API docs to fix wording and clarify
semantics ("earliest/soonest event", ticks vs cycles).

2. Improve the event implementation to prevent list corruption on misuse:
- pm_policy_event_register() is now idempotent: re-registering
  the same evt updates its time instead of appending a duplicate node.
- pm_policy_event_update() / pm_policy_event_unregister() become clean
  no-ops if evt is not currently registered.
- Kept a basic evt != NULL assertion.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
2026-03-02 10:58:14 +01:00
Benjamin Cabé
b710a9ec77 doc: include: drop funcprops Doxygen alias
The "@funcprops" Doxygen alias was originally introduced to list all
the function properties that apply to a given function (e.g. isr_ok,
etc.) in the form of a bullet list. However, since commit a258ab32d1
that is no longer the case and all the properties are rendered as
qualifiers (https://www.doxygen.nl/manual/commands.html#cmdqualifier)
and therefore no longer need to be grouped in a separate list.

This commit also ensure all aliases for the actual qualifiers use
`@qualifier` notation instead of `\qualifier` to be consistent with
our Doxygen guidelines.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-02-13 13:55:40 +01:00
Flavio Ceolin
7a3d2b6167 pm: policy_events: Fix variable scope
next_event should not be global. This can cause
linkage conflicts if another file defines a symbol
with the same name.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2026-02-09 09:38:27 +01:00
Alessandro Manganaro
429350b1be pm: add optional custom ticks hook for system suspend
Add an optional power policy hook to provide custom "next event"
ticks during pm_system_suspend(), in addition to the existing
kernel ticks and policy event list.

This hook enables applications and SoC-specific code to derive
the next wake-up time directly from proprietary or
hardware-specific data structures (e.g. hardware registers,
binary-only modules, complex schedulers) that are not
practical to convert into the standard PM policy event list.

The feature is gated by CONFIG_PM_CUSTOM_TICKS_HOOK and is fully
backwards compatible when disabled.

Signed-off-by: Alessandro Manganaro <alessandro.manganaro@st.com>
2026-01-26 10:13:53 -06:00
Flavio Ceolin
59a57722ae pm: Fix wrong type promotion
In pm_system_suspend there is a possible invalid type promotion
in sys_clock_set_timeout(MAX(0, ticks - exit_latency_ticks), true);

ticks is int32_t and exit_latency_ticks is uint32_t consequently
ticks is promoted to uint32_t resulting in a possible underflow and
setting a wrong value in sys_clock_set_timeout().

Fixes #100005
Coverity CID: 535628

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2026-01-22 12:54:52 -05:00
Flavio Ceolin
15d6ab8ae8 pm: device: Acknowledge unused variable
action_cb is not used in pm_device_driver_deinit is
not used when CONFIG_PM_DEVICE is enabled.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2026-01-20 13:17:37 +00:00
Flavio Ceolin
96c44c540b pm: device_runtime: Fix possible inconsistent state
In pm_device_runtime_get, when resume fails after the domain as claimed,
the flag PM_DEVICE_FLAG_PD_CLAIMED is not cleared (but the domain is
released). This leaves this flag in a consistent state and in a further
resume this device won't resume its domain leading to bigger problems.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
2026-01-20 13:17:37 +00:00
Albort Xue
3781f6bf9f pm: device_runtime: fix PD_CLAIMED flag logic in ISR_SAFE context
Fix the power domain claiming condition which was inverted, causing
domains to be claimed when they were already claimed instead of when
they weren't. Add null check for power domain before accessing its
properties to prevent potential null pointer dereference. Also ensure
the PD_CLAIMED flag is properly cleared when putting the power domain.

Signed-off-by: Albort Xue <yao.xue@nxp.com>
2025-11-13 23:13:57 +02:00
Mahesh Mahadevan
11dbae8a32 pm: Fix warning with pm_policy_priv_device_find_device_constraints
pm_policy_priv_device_find_device_constraints() function
is available only when zephyr_power_state is defined.

Signed-off-by: Mahesh Mahadevan <mahesh.mahadevan@nxp.com>
2025-11-04 13:45:14 -05:00
Krzysztof Chruściński
3595c9d0d4 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>
2025-10-16 17:15:20 +03:00
Krzysztof Chruściński
336e89efd6 pm: policy: Add option to lock all power states
Add function for getting and putting a lock for all power states.
It is much faster version of requesting 0 us latency with actual
intention to disable all power states.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-10-16 17:15:20 +03:00
Declan Snyder
74d715c5ef pm: Remove "power state consistency" check
This check is totally wrong, min-residency-us and exit-latency-us are
not related to each other at all except that the end of the residency
period occurs when the exit starts, so they are totally orthogonal
values and should not be checking if one is less than the other or
anything like this. Even in the code for the default policy manager,
they are added together, so this check is incompatible / in disagreement
with that code, so it should be removed.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-10-09 12:37:07 -04:00
Declan Snyder
a4225b1ee8 pm: Add constraint lists and APIs
Some pieces of the system may have custom types of constraints that they
define based on different effects and reasons than just the standard
"zephyr,disabling-states". To avoid every single one of these component
reinventing the wheel, make some common APIs to handle these type of
custom constraint lists.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-09-25 14:18:05 -04:00
Declan Snyder
ae1f13149f pm: Extend pm notifier to be able to report substate
I am surprised this notifier didn't already report the substate id, it
seems important since different substate obviously are defined for a
reason, they can be having a different effect on the system.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-09-25 14:18:05 -04:00
Aksel Skauge Mellbye
cfa2992a64 pm: Mark pm_device_driver_init as boot function
Mark pm_device_driver_init() as a boot function to support using it
for early init on devices with demand paging.

Signed-off-by: Aksel Skauge Mellbye <aksel.mellbye@silabs.com>
2025-09-23 17:49:38 -04:00
Bjarki Arge Andreasen
1767f131aa pm: refactor PM_S2RAM_CUSTOM_MARKING option to be promptless
The config PM_S2RAM_CUSTOM_MARKING is not an optional config for a
user to select, it is required by some soc implementations of S2RAM,
in which case it must be selected by the soc.

Refactor the configuration to be HAS_PM_S2RAM_CUSTOM_MARKING, and
make the currently only soc which needs it select it. Then update
samples which previously had to select this option for this soc.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-09-23 12:07:59 -04:00
Bjarki Arge Andreasen
4626f6b776 pm: bind the PM_S2RAM kconfig option to devicetree
Currently it is possible to have an enabled (status="okay")
"suspend-to-ram" "zephyr,power-state" in the devicetree,
CONFIG_PM=y, but CONFIG_PM_S2RAM=n

This means the presence and state of the "suspend-to-ram" power
state in the devicetree does not match the PM_S2RAM option,
despite PM_S2RAM being dependent on the devicetree state.

This commit makes the devicetree the "source of truth" for
whether one or more s2ram power states shall be included and
supported, by enabling PM_S2RAM if any s2ram power state with
status "okay" is present in the devicetree. To disable the
s2ram power state, like with any other state, disable it by
setting its state to "disabled".

The help of the now promptless PM_S2RAM config has been
updated to reflect the new meaning. It previously held
cortex-m specific implementation details, these have been
removed as these details are already present in the file
pointed to as well (arch/arm/core/cortex_m/pm_s2ram.c
line 22)

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-09-23 12:07:59 -04:00
Bjarki Arge Andreasen
5438462a6a pm: device: runtime: Only select EVENTS if PM_DEVICE_RUNTIME_ASYNC
The k_event event is only used if CONFIG_PM_DEVICE_RUNTIME_ASYNC is
selected, but EVENTS is selected, and the k_event included in the
struct pm_device if CONFIG_PM_DEVICE_RUNTIME is selected.

Correct to only include event and EVENTS if
CONFIG_PM_DEVICE_RUNTIME_ASYNC is selected. This saves us 1
struct event per PM DEVICE.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-09-19 16:33:06 +02:00
Bjarki Arge Andreasen
34c51c5ab4 pm: device: runtime: add PM_DEVICE_RUNTIME_DEFAULT_ENABLE
Many SoCs which use PM_DEVICE_RUNTIME need every device in the system
to have PM_DEVICE_RUNTIME enabled to function. Currently, this is only
possible by adding zephyr,pm-device-runtime-auto; to every node in
every devicetree which could potentially implement device power
management. This is very error prone since its easy to miss a node,
especially if users apply overlays, where users need to know and
remember to apply or reapply this property.

This commit adds a Kconfig, disabled by default, which automatically
treats every device as if it had the zephyr,pm-device-runtime-auto
property added to every node.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-09-19 08:06:09 +02:00
Yongxu Wang
4121adff1c pm: introduce pm_cpu_shell interface
Introduce a shell for specific cpu core about PM control,
this aligns with existing design flow where the policy decides
the state based on next event timing, locks and latency constraints,
and the system naturally enters low power through the idle thread.

New shell commands under `cpu`:
  -  cpu states			: List supported CPU power states (from devicetree)
  -  cpu available		: For each state/substate, show if available
  -  cpu lock			: Lock specific low power mode
  -  cpu unlock			: Unlock specific low power mode
  -  cpu idle ms		: Sleep the current thread to let idle run PM

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
2025-09-18 13:48:30 -04:00
Yongxu Wang
8767cec875 pm: states: add enum-string conversion helpers
Add two helper functions to convert power management states between
enum and string:

- pm_state_to_string()
- pm_state_from_string()

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
2025-09-18 13:48:30 -04:00
Bjarki Arge Andreasen
682c835113 pm: device_runtime: put_sync_locked: fix usage count on err
If pm action fails within put_sync_locked, usage count should be
reset to reflect the state of the device.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-09-05 07:44:08 +02:00
Benjamin Cabé
dd4bf1aadc pm: policy: doc: fix spelling of help text
s/whithout/without/

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-09-03 17:04:13 +02:00
Bastien Beauchamp
ad849debee pm: skip min_residency_ticks calculation if input is 0
In the default PM policy, the function k_us_to_ticks_ceil32() is used
and does calculation using 64-bit integers which can be slow and
avoidable if the input is 0.

Signed-off-by: Bastien Beauchamp <bastien.beauchamp@silabs.com>
2025-08-28 08:57:06 +02:00
Dmitrii Sharshakov
b98e7f501f pm: fix NULL pointer dereference with stats
Fix null pointer dereference in pm_system_suspend

Fixes #89228

Signed-off-by: Dmitrii Sharshakov <d3dx12.xx@gmail.com>
2025-08-20 02:17:20 +02:00
Adrien Lessard
c5229fcda6 pm: device_runtime: fix pm_device_runtime_get rollback
In a call to pm_device_runtime_get, if the power domain is taken but
action_cb fails, the power domain is not released.

Signed-off-by: Adrien Lessard <adrien.lessard@rbr-global.com>
2025-08-14 15:11:13 -04:00
Andrzej Puzdrowski
20695ed091 arch/arm/core/cortex_m: optional memory section for _cp_context
More complex suspend and resume scheme might require exactly defined
location of this variable due to platform peculiar SW and HW requirement.

DTS zephyr,memory-region node with nodelabel `pm_s2ram` shall be used to
automatic definition of linker section for such objective.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2025-08-07 13:15:08 +02:00
Bjarki Arge Andreasen
cd16d17dca pm: device: introduce pm_device_driver_deinit()
Introduce pm_device_driver_deinit() which is required to fully
support device_deinit(). This function shall be called by drivers
when device_deinit() is called.

If PM_DEVICE is enabled, it will
simply check whether the device is either SUSPENDED or OFF, this
will prevent device_deinit() on a device which is currently in
use, and ensure the device is in the correct state if device_init()
is called later.

If PM_DEVICE is not enabled, it will invoke the SUSPEND action which
mirrors the pm_device_driver_init() behavior. Note that TURN_OFF
action is not called since the device is deinitialized after this
call.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-07-29 22:56:02 -04:00
Serhiy Katsyuba
98dfa0aa0b pm: Fix to prevent incorrect forever timeout
K_TICKS_FOREVER is defined as -1. Guard logic has been added for the case
when `ticks - exit_latency_ticks == -1` to prevent sys_clock_set_timeout()
from incorrectly setting a forever timeout.

Signed-off-by: Serhiy Katsyuba <serhiy.katsyuba@intel.com>
2025-07-19 13:27:08 -04:00
Christoph Mair
f64358fcdb pm: fix typo in KConfig
Workqueue is missing a "k".

Signed-off-by: Christoph Mair <christoph.mair@husqvarnagroup.com>
2025-07-04 13:05:56 -05:00
Bjarki Arge Andreasen
ce95b7033c pm: device_runtime: fix unbalanced domain get/put
pm_device_runtime has inconsistent behavior regarding
getting and putting a device's domain. This commit
aligns it to get the domain only once, once device
is resumed, and put only once, once device is
suspended.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-06-24 09:14:20 +02:00
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