Skip to content

pm: Declare pm state constraints for a device #70111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2024

Conversation

ceolin
Copy link
Member

@ceolin ceolin commented Mar 12, 2024

Declare in devicetree which power states would cause power loss in a device. This information can be used by devices
to set/release power state constraints to avoid being suspended when they are in the middle of an operation.

@ceolin
Copy link
Member Author

ceolin commented Mar 14, 2024

@JordanYates @erwango @hubertmis please a take a look if you can. Any feedback is welcome, especially around naming convention.

@erwango
Copy link
Member

erwango commented Mar 15, 2024

On my side, I don't think it is a good approach to declare pm states in device nodes.
Interest of dts is when you need to be able to distinguish various behaviors of different instances in the same device driver (either due to devices specifics (IRQ number) or application specifics (speed, pins, ...). I don't think this could happen in current case as all instances are supposed to behave the same way (ie PM state support implemented or not) and have the same constraints.
I some rare occasions (see #60369), this might be different, but it doesn't justify the need to have to populate supported PM states in each and every node implementing PM.
Proposed approach is overkill to me.

@ceolin
Copy link
Member Author

ceolin commented Mar 15, 2024

On my side, I don't think it is a good approach to declare pm states in device nodes. Interest of dts is when you need to be able to distinguish various behaviors of different instances in the same device driver (either due to devices specifics (IRQ number) or application specifics (speed, pins, ...). I don't think this could happen in current case as all instances are supposed to behave the same way (ie PM state support implemented or not) and have the same constraints. I some rare occasions (see #60369), this might be different, but it doesn't justify the need to have to populate supported PM states in each and every node implementing PM. Proposed approach is overkill to me.

It may be indeed. The problem is that is hard to make generic drivers without this information, but to be honest that is not being a source of complains. I was already thinking about making it protected under a build option to avoid waste memory.

But yeah, this increases the complexity and the number of customization for power management. The problem is that we have really different use cases to support. I don't see a way now to a driver, that can be used in different targets. to set constraints to avoid being power off without it. At the moment the driver needs to exactly know which power states the soc uses.

Copy link
Member

@hubertmis hubertmis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this looks like the missing feature I was looking for!

@hubertmis
Copy link
Member

Interest of dts is when you need to be able to distinguish various behaviors of different instances in the same device driver

That's exactly the reason to define power states in .dts. An example is nRF54H20 in which one instance of MVDMA gets power cut off in 2 sleep states while the remaining instances lose power in 3 sleep states. So the MVDMA driver needs to distinguish behaviors of different instances regarding power state locking.

@erwango
Copy link
Member

erwango commented Mar 19, 2024

An example is nRF54H20 in which one instance of MVDMA gets power cut off in 2 sleep states while the remaining instances lose power in 3 sleep states. So the MVDMA driver needs to distinguish behaviors of different instances regarding power state locking.

Then maybe instead of defining all states in all nodes, we can define only the specific states in specific nodes. See https://github.com/zephyrproject-rtos/zephyr/pull/59200/files#diff-5bd998735a6cb86347623d54854157ee8e578ecee7507873c953c18fe4d01d2c

@ceolin
Copy link
Member Author

ceolin commented Mar 19, 2024

An example is nRF54H20 in which one instance of MVDMA gets power cut off in 2 sleep states while the remaining instances lose power in 3 sleep states. So the MVDMA driver needs to distinguish behaviors of different instances regarding power state locking.

Then maybe instead of defining all states in all nodes, we can define only the specific states in specific nodes. See https://github.com/zephyrproject-rtos/zephyr/pull/59200/files#diff-5bd998735a6cb86347623d54854157ee8e578ecee7507873c953c18fe4d01d2c

I don't think it is the same thing (unless I am missing something). These notifications don't allow you to stop the SoC to sleep, they are sent after the policy has chosen the power state. What this change provides is a way to devices set constraints to forbid the SoC to use specific power states.

@erwango
Copy link
Member

erwango commented Mar 20, 2024

I don't think it is the same thing (unless I am missing something). These notifications don't allow you to stop the SoC to sleep, they are sent after the policy has chosen the power state. What this change provides is a way to devices set constraints to forbid the SoC to use specific power states.

I'm not commenting about the notifications, but about what to document in device tree.
In this regard, this is close:
The point is to be able to document in which states a peripheral supporting PM will loose context.
For peripheral where PM is not supported, device PM is not coming into play anyway.
Note: we don't need to document states where context is lost by default (standby for instance)

Also one question for @hubertmis: what action do you want to take for the states where MVDMA gets cut off ?

  • prevent the system to enter these states ?
  • be able to re-initialize MVDMA when you detect that this state is exited ?

@hubertmis
Copy link
Member

hubertmis commented Mar 20, 2024

Also one question for @hubertmis: what action do you want to take for the states where MVDMA gets cut off ?

While MVDMA operation is active, the CPU shall not enter any of the power states cutting power off the active MVDMA instance. Otherwise, interrupted DMA operations could result in data corruption.

So I expect code like this in the MVDMA driver:

pm_handling_function() {
  switch (action) {
    case PM_DEVICE_ACTION_SUSPEND:
        /* suspend the device */
        pm_policy_device_power_lock_put(dev);
        break;
    case PM_DEVICE_ACTION_RESUME:
        /* resume the device */
        pm_policy_device_power_lock_get(dev);
        reinit_mvdma_instance(dev); // because power of this instance might have been disabled
        break;
}

dma_start() {
#ifdef CONFIG_PM_DEVICE_RUNTIME
  pm_device_runtime_get(dev);
#else
  pm_policy_device_power_lock_get(dev);
  reinit_mvdma_instance(dev);
#endif
  ...
}

dma_end_handler() {
  ...
#ifdef CONFIG_PM_DEVICE_RUNTIME
  pm_device_runtime_put(dev);
#else
  pm_policy_device_power_lock_put(dev);
#endif
}

@ceolin
Copy link
Member Author

ceolin commented Mar 20, 2024

I don't think it is the same thing (unless I am missing something). These notifications don't allow you to stop the SoC to sleep, they are sent after the policy has chosen the power state. What this change provides is a way to devices set constraints to forbid the SoC to use specific power states.

I'm not commenting about the notifications, but about what to document in device tree. In this regard, this is close: The point is to be able to document in which states a peripheral supporting PM will loose context. For peripheral where PM is not supported, device PM is not coming into play anyway.

That is not true, even if the device does not implement "device pm" it still may need to prevent the system to sleep when it is doing something.

Also one question for @hubertmis: what action do you want to take for the states where MVDMA gets cut off ?

  • prevent the system to enter these states ?
  • be able to re-initialize MVDMA when you detect that this state is exited ?

@ceolin ceolin force-pushed the pm/device-power-state branch from 8b050bd to 7c06db8 Compare April 24, 2024 23:19
@ceolin ceolin requested review from mmahadevan108 and dleach02 May 9, 2024 16:33
@ceolin ceolin force-pushed the pm/device-power-state branch from 7c06db8 to 7609147 Compare May 14, 2024 04:32
@ceolin ceolin marked this pull request as ready for review May 14, 2024 04:33
@erwango
Copy link
Member

erwango commented May 24, 2024

@ceolin As already mentioned, what bothers me with this PR is the need to document this property in all devices nodes, while in most cases, the behavior is just "by definition" of the power state.
What about adding a property in power states nodes, which will provide the default behavior applied in general and then this behavior could be amended upon use of the use of zephyr,disabling-power-states device property on devices that have a specific behavior vs the default power state definition ?

@ceolin
Copy link
Member Author

ceolin commented May 24, 2024

@ceolin As already mentioned, what bothers me with this PR is the need to document this property in all devices nodes, while in most cases, the behavior is just "by definition" of the power state. What about adding a property in power states nodes, which will provide the default behavior applied in general and then this behavior could be amended upon use of the use of zephyr,disabling-power-states device property on devices that have a specific behavior vs the default power state definition ?

@erwango that is optional. Only devices that need to set constraints need to use it. I see what you mean, and that is true for most scenarios. But depending on the topology, a devices may be in different power rails and different power states can turn off different power rails. Keeping it close to the device provides a fine grain that covers all cases (at least it should).

If your concern is due resources needed, we can optimize it, keeping this information out of the device struct and only devices that need it will create it. Similar to what we do with the pm struct. But honestly, this can be addressed later. For now that is optional, as you can see there is no change in any device and consequently no changes in resources consumption.

So far, I got feedback from different targets that this feature is useful.

@ceolin ceolin force-pushed the pm/device-power-state branch 2 times, most recently from fd261aa to 58dd978 Compare May 30, 2024 20:30
@erwango
Copy link
Member

erwango commented May 31, 2024

@erwango that is optional. Only devices that need to set constraints need to use it. I see what you mean, and that is true for most scenarios. But depending on the topology, a devices may be in different power rails and different power states can turn off different power rails. Keeping it close to the device provides a fine grain that covers all cases (at least it should).

Ok for this concern.

Would it then be possible to address following other comments:

@ceolin
Copy link
Member Author

ceolin commented May 31, 2024

@erwango that is optional. Only devices that need to set constraints need to use it. I see what you mean, and that is true for most scenarios. But depending on the topology, a devices may be in different power rails and different power states can turn off different power rails. Keeping it close to the device provides a fine grain that covers all cases (at least it should).

Ok for this concern.

Would it then be possible to address following other comments:

I don't exactly how to answer it generically since we don't have this information anywhere today, I mean no power state tells you it. From device perspective (when the soc goes to listed states) it is the same of turn off action. Each device may interpret it as it needs. That is one of the reason it is optional.

It can be done, but the cost is making it less flexible and transfer some logic to the policy. I rather to have it generic and then based on more usage see if we can generalize it.

@ceolin ceolin force-pushed the pm/device-power-state branch from 58dd978 to f0d29a7 Compare May 31, 2024 19:23
hubertmis
hubertmis previously approved these changes Jun 3, 2024
Flavio Ceolin added 4 commits June 5, 2024 12:11
Declare power state constraints for a device in devicetree.
It allows a map between device instances and power states that disable
their power. This information is used by a new API
(pm_policy_device_power_lock_put/get) that automically set/release
pm state constraints.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Test that device pm state constraints work as expected.
It declares a device in DT that specify that two pm states cause
power loss and use this information when the device is in the middle
of an action.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add a symbol to enable device power state constraints this
saves resources when this feature is not needed.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
Add information about device power management and system power
management constarints.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
@ceolin
Copy link
Member Author

ceolin commented Jun 5, 2024

@hubertmis thanks for the review. I've to rebase it. Would you mind to refresh your approval ?

@ceolin
Copy link
Member Author

ceolin commented Jun 5, 2024

@erwango can you take another look please ?

@erwango erwango self-requested a review June 6, 2024 06:42
@erwango
Copy link
Member

erwango commented Jun 6, 2024

Removing my blocker, but not approving as I stand on my last comments. See #70111 (comment)

@ceolin
Copy link
Member Author

ceolin commented Jun 6, 2024

Removing my blocker, but not approving as I stand on my last comments. See #70111 (comment)

Fair enough, we can revisit it later, it is not written in stone. It has valid use cases now and if we find a simpler way to do it after using it we can always change :)

Copy link
Collaborator

@mmahadevan108 mmahadevan108 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good.

@nashif nashif merged commit 9ba65ab into zephyrproject-rtos:main Jun 7, 2024
22 checks passed
@gmarull
Copy link
Member

gmarull commented Jun 25, 2024

I realize now that this PR pollutes struct device for no good reason, introducing a questionable design: introducing PM code in device.h (again). This was merged without any approval from device subsystem maintainers, so I think it should be reverted or provide an urgent fix. We have for a reason a structure for PM resources in struct device, passed as a pointer when devices are initialized. Please provide some feedback, or I'll send a revert PR.

@gmarull
Copy link
Member

gmarull commented Jun 25, 2024

FYI, #74967

@ceolin
Copy link
Member Author

ceolin commented Jun 25, 2024

I realize now that this PR pollutes struct device for no good reason, introducing a questionable design: introducing PM code in device.h (again). This was merged without any approval from device subsystem maintainers, so I think it should be reverted or provide an urgent fix. We have for a reason a structure for PM resources in struct device, passed as a pointer when devices are initialized. Please provide some feedback, or I'll send a revert PR.

You were assigned to review it since the beginning and discussions happened for three months, it was plenty of time.
The revert pr was sent at same time you commented here, that is not like giving any time for feedback :|

I have commented there and can comment here, this is not device power management and that is why it was not added in the device pm code. I don't think removing it is fair, honestly. There is a reason for the feature, the implementation can be changed but removing the whole feature this way is unacceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PM policy: map device driver instances to power states disabling power to the devices
8 participants