Support exporting SoC info from Rust
Subsystems (2)
v2 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 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 | 145 ++++++++++++++++++++++++++++ samples/rust/Kconfig | 11 +++ samples/rust/Makefile | 1 + samples/rust/rust_soc.rs | 82 ++++++++++++++++ 8 files changed, 246 insertions(+), 2 deletions(-)
---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-soc-bindings-9b0731bcdbed
Best regards,
Signature · 1 line
Matthew Maurer <[email protected]>
PATCH v2 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 | 145 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 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 +145 -0
145 lines not on this page · raw message
Signature · 1 line
2.52.0.305.g3fc767764a-goog
Danilo Krummrich
maintainerRe: [PATCH v2 1/3] rust: Add soc_device support
Quoted 26 lines · Matthew Maurer
(quoted diff) +26
-0+/// Attributes for a SoC device. +/// +/// These are both exported to userspace under /sys/devices/socX and provided to other drivers to +/// match against via `soc_device_match` (not yet available in Rust) to enable quirks or +/// device-specific support where necessary. +/// +/// All fields are freeform - they have no specific formatting, just defined meanings. +/// For example, the [`machine`](`Attributes::machine`) field could be "DB8500" or +/// "Qualcomm Technologies, Inc. SM8560 HDK", but regardless it should identify a board or product. +pub struct Attributes { + /// Should generally be a board ID or product ID. Examples + /// include DB8500 (ST-Ericsson) or "Qualcomm Technologies, inc. SM8560 HDK". + /// + /// If this field is not populated, the SoC infrastructure will try to populate it from + /// `/model` in the device tree. + pub machine: Option<CString>, + /// The broader class this SoC belongs to. Examples include ux500 + /// (for DB8500) or Snapdragon (for SM8650). + /// + /// On chips with ARM firmware supporting SMCCC v1.2+, this may be a JEDEC JEP106 manufacturer + /// identification. + pub family: Option<CString>, + /// The manufacturing revision of the part. Frequently this is MAJOR.MINOR, but not always. + pub revision: Option<CString>, + /// Serial Number - uniquely identifies a specific SoC. If present, should be unique (buying a + /// replacement part should change it if present). This field cannot be matched on and is
Review
Quoted 10 lines
(quoted diff) +10
-0+ /// solely present to export through /sys. + pub serial_number: Option<CString>, + /// SoC ID - identifies a specific SoC kind in question, sometimes more specifically than + /// `machine` if the same SoC is used in multiple products. Some devices use this to specify a + /// SoC name, e.g. "I.MX??", and others just print an ID number (e.g. Tegra and Qualcomm). + /// + /// On chips with ARM firmware supporting SMCCC v1.2+, this may be a JEDEC JEP106 manufacturer + /// identification (the family value) followed by a colon and then a 4-digit ID value. + pub soc_id: Option<CString>, +}
Thanks for expanding the documentation!
Quoted 42 lines
(quoted diff) +42
-0+struct BuiltAttributes { + // While `inner` has pointers to `_backing`, it is to the interior of the `CStrings`, not + // `backing` itself, so it does not need to be pinned. + _backing: Attributes, + // `Opaque` makes us `!Unpin`, as the registration holds a pointer to `inner` when used. + inner: Opaque<bindings::soc_device_attribute>, +} + +fn cstring_to_c(mcs: &Option<CString>) -> *const kernel::ffi::c_char { + mcs.as_ref() + .map(|cs| cs.as_char_ptr()) + .unwrap_or(core::ptr::null()) +} + +impl BuiltAttributes { + fn as_mut_ptr(&self) -> *mut bindings::soc_device_attribute { + self.inner.get() + } +} + +impl Attributes { + fn build(self) -> BuiltAttributes { + BuiltAttributes { + inner: Opaque::new(bindings::soc_device_attribute { + machine: cstring_to_c(&self.machine), + family: cstring_to_c(&self.family), + revision: cstring_to_c(&self.revision), + serial_number: cstring_to_c(&self.serial_number), + soc_id: cstring_to_c(&self.soc_id), + data: core::ptr::null(), + custom_attr_group: core::ptr::null(), + }), + _backing: self, + } + } +} + +/// # Safety +/// If a device is returned (e.g. no error), `attr` must remain valid for reads until the +/// returned pointer is released through `soc_device_unregister`. +unsafe fn register_device(attr: Pin<&BuiltAttributes>) -> Result<NonNull<bindings::soc_device>> { + let raw_soc =
Review
Quoted 10 lines
(quoted diff) +10
-0+ // SAFETY: + // * The struct provided through attr is backed by pinned data next to it, so as + // long as attr lives, the strings pointed to by the struct will too. + // * `attr` is pinned, so the pinned data won't move. + // * If it returns a device, and so others may try to read this data, by caller + // invariant, `attr` won't be released until the device is. + error::from_err_ptr(unsafe { bindings::soc_device_register(attr.as_mut_ptr()) })?; + // `soc_device_register` should not return NULL, but it doesn't hurt to be paranoid. + NonNull::new(raw_soc).ok_or(EINVAL) +}
I think it turns out cleaner without this helper function, inlining the contained code directly into Registration::new().
Quoted 16 lines
(quoted diff) +16
-0+#[pin_data(PinnedDrop)] +/// Registration handle for your soc_dev. If you let it go out of scope, your soc_dev will be +/// unregistered. +pub struct Registration { + #[pin] + attr: BuiltAttributes, + soc_dev: NonNull<bindings::soc_device>, +} + +// SAFETY: We provide no operations through `&Registration`. +unsafe impl Sync for Registration {} + +// SAFETY: All pointers are normal allocations, not thread-specific. +unsafe impl Send for Registration {} + +#[pinned_drop]
Review
Quoted 10 lines
(quoted diff) +10
-0+impl PinnedDrop for Registration { + fn drop(self: Pin<&mut Self>) { + // SAFETY: Device always contains a live pointer to a soc_device that can be unregistered + unsafe { bindings::soc_device_unregister(self.soc_dev.as_ptr()) } + } +} + +impl Registration { + /// Register a new SoC device + pub fn register(attr: Attributes) -> impl PinInit<Self, Error> {
Let's just call this Registration::new() please. We usually use new() if we return a Registration object (or an initializer as in this case) and register() if we do not return a Registration object, but rather automatically clean up the registration silently, e.g. through devres.
Quoted 1 line
+ try_pin_init!(&this in Self {
You should be able to just access Self::attr directly, i.e. no need for &this. (When you access attr within the code block of soc_dev it will be Self::attr and not the attr from the function argument.)
Please find a diff [1] below.
Review
Quoted 11 lines
(quoted diff) +11
-0+ attr: attr.build(), + // SAFETY: We have already initialized attr, and we are inside PinInit and Self + // is !Unpin, so attr won't be moved and is valid. If it returns success, attr + // will not be dropped until after our `PinnedDrop` implementation runs, so the + // device will be unregistered first. + soc_dev: unsafe { + register_device(Pin::new_unchecked(&(*this.as_ptr()).attr))? + }, + }? Error) + } +}
[1]
rust/kernel/soc.rs +13 -19
@@ -89,22 +89,6 @@ fn build(self) -> BuiltAttributes { } } -/// # Safety -/// If a device is returned (e.g. no error), `attr` must remain valid for reads until the -/// returned pointer is released through `soc_device_unregister`. -unsafe fn register_device (attr:Pin <&BuiltAttributes >) ->Result <NonNull <bindings::soc_device >> { -let raw_soc = -// SAFETY: -// * The struct provided through attr is backed by pinned data next to it, so as -// long as attr lives, the strings pointed to by the struct will too. -// * `attr` is pinned, so the pinned data won't move. -// * If it returns a device, and so others may try to read this data, by caller -// invariant, `attr` won't be released until the device is. - error::from_err_ptr (unsafe { bindings::soc_device_register (attr.as_mut_ptr ()) })?; -// `soc_device_register` should not return NULL, but it doesn't hurt to be paranoid. -NonNull ::new (raw_soc).ok_or (EINVAL ) -} -#[pin_data( PinnedDrop )] /// Registration handle for your soc_dev. If you let it go out of scope, your soc_dev will be /// unregistered.
@@ -131,14 +115,24 @@ fn drop(self: Pin<&mut Self>) {impl Registration {/// Register a new SoC device pub fn register (attr:Attributes ) ->impl PinInit <Self ,Error > { -try_pin_init! (&this inSelf { +try_pin_init! (Self { attr: attr.build (),// SAFETY: We have already initialized attr, and we are inside PinInit and Self // is !Unpin, so attr won't be moved and is valid. If it returns success, attr // will not be dropped until after our `PinnedDrop` implementation runs, so the // device will be unregistered first. - soc_dev:unsafe { -register_device (Pin ::new_unchecked (&(*this.as_ptr ()).attr))? + soc_dev: { +// SAFETY: +// * The struct provided through attr is backed by pinned data next to it, +// so as long as attr lives, the strings pointed to by the struct will too. +// * `attr` is pinned, so the pinned data won't move. +// * If it returns a device, and so others may try to read this data, by +// caller invariant, `attr` won't be released until the device is. +let raw_soc = error::from_err_ptr (unsafe { + bindings::soc_device_register (attr.as_mut_ptr ()) + })?; + +NonNull ::new (raw_soc).ok_or (EINVAL )? }, }?Error ) }
PATCH v2 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.305.g3fc767764a-goog
Re: [PATCH v2 2/3] docs: ABI: sysfs-devices-soc: Fix swapped sample values
Quoted 7 lines · Matthew Maurer
The sample values for
familyandmachinewere swapped relative to what the driver actually does, and doesn't match the field description.Signed-off-by Matthew Maurer <[email protected]>--- Documentation/ABI/testing/sysfs-devices-soc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 21 lines
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
Lee Jones [李琼斯]
PATCH v2 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 | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 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 +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.52.0.305.g3fc767764a-goog