Commit graph

335 commits

Author SHA1 Message Date
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