From: Danilo Krummrich <dakr@kernel.org>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Mika Westerberg" <mika.westerberg@linux.intel.com>,
"Ying Huang" <huang.ying.caritas@gmail.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v9 4/4] rust: platform: allow ioremap of platform resources
Date: Thu, 5 Jun 2025 00:21:58 +0200 [thread overview]
Message-ID: <aEDHBrIVNcMS7j9O@pollux> (raw)
In-Reply-To: <20250603-topics-tyr-platform_iomem-v9-4-a27e04157e3e@collabora.com>
On Tue, Jun 03, 2025 at 12:05:30PM -0300, Daniel Almeida wrote:
> The preceding patches added support for resources, and for a general
> IoMem abstraction, but thus far there is no way to access said IoMem
> from drivers, as its creation is unsafe and depends on a resource that
> must be acquired from some device first.
>
> Now, allow the ioremap of platform resources themselves, thereby making
> the IoMem available to platform drivers.
>
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
> ---
> rust/kernel/platform.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 121 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
> index 427bc706f4349083b6bab81a02cb8ed803903590..57414fd0db296e412f45fbefd7707b338b37332a 100644
> --- a/rust/kernel/platform.rs
> +++ b/rust/kernel/platform.rs
> @@ -5,9 +5,14 @@
> //! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h)
>
> use crate::{
> - bindings, container_of, device, driver,
> + bindings, container_of, device,
> + devres::Devres,
> + driver,
> error::{to_result, Result},
> - io::resource::Resource,
> + io::{
> + mem::{ExclusiveIoMem, IoMem},
> + resource::Resource,
I think we should add
pub use resource::Resource;
into io.rs, such that we can refer to this as ::kernel::io::Resource.
> + },
> of,
> prelude::*,
> str::CStr,
> @@ -227,6 +232,120 @@ pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> {
> }
> }
>
> +impl Device<device::Bound> {
> + /// Maps a platform resource where the size is known at compile time.
> + ///
> + /// This uses the
> + /// [`ioremap()`](https://docs.kernel.org/driver-api/device-io.html#getting-access-to-the-device)
> + /// C API.
> + ///
> + /// # Examples
> + ///
> + /// ```no_run
> + /// # use kernel::{bindings, c_str, platform, of, device::Core};
> + /// # struct SampleDriver;
> + ///
> + /// impl platform::Driver for SampleDriver {
> + /// # type IdInfo = ();
> + /// # const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
> + ///
> + /// fn probe(
> + /// pdev: &platform::Device<Core>,
> + /// info: Option<&Self::IdInfo>,
> + /// ) -> Result<Pin<KBox<Self>>> {
> + /// let offset = 0; // Some offset.
> + ///
> + /// // If the size is known at compile time, use [`Self::iomap_resource_sized`].
> + /// //
> + /// // No runtime checks will apply when reading and writing.
> + /// let resource = pdev.resource_by_index(0).ok_or(ENODEV)?;
> + /// let iomem = pdev.iomap_resource_sized::<42>(&resource)?;
> + /// let io = iomem.access(pdev.as_ref())?;
> + ///
> + /// // Read and write a 32-bit value at `offset`. Calling `try_access()` on
> + /// // the `Devres` makes sure that the resource is still valid.
> + /// let data = io.read32_relaxed(offset);
> + ///
> + /// iomem.try_access().ok_or(ENODEV)?.write32_relaxed(data, offset);
io.write32_relaxed(data, offset);
> + ///
> + /// # let sample_driver = KBox::new(SampleDriver, GFP_KERNEL).map_err(|_| ENOMEM)?;
Why the map_err()? We have
impl From<AllocError> for Error {
fn from(_: AllocError) -> Error {
code::ENOMEM
}
}
so you shouldn't need that.
Also, please just write this as
# Ok(KBox::new(SampleDriver, GFP_KERNEL)?.into())
> + /// # Ok(sample_driver.into())
> + /// }
> + /// }
> + /// ```
> + pub fn iomap_resource_sized<const SIZE: usize>(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<IoMem<SIZE>>> {
> + IoMem::new(resource, self.as_ref())
> + }
> +
> + /// Same as [`Self::iomap_resource_sized`] but with exclusive access to the
> + /// underlying region.
> + ///
> + /// This uses the
> + /// [`ioremap()`](https://docs.kernel.org/driver-api/device-io.html#getting-access-to-the-device)
> + /// C API.
> + pub fn iomap_resource_exclusive_sized<const SIZE: usize>(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<ExclusiveIoMem<SIZE>>> {
> + ExclusiveIoMem::new(resource, self.as_ref())
> + }
> +
> + /// Maps a platform resource through iomap().
> + ///
> + /// This uses the
> + /// [`ioremap()`](https://docs.kernel.org/driver-api/device-io.html#getting-access-to-the-device)
> + /// C API.
> + ///
> + /// # Examples
> + ///
> + /// ```no_run
> + /// # use kernel::{bindings, c_str, platform, of, device::Core};
> + /// # struct SampleDriver;
> + ///
> + /// impl platform::Driver for SampleDriver {
> + /// # type IdInfo = ();
> + /// # const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
> + ///
> + /// fn probe(
> + /// pdev: &platform::Device<Core>,
> + /// info: Option<&Self::IdInfo>,
> + /// ) -> Result<Pin<KBox<Self>>> {
> + /// let offset = 0; // Some offset.
> + ///
> + /// // Unlike [`Self::iomap_resource_sized`], here the size of the memory region
> + /// // is not known at compile time, so only the `try_read*` and `try_write*`
> + /// // family of functions should be used, leading to runtime checks on every
> + /// // access.
> + /// let resource = pdev.resource_by_index(0).ok_or(ENODEV)?;
> + /// let iomem = pdev.iomap_resource(&resource)?;
> + /// let io = iomem.access(pdev.as_ref())?;
> + ///
> + /// let data = io.try_read32_relaxed(offset)?;
> + ///
> + /// io.try_write32_relaxed(data, offset)?;
> + ///
> + /// # let sample_driver = KBox::new(SampleDriver, GFP_KERNEL).map_err(|_| ENOMEM)?;
Same as above.
> + /// # Ok(sample_driver.into())
> + /// }
> + /// }
> + /// ```
> + pub fn iomap_resource(&self, resource: &Resource) -> Result<Devres<IoMem<0>>> {
> + self.iomap_resource_sized::<0>(resource)
> + }
> +
> + /// Same as [`Self::iomap_resource`] but with exclusive access to the underlying
> + /// region.
> + pub fn iomap_resource_exclusive(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<ExclusiveIoMem<0>>> {
> + self.iomap_resource_exclusive_sized::<0>(resource)
> + }
> +}
> +
> // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
> // argument.
> kernel::impl_device_context_deref!(unsafe { Device });
>
> --
> 2.49.0
>
next prev parent reply other threads:[~2025-06-04 22:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-03 15:05 [PATCH v9 0/4] (no cover subject) Daniel Almeida
2025-06-03 15:05 ` [PATCH v9 1/4] rust: io: add resource abstraction Daniel Almeida
2025-06-03 15:05 ` [PATCH v9 2/4] rust: io: mem: add a generic iomem abstraction Daniel Almeida
2025-06-03 15:05 ` [PATCH v9 3/4] rust: platform: add resource accessors Daniel Almeida
2025-06-03 15:05 ` [PATCH v9 4/4] rust: platform: allow ioremap of platform resources Daniel Almeida
2025-06-04 22:21 ` Danilo Krummrich [this message]
2025-06-03 15:11 ` [PATCH v9 0/4] (no cover subject) Daniel Almeida
2025-06-03 15:17 ` Ilpo Järvinen
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=aEDHBrIVNcMS7j9O@pollux \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=benno.lossin@proton.me \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=huang.ying.caritas@gmail.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mika.westerberg@linux.intel.com \
--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;
as well as URLs for NNTP newsgroup(s).