Skip to contentSkip to conversation

rust: reset: add reset control abstractions

PATCH In review Daniel Almeida 5 participants 16 messages Nov 26 – Nov 27 Rust

lore
Share & tools
lore

Daniel Almeida

author reviewer
#0 lore raw

cover rust: reset: add reset control abstractions

This series adds a Rust abstraction for the consumer side of the reset controller API. A driver can get the reset lines of its device, as an exclusive or a shared reset control, and assert and deassert them.

This will be used by the upcoming rkvenc driver that my colleagues Detlev Casanova, Nicolas Dufresne and I are working on, to recover a hung core of the encoder without stopping the other one. More details on this driver can be found by watching Nicolas Dufresne's talk at LPC 2026 [0]. Samsung's Rust UFS RFC [1] also uses reset lines, through an abstraction that it has not posted.

The abstraction follows the regulator abstraction and the clk series [2], with the reset state as a type parameter.

Patch 1 adds the C helpers that patch 2 needs.

Patch 2 adds ExclusiveResetControl, with the Unknown, Asserted and Deasserted states.

Patch 3 adds ExclusiveResetControl::get_array() and get_array_optional(), which get all the reset lines of a device as one control.

Patch 4 adds AssertedGuard, which asserts the line of an exclusive control in the Unknown state and deasserts it when dropped, as a convenience for drivers.

Patch 5 adds SharedResetControl, with the Unknown and Deasserted states only, and patch 6 adds its array getters.

Patch 7 adds ExclusiveResetControl::devm_deassert() and devm_deassert_optional(), for drivers that only need their line deasserted while they are bound.

reset_control_reset(), the bulk API and the shared devm getters are not covered, and there is no guard for a Deasserted control or for a shared one. Nothing in the tree uses the abstraction yet.

Patches 1 and 2 add the two new files to the RESET CONTROLLER FRAMEWORK entry of MAINTAINERS. Philipp, please let me know if you would rather have them elsewhere.

This is based on v7.4-rc2.

[0]: https://www.youtube.com/watch?v=75E15eHY7ac
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]

---
Daniel Almeida (7):
rust: reset: add reset control helpers
rust: reset: add ExclusiveResetControl
rust: reset: add ExclusiveResetControl::get_array() and get_array_optional()
rust: reset: add AssertedGuard
rust: reset: add SharedResetControl
rust: reset: add SharedResetControl::get_array() and get_array_optional()
rust: reset: add devm_deassert() and devm_deassert_optional()

 MAINTAINERS                     |   2 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/helpers.c          |   1 +
 rust/helpers/reset.c            |  67 ++++
 rust/kernel/lib.rs              |   1 +
 rust/kernel/reset.rs            | 870 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 942 insertions(+)

---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261113-b4-rust-reset-ca5deca6e036

Best regards,

Signature · 1 line
Daniel Almeida <[email protected]>
author reviewer
#1 lore raw

PATCH 1/7 rust: reset: add reset control helpers

In preparation for the reset control abstraction added by the next patch, make the static inline functions it uses available to bindgen.

---
 MAINTAINERS                     |  1 +
 rust/bindings/bindings_helper.h |  1 +
 rust/helpers/helpers.c          |  1 +
 rust/helpers/reset.c            | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 36 insertions(+)
MAINTAINERS +1 -0
index 222b7fc2c3aa..d8bcacb7d8ef 100644
@@ -24063,6 +24063,7 @@ F:	include/dt-bindings/reset/
 F:	include/linux/reset-controller.h
 F:	include/linux/reset.h
 F:	include/linux/reset/
+F:	rust/helpers/reset.c
 K:	\b(?:devm_|of_)?reset_control(?:ler_[a-z]+|_[a-z_]+)?\b
 
 RESTARTABLE SEQUENCES SUPPORT
rust/bindings/bindings_helper.h +1 -0
index 3791af0aadc7..c96634bfaacb 100644
@@ -85,6 +85,7 @@
 #include <linux/random.h>
 #include <linux/refcount.h>
 #include <linux/regulator/consumer.h>
+#include <linux/reset.h>
 #include <linux/sched.h>
 #include <linux/security.h>
 #include <linux/serdev.h>
rust/helpers/helpers.c +1 -0
index d62c7be1ca7b..a5a127935961 100644
@@ -92,6 +92,7 @@
 #include "rcu.c"
 #include "refcount.c"
 #include "regulator.c"
+#include "reset.c"
 #include "scatterlist.c"
 #include "security.c"
 #include "serdev.c"
