Support exporting SoC info from Rust
Subsystems (2)
v4 cover Support exporting SoC info from Rust
This is a fairly straightforward binding of soc_device_register and soc_device_unregister which allows a driver to export basic info about a SoC.
The last patch is a sample demonstrating usage, and can be dropped without issue.
---
Changes in v4:
- Switched c_str!("foo") usage to c"foo"
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Renamed Registration::register to Registration::new
- Inlined registration function and avoided this usage, per Danilo's suggestion.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Switch to new import style
- Increased documentation. Some of this had to be gathered by looking at what is done in practice at the moment, as documentation was absent or did not match code.
- Remove Device intermediate abstraction
- Removed unnecessary pinning of BuiltDeviceAttributes - it only needs
to be pinned for registration, not to exist.
- Aesthetic renames (Attributes pluralization, dropping Device,
etc.)
- Use more representative values for attributes in the sample driver
- Fix swap of example values in the documentation for machine vs family
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Matthew Maurer (3):
rust: Add soc_device support
docs: ABI: sysfs-devices-soc: Fix swapped sample values
rust: Add SoC Driver Sample
Documentation/ABI/testing/sysfs-devices-soc | 4 +- MAINTAINERS | 2 + rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/soc.rs | 135 ++++++++++++++++++++++++++++ samples/rust/Kconfig | 11 +++ samples/rust/Makefile | 1 + samples/rust/rust_soc.rs | 81 +++++++++++++++++ 8 files changed, 235 insertions(+), 2 deletions(-)
---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-soc-bindings-9b0731bcdbed
Best regards,
Signature · 1 line
Matthew Maurer <[email protected]>
PATCH v4 1/3 rust: Add soc_device support
Allow SoC drivers in Rust to present metadata about their devices to userspace through /sys/devices/socX and other drivers to identify their properties through soc_device_match.
--- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/soc.rs | 135 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+)
MAINTAINERS +1 -0
@@ -7700,6 +7700,7 @@ F: rust/kernel/devres.rs F: rust/kernel/driver.rs F: rust/kernel/faux.rs F: rust/kernel/platform.rs +F: rust/kernel/soc.rs F: samples/rust/rust_debugfs.rs F: samples/rust/rust_debugfs_scoped.rs F: samples/rust/rust_driver_platform.rs
rust/bindings/bindings_helper.h +1 -0
@@ -80,6 +80,7 @@#include <linux/sched.h> #include <linux/security.h> #include <linux/slab.h> +#include <linux/sys_soc.h> #include <linux/task_work.h> #include <linux/tracepoint.h> #include <linux/usb.h>
rust/kernel/lib.rs +2 -0
@@ -138,6 +138,8 @@pub mod seq_file;pub mod sizes;pub mod slice; +#[cfg( CONFIG_SOC_BUS )] +pub mod soc;mod static_assert;#[doc(hidden)] pub mod std_vendor;
rust/kernel/soc.rs new file +135 -0
135 lines not on this page · raw message
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
PATCH v4 2/3 docs: ABI: sysfs-devices-soc: Fix swapped sample values
In reply to Matthew MaurerThe sample values for family and machine were swapped relative to what the driver actually does, and doesn't match the field description.
--- Documentation/ABI/testing/sysfs-devices-soc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/ABI/testing/sysfs-devices-soc +2 -2
@@ -17,14 +17,14 @@ Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains the SoC machine - name (e.g. Ux500). + name (e.g. DB8500). What: /sys/devices/socX/family Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains SoC family name - (e.g. DB8500). + (e.g. ux500). On many of ARM based silicon with SMCCC v1.2+ compliant firmware this will contain the JEDEC JEP106 manufacturer’s identification
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
Danilo Krummrich
maintainerRe: [PATCH v4 2/3] docs: ABI: sysfs-devices-soc: Fix swapped sample values
Quoted 5 lines · Matthew Maurer
The sample values for
familyandmachinewere swapped relative to what the driver actually does, and doesn't match the field description.Reviewed-by Lee Jones <[email protected]>Signed-off-by Matthew Maurer <[email protected]>
Quoted 12 lines
--- Documentation/ABI/testing/sysfs-devices-soc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Documentation/ABI/testing/sysfs-devices-soc +0
-0@@ -17,14 +17,14 @@ Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains the SoC machine
Documentation/ABI/testing/sysfs-devices-soc Date: January 2012 · line 27
Quoted 10 lines
Documentation/ABI/testing/sysfs-devices-soc +2
-2- name (e.g. Ux500). + name (e.g. DB8500). What: /sys/devices/socX/family Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains SoC family name - (e.g. DB8500). + (e.g. ux500).
Is the change from "Ux500" to "ux500" intended?
(If not, no need to resend, I can fix it up on apply.)
Re: [PATCH v4 2/3] docs: ABI: sysfs-devices-soc: Fix swapped sample values
Quoted 25 lines · Danilo Krummrich
Quoted 5 lines · Matthew Maurer
The sample values for
familyandmachinewere swapped relative to what the driver actually does, and doesn't match the field description.Reviewed-by Lee Jones <[email protected]>Signed-off-by Matthew Maurer <[email protected]>Fixes da5a70f3519f ("Documentation: add information for new sysfs soc bus functionality")Quoted 15 lines
--- Documentation/ABI/testing/sysfs-devices-soc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Documentation/ABI/testing/sysfs-devices-soc +1
-1@@ -17,14 +17,14 @@ Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains the SoC machine - name (e.g. Ux500). + name (e.g. DB8500).
Quoted 9 lines
Documentation/ABI/testing/sysfs-devices-soc Date: January 2012 · line 27
Quoted 7 lines
Documentation/ABI/testing/sysfs-devices-soc +1
-1What: /sys/devices/socX/family Date: January 2012 contact: Lee Jones <[email protected]> Description: Read-only attribute common to all SoCs. Contains SoC family name - (e.g. DB8500). + (e.g. ux500).Is the change from "Ux500" to "ux500" intended?
Yes, the sample SoC being described in the documentation uses "ux500"[1] for the family value.
Quoted 2 lines
(If not, no need to resend, I can fix it up on apply.)
Re: [PATCH v4 2/3] docs: ABI: sysfs-devices-soc: Fix swapped sample values
In reply to Matthew MaurerQuoted 5 lines · Matthew Maurer
The sample values for
familyandmachinewere swapped relative to what the driver actually does, and doesn't match the field description.Reviewed-by Lee Jones <[email protected]>Signed-off-by Matthew Maurer <[email protected]>
Applied to driver-core-linus, thanks!
PATCH v4 3/3 rust: Add SoC Driver Sample
In reply to Matthew MaurerShows registration of a SoC device upon receipt of a probe.
--- MAINTAINERS | 1 + samples/rust/Kconfig | 11 +++++++ samples/rust/Makefile | 1 + samples/rust/rust_soc.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+)
MAINTAINERS +1 -0
@@ -7705,6 +7705,7 @@ F: samples/rust/rust_debugfs.rs F: samples/rust/rust_debugfs_scoped.rs F: samples/rust/rust_driver_platform.rs F: samples/rust/rust_driver_faux.rs +F: samples/rust/rust_soc.rs DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS) M: Nishanth Menon <[email protected]>
samples/rust/Kconfig +11 -0
@@ -161,6 +161,17 @@ config SAMPLE_RUST_DRIVER_AUXILIARY If unsure, say N. +config SAMPLE_RUST_SOC + tristate "SoC Driver" + select SOC_BUS + help + This option builds the Rust SoC driver sample. + + To compile this as a module, choose M here: + the module will be called rust_soc. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help
samples/rust/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o +obj-$(CONFIG_SAMPLE_RUST_SOC) += rust_soc.o rust_print-y := rust_print_main.o rust_print_events.o
samples/rust/rust_soc.rs new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
Re: [PATCH v4 3/3] rust: Add SoC Driver Sample
Quoted 13 lines · Matthew Maurer
samples/rust/rust_soc.rs new file +13
-0+fn probe ( + pdev: &platform::Device <Core >, + _info:Option <&Self ::IdInfo >, + ) ->impl PinInit <Self ,Error > { +let dev = pdev.as_ref (); + +dev_dbg! (dev,"Probe Rust SoC driver sample.\n" ); + +let pdev = pdev.into (); +pin_init_scope (move || { +let machine =CString ::try_from (c"My cool ACME15 dev board" )?; +let family =CString ::try_from (c"ACME" )?; +let revision =CString ::try_from (c"1.2" )?;
samples/rust/rust_soc.rs line 65
Quoted 10 lines
samples/rust/rust_soc.rs new file +10
-0+let serial_number =CString ::try_from (c"12345" )?; +let soc_id =CString ::try_from (c"ACME15" )?; + +let attr = soc::Attributes { + machine:Some (machine), + family:Some (family), + revision:Some (revision), + serial_number:Some (serial_number), + soc_id:Some (soc_id), + };
To me it seems little bit awkward that all needs to be CStrings. Maybe something like this could be used (basically cow)
```rust
pub enum AttrStr {
Static(&'static CStr),
Owned(CString),
None,
}
```This will also take out Option with same time. Then some nice ergonomic way to use this. This is suggestion and I have nothing to do with soc layer.
Argillander
Re: [PATCH v4 3/3] rust: Add SoC Driver Sample
Quoted 38 lines · Kari Argillander
Quoted 13 lines · Matthew Maurer
samples/rust/rust_soc.rs new file +13
-0+fn probe ( + pdev: &platform::Device <Core >, + _info:Option <&Self ::IdInfo >, + ) ->impl PinInit <Self ,Error > { +let dev = pdev.as_ref (); + +dev_dbg! (dev,"Probe Rust SoC driver sample.\n" ); + +let pdev = pdev.into (); +pin_init_scope (move || { +let machine =CString ::try_from (c"My cool ACME15 dev board" )?; +let family =CString ::try_from (c"ACME" )?; +let revision =CString ::try_from (c"1.2" )?;samples/rust/rust_soc.rs line 65
Quoted 10 lines
samples/rust/rust_soc.rs new file +10
-0+let serial_number =CString ::try_from (c"12345" )?; +let soc_id =CString ::try_from (c"ACME15" )?; + +let attr = soc::Attributes { + machine:Some (machine), + family:Some (family), + revision:Some (revision), + serial_number:Some (serial_number), + soc_id:Some (soc_id), + };To me it seems little bit awkward that all needs to be CStrings. Maybe something like this could be used (basically cow)
```rust pub enum AttrStr { Static(&'static CStr), Owned(CString), None, } ```
Quoted 2 lines
This will also take out Option with same time. Then some nice ergonomic way to use this. This is suggestion and I have nothing to do with soc layer.
IMO something like this should not be a SoC-specific special type. Feel free to propose a CowCStr in kernel::str, or adding the more general Cow and appropriate trait definitions to kernel::alloc, and we could modify the API in the future. Basically, this seems like a suggestion for a new vocabulary type rather than something for SoC.
We could technically also do a many-parameter'd struct and put `:
Deref<Target=CStr> bounds on everything, but putting 6 type parameters on something, which would then need explicit resolution when None` was used, seems potentially worse ergonomically.
Quoted 2 lines
Argillander
Danilo Krummrich
maintainerQuoted 6 lines · Matthew Maurer
This is a fairly straightforward binding of
soc_device_registerandsoc_device_unregisterwhich allows a driver to export basic info about a SoC.The last patch is a sample demonstrating usage, and can be dropped without issue.
Applied to driver-core-testing, thanks!
Quoted 3 lines
Matthew Maurer (3):
rust: Addsoc_devicesupport
docs: ABI: sysfs-devices-soc: Fix swapped sample values
I will pick this one up once -rc3 is out.
Quoted 1 line
rust: Add SoC Driver Sample