We can't enable device runtime power management in a device that is
set busy since it may suspend this device.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add support for a "pm" shell command to trigger suspend/resume as well
as runtime-get/put on devices. This is useful for testing during driver
development.
Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
In the follow expression:
cyc_evt += UINT32_MAX + 1U
first it is evaluated (UINT32_MAX + 1U), since both types
interpreted as uint32_t, this operation causes an overflow resulting
in 0U.Then we have
cyc_evt = (uint64_t)cyc_evt + 0U
Fix it casting of the operands in the first operation to uint64_t.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
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>
To wait for the asynchronous suspending work item to complete, a
combination of semaphores and events is used. First, the semaphore is
released, then the events are cleared (through the boolean argument to
k_event_wait), then events are awaited.
However, if the event flag happens to be set by the work handler in the
short time between k_sem_give and k_event_wait, it is then cleared by
k_event_wait and k_event_wait blocks forever waiting for the event.
Make sure that we clear the event flag before releasing the semaphore.
Signed-off-by: Marco Widmer <marco.widmer@bytesatwork.ch>
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>
Move the check of PM_DEVICE_FLAG_RUNTIME_ENABLED to the beginning of
the function. With this we avoid taking/release the semaphore and also
we no longer need check it in runtime_enable_sync(), because it was
already checked in pm_device_runtime_enable().
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Move around SYS_PORT_FUNC_ENTER and change a early return
in a way that when this function is called it will generate
tracing for entering / leaving this function.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Move the check of PM_DEVICE_FLAG_RUNTIME_ENABLED to the beginning of
the function. With this we avoid taking/release the semaphore and also
we no longer need check it in runtime_disable_sync(), because it was
already checked in pm_device_runtime_disable().
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Move around SYS_PORT_FUNC_ENTER and change a early return
in a way that when this function is called it will generate
tracing for entering / leaving this function.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
In many cases suspending or resuming of a device is limited to
just a few register writes. Current solution assumes that those
operations may be blocking, asynchronous and take a lot of time.
Due to this assumption runtime PM API cannot be effectively used
from the interrupt context. Zephyr has few driver APIs which
can be used from an interrupt context and now use of runtime PM
is limited in those cases.
Patch introduces a new type of PM device - synchronous PM. If
device is specified as capable of synchronous PM operations then
device runtime getting and putting is executed in the critical
section. In that case, runtime API can be used from an interrupt
context. Additionally, this approach reduces RAM needed for
PM device (104 -> 20 bytes of RAM on ARM Cortex-M).
Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
If runtime_suspend() is called early during Zephyr initialisation,
while k_is_pre_kernel() returns 'true,' 'async' is set to 'false,' so
if 'async' is 'true,' Zephyr initialisation is definitely complete,
so there is no need to check k_is_pre_kernel() again.
Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
The asynchronous put is not checking if the device was successfully
suspended before suspending its domain and it is not checking if the
domain was claimed. This patch adds these two checks.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
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>
Similar to what is done in pm_device_runtime_get,
we don't need to wait the device be suspended if
the work is still in the work queue. In this case
we just cancel the work and update the device state
to active.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add a delay parameter to asynchronous device runtim put. This allows
to delay the put operation what is useful to avoid multiple states
transitions.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
We don't need to wait an async put to happen in case it has not
started yet. In this case we can simply cancelling the pending work
and change the internal state because the device is still active.
This way we avoid a suspend and resume cycle.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Don't assert that `pm_device_runtime_get` is not running in an ISR
context, as `runtime_suspend` properly handles that condition.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Enable the automatic power domain management to release the domain as
many times as it was claimed, instead of only once.
This fixes the domain being permanently enabled if the supported device
is claimed more than once.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add check for device busy when CONFIG_PM_NEED_ALL_DEVICES_IDLE is
set to y because one or more devices may still in busy and causes
problem when system go into low power in Intel ISH platform.
Signed-off-by: Leifu Zhao <leifu.zhao@intel.com>
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>
In some platforms it may be desirable to disable certain CPU power
states, for example, because they have extra requirements not available
on all boards/applications. Because `cpu-power-states` are defined at
SoC dts file levels, the only way to achieve that now was by re-defining
`cpu-power-states` property in e.g. a board file. With this patch, one
can now selectively set `status = "disabled";` to any power state and it
will be skipped by the PM subsystem.
Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
pm_device_runtime_get() uses a semaphore to protect resources.
pm_device_runtime_get() blocked forever until obtaining the lock, making
it unusable from contexts where blocking is not allowed, e.g. ISRs. With
this patch, we give the chance to use the function from an ISR by not
waiting on the lock and returning -EWOULDBLOCK instead. If device is
suspending (from a previous put_async() operation) the same value is
returned as we can't wait either.
Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
Adds a helper function for initializing devices into the expected power
state, through the devices `pm_device_action_cb_t`. This eliminates code
duplication between the init functions and the PM callback.
The expected device states in order of priority are:
* No power applied to device, `OFF`
* `zephyr,pm-device-runtime-auto` enabled, `SUSPEND`
* Otherwise, `ACTIVE`
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Adds a helper function for initializing devices into the expected power
state, through the devices `pm_device_action_cb_t`. This eliminates code
duplication between the init functions and the PM callback.
The expected device states in order of priority are:
* No power applied to device, `OFF`
* `zephyr,pm-device-runtime-auto` enabled, `SUSPEND`
* Otherwise, `ACTIVE`
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
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>
Add a new Kconfig option that has to be selected by SoCs providing PM
hooks. This option will be now required to enable CONFIG_PM. Before this
change, CONFIG_PM could always be enabled, regardless of SoC providing
any kind of low-power support.
Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
Device dependencies are not always required, so make them optional via
CONFIG_DEVICE_DEPS. When enabled, the gen_device_deps script will run so
that dependencies are collected and part of the final image. Related
APIs will be also made available. Since device dependencies are used in
just a few places (power domains), disable the feature by default. When
not enabled, a second linking pass will not be required.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The option can now be set by projects. This change will also allow to
make it dependent on a future CONFIG_DEVICE_DEPS option.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Rename the Kconfig option to be in line with recent renamings in device
handles/dependencies.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Rename struct device `handles` member to `deps`, in line with previous
renamings in the device API.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Rename multiple internal device macros to use the DEVICE_DEPS naming, so
that it is clear they belong to the device dependencies APIs.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
These macros are used internally by the device dependencies functions.
There's no need to expose them publicly, so prefix them with Z_ and add
them under INTERNAL_HIDDEN docs section.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Coverity is complaining:
CID 316017: Control flow issues (NO_EFFECT)
>>> This less-than-zero comparison of an unsigned value is never true.
148 for (size_t i = 0; i < ARRAY_SIZE(substate_lock_t); i++) {
This is a false positive but we can still optimize the code and making
coverity happy by simply avoiding compiling the offending code when no
power states are defined into the DT.
Signed-off-by: Carlo Caione <ccaione@baylibre.com>
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>
Events in the power-management policy context are defined as any source
that will wake up the system at a known time in the future. By
registering such event, the policy manager will be able to decide wether
certain power states are worth entering or not.
Events will bypass the ticks argument received by the policy manager if
they occur earlier.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Latency is always specified in microseconds, so apend `us` to the
relevant variables. Also, make it clear that latency request structures
are private (they are just exposed publicly to allow static allocation).
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Use iterable sections to handle devices list. This simplifies devices
implementation by using standard APIs.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
When demand paging is enabled, only a minimal set of functions
are available at boot. Anything not marked as boot functions
would not be loading in memory at boot which would result in
page faults when jumping to those functions. However, at early
boot, demand paging has not been enabled yet. So we need to
mark necessary functions as boot functions so they are placed
in the correct linker section where they are loaded at boot.
Fixes#56414
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
Add the `zephyr,pm-device-runtime-auto` flag to `pm.yaml` and
`struct pm_device`.
This flag is intended to signify to the boot system that device runtime
PM should be automatically enabled on the device after the init function
has run.
Only run `pm_device_runtime_auto_enable` function on a device if
initialisation succeeded. This prevents actions being run on devices
that are not ready.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Returning an error code when PM is not supported for a device only makes
writing drivers harder, as instead of checking for failures with `< 0`,
they also need to check for `-ENOTSUP`.
From the point of view of a driver, an `-ENOTSUP` return value is
equivalent to success, as if the device it is trying to turn on doesn't
support PM, by definition it is already enabled. This is equivalent to
the API behaviour when `CONFIG_PM_DEVICE_RUNTIME=n`.
Whether a device supports PM or not can still be determined at runtime
by inspecting the return code of `pm_device_runtime_enable` if
necessary.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>