rust/helpers/reset.c new file +33 -0
new file mode 100644
index 000000000000..ea2e6f09b7d5
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/reset.h>
+
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_exclusive(struct device *dev, const char *id)
+{
+	return reset_control_get_exclusive(dev, id);
+}
+
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_optional_exclusive(struct device *dev,
+						 const char *id)
+{
+	return reset_control_get_optional_exclusive(dev, id);
+}
+
+#ifndef CONFIG_RESET_CONTROLLER
+__rust_helper void rust_helper_reset_control_put(struct reset_control *rstc)
+{
+	reset_control_put(rstc);
+}
+
+__rust_helper int rust_helper_reset_control_assert(struct reset_control *rstc)
+{
+	return reset_control_assert(rstc);
+}
+
+__rust_helper int rust_helper_reset_control_deassert(struct reset_control *rstc)
+{
+	return reset_control_deassert(rstc);
+}
+#endif
Signature · 1 line
2.56.0
author reviewer
#2 lore raw

PATCH 2/7 rust: reset: add ExclusiveResetControl

In reply to Daniel Almeida

Add an abstraction for exclusive reset controls, so that Rust drivers can assert and deassert the reset lines of their device.

It follows the type-state pattern of the regulator abstraction. It borrows the bound device it was obtained from, which ties it to the binding as devm_reset_control_get_exclusive() does in C.

---
 MAINTAINERS          |   1 +
 rust/kernel/lib.rs   |   1 +
 rust/kernel/reset.rs | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 335 insertions(+)
MAINTAINERS +1 -0
index d8bcacb7d8ef..e427742252d0 100644
@@ -24064,6 +24064,7 @@ F:	include/linux/reset-controller.h
 F:	include/linux/reset.h
 F:	include/linux/reset/
 F:	rust/helpers/reset.c
+F:	rust/kernel/reset.rs
 K:	\b(?:devm_|of_)?reset_control(?:ler_[a-z]+|_[a-z_]+)?\b
 
 RESTARTABLE SEQUENCES SUPPORT
rust/kernel/lib.rs +1 -0
index 1ab41129d557..c0199ec055b8 100644
@@ -125,6 +125,7 @@
 pub mod ratelimit;
 pub mod rbtree;
 pub mod regulator;
+pub mod reset;
 pub mod revocable;
 pub mod safety;
 pub mod scatterlist;
rust/kernel/reset.rs new file +333 -0

333 lines not on this page · raw message

Signature · 1 line
2.56.0
author reviewer
#3 lore raw

PATCH 3/7 rust: reset: add ExclusiveResetControl::get_array() and get_array_optional()

In reply to Daniel Almeida

Add ExclusiveResetControl::get_array() and get_array_optional(), which get all the reset lines in the resets property of a device as a single reset control. This is for drivers that assert and deassert all the reset lines of their device together.

---
 rust/helpers/reset.c |  7 ++++
 rust/kernel/reset.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 101 insertions(+), 2 deletions(-)
rust/helpers/reset.c +7 -0
index ea2e6f09b7d5..435f2c7c815d 100644
@@ -16,6 +16,13 @@ rust_helper_reset_control_get_optional_exclusive(struct device *dev,
 }
 
 #ifndef CONFIG_RESET_CONTROLLER
+__rust_helper struct reset_control *
+rust_helper_fwnode_reset_control_array_get(struct fwnode_handle *fwnode,
+					   enum reset_control_flags flags)
+{
+	return fwnode_reset_control_array_get(fwnode, flags);
+}
+
 __rust_helper void rust_helper_reset_control_put(struct reset_control *rstc)
 {
 	reset_control_put(rstc);
rust/kernel/reset.rs +94 -2

120 lines not on this page · raw message

Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH 3/7] rust: reset: add ExclusiveResetControl::get_array() and get_array_optional()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] rust: reset: missing #[inline] on ExclusiveResetControl::get_array_internal() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] rust: reset: missing #[inline] on ExclusiveResetControl::get_array_internal()
--

commit 5faf22f79dec9848d60fc84ac341d26a05013a7f
Author: Daniel Almeida <[email protected]>

rust: reset: add ExclusiveResetControl::get_array() and get_array_optional()

This commit adds ExclusiveResetControl::get_array() and get_array_optional() to acquire all reset lines listed in a device's resets property as a single reset control.

Quoted 4 lines
rust/kernel/reset.rs +0 -0
index 396f1e19ee2e3..7ca83f2d6abf1 100644

[ ... ]

Quoted 21 lines
rust/kernel/reset.rs +17 -0
@@ -213,6 +219,92 @@ pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self>
             _phantom: PhantomData,
         })
     }
