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 | 128 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 127 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 4917cb34e2fe8027d3d861e90de51de85f006735..5c550fc6d429ffc541a17fb5f8a1c2eb4476b560 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -5,8 +5,14 @@
//! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h)
use crate::{
- bindings, device, driver,
+ bindings, device,
+ devres::Devres,
+ driver,
error::{to_result, Result},
+ io::{
+ mem::{ExclusiveIoMem, IoMem},
+ resource::Resource,
+ },
of,
prelude::*,
str::CStr,
@@ -223,6 +229,126 @@ unsafe fn dec_ref(obj: NonNull<Self>) {
}
}
+impl Device<device::Core> {
+ /// Maps a platform resource through ioremap() where the size is known at
+ /// compile time.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// # use kernel::{bindings, c_str, platform};
+ /// # use kernel::device::Core;
+ ///
+ ///
+ /// fn probe(pdev: &mut platform::Device<Core>, /* ... */) -> Result<()> {
+ /// let offset = 0; // Some offset.
+ ///
+ /// // If the size is known at compile time, use `ioremap_resource_sized`.
+ /// // No runtime checks will apply when reading and writing.
+ /// let resource = pdev.resource(0).ok_or(ENODEV)?;
+ /// let iomem = pdev.ioremap_resource_sized::<42>(&resource)?;
+ ///
+ /// // 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 = iomem.try_access().ok_or(ENODEV)?.read32_relaxed(offset);
+ ///
+ /// iomem.try_access().ok_or(ENODEV)?.write32_relaxed(data, offset);
+ ///
+ /// # Ok::<(), Error>(())
+ /// }
+ /// ```
+ pub fn ioremap_resource_sized<const SIZE: usize>(
+ &self,
+ resource: &Resource,
+ ) -> Result<Devres<IoMem<SIZE>>> {
+ IoMem::new(resource, self.as_ref())
+ }
+
+ /// Same as [`Self::ioremap_resource_sized`] but with exclusive access to the
+ /// underlying region.
+ pub fn ioremap_resource_exclusive_sized<const SIZE: usize>(
+ &self,
+ resource: &Resource,
+ ) -> Result<Devres<ExclusiveIoMem<SIZE>>> {
+ ExclusiveIoMem::new(resource, self.as_ref())
+ }
+
+ /// Maps a platform resource through ioremap().
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// # use kernel::{bindings, c_str, platform};
+ /// # use kernel::device::Core;
+ ///
+ /// fn probe(pdev: &mut platform::Device<Core>, /* ... */) -> Result<()> {
+ /// let offset = 0; // Some offset.
+ ///
+ /// // Unlike `ioremap_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(0).ok_or(ENODEV)?;
+ /// let iomem = pdev.ioremap_resource(&resource)?;
+ ///
+ /// let data = iomem.try_access().ok_or(ENODEV)?.try_read32_relaxed(offset)?;
+ ///
+ /// iomem.try_access().ok_or(ENODEV)?.try_write32_relaxed(data, offset)?;
+ ///
+ /// # Ok::<(), Error>(())
+ /// }
+ /// ```
+ pub fn ioremap_resource(&self, resource: &Resource) -> Result<Devres<IoMem<0>>> {
+ self.ioremap_resource_sized::<0>(resource)
+ }
+
+ /// Same as [`Self::ioremap_resource`] but with exclusive access to the underlying
+ /// region.
+ pub fn ioremap_resource_exclusive(
+ &self,
+ resource: &Resource,
+ ) -> Result<Devres<ExclusiveIoMem<0>>> {
+ self.ioremap_resource_exclusive_sized::<0>(resource)
+ }
+
+ /// Returns the resource at `index`, if any.
+ pub fn resource(&self, index: u32) -> Option<&Resource> {
+ // SAFETY: `self.as_raw()` returns a valid pointer to a `struct platform_device`.
+ let resource = unsafe {
+ bindings::platform_get_resource(self.as_raw(), bindings::IORESOURCE_MEM, index)
+ };
+
+ if resource.is_null() {
+ return None;
+ }
+
+ // SAFETY: `resource` is a valid pointer to a `struct resource` as
+ // returned by `platform_get_resource`.
+ Some(unsafe { Resource::from_ptr(resource) })
+ }
+
+ /// Returns the resource with a given `name`, if any.
+ pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> {
+ // SAFETY: `self.as_raw()` returns a valid pointer to a `struct
+ // platform_device` and `name` points to a valid C string.
+ let resource = unsafe {
+ bindings::platform_get_resource_byname(
+ self.as_raw(),
+ bindings::IORESOURCE_MEM,
+ name.as_char_ptr(),
+ )
+ };
+
+ if resource.is_null() {
+ return None;
+ }
+
+ // SAFETY: `resource` is a valid pointer to a `struct resource` as
+ // returned by `platform_get_resource`.
+ Some(unsafe { Resource::from_ptr(resource) })
+ }
+}
+
impl AsRef<device::Device> for Device {
fn as_ref(&self) -> &device::Device {
// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
--
2.49.0
On Fri, May 09, 2025 at 05:29:48PM -0300, Daniel Almeida wrote:
> +impl Device<device::Core> {
For the ioremap_*() ones, `impl Device<device::Bound>` should be enough.
Also, PCI names those just iomap_*(), maybe we should align those names for
consistency.
On Fri, May 09, 2025 at 05:29:48PM -0300, Daniel Almeida wrote:
> +impl Device<device::Core> {
> + /// Maps a platform resource through ioremap() where the size is known at
> + /// compile time.
> + ///
> + /// # Examples
> + ///
> + /// ```no_run
> + /// # use kernel::{bindings, c_str, platform};
> + /// # use kernel::device::Core;
> + ///
> + ///
> + /// fn probe(pdev: &mut platform::Device<Core>, /* ... */) -> Result<()> {
Should be &platform::Device<Core> (i.e. not mutable). You should also be able to
just use `Result` as return type. Though, it would probably be better to use the
real probe() function here, i.e.
# struct Driver;
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 `ioremap_resource_sized`.
> + /// // No runtime checks will apply when reading and writing.
> + /// let resource = pdev.resource(0).ok_or(ENODEV)?;
> + /// let iomem = pdev.ioremap_resource_sized::<42>(&resource)?;
> + ///
> + /// // 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 = iomem.try_access().ok_or(ENODEV)?.read32_relaxed(offset);
> + ///
> + /// iomem.try_access().ok_or(ENODEV)?.write32_relaxed(data, offset);
Since this won't land for v6.16, can you please use Devres::access() [1]
instead? I.e.
let iomem = pdev.ioremap_resource_sized::<42>(&resource)?;
let io = Devres::access(pdev.as_ref())?;
let data = io.read32_relaxed(offset);
io.write32_relaxed(data, offset);
Devres::access() is in nova-next and lands in v6.16.
[1] https://gitlab.freedesktop.org/drm/nova/-/commit/f301cb978c068faa8fcd630be2cb317a2d0ec063
> + ///
> + /// # Ok::<(), Error>(())
> + /// }
> + /// ```
> + pub fn ioremap_resource_sized<const SIZE: usize>(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<IoMem<SIZE>>> {
> + IoMem::new(resource, self.as_ref())
> + }
> +
> + /// Same as [`Self::ioremap_resource_sized`] but with exclusive access to the
> + /// underlying region.
> + pub fn ioremap_resource_exclusive_sized<const SIZE: usize>(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<ExclusiveIoMem<SIZE>>> {
> + ExclusiveIoMem::new(resource, self.as_ref())
> + }
> +
> + /// Maps a platform resource through ioremap().
> + ///
> + /// # Examples
> + ///
> + /// ```no_run
> + /// # use kernel::{bindings, c_str, platform};
> + /// # use kernel::device::Core;
> + ///
> + /// fn probe(pdev: &mut platform::Device<Core>, /* ... */) -> Result<()> {
> + /// let offset = 0; // Some offset.
> + ///
> + /// // Unlike `ioremap_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(0).ok_or(ENODEV)?;
> + /// let iomem = pdev.ioremap_resource(&resource)?;
> + ///
> + /// let data = iomem.try_access().ok_or(ENODEV)?.try_read32_relaxed(offset)?;
> + ///
> + /// iomem.try_access().ok_or(ENODEV)?.try_write32_relaxed(data, offset)?;
> + ///
> + /// # Ok::<(), Error>(())
> + /// }
Same as above.
> + /// ```
> + pub fn ioremap_resource(&self, resource: &Resource) -> Result<Devres<IoMem<0>>> {
> + self.ioremap_resource_sized::<0>(resource)
> + }
> +
> + /// Same as [`Self::ioremap_resource`] but with exclusive access to the underlying
> + /// region.
> + pub fn ioremap_resource_exclusive(
> + &self,
> + resource: &Resource,
> + ) -> Result<Devres<ExclusiveIoMem<0>>> {
> + self.ioremap_resource_exclusive_sized::<0>(resource)
> + }
> +
> + /// Returns the resource at `index`, if any.
> + pub fn resource(&self, index: u32) -> Option<&Resource> {
> + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct platform_device`.
> + let resource = unsafe {
> + bindings::platform_get_resource(self.as_raw(), bindings::IORESOURCE_MEM, index)
> + };
> +
> + if resource.is_null() {
> + return None;
> + }
> +
> + // SAFETY: `resource` is a valid pointer to a `struct resource` as
> + // returned by `platform_get_resource`.
> + Some(unsafe { Resource::from_ptr(resource) })
> + }
> +
> + /// Returns the resource with a given `name`, if any.
> + pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> {
This method should be a separate patch, no? Also, I think this one can go into
the `impl<Ctx: device::DeviceContext> Device<Ctx>` block, since it should be
valid to call from any device context.
Hi Danilo, […] > >> + /// let offset = 0; // Some offset. >> + /// >> + /// // If the size is known at compile time, use `ioremap_resource_sized`. >> + /// // No runtime checks will apply when reading and writing. >> + /// let resource = pdev.resource(0).ok_or(ENODEV)?; >> + /// let iomem = pdev.ioremap_resource_sized::<42>(&resource)?; >> + /// >> + /// // 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 = iomem.try_access().ok_or(ENODEV)?.read32_relaxed(offset); >> + /// >> + /// iomem.try_access().ok_or(ENODEV)?.write32_relaxed(data, offset); > > Since this won't land for v6.16, can you please use Devres::access() [1] > instead? I.e. > > let iomem = pdev.ioremap_resource_sized::<42>(&resource)?; > let io = Devres::access(pdev.as_ref())?; > > let data = io.read32_relaxed(offset); > io.write32_relaxed(data, offset); > > Devres::access() is in nova-next and lands in v6.16. > > [1] https://gitlab.freedesktop.org/drm/nova/-/commit/f301cb978c068faa8fcd630be2cb317a2d0ec063 Devres:access takes &Device<Bound>, but the argument in probe() is &Device<Core>. Are these two types supposed to convert between them? I see no explicit function to do so. — Daniel
On Wed, May 28, 2025 at 02:29:44PM -0300, Daniel Almeida wrote: > Hi Danilo, > > […] > > > > >> + /// let offset = 0; // Some offset. > >> + /// > >> + /// // If the size is known at compile time, use `ioremap_resource_sized`. > >> + /// // No runtime checks will apply when reading and writing. > >> + /// let resource = pdev.resource(0).ok_or(ENODEV)?; > >> + /// let iomem = pdev.ioremap_resource_sized::<42>(&resource)?; > >> + /// > >> + /// // 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 = iomem.try_access().ok_or(ENODEV)?.read32_relaxed(offset); > >> + /// > >> + /// iomem.try_access().ok_or(ENODEV)?.write32_relaxed(data, offset); > > > > Since this won't land for v6.16, can you please use Devres::access() [1] > > instead? I.e. > > > > let iomem = pdev.ioremap_resource_sized::<42>(&resource)?; > > let io = Devres::access(pdev.as_ref())?; > > > > let data = io.read32_relaxed(offset); > > io.write32_relaxed(data, offset); > > > > Devres::access() is in nova-next and lands in v6.16. > > > > [1] https://gitlab.freedesktop.org/drm/nova/-/commit/f301cb978c068faa8fcd630be2cb317a2d0ec063 > > Devres:access takes &Device<Bound>, but the argument in probe() is > &Device<Core>. > > Are these two types supposed to convert between them? I see no explicit > function to do so. Yes, it comes from impl_device_context_deref!() [1], which, as the name implies, implements the corresponding Deref traits. Device dereference in the following way: &Device<Core> -> &Device<Bound> -> &Device (i.e. &Device<Normal>) You can just pass in the &Device<Core>, it will work. [1] https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/tree/rust/kernel/device.rs?h=driver-core-next#n284
© 2016 - 2025 Red Hat, Inc.