Commit graph

282 commits

Author SHA1 Message Date
Flavio Ceolin c4258dbde3 pm: device_runtime: Early feature enabled check
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>
2023-02-28 10:41:30 +01:00
Jordan Yates fb1ee429a2 pm: device: rework pm_device_children_action_run
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>
2023-01-25 14:58:52 +00:00
Mahesh Mahadevan ae97eae8c2 pm: Error with handling z_cpus_active count
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>
2023-01-23 10:06:17 +00:00
Corey Wharton 21a7c2d7eb pm: don't suspend unready devices when entering low power states
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>
2022-12-19 09:46:17 +00:00
Carlo Caione a337bfc3b3 pm: runtime: Move from mutexes to semaphores
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>
2022-12-15 22:35:38 +01:00
Carlo Caione d38a6b4bcd pm: runtime: Migrate from condition variables to events
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>
2022-12-15 22:35:38 +01:00
Kumar Gala dd03af653d pm: Rework usage of CONFIG_MP_NUM_CPUS to CONFIG_MP_MAX_NUM_CPUS
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>
2022-10-27 13:51:55 -04:00
Kumar Gala a1195ae39b smp: Move for loops to use arch_num_cpus instead of CONFIG_MP_NUM_CPUS
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>
2022-10-21 13:14:58 +02:00
Kumar Gala c778eb2a56 smp: Move arrays to use CONFIG_MP_MAX_NUM_CPUS
Move to use CONFIG_MP_MAX_NUM_CPUS for array size declarations instead
of CONFIG_MP_NUM_CPUS.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2022-10-17 14:40:12 +09:00
Gerard Marull-Paretas 6a0f554ffa include: add missing kernel.h include
Some files make use of Kernel APIs without including kernel.h, fix this
problem.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-11 18:05:17 +02:00
Gerard Marull-Paretas 2e0899e8ca pm: state: use DT_FOREACH_CHILD_SEP
Avoid usage of auxiliary macros by using DT_FOREACH_CHILD_SEP.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-30 16:19:57 +02:00
Steven Slupsky fe716dbb09 pm: stats: fix typo
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>
2022-08-18 15:13:47 -05:00
Gerard Marull-Paretas a650c6c554 pm: policy: allow multiple subscribers to latency changes
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>
2022-08-03 11:19:17 +02:00
Gerard Marull-Paretas 41a00438cb pm: policy: keep microseconds resolution
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>
2022-08-03 11:19:17 +02:00
Jordan Yates 962eca65af pm: device_runtime: detect power up failures
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>
2022-07-12 10:07:18 +02:00
Jordan Yates 317679031d pm: device: store power up failures
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>
2022-07-12 10:07:18 +02:00
Carlo Caione 1e74f1bff5 arch: Introduce S2RAM interface
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>
2022-07-11 15:26:26 +02:00
Jordan Yates 8a2eed6cbd pm: device_runtime: use atomic_test_bit
Use `atomic_test_bit` when checking pm flags instead of manually using
bitwise operations.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-07-11 11:33:26 +02:00
Jordan Yates ab0bf48660 pm: device: store power state in all scenarios
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>
2022-07-11 11:33:26 +02:00
Jordan Yates caca393aad pm: device: more complete state checking
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>
2022-07-11 11:33:26 +02:00
Gerard Marull-Paretas 32b9934b0f pm: remove PM_DEVICE_ACTION_FORCE_SUSPEND
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>
2022-07-06 10:30:14 +02:00
Carlo Caione 1608c8adba pm: Introduce CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE
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>
2022-06-22 12:24:43 +02:00
Flavio Ceolin f232db4630 pm: Avoid unnecessary check in SMP
_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>
2022-06-16 19:53:20 -04:00
Flavio Ceolin 2cd49ca0fe pm: Fix possible assertion when resuming
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>
2022-06-16 19:53:20 -04:00
Gerard Marull-Paretas 8cb33df108 pm: remove redundant <zephyr/zephyr.h> includes
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>
2022-06-15 09:13:11 +02:00
Jordan Yates 319ffaed0a pm: device: helper to query power state
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>
2022-06-08 12:43:00 +02:00
Jordan Yates e83e9b08b5 pm: device_runtime: return -ENOTSUP if PM not supported
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>
2022-05-15 21:34:46 +02:00
Flavio Ceolin bbb8c91afa pm: Fix possible race condition in multicore
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>
2022-05-13 08:48:29 -07:00
Flavio Ceolin 2b02843ca3 pm: Remove unnecessary goto
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>
2022-05-13 08:48:29 -07:00
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
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>
2022-05-09 12:07:35 +02:00
Carlo Caione 69b28bfd07 pm: policy: Consider substates for state lock functions
Extend the current pm_policy_state_lock_*() functions to support
substates.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-04-28 16:32:23 +02:00
Flavio Ceolin 0b13b44a66 pm: device: Dynamically add a device to a power domain
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>
2022-04-18 17:25:01 -07:00
Gerard Marull-Paretas c925b5991a include: remove unnecessary autoconf.h includes
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>
2022-04-05 11:18:20 +02:00
Gerard Marull-Paretas 8f913d1977 pm: policy: allow to get notifief when maximum latency changes
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas 35fd6eadbf pm: policy: new API to set maximum latency requirements
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas e6339eac1a pm: policy: assert on unbalanced state lock get/put
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas 5a71eeb35c pm: policy: move constraints to policy API
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas d158f512ac pm: constraint: cleanup include list
Most include files were not used, and `toolchain.h` was missing (needed
by `__weak`).

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas ebb88fb3a2 pm: constraint: remove unused logging
The constraint code does not log, so remove include/module declaration.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas a66a4cdf6e pm: constraint: remove redundant assertions
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas 3c38a41991 pm: policy: remove unused logging
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas 2238387285 pm: policy: move to pm folder
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>
2022-03-16 15:26:47 +01:00
Gerard Marull-Paretas 22fe2142f0 pm: policy: use DEFAULT/CUSTOM naming
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>
2022-03-16 15:26:47 +01:00
Tomasz Bursztyka 521c9d13fb pm: constify all device instances
Run cocci script to constify device instances.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2022-03-16 12:33:12 +01:00
Gerard Marull-Paretas 95fb0ded6b kconfig: remove Enable from boolean prompts
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>
2022-03-09 15:35:54 +01:00
Krzysztof Chruscinski 47ae656cc1 all: Deprecate UTIL_LISTIFY and replace with LISTIFY
UTIL_LISTIFY is deprecated. Replacing it with LISTIFY.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-08 11:03:30 +01:00
Gerard Marull-Paretas 2903680822 pm: constraint: drop __weak
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>
2022-02-28 10:18:38 +01:00
Gerard Marull-Paretas 3b51f9b9e8 pm: device: remove deprecated functions
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>
2022-02-23 07:34:26 -05:00
Flavio Ceolin 78454e5e93 pm: Rename pm_power_state_exit_post_ops
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>
2022-02-23 07:33:46 -05:00
Flavio Ceolin f74c38e061 pm: Rename pm_power_state_next_get
Aligning with the rest of PM API, replace pm_power_state_next_get with
pm_state_next_get.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Flavio Ceolin 6142fcb8ba pm: Rename pm_power_state_force
Aligning with the rest of PM API, replace pm_power_state_force with
pm_state_force.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Flavio Ceolin 89fee61b43 pm: Rename pm_power_state_set
Aligning with the rest of PM API, replace pm_power_state_set with
pm_state_set.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-23 07:33:46 -05:00
Carlo Caione c5894df5ec pm: Change parameters of pm_power_state_{set,exit_post_ops}
Just pass state and substate_id instead of the whole info structure
pointer.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Carlo Caione 8143f206dd pm: Rename exit_pos_ops to pm_exit_pos_ops
Because exit_pos_ops is the only function without the prefix.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Carlo Caione 11f1dd2370 pm: Reference pm_state_info only by pointer
It's unnecessary to move the pm_state_info around by value, just use a
pointer.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 20:58:01 -05:00
Yong Cong Sin 731241f8d0 kernel: workq: Fix type errors in delayable work handlers
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>
2022-02-02 18:43:12 -05:00
Flavio Ceolin 350014d399 pm: Clear the forced flag in the error path
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>
2022-02-01 14:41:05 -05:00
Flavio Ceolin 2e9589bd44 pm: Just check the forced flag
When suspending we don't need to set the forced flag but just check if
it is set.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-02-01 14:41:05 -05:00
Flavio Ceolin fdf7465851 pm: Fix multithread issue with force state
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>
2022-02-01 14:41:05 -05:00
Andy Ross 0c30db1ec5 subsys/pm: C99-legalize declaration of CPU power states
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>
2022-01-27 16:26:27 -05:00
Flavio Ceolin 06bdd1001c pm: Better name for cpus state variable
A better name for array tracking cpus pm state.
s/z_power_states/z_cpus_pm_state/g

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-01-19 14:22:08 -05:00
Flavio Ceolin baf50b5cdb pm: Proper initialize cpu power states
Do not make assumptions about the enum value for ACTIVE state and
explicitly set it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-01-19 14:22:08 -05:00
Flavio Ceolin f6f710b142 pm: device: Properly handle -ENOTSUP
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>
2022-01-19 13:35:32 -05:00
Jordan Yates 7101899328 pm: device_runtime: init into PM_DEVICE_STATE_OFF
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>
2022-01-19 13:35:32 -05:00
Jordan Yates 3f7f8d8f75 pm: device: helper to query power domain
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>
2022-01-19 13:35:32 -05:00
Jordan Yates 9e9fb319a7 pm: device: helper for running actions on children
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>
2022-01-19 13:35:32 -05:00
Jordan Yates 79f382e96e pm: device: add action to turn device on
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>
2022-01-19 13:35:32 -05:00
Flavio Ceolin 2e732dff6d pm: device: Make power domain optional
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>
2022-01-19 13:35:32 -05:00
Flavio Ceolin ddfa048058 pm: Add power domain infra structure
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>
2022-01-19 13:35:32 -05:00
Gerard Marull-Paretas 7eb50bebe4 pm: state: require cpus node
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>
2022-01-11 10:46:20 +01:00
Gerard Marull-Paretas e239f94457 pm: policy: residency: remove redundant logging
The pm.c already performs sufficient logging/tracing, so cleanup the
policy file.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-01-11 10:46:20 +01:00
Gerard Marull-Paretas 696caa0524 pm: policy: return a reference to the next state
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>
2022-01-11 10:46:20 +01:00
Gerard Marull-Paretas e5df3a8cc4 pm: policy: residency: use pm_state_cpu_get_all
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>
2022-01-11 10:46:20 +01:00
Gerard Marull-Paretas 8385d1bce5 pm: state: add pm_state_cpu_get_all
Add a new API to obtain the list of power states available for a given
CPU.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-01-11 10:46:20 +01:00
Flavio Ceolin ce170fa029 pm: device_runtime: Fix reference count when action fails
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>
2022-01-10 10:22:37 -05:00
Gerard Marull-Paretas 91b8abfb4d pm: device_runtime: suspend device on enable
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>
2021-12-16 10:41:04 +01:00
Gerard Marull-Paretas 59b455eb9c pm: device_runtime: simplify error handling for get/put
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>
2021-12-16 10:41:04 +01:00
Daniel DeGrasse 42293cfffe pm: policy: Fix address of cpus_states variable
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>
2021-12-15 16:47:18 -05:00
Flavio Ceolin 94647d8e67 pm: device: Account state locked in action_run
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>
2021-12-15 16:47:01 -05:00
Gerard Marull-Paretas 4cc018bcd6 pm: policy: header cleanup
Include just the necessary headers.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas a269654243 pm: policy: s/policy_residency.c/residency.c
The file is already inside the `policy` folder, so don't repeat policy
in the name.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas d03307bffa pm: policy: delete redundant PM_POLICY_RESIDENCY_DEFAULT config
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>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas 80021f2491 pm: policy: drop dummy policy
The dummy policy is not being used anywhere, not even for testing as it
claims.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas 03232f925c pm: policy: residency: add compile time checks for timings
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>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas d5036eca42 pm: policy: residency: improve DT collection
Use COND_CODE_1 to improve the collection of CPUs power states.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas 25fb8664b1 pm: policy: residency: improve readability
Improve readability and reduce the scope of some variables.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas 12915727e9 pm: policy: residency: constify and make uint8_t states_per_cpu
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>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas dbfb0dcb97 pm: policy: residency: s/pm_min_residency/cpus_states
Adjust pm_min_residency variable name to make it more clear.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas ba39c98821 pm: policy: residency: s/pm_min_residency_sizes/states_per_cpu
Rename variable name to make it clear what it does store.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Gerard Marull-Paretas b176edea1f pm: policy: residency: s/CPU_STATES_SIZE/NUM_CPU_STATES
Use NUM_CPU_STATES name (more in line with the DT macro).

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-15 11:27:39 +01:00
Flavio Ceolin 18b932f10d pm: device_runtime: Return possible error on enable
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>
2021-12-14 19:23:05 -05:00
Flavio Ceolin 0a32eadd13 pm: Account device pm state lock
Do not execute pm operations on devices that the device pm state is
locked.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-12-14 19:23:05 -05:00
Flavio Ceolin 097e08c71a pm: device: Add device pm state lock
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>
2021-12-14 19:23:05 -05:00
Flavio Ceolin 4184678613 pm: device_runtime: Reset usage count on enable
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>
2021-12-13 20:38:52 -05:00
Flavio Ceolin a6d34becb5 pm: device_runtime: Keep enable behavior consistent
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>
2021-12-13 20:38:52 -05:00
Flavio Ceolin 3624b51f24 pm: device_runtime: Use pm flags for runtime state
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>
2021-12-06 13:41:25 -05:00
Gerard Marull-Paretas 7bfd0976aa pm: state: PM_STATE_DT_ITEMS_LEN->DT_NUM_CPU_POWER_STATES
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>
2021-12-04 12:33:57 -05:00
Gerard Marull-Paretas fa96955305 pm: state: PM_STATE_INFO_DT_ITEMS_LIST->PM_STATE_INFO_LIST_FROM_DT_CPU
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>
2021-12-04 12:33:57 -05:00
Emil Lindqvist 5af3bbb27b pm: remove irrelevant type qualifier on pm_power_state_next_get
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>
2021-12-01 14:03:55 -06:00
Flavio Ceolin 54176127ec pm: Fix idle post ops on multicore
Post operations have to be tracked per cpu.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-11-29 19:37:55 -05:00