+
+    /// Gets the reset lines of `dev` as one [`ExclusiveResetControl`], as [`Self::get_array`] does
+    /// or, if `optional` is set, as [`Self::get_array_optional`] does.
+    fn get_array_internal(dev: &'a Device<Bound>, optional: bool) -> Result<Self> {
+        let flags = if optional {
+            bindings::reset_control_flags_RESET_CONTROL_OPTIONAL_EXCLUSIVE
+        } else {
+            bindings::reset_control_flags_RESET_CONTROL_EXCLUSIVE
+        };
+
+        let fwnode = dev
+            .fwnode()
+            .map_or(ptr::null_mut(), |fwnode| fwnode.as_raw());
+
+        // SAFETY: `fwnode` is the firmware node of `dev` or `NULL`, which
+        // `fwnode_reset_control_array_get()` checks for.
+        let inner = unsafe { bindings::fwnode_reset_control_array_get(fwnode, flags) };

rust/kernel/reset.rs pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> · line 248

Quoted 10 lines
rust/kernel/reset.rs +10 -0
+        let inner = from_err_ptr(inner)?;
+
+        // INVARIANT: `inner` comes from a successful call to `fwnode_reset_control_array_get()`
+        // with one of the two flags above, and the returned value owns the reference from that
+        // call.
+        Ok(Self {
+            inner,
+            _phantom: PhantomData,
+        })
+    }

[Severity: Low]
Should get_array_internal() be marked #[inline]? Like the other small C binding wrappers in this module, it forwards directly to bindings::fwnode_reset_control_array_get(), and without #[inline] cross-crate calls to the #[inline] get_array() and get_array_optional() methods cannot inline it or constant-fold the optional flag.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#5 lore raw

PATCH 4/7 rust: reset: add AssertedGuard

In reply to Daniel Almeida

Some drivers hold their device in reset for a short time while they are bound.

Add ExclusiveResetControl::assert_guard() as a convenience for them. It asserts the reset line of an Unknown control and returns an AssertedGuard that deasserts it when dropped, so the control stays in the Unknown state.

The guard borrows the control mutably because, unlike regulators, exclusive resets do not have an "enabled" count. Therefore, allowing more than one guard at a time would not work, as dropping one of them would invalidate the other while it is still alive.

---
 rust/kernel/reset.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
rust/kernel/reset.rs +82 -0
index 7ca83f2d6abf..3bbd226825d8 100644
@@ -12,6 +12,10 @@
 //! A driver can also get a single reset control for all the reset lines in the `resets`
 //! property of its device. Its operations then act on all of those lines.
 //!
+//! An [`ExclusiveResetControl`] in the [`Unknown`] state can also assert its line for a limited
+//! time, with [`ExclusiveResetControl::assert_guard`]. The [`AssertedGuard`] that it returns
+//! deasserts the line when dropped, and the control stays in the [`Unknown`] state.
+//!
 //! C header: [`include/linux/reset.h`](srctree/include/linux/reset.h)
 //!
 //! See [Reset controller API](https://docs.kernel.org/driver-api/reset.html) for more information.
@@ -381,6 +385,24 @@ pub fn assert(self) -> Result<ExclusiveResetControl<'a, Asserted>, Error<Self>>
     pub fn deassert(self) -> Result<ExclusiveResetControl<'a, Deasserted>, Error<Self>> {
         self.deassert_internal()
     }
+
+    /// Asserts the reset line and returns an [`AssertedGuard`], which deasserts it when dropped.
+    ///
+    /// The [`ExclusiveResetControl`] stays in the [`Unknown`] state, and it cannot be used while
+    /// the guard exists.
+    ///
+    /// The line is asserted with the kernel's [`reset_control_assert()`].
+    ///
+    /// [`reset_control_assert()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_assert
+    #[inline]
+    pub fn assert_guard(&mut self) -> Result<AssertedGuard<'_, 'a>> {
+        // SAFETY: By the type invariants, `self.inner` is a pointer that a C getter returned, and
+        // `self` owns its reference.
+        to_result(unsafe { bindings::reset_control_assert(self.inner) })?;
+
+        Ok(AssertedGuard { reset: self })
+    }
 }
 
 impl<'a> ExclusiveResetControl<'a, Asserted> {
@@ -423,3 +445,63 @@ fn drop(&mut self) {
         unsafe { bindings::reset_control_put(self.inner) };
     }
 }
