Skip to content
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

device: refactor to aggregate common dynamic state for devices #31880

Merged
merged 5 commits into from
Feb 19, 2021

Conversation

pabigot
Copy link
Collaborator

@pabigot pabigot commented Feb 2, 2021

This PR adds a common state structure to devices, and refactors so that this holds not only the device initialization status but also all device power management state. The refactoring reduces flash usage by changing initialization to allow the kernel to initialize the state without having a per-device backing value in flash. It increases RAM but some of the increase will go away with device PM refactoring, and there's space in other memory so that new enhancements won't incur an additional cost.

Fixes #28873

@github-actions github-actions bot added area: API Changes to public APIs area: Kernel labels Feb 2, 2021
@pabigot pabigot marked this pull request as ready for review February 10, 2021 14:34
@pabigot pabigot added the dev-review To be discussed in dev-review meeting label Feb 10, 2021
@pabigot
Copy link
Collaborator Author

pabigot commented Feb 10, 2021

Added to dev-review to introduce this to the community.

@galak galak removed the dev-review To be discussed in dev-review meeting label Feb 11, 2021
* an 8-bit integer. If initialized is also set, a zero value
* indicates initialization succeeded.
*/
unsigned int init_res : 8;
Copy link
Member

Choose a reason for hiding this comment

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

I have the impression that initialization functions in general return a negative value in case of error, and we will end up always with 0 when it succeed or 255 on error ... That means that use one bit would give the same info, We should probably keep it signed.

Copy link
Member

Choose a reason for hiding this comment

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

nevermind, you invert the signal later

Copy link
Member

@ceolin ceolin left a comment

Choose a reason for hiding this comment

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

it looks good for me

While devices have driver-specific dynamic state accessed through the
data field, there is also dynamic state that is common to most if not
all devices.  Add a structure to hold that data.

Signed-off-by: Peter Bigot <[email protected]>
Initialize all device objects in a batch before invoking any code that
might try to reference data in them.  This eliminates a race condition
enabled by the ability to resolve a device structure at build time,
and reference it from one device's initialization routine before the
device itself has been initialized.

While the device is pulled from the sys_init records rather than
static devices, all in-tree init_entry records that are associated
with devices are produced via Z_DEVICE_DEFINE(), so there should be no
static devices that would be missed by instead iterating over the
device records.

Signed-off-by: Peter Bigot <[email protected]>
This avoids the need for distinct object that uses flash to store its
initializer.  Instead the state is initialized when the kernel is
starting up, before anything can reference it.  In future refactoring
the PM state could be accessed directly without storing an extra
pointer in the static device state.

Signed-off-by: Peter Bigot <[email protected]>
Separate the state indicator of whether the initialization function
has been invoked from the success or failure of the initialization.
This allows precise confirmation that the device is ready (i.e. it has
been initialized, and that initialization succeeded).

Signed-off-by: Peter Bigot <[email protected]>
Move the busy status from a global atomic bit sequence to atomic flags
in the device PM state.  While this temporarily adds 4 bytes to each
PM structure the whole device PM infrastructure will be refactored and
it's likely the extra memory can be recovered.

Signed-off-by: Peter Bigot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

z_device_ready() lies
6 participants