Re: [PATCH v2 07/10] rust: add `io::Io` base type

Danilo Krummrich
2024-06-25T13:12:20Z

On Tue, Jun 25, 2024 at 12:59:24PM +0200, Andreas Hindborg wrote:
> Hi Danilo,
>
> Danilo Krummrich <dakr@redhat.com> writes:
>
> [...]
>
> > +
> > +macro_rules! define_write {
> > + ($(#[$attr:meta])* $name:ident, $try_name:ident, $type_name:ty) => {
> > + /// Write IO data from a given offset known at compile time.
> > + ///
> > + /// Bound checks are performed on compile time, hence if the offset is not known at compile
> > + /// time, the build will fail.
> > + $(#[$attr])*
> > + #[inline]
> > + pub fn $name(&self, value: $type_name, offset: usize) {
> > + let addr = self.io_addr_assert::<$type_name>(offset);
> > +
> > + unsafe { bindings::$name(value, addr as _, ) }
> > + }
> > +
> > + /// Write IO data from a given offset.
> > + ///
> > + /// Bound checks are performed on runtime, it fails if the offset (plus the type size) is
> > + /// out of bounds.
> > + $(#[$attr])*
> > + pub fn $try_name(&self, value: $type_name, offset: usize) -> Result {
> > + let addr = self.io_addr::<$type_name>(offset)?;
> > +
> > + unsafe { bindings::$name(value, addr as _) }
> > + Ok(())
> > + }
> > + };
> > +}
> > +
>
> I am curious why we do not need `&mut self` to write to this memory? Is
> it OK to race on these writes?

Yes, concurrent writes to the same I/O mapped memory region (within the same
driver) should be totally fine.

In the end it's only the driver that can know about (and has to ensure) the
semantics, concurrency and ordering of those writes.

>
>
> Best regards,
> Andreas
>