+
+/// A guard returned by [`ExclusiveResetControl::assert_guard`], which deasserts the reset line when
+/// dropped.
+///
+/// An error from the deassert done on drop is discarded. [`AssertedGuard::deassert`] deasserts the
+/// line and returns the error.
+///
+/// # Examples
+///
+/// Holding a device in reset for a limited time:
+///
+/// ```
+/// use kernel::reset::{
+///     ExclusiveResetControl,
+///     Unknown, //
+/// };
+///
+/// # fn restart_clocks() -> Result { Ok(()) }
+/// fn reset_device(reset: &mut ExclusiveResetControl<'_, Unknown>) -> Result {
+///     let asserted = reset.assert_guard()?;
+///
+///     // If this fails, dropping `asserted` deasserts the line.
+///     restart_clocks()?;
+///
+///     asserted.deassert()
+/// }
+/// ```
+#[must_use = "the reset line is deasserted immediately when the guard is unused"]
+pub struct AssertedGuard<'r, 'a> {
+    reset: &'r mut ExclusiveResetControl<'a, Unknown>,
+}
+
+impl<'r, 'a> AssertedGuard<'r, 'a> {
+    /// Deasserts the reset line and consumes the guard.
+    ///
+    /// The only difference from dropping the guard is that this returns an error from the
+    /// deassert, which a drop discards.
+    ///
+    /// Equivalent to the kernel's [`reset_control_deassert()`].
+    ///
+    /// [`reset_control_deassert()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_deassert
+    #[inline]
+    pub fn deassert(self) -> Result {
+        // Skip `drop()`, which would deassert the line a second time.
+        let guard = ManuallyDrop::new(self);
+
+        // SAFETY: By the type invariants of `ExclusiveResetControl`, `guard.reset.inner` is a
+        // pointer that a C getter returned, and `guard.reset` owns its reference.
+        to_result(unsafe { bindings::reset_control_deassert(guard.reset.inner) })
+    }
+}
+
+impl<'r, 'a> Drop for AssertedGuard<'r, 'a> {
+    fn drop(&mut self) {
+        // SAFETY: By the type invariants of `ExclusiveResetControl`, `self.reset.inner` is a
+        // pointer that a C getter returned, and `self.reset` owns its reference.
+        unsafe { bindings::reset_control_deassert(self.reset.inner) };
+    }
+}
Signature · 1 line
2.56.0
author reviewer
#6 lore raw

PATCH 5/7 rust: reset: add SharedResetControl

In reply to Daniel Almeida

A reset line can be shared between devices. The reset core then counts the deasserts on the line, and only the first deassert and the last assert act on it.

Add SharedResetControl for such lines, with get() and get_optional(). It has two states, Unknown and Deasserted, which correspond to Disabled and Enabled of Regulator<T>. An assert returns it to Unknown, since other users may keep the line deasserted.

Like ExclusiveResetControl, it borrows the bound device it was obtained from, which ties it to the binding as devm_reset_control_get_shared() does in C.

Nothing uses SharedResetControl yet.

---
 rust/helpers/reset.c |  13 ++++
 rust/kernel/reset.rs | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 219 insertions(+)
rust/helpers/reset.c +13 -0
index 435f2c7c815d..9c106d8b6de1 100644
@@ -15,6 +15,19 @@ rust_helper_reset_control_get_optional_exclusive(struct device *dev,
 	return reset_control_get_optional_exclusive(dev, id);
 }
 
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_shared(struct device *dev, const char *id)
+{
+	return reset_control_get_shared(dev, id);
+}
+
+__rust_helper struct reset_control *
+rust_helper_reset_control_get_optional_shared(struct device *dev,
+					      const char *id)
+{
+	return reset_control_get_optional_shared(dev, id);
+}
+
 #ifndef CONFIG_RESET_CONTROLLER
 __rust_helper struct reset_control *
 rust_helper_fwnode_reset_control_array_get(struct fwnode_handle *fwnode,
rust/kernel/reset.rs +206 -0

227 lines not on this page · raw message

Signature · 1 line
2.56.0
reviewer
#7 lore raw

Re: [PATCH 5/7] rust: reset: add SharedResetControl

Quoted 13 lines · Daniel Almeida
rust/kernel/reset.rs +13 -0
+    /// Deasserts the reset line and moves the [`SharedResetControl`] to the [`Deasserted`] state.
+    ///
+    /// Equivalent to the kernel's [`reset_control_deassert()`].
+    ///
+    /// [`reset_control_deassert()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_deassert
+    #[inline]
+    pub fn deassert(self) -> Result<SharedResetControl<'a, Deasserted>, Error<Self>> {
+        // INVARIANT: The reference that `self` owns moves to the value that this returns.
+        let reset = ManuallyDrop::new(self);
+
+        // SAFETY: By the type invariants, `reset.inner` is a pointer that a C getter returned, and
+        // `reset` owns its reference.

rust/kernel/reset.rs fn drop(&mut self) { · line 664

Quoted 10 lines
rust/kernel/reset.rs +10 -0
+        to_result(unsafe { bindings::reset_control_deassert(reset.inner) })
+            .map(|()| SharedResetControl {
+                inner: reset.inner,
+                _phantom: PhantomData,
+            })
+            .map_err(|error| Error {
+                error,
+                reset: ManuallyDrop::into_inner(reset),
+            })
+    }

The C side here interacts badly with the error path of SharedResetControl::deassert().

In drivers/reset/core.c, reset_control_deassert() does:

    if (rstc->shared) {
        if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
            return -EINVAL;

        if (atomic_inc_return(&rstc->deassert_count) != 1)
            return 0;
    }
    ...
    return rcdev->ops->deassert(rcdev, rstc->id);

Unlike reset_control_reset(), reset_control_deassert() does not decrement rstc->deassert_count when rcdev->ops->deassert() returns an error.

Because SharedResetControl::deassert() returns reset in the Unknown state (ASSERT_ON_DROP == false) on error:

- If the caller drops reset, deassert_count stays leaked at 1, so future shared users will see deassert_count != 0 and skip ops->deassert().

- If the caller retries reset.deassert(), atomic_inc_return() returns 2 (!= 1), so reset_control_deassert() returns 0 without calling ops->deassert(), leaving the line asserted while Rust treats the control as Deasserted.

I wonder if this is a bug on the C side we should fix in core.c?

Alice

author reviewer
#8 lore raw

PATCH 6/7 rust: reset: add SharedResetControl::get_array() and get_array_optional()

In reply to Daniel Almeida

Add SharedResetControl::get_array() and get_array_optional(), which get all the reset lines in the resets property of a device as a single shared reset control, as devm_reset_control_array_get_shared() and devm_reset_control_array_get_optional_shared() do in C. This is for drivers that assert and deassert all the shared reset lines of their device together.

Nothing uses them yet.

---
 rust/kernel/reset.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
rust/kernel/reset.rs +91 -0
index 4d241ec6e549..85c51fc153ac 100644
@@ -568,6 +568,8 @@ fn drop(&mut self) {
 ///
 /// - [`reset_control_get_shared()`]
 /// - [`reset_control_get_optional_shared()`]
+/// - `fwnode_reset_control_array_get()` with the `RESET_CONTROL_SHARED` or
+///   `RESET_CONTROL_OPTIONAL_SHARED` flag
 ///
 /// It is treated as an opaque token that may only be accessed using C API methods (e.g., it may be
 /// `NULL` if the C API returns `NULL`). The [`SharedResetControl`] owns the reference that the
@@ -639,6 +641,95 @@ pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self>
         })
     }
 
