[PATCH RESEND v5 0/3] rust: platform: add Io support

Daniel Almeida posted 3 patches 2 weeks, 6 days ago
There is a newer version of this series
rust/bindings/bindings_helper.h |   1 +
rust/helpers/io.c               |  17 ++++++
rust/kernel/io.rs               |   3 +
rust/kernel/io/mem.rs           |  98 +++++++++++++++++++++++++++++
rust/kernel/io/resource.rs      |  53 ++++++++++++++++
rust/kernel/platform.rs         | 105 +++++++++++++++++++++++++++++++-
6 files changed, 276 insertions(+), 1 deletion(-)
create mode 100644 rust/kernel/io/mem.rs
create mode 100644 rust/kernel/io/resource.rs
[PATCH RESEND v5 0/3] rust: platform: add Io support
Posted by Daniel Almeida 2 weeks, 6 days ago
Changes in v5:

- resend v5, as the r4l list was not cc'd
- use srctree where applicable in the docs (Alice)
- Remove 'mut' in Resource::from_ptr() (Alice)
- Add 'invariants' section for Resource (Alice)
- Fix typos in mem.rs (Alice)
- Turn 'exclusive' into a const generic (Alice)
- Fix example in platform.rs (Alice)
- Rework the resource.is_null() check (Alice)
- Refactor IoMem::new() to return DevRes<IoMem> directly (Danilo)

link to v4: https://lore.kernel.org/rust-for-linux/20250109133057.243751-1-daniel.almeida@collabora.com/

Changes in v4:

- Rebased on top of driver-core-next
- Split series in multiple patches (Danilo)
- Move IoMem and Resource into its own files (Danilo)
- Fix a missing "if exclusive {...}" check (Danilo)
- Fixed the example, since it was using the old API (Danilo)
- Use Opaque in `Resource`, instead of NonNull and PhantomData (Boqun)
- Highlight that non-exclusive access to the iomem might be required in some cases
- Fixed the safety comment in IoMem::deref()

Link to v3: https://lore.kernel.org/rust-for-linux/20241211-topic-panthor-rs-platform_io_support-v3-1-08ba707e5e3b@collabora.com/

Changes in v3:
- Rebased on top of v5 for the PCI/Platform abstractions
- platform_get_resource is now called only once when calling ioremap
- Introduced a platform::Resource type, which is bound to the lifetime of the
 platform Device
- Allow retrieving resources from the platform device either by index or
 name
- Make request_mem_region() optional
- Use resource.name() in request_mem_region
- Reword the example to remove an unaligned, out-of-bounds offset
- Update the safety requirements of platform::IoMem

Changes in v2:
- reworked the commit message
- added missing request_mem_region call (Thanks Alice, Danilo)
- IoMem::new() now takes the platform::Device, the resource number and
 the name, instead of an address and a size (thanks, Danilo)
- Added a new example for both sized and unsized versions of IoMem.
- Compiled the examples using kunit.py (thanks for the tip, Alice!)
- Removed instances of `foo as _`. All `as` casts now spell out the actual
 type.
- Now compiling with CLIPPY=1 (I realized I had forgotten, sorry)
- Rebased on top of rust-next to check for any warnings given the new
 unsafe lints.

Daniel Almeida (3):
  rust: io: add resource abstraction
  rust: io: mem: add a generic iomem abstraction
  rust: platform: allow ioremap of platform resources

 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/io.c               |  17 ++++++
 rust/kernel/io.rs               |   3 +
 rust/kernel/io/mem.rs           |  98 +++++++++++++++++++++++++++++
 rust/kernel/io/resource.rs      |  53 ++++++++++++++++
 rust/kernel/platform.rs         | 105 +++++++++++++++++++++++++++++++-
 6 files changed, 276 insertions(+), 1 deletion(-)
 create mode 100644 rust/kernel/io/mem.rs
 create mode 100644 rust/kernel/io/resource.rs

-- 
2.48.0
Re: [PATCH RESEND v5 0/3] rust: platform: add Io support
Posted by Christian Schrefl 2 weeks, 5 days ago
Hi Daniel

