Skip to content

Conversation

@JordanYates
Copy link
Contributor

@JordanYates JordanYates commented Jul 16, 2023

Add a sub-priority field to Z_INIT_ENTRY_SECTION, which is used to
ensure that multiple drivers running at the same init priority still
run in an order that respects their devicetree dependencies.

This is primarily useful when multiple instances of the same device are
instantiated that can depend on each other, for example power domains
and i2c muxes.

After this PR, an init level now runs all SYS_INIT/DEVICE_DEFINE macros first,
then DEVICE_DT_DEFINE macros in order of devicetree ordinals.

Linker section names now look something like this:

 *(SORT_BY_NAME(SORT_BY_ALIGNMENT(.z_init_POST_KERNEL?_*)))
 *(SORT_BY_NAME(SORT_BY_ALIGNMENT(.z_init_POST_KERNEL??_*)))
 .z_init_POST_KERNEL40_0006_
                0x00000000000042b8        0x8 zephyr/drivers/gpio/libdrivers__gpio.a(gpio_stellaris.c.obj)
 .z_init_POST_KERNEL40_0017_
                0x00000000000042c0        0x8 zephyr/drivers/gpio/libdrivers__gpio.a(gpio_stellaris.c.obj)
 .z_init_POST_KERNEL40_0018_
                0x00000000000042c8        0x8 zephyr/drivers/gpio/libdrivers__gpio.a(gpio_stellaris.c.obj)
 .z_init_POST_KERNEL40_0019_
                0x00000000000042d0        0x8 zephyr/drivers/gpio/libdrivers__gpio.a(gpio_stellaris.c.obj)
 .z_init_POST_KERNEL40_0020_
                0x00000000000042d8        0x8 zephyr/drivers/gpio/libdrivers__gpio.a(gpio_stellaris.c.obj)

Resolves #22545 for multiple instances of the same device

Jordan Yates added 2 commits July 16, 2023 10:47
Generate a zero padded variant of `_ORD` that is suitable for use in
linker scripts with the `SORT` property, so that `6` is correctly placed
before `24`, and so on.

Signed-off-by: Jordan Yates <[email protected]>
Add a sub-priority field to `Z_INIT_ENTRY_SECTION`, which is used to
ensure that multiple drivers running at the same init priority still
run in an order that respects their devicetree dependencies.

This is primarily useful when multiple instances of the same device are
instantiated that can depend on each other, for example power domains
and i2c muxes.

Signed-off-by: Jordan Yates <[email protected]>
Add tests for sorting devices at the same init priority based on their
devicetree ordinals.

Signed-off-by: Jordan Yates <[email protected]>
Add basic testing of the new `DT_DEP_ORD_STR_SORTABLE` macro.

Signed-off-by: Jordan Yates <[email protected]>
gmarull
gmarull previously approved these changes Jul 18, 2023
Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

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

Jordan Yates added 2 commits July 19, 2023 21:20
Update the script to parse the new section naming. The ordering type
is converted from an integer to a tuple, which still compares correctly
due to the elementwise behaviour of tuple comparison.

Signed-off-by: Jordan Yates <[email protected]>
Fix the test script for the changes made to section naming.

Signed-off-by: Jordan Yates <[email protected]>
Copy link
Member

@fabiobaltieri fabiobaltieri left a comment

Choose a reason for hiding this comment

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

Thanks for the extra fixes!

Copy link
Contributor

@andyross andyross left a comment

Choose a reason for hiding this comment

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

Seems reasonable, but also seems worth pointing out that this is yet more code we're adding to make up for our lack of proper dependency tracking on static devices...

@JordanYates
Copy link
Contributor Author

Seems reasonable, but also seems worth pointing out that this is yet more code we're adding to make up for our lack of proper dependency tracking on static devices...

Totally agree, but as a minimal internal change with no effect on the API that solves a real problem I think it is worth it

@gmarull
Copy link
Member

gmarull commented Jul 20, 2023

Seems reasonable, but also seems worth pointing out that this is yet more code we're adding to make up for our lack of proper dependency tracking on static devices...

Totally agree, but as a minimal internal change with no effect on the API that solves a real problem I think it is worth it