+    /// Gets the reset lines of `dev` as one [`SharedResetControl`], as [`Self::get_array`] does
+    /// or, if `optional` is set, as [`Self::get_array_optional`] does.
+    fn get_array_internal(dev: &'a Device<Bound>, optional: bool) -> Result<Self> {
+        let flags = if optional {
+            bindings::reset_control_flags_RESET_CONTROL_OPTIONAL_SHARED
+        } else {
+            bindings::reset_control_flags_RESET_CONTROL_SHARED
+        };
+
+        let fwnode = dev
+            .fwnode()
+            .map_or(ptr::null_mut(), |fwnode| fwnode.as_raw());
+
+        // SAFETY: `fwnode` is the firmware node of `dev` or `NULL`, which
+        // `fwnode_reset_control_array_get()` checks for.
+        let inner = unsafe { bindings::fwnode_reset_control_array_get(fwnode, flags) };
+        let inner = from_err_ptr(inner)?;
+
+        // INVARIANT: `inner` comes from a successful call to `fwnode_reset_control_array_get()`
+        // with one of the two flags above, and the returned value owns the reference from that
+        // call.
+        Ok(Self {
+            inner,
+            _phantom: PhantomData,
+        })
+    }
+
+    /// Gets the reset lines listed in the `resets` property of `dev` as one
+    /// [`SharedResetControl`].
+    ///
+    /// Its operations act on all the lines, in no guaranteed order. If one of them fails on a
+    /// line, the opposite operation is run on the lines it had already succeeded on. This
+    /// includes the assert that is done on drop. The caller only gets the error from the line
+    /// that failed, while errors from the opposite operation are discarded.
+    ///
+    /// A [`SharedResetControl::assert`] that fails on one line therefore leaves the control
+    /// holding its deasserts on the other lines. The control is in the [`Unknown`] state then,
+    /// and does not give them back.
+    ///
+    /// Equivalent to the kernel's `fwnode_reset_control_array_get()` on the firmware node of
+    /// `dev`, as `devm_reset_control_array_get_shared()` does.
+    ///
+    /// # Examples
+    ///
+    /// Keeping all the shared reset lines of a device deasserted:
+    ///
+    /// ```
+    /// use kernel::{
+    ///     device::{
+    ///         Bound,
+    ///         Device, //
+    ///     },
+    ///     reset::{
+    ///         Deasserted,
+    ///         SharedResetControl, //
+    ///     },
+    /// };
+    ///
+    /// struct Data<'a> {
+    ///     // Dropping `Data` asserts the reset control again.
+    ///     resets: SharedResetControl<'a, Deasserted>,
+    /// }
+    ///
+    /// fn probe<'a>(dev: &'a Device<Bound>) -> Result<Data<'a>> {
+    ///     let resets = SharedResetControl::get_array(dev)?.deassert()?;
+    ///
+    ///     Ok(Data { resets })
+    /// }
+    /// ```
+    #[inline]
+    pub fn get_array(dev: &'a Device<Bound>) -> Result<Self> {
+        Self::get_array_internal(dev, false)
+    }
+
+    /// Gets the reset lines listed in the `resets` property of `dev` as one
+    /// [`SharedResetControl`], if there are any.
+    ///
+    /// Behaves the same as [`Self::get_array`], except when `dev` has no firmware node, when its
+    /// `resets` property has no entry or cannot be read, or when `CONFIG_RESET_CONTROLLER` is
+    /// disabled. In those cases, it returns a [`SharedResetControl`] on which every operation
+    /// succeeds and does nothing.
+    ///
+    /// Equivalent to the kernel's `fwnode_reset_control_array_get()` on the firmware node of
+    /// `dev`, as `devm_reset_control_array_get_optional_shared()` does.
+    #[inline]
+    pub fn get_array_optional(dev: &'a Device<Bound>) -> Result<Self> {
+        Self::get_array_internal(dev, true)
+    }
+
     /// Deasserts the reset line and moves the [`SharedResetControl`] to the [`Deasserted`] state.
     ///
     /// Equivalent to the kernel's [`reset_control_deassert()`].
