Check if device runtime is enabled in the beginning of
pm_device_runtime_get to avoid unnecessary checks.
This does not change the current behavior.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Rework the implementation of `pm_device_children_action_run` to use the
common `device_supported_foreach` iterator.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
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>
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>
To be able to call pm_device_runtime_put() from an IRQ context, move
from mutexes to semaphores and force the async path when the put
operation is called from an ISR.
Signed-off-by: Carlo Caione <ccaione@baylibre.com>
For the async operation move from condition variables to events to
reduce the dependency on the mutexes that cannot be used in IRQ
context.
Signed-off-by: Carlo Caione <ccaione@baylibre.com>
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>
Change for loops of the form:
for (i = 0; i < CONFIG_MP_NUM_CPUS; i++)
...
to
unsigned int num_cpus = arch_num_cpus();
for (i = 0; i < num_cpus; i++)
...
We do the call outside of the for loop so that it only happens once,
rather than on every iteration.
Signed-off-by: Kumar Gala <kumar.gala@intel.com>
There is a typo in argument for the STATS_NAME_INIT_PARMS macro.
This causes an error during build.
The correct reference should be "pm_stats".
Signed-off-by: Steven Slupsky <sslupsky@gmail.com>
Current API allowed to get notified when the maximum system latency
changes, however, a single callback was allowed. The design was intended
for SoC specific actions when latency changes. However, in some cases
drivers may also want to know the current maximum latency to perform
local actions if other parts of the system modify it.
This patch updates the API with a pair of subscribe/unsubscribe calls to
achieve such goal. Tests have been updated to show how the API can be
used.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
When new latency requirements are introduced/updated, the microseconds
value gets converted down to ticks. Ticks usually have a coarse
resolution compared to the microseconds scale. This is fine for making
PM state change decisions, however, when getting notified about latency
changes, we may want to know the real value in microseconds, even if the
system rounds to ticks internally. This patch stores the value in both
us and ticks (in ticks to cache the conversion, really), so that the
user will get notified with precise latency values.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Check that the device we are trying to use has successfully powered up
when it is on a power domain. If it has not, release the request for the
power domain to be powered to ensure we do not have dangling requests.
As a result of this, `PM_DEVICE_ACTION_RESUME` will not be run on a
device if `PM_DEVICE_ACTION_TURN_ON` has already failed.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Store errors powering up devices in a flag in the PM struct. This is
required as the `pm->state` variable always transitions to
`PM_DEVICE_STATE_SUSPENDED`, even when `PM_DEVICE_ACTION_TURN_ON`
returns an error. Marking the transition as failed will allow the
transition logic in `pm_device_runtime_get` to detect the problem.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add a new API used by arch to implement suspend-to-RAM (S2RAM).
The API is composed by a single function to save the CPU context on
suspend.
A CPU context is the arch-specific set of registers that must be
preserved on power-off (in retained RAM) to be able to resume the
execution from the point it was suspended without going through the
whole kernel startup stage.
Signed-off-by: Carlo Caione <ccaione@baylibre.com>
Update the internal power state tracking for `PM_DEVICE_ACTION_TURN_ON`
and `PM_DEVICE_ACTION_TURN_OFF` even when the transition function fails.
Just because the driver couldn't perform some action does not mean the
device is still powered/unpowered.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add state checking to every transition to ensure that the device is in
the state we think it is before running the action.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
PM_DEVICE_ACTION_FORCE_SUSPEND has been equivalent to
PM_DEVICE_ACTION_SUSPEND for a while. Release notes for Zephyr 2.7
mentioned its removal (see 76702e8ff3) but
for some reason it was kept.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
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>
_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>
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>
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>
Adds a helper function to query whether a device is currently powered.
This can be used to determine if the chip can be initialised now, or if
it needs to be deferred.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Return `-ENOTSUP` on calls to device_runtime functions if the underlying
device does not support power management.
Fixes#45648.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
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>
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>
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>
Add API to add devices to a power domain in runtime. The number of
devices that can be added is defined in build time.
The script gen_handles.py will check the number defined in
`CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC` to resize the handles vector,
adding empty slots in the supported sector to be used later.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
The autoconf.h header is not required because the definitions present in
the file are exposed using the compiler `-imacros` flag.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Add a new API that allows to register a callback that is called when the
maximum latency value changes. This can be used by SoC code to perform
actions based on certain latency values.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Add a new API that allows to configure maximum latency requirements.
When the policy manager computes the next state, it will check if the
state brings too much latency based on requirements. This can be useful,
for example, if a certain driver or the application want a system to
respond fast, since any low power state that brings too much latency
will not be used.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The state lock get/put calls should always be balanced: first call get
and then put. Add an assertion in case lock counter is about to go
negative, indicating a programming error.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The pm_constraint_* APIs were effectively used by the policy manager
only. This patch renames the API to the policy namespace and makes its
naming more explicit:
- pm_constraint_set -> pm_policy_state_lock_get()
- pm_constraint_release -> pm_policy_state_lock_put()
- pm_constraint_get -> pm_policy_state_lock_is_active()
The reason for these changes is that constraints can be of many types:
allow/disallow states, impose latency requirements, etc. The new naming
also makes explicit that the API calls will influence the PM policy
behavior.
All drivers and documentation have been updated accordingly.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Most include files were not used, and `toolchain.h` was missing (needed
by `__weak`).
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
1. state < PM_STATE_COUNT: the function argument is an enumeration that
already indicates the valid values. It is a programming error to pass
random integer values, the API should not be defensive "just in case
the application has messed things up".
2. v < UINT_MAX: first, this assumes type used by `atomic_t` (opaque).
Second, if the integer overflows it will just wrap around, so the
check does nothing in practice.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The policy file does not perform any logging, so just drop logging
include and declaration.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Move policy code one level up for simplicity, since there is a single
source file. Source file has been renamed to "policy.c" to make things
clear.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The residency policy, is in reality, influences by other parameters for
example constraints. It has been renamed to "DEFAULT" policy to make it
more general. The "APP" policy has been renamed to "CUSTOM" to better
represent its purpose.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
According to Kconfig guidelines, boolean prompts must not start with
"Enable...". The following command has been used to automate the changes
in this patch:
sed -i "s/bool \"[Ee]nables\? \(\w\)/bool \"\U\1/g" **/Kconfig*
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
No SoCs are re-implementing the constraints API. This _feature_ was
mostly put in place to cover TI platform needs, but it is no longer
needed. Refer to previous commit for more details.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Remove deprecated functions in the previous release. Note that PM API is
not marked as stable.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
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>
A common pattern here was to take the work item as the subfield of a
containing object. But the contained field is not a k_work, it's a
k_work_delayable.
Things were working only because the work field was first, so the
pointers had the same value. Do things right and fix things to
produce correct code if/when that field ever moves within delayable.
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
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>
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>
The CPU power states were declared with a typecast array literal,
which is a GNU extension.
Unfortunately some compilers (xt-xcc even in very recent versions,
when used with -fdata-sections) will die with a compiler error when
those rvalues are used in an expression that also takes their address,
e.g.:
/* this all by itself crashes xcc -fdata-sections */
int *foo = (int[]){0};
Declare the array elments in two steps, making the code standard C.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
When a device returns -ENOTSUP for actions TURN_ON or TURN_OFF
the device state still has to be updated since the domain will
cut or restore the energy.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add a function to tell runtime power management that the device is
starting in the off state instead of active or suspended.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Adds a helper for devices to check if they are on a power-domain or not.
Drivers can use this information to determine if they will be turned on
at some point in the future, or if they are already turned on.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Adds a helper function for running PM actions on child devices. A custom
implementation of `device_supported_foreach` is used as we don't
necessarily want the early exit behaviour of that function in the power
domain context.
The early exit behaviour can be obained through a return value in the
failure callback if that is desired.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Adds `PM_DEVICE_ACTION_TURN_ON` to transition from `PM_DEVICE_STATE_OFF`
to `PM_DEVICE_STATE_SUSPENDED`.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add a Kconfig symbol to enable/disable power domain on Zephyr.
Disabling power domain save some memory / space.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add support for power domains on Zephyr. Power domains are implemented
as simple devices so they can use the existent Zephyr API, for resume
and suspend sync and async and also reference count.
The pm subsystem will ensure that domains are resumed before and
suspended after devices using them. For device runtime power
management, every time the device is got or released the same actions
is done to the domain it belongs.
As domains are implemented as simple devices, it is totally acceptable
a domain belongs to another domain.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
The PM state code is in practice useless when no cpus are defined in DT,
so require this node to be defined.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
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>
Use the pm_state_cpu_get_all API to obtain the list of available CPU
states. This changes reduces the need for non-interesting code within
the policy codebase.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The usage count and device power state has to be restored when the
device action callback fails in the async operation. Otherwise it will
lead to an inconsistent state.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
The pm_device_runtime_enable did not suspend devices, so it assumed that
the device was in a physically suspended state. This change makes sure
that device is left in a suspended state if the device is initially
active.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
In case runtime PM is not enabled (or not built-in), the get/put
functions always return 0 (instead of -ENOTSUP/-ENOSYS). When runtime PM
is disabled, a device is left into active state. Similarly, when device
runtime PM is not built-in, it is safe to assume that a device will
be active when it is called. If a user implements a custom solution, it
is its responsability to make sure that a device is active when using
it. For all these reasons, the -ENOTSUP/-ENOSYS are error codes that
should always be ignored by devices using get/put, since in practice it
means that: device is active, function is a no-op. The example below
illustrates how error handling is simplified:
```c
/* before: safe to ignore -ENOSYS/-ENOTSUP since device is active (we
* can continue)
*/
ret = pm_device_runtime_get(dev);
if ((ret < 0) && (ret != -ENOSYS) && (ret != -ENOTSUP)) {
return ret;
}
/* now */
ret = pm_device_runtime_get(dev);
if (ret < 0) {
return ret;
}
```
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Incorrect address was being taken for cpus_states variable, causing
transition to low power modes to fail with an assertion (since the data
in the pm_state_info struct is no longer valid). Fix the address.
Fixes#41244
Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
Similar what is done in pm_device_state_set, checking if the power
state for a given device in pm_device_action_run is locked.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Kconfig choice allows to set a default, so there is no need for an extra
hidden Kconfig option.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Check that minimum residency time is greater than exit latency time for
all CPUs power states at compile time.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The values contained by the array are const, and number of states is
usually far below 255, so uint8_t can be used.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Change the function pm_device_runtime_enable() to return 0 on
success or an error code in case of error.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add a new API to lock a device pm state.
When the device has its state locked, the kernel will no longer
suspend / resume devices when the system goes to sleep and device
runtime power management operations will fail.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Since enabling runtime pm on a device sets the device state to
suspended. The usage count has to be zeroed otherwise we may
have an inconsistency.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
It is not possible to rely on pm->dev to do lazy initialization of
some components. For example, in the follow sequence of commands:
1 - pm_device_runtime_enable()
pm->state == PM_DEVICE_STATE_SUSPENDED;
2 - pm_device_runtime_disable()
pm->state == PM_DEVICE_STATE_ACTIVE;
3 - pm_device_runtime_enable()
pm->state == PM_DEVICE_STATE_ACTIVE
After the first time pm_device_runtime_enable(), the device state will
be suspended, but after the second time this function executes the state
will be active. That is not consistent.
It is just easier to remove the branch check and always set those
fields for the sake of consistent.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Although we are declaring `pm->enable`as bitfield, it ends up using
more memory due memory alignment.
Since we already have an atomic variable for device flags, this commit
adds a new flag to indicates whether or not device runtime is enabled.
Doing it we are saving some extra bits and avoiding need to lock the
mutex in several situations since we can atomically check if pm
runtime is enabled on a given device.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Rename PM_STATE_DT_ITEMS_LEN to DT_NUM_CPU_POWER_STATES to make its
purpose more clear. This macro could be made part of a Devicetree API
for PM in the future.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Rename the PM_STATE_INFO_DT_ITEMS_LIST macro to
PM_STATE_INFO_LIST_FROM_DT_CPU to make its purpose more clear. Similar
naming scheme is found e.g. in the GPIO API.
Associated internal macros and docstrings have been adjusted, too.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The type qualifier of the return type is causing warnings if
-Wignored-qualifiers, and it's irrelevant anyways since
the function returns a value and not a pointer
Signed-off-by: Emil Lindqvist <emil@lindq.gr>