On 16.01.25 1:56 PM, Daniel Almeida wrote:
> Changes in v5:
> 
> - resend v5, as the r4l list was not cc'd
> - use srctree where applicable in the docs (Alice)
> - Remove 'mut' in Resource::from_ptr() (Alice)
> - Add 'invariants' section for Resource (Alice)
> - Fix typos in mem.rs (Alice)
> - Turn 'exclusive' into a const generic (Alice)
> - Fix example in platform.rs (Alice)
> - Rework the resource.is_null() check (Alice)
> - Refactor IoMem::new() to return DevRes<IoMem> directly (Danilo)
> 
> link to v4: https://lore.kernel.org/rust-for-linux/20250109133057.243751-1-daniel.almeida@collabora.com/
> 
> Changes in v4:
> 
> - Rebased on top of driver-core-next
> - Split series in multiple patches (Danilo)
> - Move IoMem and Resource into its own files (Danilo)
> - Fix a missing "if exclusive {...}" check (Danilo)
> - Fixed the example, since it was using the old API (Danilo)
> - Use Opaque in `Resource`, instead of NonNull and PhantomData (Boqun)
> - Highlight that non-exclusive access to the iomem might be required in some cases
> - Fixed the safety comment in IoMem::deref()
> 
> Link to v3: https://lore.kernel.org/rust-for-linux/20241211-topic-panthor-rs-platform_io_support-v3-1-08ba707e5e3b@collabora.com/
> 
> Changes in v3:
> - Rebased on top of v5 for the PCI/Platform abstractions
> - platform_get_resource is now called only once when calling ioremap
> - Introduced a platform::Resource type, which is bound to the lifetime of the
>  platform Device
> - Allow retrieving resources from the platform device either by index or
>  name
> - Make request_mem_region() optional
> - Use resource.name() in request_mem_region
> - Reword the example to remove an unaligned, out-of-bounds offset
> - Update the safety requirements of platform::IoMem
> 
> Changes in v2:
> - reworked the commit message
> - added missing request_mem_region call (Thanks Alice, Danilo)
> - IoMem::new() now takes the platform::Device, the resource number and
>  the name, instead of an address and a size (thanks, Danilo)
> - Added a new example for both sized and unsized versions of IoMem.
> - Compiled the examples using kunit.py (thanks for the tip, Alice!)
> - Removed instances of `foo as _`. All `as` casts now spell out the actual
>  type.
> - Now compiling with CLIPPY=1 (I realized I had forgotten, sorry)
> - Rebased on top of rust-next to check for any warnings given the new
>  unsafe lints.
> 
> Daniel Almeida (3):
>   rust: io: add resource abstraction
>   rust: io: mem: add a generic iomem abstraction
>   rust: platform: allow ioremap of platform resources
> 
>  rust/bindings/bindings_helper.h |   1 +
>  rust/helpers/io.c               |  17 ++++++
>  rust/kernel/io.rs               |   3 +
>  rust/kernel/io/mem.rs           |  98 +++++++++++++++++++++++++++++
>  rust/kernel/io/resource.rs      |  53 ++++++++++++++++
>  rust/kernel/platform.rs         | 105 +++++++++++++++++++++++++++++++-
>  6 files changed, 276 insertions(+), 1 deletion(-)
>  create mode 100644 rust/kernel/io/mem.rs
>  create mode 100644 rust/kernel/io/resource.rs
> 


I've tested this patch-set with my ARM 32-bit arm patches[0] on a De1Soc (Fpga + Arm Cortex A9).

I've got it to work by changing the three instances of u64 (mentioned on the individual patches) to bindings::resource_size_t.
I used a small Fpga configuration that allows me to change a led from the kernel driver.

So with these fixed:

Tested-by: Christian Schrefl <chrisi.schrefl@gmail.com>

[0]: https://github.com/onestacked/linux/commit/f84be45f7311ea5b5a76123028f9fb8fda9f9e7f

Cheers
Christian