mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Philipp Zabel" <p.zabel@pengutronix.de>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 dri-devel@lists.freedesktop.org,
	 Detlev Casanova <detlev.casanova@collabora.com>,
	 Nicolas Dufresne <nicolas.dufresne@collabora.com>
Subject: [PATCH 2/7] rust: reset: add ExclusiveResetControl
Date: Thu, 26 Nov 2026 13:04:29 -0300	[thread overview]
Message-ID: <20261126-b4-rust-reset-v1-2-9c4c3d029080@collabora.com> (raw)
In-Reply-To: <20261126-b4-rust-reset-v1-0-9c4c3d029080@collabora.com>

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.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 MAINTAINERS          |   1 +
 rust/kernel/lib.rs   |   1 +
 rust/kernel/reset.rs | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 335 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index d8bcacb7d8ef..e427742252d0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 1ab41129d557..c0199ec055b8 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -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;
diff --git a/rust/kernel/reset.rs b/rust/kernel/reset.rs
new file mode 100644
index 000000000000..396f1e19ee2e
--- /dev/null
+++ b/rust/kernel/reset.rs
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Reset control abstractions, the consumer side of the reset controller API.
+//!
+//! A reset controller controls a number of reset lines. A device is held in reset while its reset
+//! line is asserted.
+//!
+//! A driver gets an [`ExclusiveResetControl`] for a reset line of its device, and uses it to
+//! assert and deassert the line. A new control is in the [`Unknown`] state. Its `assert()` and
+//! `deassert()` methods, when they succeed, move it to the [`Asserted`] or [`Deasserted`] 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.
+
+use crate::{
+    bindings,
+    device::{
+        Bound,
+        Device, //
+    },
+    error::{
+        from_err_ptr,
+        to_result, //
+    },
+    prelude::*, //
+};
+
+use core::{
+    marker::PhantomData,
+    mem::ManuallyDrop,
+    ptr, //
+};
+
+mod private {
+    pub trait Sealed {}
+
+    impl Sealed for super::Unknown {}
+    impl Sealed for super::Asserted {}
+    impl Sealed for super::Deasserted {}
+}
+
+/// A trait representing the different states a reset control can be in.
+pub trait ResetState: private::Sealed {
+    /// Whether dropping a reset control in this state asserts the line.
+    const ASSERT_ON_DROP: bool;
+}
+
+/// A state that records nothing about the reset line, which may be asserted or deasserted.
+///
+/// Dropping a reset control in this state does not change the line.
+pub struct Unknown;
+
+/// A state where the last successful operation on the reset control was an assert.
+///
+/// Dropping a reset control in this state does not change the line.
+pub struct Asserted;
+
+/// A state where the last successful operation on the reset control was a deassert.
+///
+/// Dropping a reset control in this state asserts the line. An error from that assert is
+/// discarded.
+pub struct Deasserted;
+
+impl ResetState for Unknown {
+    const ASSERT_ON_DROP: bool = false;
+}
+
+impl ResetState for Asserted {
+    const ASSERT_ON_DROP: bool = false;
+}
+
+impl ResetState for Deasserted {
+    const ASSERT_ON_DROP: bool = true;
+}
+
+/// An error that can occur when trying to move a reset control between states.
+pub struct Error<T> {
+    /// The error that occurred.
+    pub error: kernel::error::Error,
+
+    /// The reset control the operation failed on.
+    pub reset: T,
+}
+
+impl<T> From<Error<T>> for kernel::error::Error {
+    /// Drops the reset control and keeps the error, so that `?` works on the state transitions.
+    #[inline]
+    fn from(err: Error<T>) -> Self {
+        err.error
+    }
+}
+
+/// An exclusive reset control of a device.
+///
+/// Exclusive means that the driver has direct control over the reset line. A reset line can be
+/// held by one [`ExclusiveResetControl`] at a time.
+///
+/// A reset control obtained with [`ExclusiveResetControl::get_optional`] may have no reset line,
+/// in which case every operation on it succeeds and does nothing.
+///
+/// The reset control is released when the [`ExclusiveResetControl`] is dropped. The lifetime `'a`
+/// is that of the reference to the bound device it was obtained from, so an
+/// [`ExclusiveResetControl`] cannot outlive the binding of that device to its driver.
+///
+/// # Examples
+///
+/// Holding a device in reset while its clocks are turned on, then releasing it:
+///
+/// ```
+/// use kernel::{
+///     device::{
+///         Bound,
+///         Device, //
+///     },
+///     reset::{
+///         Deasserted,
+///         ExclusiveResetControl, //
+///     },
+/// };
+///
+/// struct Data<'a> {
+///     // Dropping `Data` asserts the line again.
+///     reset: ExclusiveResetControl<'a, Deasserted>,
+/// }
+///
+/// fn probe<'a>(dev: &'a Device<Bound>) -> Result<Data<'a>> {
+///     // Put the device in reset.
+///     let reset = ExclusiveResetControl::get(dev, None)?.assert()?;
+///
+///     // For example, turn on the clocks of the device here, while it is held in reset.
+///
+///     // Release the device from reset.
+///     let reset = reset.deassert()?;
+///
+///     Ok(Data { reset })
+/// }
+/// ```
+///
+/// # Invariants
+///
+/// `inner` is a pointer obtained from a successful call to one of the following:
+///
+/// - [`reset_control_get_exclusive()`]
+/// - [`reset_control_get_optional_exclusive()`]
+///
+/// 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 [`ExclusiveResetControl`] owns the reference that the
+/// call returned.
+///
+/// [`reset_control_get_exclusive()`]:
+/// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_exclusive
+/// [`reset_control_get_optional_exclusive()`]:
+/// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_optional_exclusive
+pub struct ExclusiveResetControl<'a, State: ResetState> {
+    inner: *mut bindings::reset_control,
+    _phantom: PhantomData<(&'a Device<Bound>, State)>,
+}
+
+// SAFETY: A reset control can be used and released from any thread.
+unsafe impl<State: ResetState> Send for ExclusiveResetControl<'_, State> {}
+
+// SAFETY: An `ExclusiveResetControl` has no `&self` methods, so references to it can be shared
+// between threads.
+unsafe impl<State: ResetState> Sync for ExclusiveResetControl<'_, State> {}
+
+impl<'a> ExclusiveResetControl<'a, Unknown> {
+    /// Gets the reset line `name` of `dev`, or its first reset line if `name` is `None`.
+    ///
+    /// Equivalent to the kernel's [`reset_control_get_exclusive()`].
+    ///
+    /// [`reset_control_get_exclusive()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_exclusive
+    #[inline]
+    pub fn get(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> {
+        let id = name.map_or(ptr::null(), |n| n.as_char_ptr());
+
+        // SAFETY: `dev` is a valid device, and `id` is `NULL` or a valid C string.
+        let inner = unsafe { bindings::reset_control_get_exclusive(dev.as_raw(), id) };
+        let inner = from_err_ptr(inner)?;
+
+        // INVARIANT: `inner` comes from a successful call to `reset_control_get_exclusive()`, and
+        // the returned value owns the reference from that call.
+        Ok(Self {
+            inner,
+            _phantom: PhantomData,
+        })
+    }
+
+    /// Gets the reset line `name` of `dev`, if there is one.
+    ///
+    /// Behaves the same as [`Self::get`], except when the device has no such reset line or
+    /// `CONFIG_RESET_CONTROLLER` is disabled. In those cases, it returns an
+    /// [`ExclusiveResetControl`] on which every operation succeeds and does nothing.
+    ///
+    /// Equivalent to the kernel's [`reset_control_get_optional_exclusive()`].
+    ///
+    /// [`reset_control_get_optional_exclusive()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_get_optional_exclusive
+    #[inline]
+    pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> {
+        let id = name.map_or(ptr::null(), |n| n.as_char_ptr());
+
+        // SAFETY: `dev` is a valid device, and `id` is `NULL` or a valid C string.
+        let inner = unsafe { bindings::reset_control_get_optional_exclusive(dev.as_raw(), id) };
+        let inner = from_err_ptr(inner)?;
+
+        // INVARIANT: `inner` comes from a successful call to
+        // `reset_control_get_optional_exclusive()`, and the returned value owns the reference from
+        // that call.
+        Ok(Self {
+            inner,
+            _phantom: PhantomData,
+        })
+    }
+}
+
+impl<'a, State: ResetState> ExclusiveResetControl<'a, State> {
+    /// Asserts the reset line and moves the [`ExclusiveResetControl`] to the [`Asserted`] state.
+    ///
+    /// Equivalent to the kernel's [`reset_control_assert()`].
+    ///
+    /// [`reset_control_assert()`]:
+    /// https://docs.kernel.org/driver-api/reset.html#c.reset_control_assert
+    #[inline]
+    fn assert_internal(self) -> Result<ExclusiveResetControl<'a, Asserted>, 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.
+        to_result(unsafe { bindings::reset_control_assert(reset.inner) })
+            .map(|()| ExclusiveResetControl {
+                inner: reset.inner,
+                _phantom: PhantomData,
+            })
+            .map_err(|error| Error {
+                error,
+                reset: ManuallyDrop::into_inner(reset),
+            })
+    }
+
+    /// Deasserts the reset line and moves the [`ExclusiveResetControl`] 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]
+    fn deassert_internal(self) -> Result<ExclusiveResetControl<'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.
+        to_result(unsafe { bindings::reset_control_deassert(reset.inner) })
+            .map(|()| ExclusiveResetControl {
+                inner: reset.inner,
+                _phantom: PhantomData,
+            })
+            .map_err(|error| Error {
+                error,
+                reset: ManuallyDrop::into_inner(reset),
+            })
+    }
+}
+
+impl<'a> ExclusiveResetControl<'a, Unknown> {
+    /// Asserts the reset line and moves the [`ExclusiveResetControl`] to the [`Asserted`] state.
+    ///
+    /// Equivalent to 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(self) -> Result<ExclusiveResetControl<'a, Asserted>, Error<Self>> {
+        self.assert_internal()
+    }
+
+    /// Deasserts the reset line and moves the [`ExclusiveResetControl`] 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<ExclusiveResetControl<'a, Deasserted>, Error<Self>> {
+        self.deassert_internal()
+    }
+}
+
+impl<'a> ExclusiveResetControl<'a, Asserted> {
+    /// Deasserts the reset line and moves the [`ExclusiveResetControl`] 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<ExclusiveResetControl<'a, Deasserted>, Error<Self>> {
+        self.deassert_internal()
+    }
+}
+
+impl<'a> ExclusiveResetControl<'a, Deasserted> {
+    /// Asserts the reset line and moves the [`ExclusiveResetControl`] to the [`Asserted`] state.
+    ///
+    /// Equivalent to 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(self) -> Result<ExclusiveResetControl<'a, Asserted>, Error<Self>> {
+        self.assert_internal()
+    }
+}
+
+impl<State: ResetState> Drop for ExclusiveResetControl<'_, State> {
+    fn drop(&mut self) {
+        if State::ASSERT_ON_DROP {
+            // SAFETY: By the type invariants, `self.inner` is a pointer that a C getter returned,
+            // and `self` owns its reference.
+            unsafe { bindings::reset_control_assert(self.inner) };
+        }
+
+        // SAFETY: By the type invariants, `self` owns the reference that the C getter returned,
+        // so it is safe to relinquish it now.
+        unsafe { bindings::reset_control_put(self.inner) };
+    }
+}

