Re: [PATCH v7 3/3] rust: platform: allow ioremap of platform resources
From: Daniel Almeida
Date: Tue Mar 18 2025 - 14:23:52 EST
Hi Danilo,
> On 18 Mar 2025, at 14:43, Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> Hi Daniel,
>
> On Tue, Mar 18, 2025 at 02:20:43PM -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@xxxxxxxxxxxxx>
>> ---
>> rust/kernel/platform.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++-
>> 1 file changed, 122 insertions(+), 1 deletion(-)
>
> You need to rebase this onto driver-core-next.
Right, I totally forgot about this.
>
>>
>> diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
>> index 1297f5292ba9b7ca9784f84979efbeccb0768bd3..56f3d7c0d536d77082d7f8d2407de17ee3e95ffa 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, container_of, device, driver,
>> + bindings, container_of, device,
>> + devres::Devres,
>> + driver,
>> error::{to_result, Result},
>> + io::{
>> + mem::{ExclusiveIoMem, IoMem},
>> + resource::Resource,
>> + },
>> of,
>> prelude::*,
>> str::CStr,
>> @@ -191,6 +197,121 @@ fn as_raw(&self) -> *mut bindings::platform_device {
>> // embedded in `struct platform_device`.
>> unsafe { container_of!(self.0.as_raw(), bindings::platform_device, dev) }.cast_mut()
>> }
>> +
>> + /// Maps a platform resource through ioremap() where the size is known at
>> + /// compile time.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```no_run
>> + /// use kernel::{bindings, c_str, platform};
>> + ///
>> + /// fn probe(pdev: &mut platform::Device, /* ... */) -> 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)?.readl(offset);
>> + ///
>> + /// iomem.try_access().ok_or(ENODEV)?.writel(data, offset);
>
> I'd probably write this as
>
> || -> Result {
> let iomem = iomem.try_access().ok_or(ENODEV)?;
>
> iomem.read32(offset);
> iomem.write32(data, offset);
>
> Ok(())
> }()?;
>
> There's also a patch [1] in progress that makes this more convenient.
>
> [1] https://lore.kernel.org/rust-for-linux/20250313-try_with-v1-1-adcae7ed98a9@xxxxxxxxxx/
Thanks!
— Daniel