Commit graph

287 commits

Author SHA1 Message Date
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
Fabio Baltieri df819a8609 pm: device_runtime: add an access function to get the usage counter
Add a pm_device_usage_get() to get the current usage counter.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2024-05-01 10:31:03 +02:00
Fabio Baltieri d5c773d373 pm: shell: filter out non PM enabled devices
Add a device lookup filter to only suggest devices that are PM enabled.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2024-04-30 19:30:15 -05:00
Flavio Ceolin a458d892e6 pm: device_runtime: Check busy status in runtime_enable
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>
2024-04-30 08:53:18 +02:00
Fabio Baltieri c5003e0eb1 pm: add device shell commands
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>
2024-04-25 18:00:05 -04:00
Flavio Ceolin ec83ab333d pm: policy: Fix event integer overflow
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>
2024-04-25 17:58:04 -04: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
Marco Widmer d83c63ecce pm: runtime: fix race when waiting for suspended event
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>
2024-03-29 15:08:56 +00: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 7812e54e8a pm: device_runtime: Simplify runtime_enable
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>
2024-02-06 09:52:38 +01:00
Flavio Ceolin 75fd75c227 pm: device_runtime: Fix tracing in runtime_enable
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>
2024-02-06 09:52:38 +01:00
Flavio Ceolin 597024449c pm: device_runtime: Simplify runtime_disable
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>
2024-02-06 09:52:38 +01:00
Flavio Ceolin 2261fae9d7 pm: device_runtime: Fix tracing in runtime_disable
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>
2024-02-06 09:52:38 +01:00
Krzysztof Chruściński 25173f71cd pm: device_runtime: Extend with synchronous runtime PM
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>
2024-02-01 15:03:42 +01: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
Guennadi Liakhovetski a1fd8cd078 runtime-pm: remove a superfluous k_is_pre_kernel() test
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>
2024-01-17 09:55:48 +01:00
Flavio Ceolin 640a4586c0 pm: device_runtime: Fix domain mgmt in async put
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>
2024-01-16 10:01:08 +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
Flavio Ceolin b16110587c pm: device_runtime: Avoid unnecessary work
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>
2023-12-14 19:07:48 +00:00
Flavio Ceolin b5ca7a06b4 pm: device_runtime: Add delay to async put
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>
2023-12-12 10:57:34 +01:00
Flavio Ceolin 0ea173b774 pm: device_runtime: Avoid unnecessary resume/suspend
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>
2023-12-12 10:57:34 +01:00
Jordan Yates 643984c432 pm: device_runtime: don't assert in ISR put
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>
2023-12-08 11:09:34 +01:00
Jordan Yates 8fab08e8e2 pm: device_runtime: release power domain multiple times
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>
2023-12-06 09:16:12 +00:00
Leifu Zhao 61ab3a844c pm: policy: add check for device busy in policy
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>
2023-09-15 14:51:50 -04: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
Benjamin Cabé d22e08c067 pm: Fix unused-parameter warnings
Added a few missing ARG_UNUSED

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2023-08-21 17:15:56 +02:00
Gerard Marull-Paretas a05371d353 pm: state: allow disabling certain power states
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>
2023-07-26 16:49:00 +02:00
Gerard Marull-Paretas 68452bd4cc pm: device_runtime: allow calling pm_device_runtime_get from ISRs
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>
2023-07-26 16:48:51 +02:00
Jordan Yates 5be8664e8d pm: device: add driver init helper
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>
2023-07-26 08:16:44 +00:00
Jordan Yates 6edab1f050 Revert "pm: device: add driver init helper"
This reverts commit 84016c1cd3.
2023-07-25 14:17:11 +02:00
Jordan Yates 84016c1cd3 pm: device: add driver init helper
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>
2023-07-25 09:13:16 +02: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
Gerard Marull-Paretas 3d2194f11e pm: introduce HAS_PM
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>
2023-07-20 10:33:00 +00:00
Gerard Marull-Paretas 26bf349ab1 pm: drop HAS_NO_PM
Remove HAS_NO_PM option, in preparation for a new HAS_PM option
(inverted logic).

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-07-20 10:33:00 +00:00
Gerard Marull-Paretas 48b201cc53 device: make device dependencies optional
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>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 1ebd76ed51 pm: add prompt to DEVICE_DEPS_DYNAMIC
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>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 319fbe57e1 device: s/HAS_DYNAMIC_DEVICE_HANDLES/DEVICE_DEPS_DYMAMIC
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>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 5982d83e2a device: s/struct device.handles/struct device.deps
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>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas 4eb406e093 device: s/DEVICE_HANDLE/DEVICE_DEPS/
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>
2023-06-21 09:32:05 +02:00
Gerard Marull-Paretas e5f82cb5dc device: hide DEVICE_HANDLE_SEP/ENDS
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>
2023-06-21 09:32:05 +02:00
Carlo Caione 9930c346af pm: policy: Selectively compile on "zephyr,power-state" compatible
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>
2023-06-02 18:56:39 -04: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
Gerard Marull-Paretas 9a145e52a0 pm: policy: add support for events
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>
2023-05-25 16:29:40 -04:00
Gerard Marull-Paretas c8cd40f5ae pm: policy: clarify latency units and clarify private members
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>
2023-05-25 16:29:40 -04:00
Gerard Marull-Paretas dacb3dbfeb iterable_sections: move to specific header
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>
2023-05-22 10:42:30 +02:00
Gerard Marull-Paretas d0e58ad0a6 device: use iterable sections
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>
2023-05-12 12:01:10 +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
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
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>
2023-04-12 14:28:07 +00:00
Daniel Leung 8dda1fe154 pm: mark pm_device_runtime_auto_enable as boot function
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>
2023-03-31 14:31:52 -04:00