Async now uses its own work queue, which means it consumes more
resources. Since not all applications need the async API, we can make
it optional without any penalty for those applications.
Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
Device runtime is using the system workqueue to do operations
that are mostly blockers (suspend a device). This should not happen.
This commit adds an option to use dedicated queue for the device runtime
async operations.
The test for this API was assuming that the system workqueue
priority (which is cooperative) so we need to change the test priority
to be lower than the device runtime workqueue priority.
Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
There is no point in using lock or semaphore to read current
usage counter as it may change after unlocking or giving
back the semaphore. Value can only be trusted in the controlled
environment (e.g. test).
Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
PM_DEVICE_FLAG_ISR_SAFE is an enum and it must be converted to
a bit mask before masking with flags.
Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
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>
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>
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>
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>
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>
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>
Only run `pm_device_runtime_put` on the dependent domain if the original
claim operation succeeded. This fixes unbalanced operations when running
```
pm_device_runtime_get(dev);
pm_device_runtime_put(dev);
```
On a device that does not have PM enabled but does have a power domain.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Check if device runtime is enabled in the beginning of
runtime_suspend to avoid unnecessary checks.
This does not change the current behavior.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
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>
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>
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>
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>
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>
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>
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>
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 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>
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>
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>
Move all PM device runtime API calls from pm_device* to the
pm_device_runtime* namespace.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This patch refactors the runtime API to make it more clear and simple.
Relevant changes:
- API uses the action callback in a direct manner, avoiding unnecessary
overhead.
- API documentation has been improved to include detailed return error
codes.
- pm_runtime_disable() is now synchronous (to simplify possible error
paths) and returns error in case it fails. It is also safe to disable
in pre-kernel now.
- pm_runtime_put(_async)() will return -EALREADY if called with usage
count at zero (result of an unbalanced get/put call sequence)
- A transitional state has been added back. This makes code more
readable, and avoids using atomics (not required).
TODO:
- Solve in a better manner the asynchronous suspend error path (now
"solved" using asserts).
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>