From: "Onur Özkan" <work@onurozkan.dev>
To: Beata Michalska <beata.michalska@arm.com>
Cc: Miguel Ojeda <ojeda@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Boqun Feng <boqun@kernel.org>, Gary Guo <gary@garyguo.net>,
Bjorn 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>,
Daniel Almeida <daniel.almeida@collabora.com>,
Boris Brezillon <boris.brezillon@collabora.com>,
rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 1/3] rust: Add runtime PM support
Date: Sun, 17 May 2026 10:20:54 +0300 [thread overview]
Message-ID: <20260517072420.38902-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260514150957.3501924-2-beata.michalska@arm.com>
Hi Beata,
Thank you for working on this. This will eventually be needed for the
tyr GPU reset implementation.
On Thu, 14 May 2026 17:09:03 +0200
Beata Michalska <beata.michalska@arm.com> wrote:
> Introduce a Rust abstraction for Linux runtime PM, centered around a
> `PMContext` ownign a device reference and modeling the runtime PM
> lifecycle. The context is initialised inactive and transitions into
> active once the runtime PM gets enabled. The drivers might opt in to
> initialize the devices power state prior to enabling the context, which
> is advised at probe, when the actual PM callbacks cannot be fully served.
> Once active, the context provides access to runtime PM operations and
> request modes used to manage device power state transitions.
>
> Runtime PM callbacks are provided via a PMOps trait. The generated
> dev_pm_ops callbacks retrieve the driver's PMContext from drvdata,
> validate the expected transition, and transfer ownership of the
> driver-defined runtime PM payload to the relevant callback. Once
> the callback completes, ownership of the returned payload is
> transferred back to the PMContext and stored for the next transition.
> The payload returned by the callback is expected to reflect the
> current runtime PM state of the device. On a successful transition,
> the returned payload should represent the new state reached by
> the device. If the transition fails, it is the responsibility of
> the driver to return a payload either rolled back to the previous
> state or left in a state that remains valid for subsequent runtime PM
> transitions. At present, only runtime_suspend and runtime_resume
> callbacks are supported.
>
> Profile and scoped runtime PM helpers are provided for common usage
> patterns. ResumeScope resumes the device for the lifetime of the scope,
> AwakeScope resumes the device while holding a runtime PM usage reference,
> and RetainScope only increments the runtime PM usage count. References
> are released on drop, and request behaviour is controlled through
> driver-defined Profile values covering the common runtime PM request
> modes.
>
> Additionally, PMConfig provides common runtime PM configuration options.
>
> Signed-off-by: Beata Michalska <beata.michalska@arm.com>
> ---
> rust/bindings/bindings_helper.h | 1 +
> rust/helpers/helpers.c | 1 +
> rust/helpers/pm_runtime.c | 38 ++
> rust/kernel/lib.rs | 1 +
> rust/kernel/pm.rs | 924 ++++++++++++++++++++++++++++++++
> 5 files changed, 965 insertions(+)
> create mode 100644 rust/helpers/pm_runtime.c
> create mode 100644 rust/kernel/pm.rs
>
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 446dbeaf0866..3a38db22681f 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -76,6 +76,7 @@
> #include <linux/pid_namespace.h>
> #include <linux/platform_device.h>
> #include <linux/pm_opp.h>
> +#include <linux/pm_runtime.h>
> #include <linux/poll.h>
> #include <linux/property.h>
> #include <linux/pwm.h>
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 625921e27dfb..d67c69ebd33e 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -75,6 +75,7 @@
> #include "pci.c"
> #include "pid_namespace.c"
> #include "platform.c"
> +#include "pm_runtime.c"
> #include "poll.c"
> #include "processor.c"
> #include "property.c"
> diff --git a/rust/helpers/pm_runtime.c b/rust/helpers/pm_runtime.c
> new file mode 100644
> index 000000000000..9b2056110199
> --- /dev/null
> +++ b/rust/helpers/pm_runtime.c
> @@ -0,0 +1,38 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/pm_runtime.h>
> +
> +void rust_helper_pm_runtime_get_noresume(struct device *dev)
> +{
> + pm_runtime_get_noresume(dev);
> +}
> +
> +void rust_helper_pm_runtime_put_noidle(struct device *dev)
> +{
> + pm_runtime_put_noidle(dev);
> +}
> +
> +void rust_helper_pm_runtime_mark_last_busy(struct device *dev)
> +{
> + pm_runtime_mark_last_busy(dev) ;
> +}
> +
> +bool rust_helper_pm_runtime_active(struct device *dev)
> +{
> + return pm_runtime_active(dev);
> +}
> +
> +void rust_helper_pm_suspend_ignore_children(struct device *dev, bool enable)
> +{
> + pm_suspend_ignore_children(dev, enable);
> +}
> +
> +int rust_helper_pm_runtime_set_active(struct device *dev)
> +{
> + return pm_runtime_set_active(dev);
> +}
> +
> +int rust_helper_pm_runtime_set_suspended(struct device *dev)
> +{
> + return pm_runtime_set_suspended(dev);
> +}
You should add __rust_helper prefixes to helper functions.
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index b72b2fbe046d..7e51e497e2f0 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -105,6 +105,7 @@
> pub mod pci;
> pub mod pid_namespace;
> pub mod platform;
> +pub mod pm;
> pub mod prelude;
> pub mod print;
> pub mod processor;
> diff --git a/rust/kernel/pm.rs b/rust/kernel/pm.rs
> new file mode 100644
> index 000000000000..7bdffa727c71
> --- /dev/null
> +++ b/rust/kernel/pm.rs
> @@ -0,0 +1,924 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rust Runtime Power Management abstraction.
> +//!
> +//! C header: [`include/linux/pm_runtime.h`](srctree/include/linux/pm_runtime.h)
> +
> +use crate::{
> + bindings, device,
> + error::{to_result, VTABLE_DEFAULT_ERROR},
> + macros::paste,
> + prelude::*,
> + sync::aref::ARef,
> + sync::atomic::{
> + ordering::{Acquire, Full, Release},
> + Atomic,
> + },
> + sync::Arc,
> + sync::{new_mutex, Mutex},
> +};
We started using vertical format on imports like:
use crate::{
bindings,
device,
error::{
to_result,
VTABLE_DEFAULT_ERROR,
//
},
...
to avoid conflict issues.
> +
> +use core::marker::PhantomData;
> +
> +/// Runtime Power Management modes that determine how a particular PM
> +/// transition is to be carried out.
> +/// Corresponds to C Runtime PM flag argument bits:
> +/// - `RPM_ASYNC`
> +/// - `RPM_NOWAIT`
> +/// - `RPM_GET_PUT`
> +/// - `RPM_AUTO`
> +#[derive(Clone, Copy, PartialEq, Eq, Debug)]
> +struct Mode(u32);
> +
> +impl Mode {
> + /// Synchronous PM operations - default.
> + #[allow(unused)]
> + const SYNC: Mode = Mode(0);
This seems to be used in Profile::new, why do we need `#[allow(unused)]` here?
> + /// Allow asynchronous PM operations.
> + const ASYNC: Mode = Mode(bindings::RPM_ASYNC);
> + /// Do not wait for any pending rquests to finish.
> + const NOWAIT: Mode = Mode(bindings::RPM_NOWAIT);
> + /// Acquire a runtime-PM usage reference.
> + const ACQUIRE: Mode = Mode(bindings::RPM_GET_PUT);
> + /// Use autosuspend.
> + const AUTO: Mode = Mode(bindings::RPM_AUTO);
> + /// Additional mode for devices supporting idle states.
> + const IDLE: Mode = Mode(1 << 16);
> +
> + const fn join(self, other: Self) -> Self {
> + Self(self.0 | other.0)
> + }
> +
> + fn contains(self, other: Self) -> bool {
> + (self.0 & other.0) != 0
> + }
> +}
> +
> +impl core::ops::BitOr for Mode {
> + type Output = Self;
> + fn bitor(self, rhs: Self) -> Self::Output {
> + Self(self.0 | rhs.0)
> + }
> +}
> +
> +impl core::ops::BitAnd for Mode {
> + type Output = Self;
> + fn bitand(self, rhs: Self) -> Self::Output {
> + Self(self.0 & rhs.0)
> + }
> +}
> +
> +impl core::ops::Not for Mode {
> + type Output = Self;
> + fn not(self) -> Self::Output {
> + Self(!self.0)
> + }
> +}
> +
> +impl From<Mode> for core::ffi::c_int {
> + #[inline]
> + fn from(mode: Mode) -> core::ffi::c_int {
> + mode.0 as core::ffi::c_int
> + }
> +}
> +
> +/// Utility macro for combining multiple request modes
> +macro_rules! mode {
> + ($mode:expr $(, $args:expr)+ $(,)?) => {{
> + let mut new_mode = $mode;
> + $( new_mode = new_mode.join($args); )+
> + new_mode
> + }};
> +}
> +
> +/// Runtime power transition scope.
> +pub struct Scope<Tag> {
> + dev: ARef<device::Device>,
> + mode: Mode,
> + _tag: PhantomData<Tag>,
> +}
> +
> +/// Device resumed without incrementing the device's usage count
> +pub struct Resume;
> +/// Device resumed with the devices'a usage count being incremented
> +pub struct Awake;
> +/// Device with increased usage reference
> +pub struct Retain;
> +
> +/// Resumes the device without acquiring the usage reference.
> +/// Note: This does not guarantee the device will be kept active
> +/// for the lifetime of the scope due to potentil pending suspend
> +/// requests.
> +///
> +/// On drop:
> +/// - If `Mode::IDLE`, calls `__pm_runtime_idle()`:
> +/// triggers idle notification before attempting to suspend
> +/// - If `Mode::AUTO`, marks last busy then calls `__pm_runtime_suspend()`.
> +/// - Otherwise calls `__pm_runtime_suspend()`.
> +///
> +/// The guard must be dropped from a context matching the requested transition
> +/// mode: sync vs async.
> +#[must_use = "dropping this guard issues the matching runtime PM release request"]
> +pub struct ResumeScope(Scope<Resume>);
> +
> +/// Acquires a runtime-PM usage reference and keeps the device powered.
> +///
> +/// Requires `Mode::ACQUIRE`. Drop behavior matches `ResumeScope`.
> +/// The guard must be dropped from a context matching the requested transition
> +/// mode: sync vs async.
> +#[must_use = "dropping this guard releases its runtime PM hold"]
> +pub struct AwakeScope(Scope<Awake>);
> +
> +/// Prevents the device from getting suspended by holding the usage reference
> +/// count.
> +///
> +/// On drop, calls `pm_runtime_put_noidle()`.
> +#[must_use = "dropping this guard releases its runtime PM hold"]
> +pub struct RetainScope(Scope<Retain>);
> +
> +impl ResumeScope {
> + fn new(dev: ARef<device::Device>, mode: Mode) -> Result<Self> {
> + if mode.contains(Mode::ACQUIRE) {
> + // Mode::ACQUIRE is intended to be used with Awake scope
> + // Avoid mixing the modes.
> + return Err(EINVAL);
> + }
> +
> + // Mode::IDLE is internal so strip it of before passing further
> + Request::resume(&dev, mode & !Mode::IDLE).map(|()| {
> + Self(Scope::<Resume> {
> + dev: dev.clone(),
> + mode,
> + _tag: PhantomData,
> + })
> + })
> + }
> +}
> +
> +impl Drop for ResumeScope {
> + fn drop(&mut self) {
> + let scope_mode = self.0.mode & !Mode::IDLE;
> +
> + match self.0.mode {
> + mode if mode.contains(Mode::IDLE) => {
> + Request::idle(&self.0.dev, scope_mode & Mode::ASYNC);
> + }
> + mode if mode.contains(Mode::AUTO) => {
> + Request::mark_last_busy(&self.0.dev);
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + _ => {
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + }
> + }
> +}
> +
> +impl AwakeScope {
> + fn new(dev: ARef<device::Device>, mode: Mode) -> Result<Self> {
> + if !mode.contains(Mode::ACQUIRE) {
> + return Err(EINVAL);
> + }
> + // Mode::IDLE is internal so strip it of before passing further
> + Request::resume(&dev, mode & !Mode::IDLE)
> + .inspect_err(|_| Request::put_noidle(&dev))
> + .map(|()| {
> + Self(Scope::<Awake> {
> + dev: dev.clone(),
> + mode,
> + _tag: PhantomData,
> + })
> + })
> + }
> +}
> +
> +impl Drop for AwakeScope {
> + fn drop(&mut self) {
> + let scope_mode = self.0.mode & !Mode::IDLE;
> + match self.0.mode {
> + mode if mode.contains(Mode::IDLE) => {
> + Request::idle(&self.0.dev, scope_mode);
> + }
> + mode if mode.contains(Mode::AUTO) => {
> + Request::mark_last_busy(&self.0.dev);
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + _ => {
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + }
> + }
> +}
> +
> +impl RetainScope {
> + fn new(dev: ARef<device::Device>) -> Result<Self> {
> + Request::get_noresume(&dev);
> + Ok(Self(Scope::<Retain> {
> + dev,
> + mode: Mode(0),
This is Mode::SYNC.
Regards,
Onur
[...]
> +
> +impl<T: PMOps, State> PMContext<T, State> {
> + /// Applies runtime PM configuration options.
> + ///
> + /// Options are applied in the order provided. The currently supported
> + /// options do not report per-option failures.
> + ///
> + /// This may be called before or after runtime PM is enabled, depending on
> + /// the option and the driver's setup sequence.
> + pub fn apply_config(&self, opts: &[PMConfig]) -> Result {
> + #[cfg(not(CONFIG_PM))]
> + let _ = opts;
> + #[cfg(CONFIG_PM)]
> + for opt in opts {
> + match opt {
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::IgnoreChildren(v) => unsafe {
> + bindings::pm_suspend_ignore_children(self.dev.as_raw(), *v)
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::NoCallbacks => unsafe {
> + bindings::pm_runtime_no_callbacks(self.dev.as_raw())
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::IrqSafe => unsafe { bindings::pm_runtime_irq_safe(self.dev.as_raw()) },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::AutoSuspend(v) => unsafe {
> + bindings::__pm_runtime_use_autosuspend(self.dev.as_raw(), *v);
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::AutoSuspendDelay(v) => unsafe {
> + bindings::pm_runtime_set_autosuspend_delay(self.dev.as_raw(), *v as i32);
> + bindings::__pm_runtime_use_autosuspend(self.dev.as_raw(), true);
> + },
> + }
> + }
> + Ok(())
> + }
> +}
> --
> 2.43.0
>
next prev parent reply other threads:[~2026-05-17 7:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-14 15:09 [RFC PATCH 0/3] Rust: add runtime PM support Beata Michalska
2026-05-14 15:09 ` [RFC PATCH 1/3] rust: Add " Beata Michalska
2026-05-17 0:08 ` Danilo Krummrich
2026-05-17 7:20 ` Onur Özkan [this message]
2026-05-14 15:09 ` [RFC PATCH 2/3] rust/platform: Add support for runtime PM Beata Michalska
2026-05-14 15:09 ` [RFC PATCH 3/3] [DO NOT MERGE] drm/tyr: wire runtime PM hooks Beata Michalska
2026-05-16 20:35 ` [RFC PATCH 0/3] Rust: add runtime PM support Miguel Ojeda
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=20260517072420.38902-1-work@onurozkan.dev \
--to=work@onurozkan.dev \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=beata.michalska@arm.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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