Signature · 1 line
2.56.0
author reviewer
#9 lore raw

PATCH 7/7 rust: reset: add devm_deassert() and devm_deassert_optional()

In reply to Daniel Almeida

Many drivers need the reset line of their device deasserted while the device is bound, but otherwise do not care about exerting finer control over it. C drivers do that with devm_reset_control_get_exclusive_deasserted() and devm_reset_control_get_optional_exclusive_deasserted().

Add ExclusiveResetControl::devm_deassert() and devm_deassert_optional(), which call them and leave the reset control to devres.

Nothing uses them yet.

---
 rust/helpers/reset.c | 14 +++++++++++
 rust/kernel/reset.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
rust/helpers/reset.c +14 -0
index 9c106d8b6de1..aa20b3e7a88b 100644
@@ -28,6 +28,20 @@ rust_helper_reset_control_get_optional_shared(struct device *dev,
 	return reset_control_get_optional_shared(dev, id);
 }
 
+__rust_helper struct reset_control *
+rust_helper_devm_reset_control_get_exclusive_deasserted(struct device *dev,
+							const char *id)
+{
+	return devm_reset_control_get_exclusive_deasserted(dev, id);
+}
+
+__rust_helper struct reset_control *
+rust_helper_devm_reset_control_get_optional_exclusive_deasserted(struct device *dev,
+								 const char *id)
+{
+	return devm_reset_control_get_optional_exclusive_deasserted(dev, id);
+}
+
 #ifndef CONFIG_RESET_CONTROLLER
 __rust_helper struct reset_control *
 rust_helper_fwnode_reset_control_array_get(struct fwnode_handle *fwnode,
rust/kernel/reset.rs +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.56.0
#10 lore raw
In reply to Daniel Almeida

Hi Daniel,

Le jeudi 26 novembre 2026 à 13:04 -0300, Daniel Almeida a écrit :

Quoted 10 lines

This series adds a Rust abstraction for the consumer side of the reset controller API. A driver can get the reset lines of its device, as an exclusive or a shared reset control, and assert and deassert them.

This will be used by the upcoming rkvenc driver that my colleagues Detlev Casanova, Nicolas Dufresne and I are working on, to recover a hung core of the encoder without stopping the other one. More details on this driver can be found by watching Nicolas Dufresne's talk at LPC 2026 [0]. Samsung's Rust UFS RFC [1] also uses reset lines, through an abstraction that it has not posted.

I don't mind being cited, but please give a heads-up, we haven't discussed any of this, so I can't really comment if its a good or bad idea. On the C side, shared reset don't really work today, so I'm left wondering.

Nicolas

Quoted 62 lines

The abstraction follows the regulator abstraction and the clk series [2], with the reset state as a type parameter.

Patch 1 adds the C helpers that patch 2 needs.

Patch 2 adds ExclusiveResetControl, with the Unknown, Asserted and Deasserted states.

Patch 3 adds ExclusiveResetControl::get_array() and get_array_optional(), which get all the reset lines of a device as one control.

Patch 4 adds AssertedGuard, which asserts the line of an exclusive control in the Unknown state and deasserts it when dropped, as a convenience for drivers.

Patch 5 adds SharedResetControl, with the Unknown and Deasserted states only, and patch 6 adds its array getters.

Patch 7 adds ExclusiveResetControl::devm_deassert() and devm_deassert_optional(), for drivers that only need their line deasserted while they are bound.

reset_control_reset(), the bulk API and the shared devm getters are not covered, and there is no guard for a Deasserted control or for a shared one. Nothing in the tree uses the abstraction yet.

Patches 1 and 2 add the two new files to the RESET CONTROLLER FRAMEWORK entry of MAINTAINERS. Philipp, please let me know if you would rather have them elsewhere.

This is based on v7.4-rc2.

[0]: https://www.youtube.com/watch?v=75E15eHY7ac
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]