-- 
2.56.0


  parent reply	other threads:[~2026-11-26 16:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 16:04 [PATCH 0/7] rust: reset: add reset control abstractions Daniel Almeida
2026-11-26 16:04 ` [PATCH 1/7] rust: reset: add reset control helpers Daniel Almeida
2026-11-26 16:04 ` Daniel Almeida [this message]
2026-11-26 16:04 ` [PATCH 3/7] rust: reset: add ExclusiveResetControl::get_array() and get_array_optional() Daniel Almeida
2026-11-26 16:04 ` [PATCH 4/7] rust: reset: add AssertedGuard Daniel Almeida
2026-11-26 16:04 ` [PATCH 5/7] rust: reset: add SharedResetControl Daniel Almeida
2026-11-26 19:14   ` Alice Ryhl
2026-11-26 16:04 ` [PATCH 6/7] rust: reset: add SharedResetControl::get_array() and get_array_optional() Daniel Almeida
2026-11-26 16:04 ` [PATCH 7/7] rust: reset: add devm_deassert() and devm_deassert_optional() Daniel Almeida
2026-11-26 16:33 ` [PATCH 0/7] rust: reset: add reset control abstractions Nicolas Dufresne
2026-11-26 18:02   ` Daniel Almeida
2026-11-26 18:11     ` Nicolas Dufresne
2026-11-26 18:36       ` Daniel Almeida
2026-11-26 18:46         ` Nicolas Dufresne

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126-b4-rust-reset-v1-2-9c4c3d029080@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=detlev.casanova@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nicolas.dufresne@collabora.com \
    --cc=ojeda@kernel.org \
    --cc=p.zabel@pengutronix.de \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®