[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260128182925.13225-3-linkmauve@linkmauve.fr>
Date: Wed, 28 Jan 2026 19:29:21 +0100
From: Link Mauve <linkmauve@...kmauve.fr>
To: rust-for-linux@...r.kernel.org
Cc: Link Mauve <linkmauve@...kmauve.fr>,
Srinivas Kandagatla <srini@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>,
Daniel Almeida <daniel.almeida@...labora.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Lyude Paul <lyude@...hat.com>,
Asahi Lina <lina+kernel@...hilina.net>,
Viresh Kumar <viresh.kumar@...aro.org>,
Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Tamir Duberstein <tamird@...nel.org>,
linux-kernel@...r.kernel.org
Subject: [RFC PATCH 2/3] rust: nvmem: Add an abstraction for nvmem providers
This is my very first Rust abstraction in the kernel, I took inspiration
from various other abstractions that had already been written.
I only implemented enough to rewrite a very simple driver I wrote in the
past, in order to test the API and make sure it’s at least as ergonomic
as the C version, without any unsafe at all.
I’m not completely happy about the API, especially the NvmemConfig type
has a bunch of setters but nothing forces you to use them correctly,
notably that .set_priv() has been called before .set(). Should I maybe
merge them into a single one which takes both arguments? Or is it ok if
priv isn’t set? I’m not sure in which case that could be useful, maybe
in C when using globals.
Signed-off-by: Link Mauve <linkmauve@...kmauve.fr>
---
rust/bindings/bindings_helper.h | 1 +
rust/kernel/lib.rs | 2 +
rust/kernel/nvmem.rs | 155 ++++++++++++++++++++++++++++++++
3 files changed, 158 insertions(+)
create mode 100644 rust/kernel/nvmem.rs
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index a067038b4b42..522a76b2e294 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -65,6 +65,7 @@
#include <linux/mdio.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>
+#include <linux/nvmem-provider.h>
#include <linux/of_device.h>
#include <linux/pci.h>
#include <linux/phy.h>
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index f812cf120042..dd0ac968d05f 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -115,6 +115,8 @@
pub mod module_param;
#[cfg(CONFIG_NET)]
pub mod net;
+#[cfg(CONFIG_NVMEM)]
+pub mod nvmem;
pub mod num;
pub mod of;
#[cfg(CONFIG_PM_OPP)]
diff --git a/rust/kernel/nvmem.rs b/rust/kernel/nvmem.rs
new file mode 100644
index 000000000000..555ed260629c
--- /dev/null
+++ b/rust/kernel/nvmem.rs
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! nvmem framework provider.
+//!
+//! Copyright (C) 2026 Link Mauve <linkmauve@...kmauve.fr>
+
+use crate::build_error;
+use crate::device::Device;
+use crate::error::{from_result, VTABLE_DEFAULT_ERROR};
+use crate::prelude::*;
+use core::marker::PhantomData;
+use macros::vtable;
+
+/// The possible types for a nvmem provider.
+#[derive(Default)]
+#[repr(u32)]
+pub enum Type {
+ /// The type of memory is unknown.
+ #[default]
+ Unknown = bindings::nvmem_type_NVMEM_TYPE_UNKNOWN,
+
+ /// Electrically erasable programmable ROM.
+ Eeprom = bindings::nvmem_type_NVMEM_TYPE_EEPROM,
+
+ /// One-time programmable memory.
+ Otp = bindings::nvmem_type_NVMEM_TYPE_OTP,
+
+ /// This memory is backed by a battery.
+ BatteryBacked = bindings::nvmem_type_NVMEM_TYPE_BATTERY_BACKED,
+
+ /// Ferroelectric RAM.
+ Fram = bindings::nvmem_type_NVMEM_TYPE_FRAM,
+}
+
+/// nvmem configuration.
+///
+/// Rust abstraction for the C `struct nvmem_config`.
+#[derive(Default)]
+pub struct NvmemConfig<T: NvmemProvider>
+where
+ T: Default,
+{
+ inner: bindings::nvmem_config,
+ _p: PhantomData<T>,
+}
+
+impl<T: NvmemProvider + Default> NvmemConfig<T> {
+ /// NvmemConfig's read callback.
+ ///
+ /// SAFETY: Called from C. Inputs must be valid pointers.
+ extern "C" fn reg_read(
+ context: *mut c_void,
+ offset: u32,
+ val: *mut c_void,
+ bytes: usize,
+ ) -> i32 {
+ from_result(|| {
+ // SAFETY: context is a valid T::Priv as defined in Self::set_priv().
+ let context = unsafe { &*(context as *mut T::Priv) };
+ let val = val as *mut u8;
+ // SAFETY: val should be non-null, and allocated for bytes bytes.
+ let data = unsafe { core::slice::from_raw_parts_mut(val, bytes) };
+ T::read(context, offset, data).map(|()| 0)
+ })
+ }
+
+ /// NvmemConfig's write callback.
+ ///
+ /// SAFETY: Called from C. Inputs must be valid pointers.
+ extern "C" fn reg_write(
+ context: *mut c_void,
+ offset: u32,
+ // TODO: Change val from void* to const void* in the C API!
+ val: *mut c_void,
+ bytes: usize,
+ ) -> i32 {
+ from_result(|| {
+ // SAFETY: context is a valid T::Priv as defined in Self::set_priv().
+ let context = unsafe { &*(context as *mut T::Priv) };
+ let val = val as *mut u8 as *const u8;
+ // SAFETY: val should be non-null, and allocated for bytes bytes.
+ let data = unsafe { core::slice::from_raw_parts(val, bytes) };
+ T::write(context, offset, data).map(|()| 0)
+ })
+ }
+
+ /// User context passed to read/write callbacks.
+ pub fn set_priv(&mut self, priv_: &T::Priv) {
+ // FIXME: This list of as indicates some unsoundness in the types…
+ self.inner.priv_ = priv_ as *const T::Priv as *const c_void as *mut c_void;
+ }
+
+ /// Sets the configuration on the given device.
+ pub fn set(mut self, dev: &Device) {
+ self.inner.reg_read = Some(Self::reg_read);
+ self.inner.reg_write = Some(Self::reg_write);
+ // SAFETY: All arguments should be non-null and what the function expects.
+ unsafe { bindings::devm_nvmem_register(dev.as_raw(), &self.inner) };
+ }
+
+ /// Optional name.
+ pub fn set_name(&mut self, name: &CStr) {
+ // TODO: Why do we have to do this cast from i8 to u8?
+ self.inner.name = name.as_ptr() as *const _;
+ }
+
+ /// Type of the nvmem storage
+ pub fn set_type(&mut self, type_: Type) {
+ self.inner.type_ = type_ as u32;
+ }
+
+ /// Device is read-only.
+ pub fn set_read_only(&mut self, read_only: bool) {
+ self.inner.read_only = read_only;
+ }
+
+ /// Device is accessibly to root only.
+ pub fn set_root_only(&mut self, root_only: bool) {
+ self.inner.root_only = root_only;
+ }
+
+ /// Device size.
+ pub fn set_size(&mut self, size: i32) {
+ self.inner.size = size;
+ }
+
+ /// Minimum read/write access granularity.
+ pub fn set_word_size(&mut self, word_size: i32) {
+ self.inner.word_size = word_size;
+ }
+
+ /// Minimum read/write access stride.
+ pub fn set_stride(&mut self, stride: i32) {
+ self.inner.stride = stride;
+ }
+}
+
+/// Helper trait to define the callbacks of a nvmem provider.
+#[vtable]
+pub trait NvmemProvider {
+ /// The type passed into the context for read and write functions.
+ type Priv;
+
+ /// Callback to read data.
+ #[inline]
+ fn read(_context: &Self::Priv, _offset: u32, _data: &mut [u8]) -> Result {
+ build_error!(VTABLE_DEFAULT_ERROR)
+ }
+
+ /// Callback to write data.
+ #[inline]
+ fn write(_context: &Self::Priv, _offset: u32, _data: &[u8]) -> Result {
+ build_error!(VTABLE_DEFAULT_ERROR)
+ }
+}
--
2.52.0
Powered by blists - more mailing lists