---
Daniel Almeida (7):
rust: reset: add reset control helpers
rust: reset: add ExclusiveResetControl
rust: reset: add ExclusiveResetControl::get_array() and get_array_optional()
rust: reset: add AssertedGuard
rust: reset: add SharedResetControl
rust: reset: add SharedResetControl::get_array() and get_array_optional()
rust: reset: add devm_deassert() and devm_deassert_optional()

 MAINTAINERS                     |   2 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/helpers.c          |   1 +
 rust/helpers/reset.c            |  67 ++++
 rust/kernel/lib.rs              |   1 +
 rust/kernel/reset.rs            | 870 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 942 insertions(+)

---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261113-b4-rust-reset-ca5deca6e036

Best regards,

Signature · 1 line
Daniel Almeida <[email protected]>
author reviewer
#11 lore raw

Hi Nicolas,

Quoted 16 lines · Nicolas Dufresne

Hi Daniel,

Quoted 10 lines · Daniel Almeida

This series adds a Rust abstraction for the consumer side of the reset controller API. A driver can get the reset lines of its device, as an exclusive or a shared reset control, and assert and deassert them.

This will be used by the upcoming rkvenc driver that my colleagues Detlev Casanova, Nicolas Dufresne and I are working on, to recover a hung core of the encoder without stopping the other one. More details on this driver can be found by watching Nicolas Dufresne's talk at LPC 2026 [0]. Samsung's Rust UFS RFC [1] also uses reset lines, through an abstraction that it has not posted.

Quoted 6 lines

I don't mind being cited, but please give a heads-up, we haven't discussed any of this, so I can't really comment if its a good or bad idea. On the C side, shared reset don't really work today, so I'm left wondering.

Nicolas

Fair, sorry for not giving you a heads-up. I have a branch a bit further ahead where I have tested both encoder cores at once, including this change. Due to the hierarchy of the power domains, to power down core 0, you also need to power down core 1. In other words, if core 0 becomes stuck, there is no way to bring it back by means of a power cycle without also stopping the jobs running on core 1.

This series mainly makes it possible to access the reset lines in an established pattern (e.g. Regulator<T> already uses it, and so does the Clk<T> series). For rkvenc, it allows pulsing only the lines of the affected core.

Reset lines haven't come up so far because with only a single core powered up, a power cycle is fine, since there is no other core to affect anyway.

Also note that rkvenc does not use SharedResetControl, but I ended up deciding to make it available as well. The same can be said of Clk<T>, where we have devm_ and other conveniences like guards that are not necessarily used at the moment, but that make sense when you look at what a lot of C drivers tend to need and carry that over to Rust.

What does not work with shared resets on the C side?

— Daniel

#12 lore raw

Le jeudi 26 novembre 2026 à 15:02 -0300, Daniel Almeida a écrit :

Quoted 1 line

What does not work with shared resets on the C side?

Above make sense, for shared, you need a mechanism to coordinate the reset. A bit like the comment above you made about flushing active jobs, and holding on new jobs until the reset has completed. I think for multiple instance of the same driver its quite feasible, but when the triggering driver have no clue what other driver it shares with, there is nothing as far as I know to help with this coordination, and its possibly non-trivial, specially if that was not part of the initial design.

Nicolas

author reviewer
#13 lore raw
Quoted 15 lines · Nicolas Dufresne
Quoted 1 line · Daniel Almeida

What does not work with shared resets on the C side?

Above make sense, for shared, you need a mechanism to coordinate the reset. A bit like the comment above you made about flushing active jobs, and holding on new jobs until the reset has completed. I think for multiple instance of the same driver its quite feasible, but when the triggering driver have no clue what other driver it shares with, there is nothing as far as I know to help with this coordination, and its possibly non-trivial, specially if that was not part of the initial design.

Quoted 1 line

Nicolas

My understanding (correct me if I am wrong?) is that this coordination is indeed needed, but baked into the C assert/deassert API:

Someone on a shared line has no power to assert the line unconditionally, all a single consumer can say is "I'd like this line to be asserted". The line is only asserted when all users agree (i.e.: when all other users also ask for a reset).

So if driver A wants a reset, it gets its house in order, and asks for it. Same for all other drivers, and it goes through when the last one does so. This is the de-facto coordination between consumers and it is not something that Rust influences one way or the other.

Sure, B is not told that A wants a reset, and A is not told by the reset APIs when the reset goes through, but at least no user gets its hardware yanked from them all of a sudden.