+1, the init problem® won't be easy to solve. For example, all the flexibility we've given with SYS_INIT is now hitting us hard.

@fabiobaltieri
Copy link
Member

Seems reasonable, but also seems worth pointing out that this is yet more code we're adding to make up for our lack of proper dependency tracking on static devices...

Totally agree, but as a minimal internal change with no effect on the API that solves a real problem I think it is worth it

+1, the init problem® won't be easy to solve. For example, all the flexibility we've given with SYS_INIT is now hitting us hard.

I'd be surprised at this point if someone comes up with a obviously perfect solution, but for now I think these progressive changes are moving in the right direction. I'm thinking, this takes care of same-level, the build time checking does multiple-level, @bjarki-trackunit were you looking into printing the init call sequence in #55654 (comment)? I'm thinking that having an init sequence printer could be used by subsystem tests and downstream application to compare the final init sequence with a golden sample so that any change gets caught and manually reviewed. That'd be a pretty solid system IMO.

@fabiobaltieri fabiobaltieri requested a review from a team July 20, 2023 11:13
@fabiobaltieri fabiobaltieri added the DNM This PR should not be merged (Do Not Merge) label Jul 20, 2023
@fabiobaltieri
Copy link
Member

@JordanYates I linked this up in #release on Discord and it's been pointed out that since it's been a hot topic recently, it may be worth running it through the Architecture WG before hitting the merge button (@carlescufi). Any chance you could talk about it? Happy to help out if the time zone is a problem btw.

Tagging as DNM for now.

@JordanYates
Copy link
Contributor Author

I don't think the change is controversial enough to require me to personally argue its merits 🙂 1am is a high bar. If you are happy to introduce the topic that's good enough for me.

@fabiobaltieri
Copy link
Member

I don't think the change is controversial enough to require me to personally argue its merits slightly_smiling_face 1am is a high bar. If you are happy to introduce the topic that's good enough for me.

Yeah well, you never know, but I think the same. Happy to present on your behalf at the next WG then, @gmarull will you be there as well?

@gmarull
Copy link
Member

gmarull commented Jul 20, 2023

I don't think the change is controversial enough to require me to personally argue its merits slightly_smiling_face 1am is a high bar. If you are happy to introduce the topic that's good enough for me.

Yeah well, you never know, but I think the same. Happy to present on your behalf at the next WG then, @gmarull will you be there as well?

I'll be there, but TBH, I'd just go ahead and merge. This is not really a substantial change, just resolves some issues present in the current implementation.

Copy link
Contributor

@dkalowsk dkalowsk left a comment

Choose a reason for hiding this comment

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

Looks pretty clean!

@cfriedt
Copy link
Member

cfriedt commented Jul 22, 2023

Seems reasonable, but also seems worth pointing out that this is yet more code we're adding to make up for our lack of proper dependency tracking on static devices...

Totally agree, but as a minimal internal change with no effect on the API that solves a real problem I think it is worth it

Honestly, an LTO plugin seems increasingly more appealing these days ..

@carlescufi
Copy link
Member

carlescufi commented Jul 25, 2023

Architecture WG:

  • Incremental improvement using current infrastructure. Removes dependency on linking order for devices that have the same priority. This instead appends the Devicetree ordinal to remove any dependency from linking order.
  • Anyone relying on linking order shouldn't have been in the first place.

@fabiobaltieri fabiobaltieri removed the DNM This PR should not be merged (Do Not Merge) label Jul 25, 2023
@fabiobaltieri fabiobaltieri merged commit f38e6aa into zephyrproject-rtos:main Jul 25, 2023
@JordanYates JordanYates deleted the 230716_device_sub_prio branch July 25, 2023 23:03
@nashif nashif added the Architecture Review Discussion in the Architecture WG required label Aug 5, 2023
carlescufi pushed a commit that referenced this pull request Aug 16, 2023
Following #60410, the NXP S32 external interrupt controller device
initializes after the core interrupt controller. Bump the NXP S32 intc
init level to initialize after the core intc and before the GPIO
device driver.

Fixes #61218

Signed-off-by: Manuel Argüelles <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Device initialization order that respects devicetree dependencies

10 participants