Skip to content

[WIP][RFC]Device control features #44693

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

Closed
wants to merge 4 commits into from

Conversation

tbursztyka
Copy link
Collaborator

Reviving #24511 (See api channel in discord)
See #22941 as well for more background information.

This is a WORK IN PROGRESS

Current status:

  • it breaks I²C, Ethernet, etc... statistics. I know, I'll come with a solution within this PR (on the first commit since it is the one breaking it)
  • it is not using a k_mutex but a k_sem: besides the fact that k_mutex are not isr proof (which is a problem in itself), this has been already discussed here Need a "lock" wrapper around k_sem #8496 and looks like priority inheritance is not such a good idea.
  • Not all device will need such mechanism: I'll make the lock (and any control structures in the context) optional in order to save so ram. In general I'll try to expose as much flexibility as possible.

More features to come

@tbursztyka tbursztyka added RFC Request For Comments: want input from the community DNM This PR should not be merged (Do Not Merge) area: Device Model labels Apr 8, 2022
@github-actions github-actions bot added area: API Changes to public APIs area: Kernel labels Apr 8, 2022
@MaureenHelm MaureenHelm self-requested a review April 19, 2022 14:32
@tbursztyka
Copy link
Collaborator Author

tbursztyka commented May 4, 2022

Something's off: I get 0 device_context in the .map file in devcontext's section, though __used is set to all of them, same for the locks. Need to investigate this.

[edit] The error was induced by what's being between the chair and the keyboard.

@tbursztyka
Copy link
Collaborator Author

Last update: finally got a clean way to make the device lock optional per-device (thanks @mbolivar-nordic for the proposal!). Introducing a generic device property "zephyr,no-lock" which permits to disable the lock allocation on an instance basis. Current drawback: not all device instance in the system are DTS based, so these ones by default do not get any lock anyway.

Tomasz Bursztyka added 4 commits May 24, 2022 14:56
Rename existing struct device_state to struct device_context as this
structure will hold more than just state stuff.
Remove struct device's state attribute, saving some ROM space. Instead
order the device_contexts sections the same way as the devices one so a
device context can be easily retrieved from it's device pointer (same
"index" in the list of device context).

Signed-off-by: Tomasz Bursztyka <[email protected]>
This introduce a generic device concurrent access protection.

Such feature already exist, scattered either through some driver
domain API - SPI for instance - or most of the time per-driver when the
maintainer has a good understanding on what to do. But not everywhere.
Many drivers are not protected against concurrent access. Generalizing
this will remove the burden for driver maintainers to do that on their
own, but also will add it to all the drivers which are lacking this.

Signed-off-by: Tomasz Bursztyka <[email protected]>
Make the lock as a pointer, and optional via a DTS property.

Currently, all devices get the lock or not depending on
CONFIG_DEVICE_CONCURRENT_ACCESS being set or not. That's too heavy as
some devices may never need such lock. Thus making it optional via a DTS
property. By default, all devices will get the lock. The instances that
do not want it will only have to set zephyry,no-lock = false in the
relevant DTS file.

The obvious drawback is that it only works for DTS based device
instances. Non DTS based instance are not getting any lock anyway then.

Signed-off-by: Tomasz Bursztyka <[email protected]>
This adds a synchronization semaphore to generalize call synchronization
among all device drivers. This will permit to avoid for drivers
maintainers to implement their own solution.

Signed-off-by: Tomasz Bursztyka <[email protected]>
Copy link
Contributor

@mbolivar-nordic mbolivar-nordic left a comment

Choose a reason for hiding this comment

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

Overall seems reasonable to me. A couple of general issues I think deserve some more discussion.

@@ -1,6 +1,6 @@
# Common fields for all devices

include: [pm.yaml]
include: [pm.yaml, "zephyr,device.yaml"]
Copy link
Contributor

Choose a reason for hiding this comment

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

One problem with the approach of putting this in base.yaml is that it will affect every binding, even those bindings which do not correspond to struct devices.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am little bit confused here, how this file may be used by bindings that will not generate any struct device instance? Seems counter intuitive, relatively to the comment telling "common fields for all devices". Do you have any example ?
PM stuff are fully tightened to struct device also.

Or should we have an intermediate device.yaml file that would include base, pm and the one I am adding then? And actual bindings generating struct device would include that one instead of base only.

Comment on lines -429 to +416
struct device_state {
struct device_context {
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion this should be dropped. I think the existing name is OK and I think there should be a high bar to changing the core device model APIs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

device_state is semantically quite off once you add more features in it. Plus, the change is currently minimal, there is only the statistics in i²c and can that directly use it. It's unrelated to pm_device_state

@tbursztyka
Copy link
Collaborator Author

After running some numbers, I feel this is not going to be the best solution for limited RAM targets.

  • it unilaterally grows the device_context structure for all devices: lock, sync, else... pointers are always going to be present for all, even if a fraction only are going to find it useful
  • many devices do no need this: all chose exposing an API which resolves to atomic operation (interrupt controllers, many gpios etc...), thus where colliding operation are unlikely to happen
  • I could not get a clean way of reinstating the can/i²c stats specific object without - again - growing the device_context, loosing then the ROM gain I made by removing the device_state pointer from device...

Anyway,I am addressing that from a different stand point on devices, keeping the improvements while solving the above issues.

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Aug 16, 2022
@github-actions github-actions bot closed this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Device Model area: Devicetree Binding PR modifies or adds a Device Tree binding area: Devicetree area: Kernel DNM This PR should not be merged (Do Not Merge) RFC Request For Comments: want input from the community Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants