Power Management¶
Zephyr RTOS power management subsystem provides several means for a system integrator to implement power management support that can take full advantage of the power saving features of SOCs.
Terminology¶
- SOC interface
This is a general term for the components that have knowledge of the SOC and provide interfaces to the hardware features. It will abstract the SOC specific implementations to the applications and the OS.
- Idle Thread
A system thread that runs when there are no other threads ready to run.
- Power gating
Power gating reduces power consumption by shutting off current to blocks of the integrated circuit that are not in use.
- Power State
SOC Power State describes processor and device power states implemented at the SOC level. Power states are represented by
pm_state
and each one has a different meaning.
Overview¶
The interfaces and APIs provided by the power management subsystem are designed to be architecture and SOC independent. This enables power management implementations to be easily adapted to different SOCs and architectures.
The architecture and SOC independence is achieved by separating the core infrastructure and the SOC specific implementations. The SOC specific implementations are abstracted to the application and the OS using hardware abstraction layers.
The power management features are classified into the following categories.
Tickless Idle
System Power Management
Device Power Management
System Power Management¶
The kernel enters the idle state when it has nothing to schedule. If enabled via
the CONFIG_PM
Kconfig option, the Power Management
Subsystem can put an idle system in one of the supported power states, based
on the selected power management policy and the duration of the idle time
allotted by the kernel.
It is an application responsibility to set up a wake up event. A wake up event will typically be an interrupt triggered by one of the SoC peripheral modules such as a SysTick, RTC, counter, or GPIO. Depending on the power mode entered, only some SoC peripheral modules may be active and can be used as a wake up source.
Power States¶
The power management subsystem contains a set of states based on power consumption and context retention.
The list of available power states is defined by pm_state
. In
general power states with higher indexes will offer greater power savings and
have higher wake latencies. Following is a thorough list of available states:
-
enumerator
PM_STATE_ACTIVE
¶ Runtime active state.
The system is fully powered and active.
Note
This state is correlated with ACPI G0/S0 state
-
enumerator
PM_STATE_RUNTIME_IDLE
¶ Runtime idle state.
Runtime idle is a system sleep state in which all of the cores enter deepest possible idle state and wait for interrupts, no requirements for the devices, leaving them at the states where they are.
Note
This state is correlated with ACPI S0ix state
-
enumerator
PM_STATE_SUSPEND_TO_IDLE
¶ Suspend to idle state.
The system goes through a normal platform suspend where it puts all of the cores in deepest possible idle state and may puts peripherals into low-power states. No operating state is lost (ie. the cpu core does not lose execution context), so the system can go back to where it left off easily enough.
Note
This state is correlated with ACPI S1 state
-
enumerator
PM_STATE_STANDBY
¶ Standby state.
In addition to putting peripherals into low-power states all non-boot CPUs are powered off. It should allow more energy to be saved relative to suspend to idle, but the resume latency will generally be greater than for that state. But it should be the same state with suspend to idle state on uniprocesser system.
Note
This state is correlated with ACPI S2 state
-
enumerator
PM_STATE_SUSPEND_TO_RAM
¶ Suspend to ram state.
This state offers significant energy savings by powering off as much of the system as possible, where memory should be placed into the self-refresh mode to retain its contents. The state of devices and CPUs is saved and held in memory, and it may require some boot- strapping code in ROM to resume the system from it.
Note
This state is correlated with ACPI S3 state
-
enumerator
PM_STATE_SUSPEND_TO_DISK
¶ Suspend to disk state.
This state offers significant energy savings by powering off as much of the system as possible, including the memory. The contents of memory are written to disk or other non-volatile storage, and on resume it’s read back into memory with the help of boot-strapping code, restores the system to the same point of execution where it went to suspend to disk.
Note
This state is correlated with ACPI S4 state
-
enumerator
PM_STATE_SOFT_OFF
¶ Soft off state.
This state consumes a minimal amount of power and requires a large latency in order to return to runtime active state. The contents of system(CPU and memory) will not be preserved, so the system will be restarted as if from initial power-up and kernel boot.
Note
This state is correlated with ACPI G2/S5 state
Power States Constraint¶
The power management subsystem allows different Zephyr components and applications to set constraints on various power states preventing the system to go these states. This can be used by devices when executing tasks in background to avoid the system to go to state where it would lose context. Constraints can be set, released and checked using the follow APIs:
-
void
pm_constraint_set
(enum pm_state state)¶ Set a constraint for a power state.
Disabled state cannot be selected by the Zephyr power management policies. Application defined policy should use the pm_constraint_get function to check if given state is enabled and could be used.
Note
This API is refcount
- Parameters
state – [in] Power state to be disabled.
-
void
pm_constraint_release
(enum pm_state state)¶ Release a constraint for a power state.
Enabled state can be selected by the Zephyr power management policies. Application defined policy should use the pm_constraint_get function to check if given state is enabled and could be used. By default all power states are enabled.
Note
This API is refcount
- Parameters
state – [in] Power state to be enabled.
-
bool
pm_constraint_get
(enum pm_state state)¶ Check if particular power state is enabled.
This function returns true if given power state is enabled.
- Parameters
state – [in] Power state.
Power Management Policies¶
The power management subsystem supports the following power management policies:
Residency
Application
Dummy
The policy manager is responsible to inform the power subsystem which power state the system should go based on states available in the platform and possible runtime constraints
Information about states can be get from device tree, see dts/bindings/power/state.yaml.
Residency¶
The power management system enters the power state which offers the highest power savings, and with a minimum residency value (in device tree, see dts/bindings/power/state.yaml) less than or equal to the scheduled system idle time duration.
Application¶
The power management policy is defined by the application which has to implement the following function.
struct pm_state_info pm_policy_next_state(int32_t ticks);
In this policy the application is free to decide which power state the system should go based on the remaining time for the next scheduled timeout.
Dummy¶
This policy returns the next supported power state in a loop. It is used mainly for testing purposes.
Device Power Management Infrastructure¶
The device power management infrastructure consists of interfaces to the Zephyr RTOS device model. These APIs send control commands to the device driver to update its power state or to get its current power state.
Zephyr RTOS supports two methods of doing device power management.
Distributed method
Central method
Distributed method¶
In this method, the application or any component that deals with devices directly
and has the best knowledge of their use does the device power management. This
saves power if some devices that are not in use can be turned off or put
in power saving mode. This method allows saving power even when the CPU is
active. The components that use the devices need to be power aware and should
be able to make decisions related to managing device power. In this method, the
SOC interface can enter CPU or SOC power states quickly when
pm_system_suspend()
gets called. This is because it does not need to
spend time doing device power management if the devices are already put in
the appropriate power state by the application or component managing the
devices.
Central method¶
In this method device power management is mostly done inside
pm_system_suspend()
along with entering a CPU or SOC power state.
If a decision to enter deep sleep is made, the implementation would enter it only after checking if the devices are not in the middle of a hardware transaction that cannot be interrupted. This method can be used in implementations where the applications and components using devices are not expected to be power aware and do not implement device power management.
This method can also be used to emulate a hardware feature supported by some SOCs which cause automatic entry to deep sleep when all devices are idle. Refer to Busy Status Indication to see how to indicate whether a device is busy or idle.
Device Power Management States¶
The Zephyr RTOS power management subsystem defines four device states. These states are classified based on the degree of device context that gets lost in those states, kind of operations done to save power, and the impact on the device behavior due to the state transition. Device context includes device registers, clocks, memory etc.
The four device power states:
PM_DEVICE_STATE_ACTIVE
Normal operation of the device. All device context is retained.
PM_DEVICE_STATE_LOW_POWER
Device context is preserved by the HW and need not be restored by the driver.
PM_DEVICE_STATE_SUSPEND
Most device context is lost by the hardware. Device drivers must save and restore or reinitialize any context lost by the hardware.
PM_DEVICE_STATE_OFF
Power has been fully removed from the device. The device context is lost when this state is entered. Need to reinitialize the device when powering it back on.
Device Power Management Operations¶
Zephyr RTOS power management subsystem provides a control function interface to device drivers to indicate power management operations to perform. The supported PM control commands are:
PM_DEVICE_STATE_SET
PM_DEVICE_STATE_GET
Each device driver defines:
The device’s supported power states.
The device’s supported transitions between power states.
The device’s necessary operations to handle the transition between power states.
The following are some examples of operations that the device driver may perform in transition between power states:
Save/Restore device states.
Gate/Un-gate clocks.
Gate/Un-gate power.
Mask/Un-mask interrupts.
Device Model with Power Management Support¶
Drivers initialize the devices using macros. See Device Driver Model for
details on how these macros are used. Use the DEVICE_DEFINE macro to initialize
drivers providing power management support via the PM control function.
One of the macro parameters is the pointer to the pm_control handler function.
If the driver doesn’t implement any power control operations, it can initialize
the corresponding pointer with NULL
.
Device Power Management API¶
The SOC interface and application use these APIs to perform power management operations on the devices.
Get Device List¶
size_t z_device_get_all_static(struct device const **device_list);
The Zephyr RTOS kernel internally maintains a list of all devices in the system. The SOC interface uses this API to get the device list. The SOC interface can use the list to identify the devices on which to execute power management operations.
Note
Ensure that the SOC interface does not alter the original list. Since the kernel uses the original list, it must remain unchanged.
Device Set Power State¶
int pm_device_state_set(const struct device *dev, uint32_t device_power_state, pm_device_cb cb, void *arg);
Calls the pm_control()
handler function implemented by the
device driver with PM_DEVICE_STATE_SET command.
Device Get Power State¶
int pm_device_state_get(const struct device *dev, uint32_t * device_power_state);
Calls the pm_control()
handler function implemented by the
device driver with PM_DEVICE_STATE_GET command.
Busy Status Indication¶
The SOC interface executes some power policies that can turn off power to devices, causing them to lose their state. If the devices are in the middle of some hardware transaction, like writing to flash memory when the power is turned off, then such transactions would be left in an inconsistent state. This infrastructure guards such transactions by indicating to the SOC interface that the device is in the middle of a hardware transaction.
When the pm_system_suspend()
is called, the SOC interface checks if any device
is busy. The SOC interface can then decide to execute a power management scheme other than deep sleep or
to defer power management operations until the next call of
pm_system_suspend()
.
An alternative to using the busy status mechanism is to use the
distributed method of device power management. In such a method where the
device power management is handled in a distributed manner rather than centrally in
pm_system_suspend()
, the decision to enter deep sleep can be made based
on whether all devices are already turned off.
This feature can be also used to emulate a hardware feature found in some SOCs
that causes the system to automatically enter deep sleep when all devices are idle.
In such an usage, the busy status can be set by default and cleared as each
device becomes idle. When pm_system_suspend()
is called, deep sleep can
be entered if no device is found to be busy.
Here are the APIs used to set, clear, and check the busy status of devices.
Indicate Busy Status API¶
void device_busy_set(const struct device *busy_dev);
Sets a bit corresponding to the device, in a data structure maintained by the kernel, to indicate whether or not it is in the middle of a transaction.
Clear Busy Status API¶
void device_busy_clear(const struct device *busy_dev);
Clears the bit corresponding to the device in a data structure maintained by the kernel to indicate that the device is not in the middle of a transaction.
Check Busy Status of Single Device API¶
int device_busy_check(const struct device *chk_dev);
Checks whether a device is busy. The API returns 0 if the device is not busy.
Check Busy Status of All Devices API¶
int device_any_busy_check(void);
Checks if any device is busy. The API returns 0 if no device in the system is busy.
Device Idle Power Management¶
The Device Idle Power Management framework is a Active Power Management mechanism which reduces the overall system Power consumtion by suspending the devices which are idle or not being used while the System is active or running.
The framework uses device_set_power_state() API set the device power state accordingly based on the usage count.
The interfaces and APIs provided by the Device Idle PM are designed to be generic and architecture independent.
Device Idle Power Management API¶
The Device Drivers use these APIs to perform device idle power management operations on the devices.
Enable Device Idle Power Management of a Device API¶
void pm_device_enable(const struct device *dev);
Enbles Idle Power Management of the device.
Disable Device Idle Power Management of a Device API¶
void pm_device_disable(const struct device *dev);
Disables Idle Power Management of the device.
Resume Device asynchronously API¶
int pm_device_get(const struct device *dev);
Marks the device as being used. This API will asynchronously bring the device to resume state. The API returns 0 on success.
Resume Device synchronously API¶
int pm_device_get_sync(const struct device *dev);
Marks the device as being used. It will bring up or resume the device if it is in suspended state based on the device usage count. This call is blocked until the device PM state is changed to active. The API returns 0 on success.
Suspend Device asynchronously API¶
int pm_device_put(const struct device *dev);
Marks the device as being released. This API asynchronously put the device to suspend state if not already in suspend state. The API returns 0 on success.
Suspend Device synchronously API¶
int pm_device_put_sync(const struct device *dev);
Marks the device as being released. It will put the device to suspended state if is is in active state based on the device usage count. This call is blocked until the device PM state is changed to resume. The API returns 0 on success. This call is blocked until the device is suspended.
Power Management Configuration Flags¶
The Power Management features can be individually enabled and disabled using the following configuration flags.
This flag enables the power management subsystem.
This flag is enabled if the SOC interface and the devices support device power management.
CONFIG_PM_DEVICE_RUNTIME
This flag enables the Runtime Power Management.
API Reference¶
Power Management Hook Interface¶
-
group
power_management_hook_interface
Power Management Hooks.
Functions
-
void
pm_power_state_set
(struct pm_state_info info)¶ Put processor into a power state.
This function implements the SoC specific details necessary to put the processor into available power states.
- Parameters
info – Power state which should be used in the ongoing suspend operation.
-
void
pm_power_state_exit_post_ops
(struct pm_state_info info)¶ Do any SoC or architecture specific post ops after sleep state exits.
This function is a place holder to do any operations that may be needed to be done after sleep state exits. Currently it enables interrupts after resuming from sleep state. In future, the enabling of interrupts may be moved into the kernel.
-
void
System Power Management APIs¶
-
group
system_power_management_api
System Power Management API.
Functions
-
void
pm_power_state_force
(struct pm_state_info info)¶ Force usage of given power state.
This function overrides decision made by PM policy forcing usage of given power state immediately.
Note
This function can only run in thread context
- Parameters
info – Power state which should be used in the ongoing suspend operation.
-
void
pm_notifier_register
(struct pm_notifier *notifier)¶ Register a power management notifier.
Register the given notifier from the power management notification list.
- Parameters
notifier – pm_notifier object to be registered.
-
int
pm_notifier_unregister
(struct pm_notifier *notifier)¶ Unregister a power management notifier.
Remove the given notifier from the power management notification list. After that this object callbacks will not be called.
- Parameters
notifier – pm_notifier object to be unregistered.
- Returns
0 if the notifier was successfully removed, a negative value otherwise.
-
struct
pm_notifier
¶ - #include <pm.h>
Power management notifier struct
This struct contains callbacks that are called when the target enters and exits power states.
As currently implemented the entry callback is invoked when transitioning from PM_STATE_ACTIVE to another state, and the exit callback is invoked when transitioning from a non-active state to PM_STATE_ACTIVE. This behavior may change in the future.
Note
These callbacks can be called from the ISR of the event that caused the kernel exit from idling.
Note
It is not allowed to call pm_notifier_unregister or pm_notifier_register from these callbacks because they are called with the spin locked in those functions.
-
void
Device Power Management APIs¶
-
group
device_power_management_api
Device Power Management API.
Defines
-
PM_DEVICE_STATE_ACTIVE
¶ device is in ACTIVE power state
Normal operation of the device. All device context is retained.
-
PM_DEVICE_STATE_LOW_POWER
¶ device is in LOW power state
Device context is preserved by the HW and need not be restored by the driver.
-
PM_DEVICE_STATE_SUSPEND
¶ device is in SUSPEND power state
Most device context is lost by the hardware. Device drivers must save and restore or reinitialize any context lost by the hardware
-
PM_DEVICE_STATE_FORCE_SUSPEND
¶ device is in force SUSPEND power state
Driver puts the device in suspended state after completing the ongoing transactions and will not process any queued work or will not take any new requests for processing. Most device context is lost by the hardware. Device drivers must save and restore or reinitialize any context lost by the hardware.
-
PM_DEVICE_STATE_OFF
¶ device is in OFF power state
Power has been fully removed from the device. The device context is lost when this state is entered, so the OS software will reinitialize the device when powering it back on
-
PM_DEVICE_STATE_RESUMING
¶ device is resuming to active state.
The device was previously suspended and is now transitioning to become ACTIVE.
-
PM_DEVICE_STATE_SUSPENDING
¶ device is suspending.
The device is currently transitioning from ACTIVE to SUSPEND.
-
PM_DEVICE_STATE_SET
¶
-
PM_DEVICE_STATE_GET
¶
-
PM_DEVICE_ATOMIC_FLAGS_BUSY_BIT
¶ Bit position in device_pm::atomic_flags that records whether the device is busy.
-
device_pm_control_nop
¶ Alias for legacy use of device_pm_control_nop
Typedefs
Functions
-
const char *
pm_device_state_str
(uint32_t state)¶ Get name of device PM state.
- Parameters
state – State id which name should be returned
-
int
pm_device_state_set
(const struct device *dev, uint32_t device_power_state, pm_device_cb cb, void *arg)¶ Call the set power state function of a device.
Called by the application or power management service to let the device do required operations when moving to the required power state Note that devices may support just some of the device power states
- Parameters
dev – Pointer to device structure of the driver instance.
device_power_state – Device power state to be set
cb – Callback function to notify device power status
arg – Caller passed argument to callback function
- Returns 0
If successful in queuing the request or changing the state.
- Returns Errno
Negative errno code if failure. Callback will not be called.
-
int
pm_device_state_get
(const struct device *dev, uint32_t *device_power_state)¶ Call the get power state function of a device.
This function lets the caller know the current device power state at any time. This state will be one of the defined power states allowed for the devices in that system
- Parameters
dev – pointer to device structure of the driver instance.
device_power_state – Device power state to be filled by the device
- Returns 0
If successful.
- Returns Errno
Negative errno code if failure.
-
struct
pm_device
¶ - #include <device.h>
Device PM info.
Public Members
-
struct k_spinlock
lock
¶ Lock to synchronize the get/put operations
-
bool
enable
¶ Device pm enable flag
-
atomic_t
usage
¶ Device usage count
-
atomic_t
state
¶ Device idle internal power state
-
struct k_work_delayable
work
¶ Work object for asynchronous calls
-
struct k_condvar
condvar
¶ Event conditional var to listen to the sync request events
-
struct k_spinlock
-