Skip to contentSkip to conversation

Support exporting SoC info from Rust

PATCH Applied Matthew Maurer 3 participants 10 messages Dec 26, 2025 – Dec 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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]>
#1 lore raw

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
index c5a7cda26c600e49c7ab0d547306d3281333f672..4ff01fb0f1bda27002094113c0bf9d074d28fdb6 100644
@@ -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
index a067038b4b422b4256f4a2b75fe644d47e6e82c8..9fdf76ca630e00715503e2a3a809bedc895697fd 100644
@@ -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
index f812cf12004286962985a068665443dc22c389a2..6d637e2fed1b605e2dfc2e7b2247179439a90ba9 100644
@@ -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
#2 lore raw

PATCH v4 2/3 docs: ABI: sysfs-devices-soc: Fix swapped sample values

In reply to Matthew Maurer

The 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
index 5269808ec35f8e2b18516556f886c77f5fac9401..cb6776a4afe02a76fe27ac6fc236babdc7865287 100644
@@ -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
maintainer
#3 lore raw

Re: [PATCH v4 2/3] docs: ABI: sysfs-devices-soc: Fix swapped sample values

Quoted 5 lines · Matthew Maurer

The sample values for family and machine were swapped relative to what the driver actually does, and doesn't match the field description.

Fixes da5a70f3519f ("Documentation: add information for new sysfs soc bus functionality")
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
index 5269808ec35f8e2b18516556f886c77f5fac9401..cb6776a4afe02a76fe27ac6fc236babdc7865287 100644
@@ -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.)

#4 lore raw

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 family and machine were swapped relative to what the driver actually does, and doesn't match the field description.

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
index 5269808ec35f8e2b18516556f886c77f5fac9401..cb6776a4afe02a76fe27ac6fc236babdc7865287 100644
@@ -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 -1
 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?

Yes, the sample SoC being described in the documentation uses "ux500"[1] for the family value.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/soc/ux500/ux500-soc-id.c#n133

Quoted 2 lines

(If not, no need to resend, I can fix it up on apply.)

#6 lore raw

PATCH v4 3/3 rust: Add SoC Driver Sample

In reply to Matthew Maurer

Shows 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
index 4ff01fb0f1bda27002094113c0bf9d074d28fdb6..bb2e710277cc84dd6042d4d46076e665d9f68752 100644
@@ -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
index 3efa51bfc8efccd91d9ee079ccd078ed1a6e8aa7..c49ab910634596aea4a1a73dac87585e084f420a 100644
@@ -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
index f65885d1d62bf406b0db13121ef3e5b09829cfbc..6c0aaa58ccccfd12ef019f68ca784f6d977bc668 100644
@@ -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
#7 lore raw

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

#8 lore raw

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

maintainer
#9 lore raw
In reply to Matthew Maurer
Quoted 6 lines · Matthew Maurer

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.

Applied to driver-core-testing, thanks!

Quoted 3 lines

Matthew Maurer (3):
rust: Add soc_device support
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