In fact, we do enforce this even harder by making sure that one cannot construct a SharedResetControl<Asserted>, for the reasons above.

-- Daniel

#14 lore raw

Le jeudi 26 novembre 2026 à 15:36 -0300, Daniel Almeida a écrit :

Quoted 25 lines
Quoted 14 lines · Nicolas Dufresne
Quoted 1 line · Daniel Almeida

What does not work with shared resets on the C side?

Above make sense, for shared, you need a mechanism to coordinate the reset. A bit like the comment above you made about flushing active jobs, and holding on new jobs until the reset has completed. I think for multiple instance of the same driver its quite feasible, but when the triggering driver have no clue what other driver it shares with, there is nothing as far as I know to help with this coordination, and its possibly non-trivial, specially if that was not part of the initial design.

Quoted 1 line

Nicolas

My understanding (correct me if I am wrong?) is that this coordination is indeed needed, but baked into the C assert/deassert API:

Someone on a shared line has no power to assert the line unconditionally, all a single consumer can say is "I'd like this line to be asserted". The line is only asserted when all users agree (i.e.: when all other users also ask for a reset).

Quoted 8 lines

So if driver A wants a reset, it gets its house in order, and asks for it. Same for all other drivers, and it goes through when the last one does so. This is the de-facto coordination between consumers and it is not something that Rust influences one way or the other.

Sure, B is not told that A wants a reset, and A is not told by the reset APIs when the reset goes through, but at least no user gets its hardware yanked from them all of a sudden.

Which comes back to my point, if the drivers have no back-channel, the lonely driver wanting to reset will wait forever. And the interaction with runtime PM seems a bit undefined too no? My question is if this is actually usable in practice.

Quoted 3 lines

In fact, we do enforce this even harder by making sure that one cannot construct a SharedResetControl<Asserted>, for the reasons above.

Maybe I'm just missing concrete examples.

Nicolas

Quoted 2 lines

-- Daniel

#15 lore raw
In reply to Daniel Almeida

Hi Daniel,

Quoted 2 lines · Daniel Almeida

This series adds a Rust abstraction for the consumer side of the reset controller API.

I've been looking forward to this for a while. I'll need some time to digest this, but let's start with some vague thoughts.

I hope we can use this opportunity to improve the somewhat fuzzy C API as well.

There's exclusive vs shared reset controls, self-clearing resets that pulse under firmware or hardware control vs direct assert/deassert level control, and as I recently discovered, sleeping vs non-sleeping use cases, all mixed into the same API. I'd like to make this more explicit in the C API, but have to weigh against potential driver churn.

And then there's device_reset(), which can also divert to ACPI _RST...

Quoted 2 lines

A driver can get the reset lines of its device, as an exclusive or a shared reset control, and assert and deassert them.

I like using the type system to separate exclusive and shared reset controls.

Quoted 6 lines

This will be used by the upcoming rkvenc driver that my colleagues Detlev Casanova, Nicolas Dufresne and I are working on, to recover a hung core of the encoder without stopping the other one. More details on this driver can be found by watching Nicolas Dufresne's talk at LPC 2026 [0]. Samsung's Rust UFS RFC [1] also uses reset lines, through an abstraction that it has not posted.

Could you point me to an example that shows how your bindings are used?

Quoted 7 lines

The abstraction follows the regulator abstraction and the clk series [2], with the reset state as a type parameter.

Patch 1 adds the C helpers that patch 2 needs.

Patch 2 adds ExclusiveResetControl, with the Unknown, Asserted and Deasserted states.

When a reset_control_assert/deassert() fails, does the state go back to Unknown?

Quoted 3 lines

Patch 3 adds ExclusiveResetControl::get_array() and get_array_optional(), which get all the reset lines of a device as one control.

Do you have any users for this?

Quoted 3 lines

Patch 4 adds AssertedGuard, which asserts the line of an exclusive control in the Unknown state and deasserts it when dropped, as a convenience for drivers.

Same question, I'd like to see how this is used.

Quoted 2 lines

Patch 5 adds SharedResetControl, with the Unknown and Deasserted states only, and patch 6 adds its array getters.

And this.

Quoted 3 lines

Patch 7 adds ExclusiveResetControl::devm_deassert() and devm_deassert_optional(), for drivers that only need their line deasserted while they are bound.

This seems like a straightforward equivalent to
Regulator::devm_enable()?

Quoted 2 lines

reset_control_reset(), the bulk API and the shared devm getters are not covered,

Why did you decide to leave those out specifically?

I think the _reset() vs _assert/deassert() APIs could be wrapped in a TriggeredResetControl vs LevelResetControl (or ResetTrigger/ResetPulse vs ResetControl?) to separate the two APIs.

regards
Philipp