xref: /linux/rust/kernel/io.rs (revision 63a43faf6a68ce0045c874b32e60acac2089a41a)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Memory-mapped IO.
4 //!
5 //! C header: [`include/asm-generic/io.h`](srctree/include/asm-generic/io.h)
6 
7 use crate::{
8     bindings,
9     prelude::*, //
10 };
11 
12 pub mod mem;
13 pub mod poll;
14 pub mod resource;
15 
16 pub use resource::Resource;
17 
18 /// Physical address type.
19 ///
20 /// This is a type alias to either `u32` or `u64` depending on the config option
21 /// `CONFIG_PHYS_ADDR_T_64BIT`, and it can be a u64 even on 32-bit architectures.
22 pub type PhysAddr = bindings::phys_addr_t;
23 
24 /// Resource Size type.
25 ///
26 /// This is a type alias to either `u32` or `u64` depending on the config option
27 /// `CONFIG_PHYS_ADDR_T_64BIT`, and it can be a u64 even on 32-bit architectures.
28 pub type ResourceSize = bindings::resource_size_t;
29 
30 /// Raw representation of an MMIO region.
31 ///
32 /// By itself, the existence of an instance of this structure does not provide any guarantees that
33 /// the represented MMIO region does exist or is properly mapped.
34 ///
35 /// Instead, the bus specific MMIO implementation must convert this raw representation into an
36 /// `Mmio` instance providing the actual memory accessors. Only by the conversion into an `Mmio`
37 /// structure any guarantees are given.
38 pub struct MmioRaw<const SIZE: usize = 0> {
39     addr: usize,
40     maxsize: usize,
41 }
42 
43 impl<const SIZE: usize> MmioRaw<SIZE> {
44     /// Returns a new `MmioRaw` instance on success, an error otherwise.
new(addr: usize, maxsize: usize) -> Result<Self>45     pub fn new(addr: usize, maxsize: usize) -> Result<Self> {
46         if maxsize < SIZE {
47             return Err(EINVAL);
48         }
49 
50         Ok(Self { addr, maxsize })
51     }
52 
53     /// Returns the base address of the MMIO region.
54     #[inline]
addr(&self) -> usize55     pub fn addr(&self) -> usize {
56         self.addr
57     }
58 
59     /// Returns the maximum size of the MMIO region.
60     #[inline]
maxsize(&self) -> usize61     pub fn maxsize(&self) -> usize {
62         self.maxsize
63     }
64 }
65 
66 /// IO-mapped memory region.
67 ///
68 /// The creator (usually a subsystem / bus such as PCI) is responsible for creating the
69 /// mapping, performing an additional region request etc.
70 ///
71 /// # Invariant
72 ///
73 /// `addr` is the start and `maxsize` the length of valid I/O mapped memory region of size
74 /// `maxsize`.
75 ///
76 /// # Examples
77 ///
78 /// ```no_run
79 /// use kernel::{
80 ///     bindings,
81 ///     ffi::c_void,
82 ///     io::{
83 ///         Io,
84 ///         IoKnownSize,
85 ///         Mmio,
86 ///         MmioRaw,
87 ///         PhysAddr,
88 ///     },
89 /// };
90 /// use core::ops::Deref;
91 ///
92 /// // See also `pci::Bar` for a real example.
93 /// struct IoMem<const SIZE: usize>(MmioRaw<SIZE>);
94 ///
95 /// impl<const SIZE: usize> IoMem<SIZE> {
96 ///     /// # Safety
97 ///     ///
98 ///     /// [`paddr`, `paddr` + `SIZE`) must be a valid MMIO region that is mappable into the CPUs
99 ///     /// virtual address space.
100 ///     unsafe fn new(paddr: usize) -> Result<Self>{
101 ///         // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is
102 ///         // valid for `ioremap`.
103 ///         let addr = unsafe { bindings::ioremap(paddr as PhysAddr, SIZE) };
104 ///         if addr.is_null() {
105 ///             return Err(ENOMEM);
106 ///         }
107 ///
108 ///         Ok(IoMem(MmioRaw::new(addr as usize, SIZE)?))
109 ///     }
110 /// }
111 ///
112 /// impl<const SIZE: usize> Drop for IoMem<SIZE> {
113 ///     fn drop(&mut self) {
114 ///         // SAFETY: `self.0.addr()` is guaranteed to be properly mapped by `Self::new`.
115 ///         unsafe { bindings::iounmap(self.0.addr() as *mut c_void); };
116 ///     }
117 /// }
118 ///
119 /// impl<const SIZE: usize> Deref for IoMem<SIZE> {
120 ///    type Target = Mmio<SIZE>;
121 ///
122 ///    fn deref(&self) -> &Self::Target {
123 ///         // SAFETY: The memory range stored in `self` has been properly mapped in `Self::new`.
124 ///         unsafe { Mmio::from_raw(&self.0) }
125 ///    }
126 /// }
127 ///
128 ///# fn no_run() -> Result<(), Error> {
129 /// // SAFETY: Invalid usage for example purposes.
130 /// let iomem = unsafe { IoMem::<{ core::mem::size_of::<u32>() }>::new(0xBAAAAAAD)? };
131 /// iomem.write32(0x42, 0x0);
132 /// assert!(iomem.try_write32(0x42, 0x0).is_ok());
133 /// assert!(iomem.try_write32(0x42, 0x4).is_err());
134 /// # Ok(())
135 /// # }
136 /// ```
137 #[repr(transparent)]
138 pub struct Mmio<const SIZE: usize = 0>(MmioRaw<SIZE>);
139 
140 /// Internal helper macros used to invoke C MMIO read functions.
141 ///
142 /// This macro is intended to be used by higher-level MMIO access macros (io_define_read) and
143 /// provides a unified expansion for infallible vs. fallible read semantics. It emits a direct call
144 /// into the corresponding C helper and performs the required cast to the Rust return type.
145 ///
146 /// # Parameters
147 ///
148 /// * `$c_fn` – The C function performing the MMIO read.
149 /// * `$self` – The I/O backend object.
150 /// * `$ty` – The type of the value to be read.
151 /// * `$addr` – The MMIO address to read.
152 ///
153 /// This macro does not perform any validation; all invariants must be upheld by the higher-level
154 /// abstraction invoking it.
155 macro_rules! call_mmio_read {
156     (infallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) => {
157         // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
158         unsafe { bindings::$c_fn($addr as *const c_void) as $type }
159     };
160 
161     (fallible, $c_fn:ident, $self:ident, $type:ty, $addr:expr) => {{
162         // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
163         Ok(unsafe { bindings::$c_fn($addr as *const c_void) as $type })
164     }};
165 }
166 
167 /// Internal helper macros used to invoke C MMIO write functions.
168 ///
169 /// This macro is intended to be used by higher-level MMIO access macros (io_define_write) and
170 /// provides a unified expansion for infallible vs. fallible write semantics. It emits a direct call
171 /// into the corresponding C helper and performs the required cast to the Rust return type.
172 ///
173 /// # Parameters
174 ///
175 /// * `$c_fn` – The C function performing the MMIO write.
176 /// * `$self` – The I/O backend object.
177 /// * `$ty` – The type of the written value.
178 /// * `$addr` – The MMIO address to write.
179 /// * `$value` – The value to write.
180 ///
181 /// This macro does not perform any validation; all invariants must be upheld by the higher-level
182 /// abstraction invoking it.
183 macro_rules! call_mmio_write {
184     (infallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr) => {
185         // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
186         unsafe { bindings::$c_fn($value, $addr as *mut c_void) }
187     };
188 
189     (fallible, $c_fn:ident, $self:ident, $ty:ty, $addr:expr, $value:expr) => {{
190         // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
191         unsafe { bindings::$c_fn($value, $addr as *mut c_void) };
192         Ok(())
193     }};
194 }
195 
196 /// Generates an accessor method for reading from an I/O backend.
197 ///
198 /// This macro reduces boilerplate by automatically generating either compile-time bounds-checked
199 /// (infallible) or runtime bounds-checked (fallible) read methods. It abstracts the address
200 /// calculation and bounds checking, and delegates the actual I/O read operation to a specified
201 /// helper macro, making it generic over different I/O backends.
202 ///
203 /// # Parameters
204 ///
205 /// * `infallible` / `fallible` - Determines the bounds-checking strategy. `infallible` relies on
206 ///   `IoKnownSize` for compile-time checks and returns the value directly. `fallible` performs
207 ///   runtime checks against `maxsize()` and returns a `Result<T>`.
208 /// * `$(#[$attr:meta])*` - Optional attributes to apply to the generated method (e.g.,
209 ///   `#[cfg(CONFIG_64BIT)]` or inline directives).
210 /// * `$vis:vis` - The visibility of the generated method (e.g., `pub`).
211 /// * `$name:ident` / `$try_name:ident` - The name of the generated method (e.g., `read32`,
212 ///   `try_read8`).
213 /// * `$call_macro:ident` - The backend-specific helper macro used to emit the actual I/O call
214 ///   (e.g., `call_mmio_read`).
215 /// * `$c_fn:ident` - The backend-specific C function or identifier to be passed into the
216 ///   `$call_macro`.
217 /// * `$type_name:ty` - The Rust type of the value being read (e.g., `u8`, `u32`).
218 #[macro_export]
219 macro_rules! io_define_read {
220     (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident($c_fn:ident) ->
221      $type_name:ty) => {
222         /// Read IO data from a given offset known at compile time.
223         ///
224         /// Bound checks are performed on compile time, hence if the offset is not known at compile
225         /// time, the build will fail.
226         $(#[$attr])*
227         // Always inline to optimize out error path of `io_addr_assert`.
228         #[inline(always)]
229         $vis fn $name(&self, offset: usize) -> $type_name {
230             let addr = self.io_addr_assert::<$type_name>(offset);
231 
232             // SAFETY: By the type invariant `addr` is a valid address for IO operations.
233             $call_macro!(infallible, $c_fn, self, $type_name, addr)
234         }
235     };
236 
237     (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ident($c_fn:ident) ->
238      $type_name:ty) => {
239         /// Read IO data from a given offset.
240         ///
241         /// Bound checks are performed on runtime, it fails if the offset (plus the type size) is
242         /// out of bounds.
243         $(#[$attr])*
244         $vis fn $try_name(&self, offset: usize) -> Result<$type_name> {
245             let addr = self.io_addr::<$type_name>(offset)?;
246 
247             // SAFETY: By the type invariant `addr` is a valid address for IO operations.
248             $call_macro!(fallible, $c_fn, self, $type_name, addr)
249         }
250     };
251 }
252 pub use io_define_read;
253 
254 /// Generates an accessor method for writing to an I/O backend.
255 ///
256 /// This macro reduces boilerplate by automatically generating either compile-time bounds-checked
257 /// (infallible) or runtime bounds-checked (fallible) write methods. It abstracts the address
258 /// calculation and bounds checking, and delegates the actual I/O write operation to a specified
259 /// helper macro, making it generic over different I/O backends.
260 ///
261 /// # Parameters
262 ///
263 /// * `infallible` / `fallible` - Determines the bounds-checking strategy. `infallible` relies on
264 ///   `IoKnownSize` for compile-time checks and returns `()`. `fallible` performs runtime checks
265 ///   against `maxsize()` and returns a `Result`.
266 /// * `$(#[$attr:meta])*` - Optional attributes to apply to the generated method (e.g.,
267 ///   `#[cfg(CONFIG_64BIT)]` or inline directives).
268 /// * `$vis:vis` - The visibility of the generated method (e.g., `pub`).
269 /// * `$name:ident` / `$try_name:ident` - The name of the generated method (e.g., `write32`,
270 ///   `try_write8`).
271 /// * `$call_macro:ident` - The backend-specific helper macro used to emit the actual I/O call
272 ///   (e.g., `call_mmio_write`).
273 /// * `$c_fn:ident` - The backend-specific C function or identifier to be passed into the
274 ///   `$call_macro`.
275 /// * `$type_name:ty` - The Rust type of the value being written (e.g., `u8`, `u32`). Note the use
276 ///   of `<-` before the type to denote a write operation.
277 #[macro_export]
278 macro_rules! io_define_write {
279     (infallible, $(#[$attr:meta])* $vis:vis $name:ident, $call_macro:ident($c_fn:ident) <-
280      $type_name:ty) => {
281         /// Write IO data from a given offset known at compile time.
282         ///
283         /// Bound checks are performed on compile time, hence if the offset is not known at compile
284         /// time, the build will fail.
285         $(#[$attr])*
286         // Always inline to optimize out error path of `io_addr_assert`.
287         #[inline(always)]
288         $vis fn $name(&self, value: $type_name, offset: usize) {
289             let addr = self.io_addr_assert::<$type_name>(offset);
290 
291             $call_macro!(infallible, $c_fn, self, $type_name, addr, value);
292         }
293     };
294 
295     (fallible, $(#[$attr:meta])* $vis:vis $try_name:ident, $call_macro:ident($c_fn:ident) <-
296      $type_name:ty) => {
297         /// Write IO data from a given offset.
298         ///
299         /// Bound checks are performed on runtime, it fails if the offset (plus the type size) is
300         /// out of bounds.
301         $(#[$attr])*
302         $vis fn $try_name(&self, value: $type_name, offset: usize) -> Result {
303             let addr = self.io_addr::<$type_name>(offset)?;
304 
305             $call_macro!(fallible, $c_fn, self, $type_name, addr, value)
306         }
307     };
308 }
309 pub use io_define_write;
310 
311 /// Checks whether an access of type `U` at the given `offset`
312 /// is valid within this region.
313 #[inline]
offset_valid<U>(offset: usize, size: usize) -> bool314 const fn offset_valid<U>(offset: usize, size: usize) -> bool {
315     let type_size = core::mem::size_of::<U>();
316     if let Some(end) = offset.checked_add(type_size) {
317         end <= size && offset % type_size == 0
318     } else {
319         false
320     }
321 }
322 
323 /// Marker trait indicating that an I/O backend supports operations of a certain type.
324 ///
325 /// Different I/O backends can implement this trait to expose only the operations they support.
326 ///
327 /// For example, a PCI configuration space may implement `IoCapable<u8>`, `IoCapable<u16>`,
328 /// and `IoCapable<u32>`, but not `IoCapable<u64>`, while an MMIO region on a 64-bit
329 /// system might implement all four.
330 pub trait IoCapable<T> {}
331 
332 /// Types implementing this trait (e.g. MMIO BARs or PCI config regions)
333 /// can perform I/O operations on regions of memory.
334 ///
335 /// This is an abstract representation to be implemented by arbitrary I/O
336 /// backends (e.g. MMIO, PCI config space, etc.).
337 ///
338 /// The [`Io`] trait provides:
339 /// - Base address and size information
340 /// - Helper methods for offset validation and address calculation
341 /// - Fallible (runtime checked) accessors for different data widths
342 ///
343 /// Which I/O methods are available depends on which [`IoCapable<T>`] traits
344 /// are implemented for the type.
345 ///
346 /// # Examples
347 ///
348 /// For MMIO regions, all widths (u8, u16, u32, and u64 on 64-bit systems) are typically
349 /// supported. For PCI configuration space, u8, u16, and u32 are supported but u64 is not.
350 pub trait Io {
351     /// Returns the base address of this mapping.
addr(&self) -> usize352     fn addr(&self) -> usize;
353 
354     /// Returns the maximum size of this mapping.
maxsize(&self) -> usize355     fn maxsize(&self) -> usize;
356 
357     /// Returns the absolute I/O address for a given `offset`,
358     /// performing runtime bound checks.
359     #[inline]
io_addr<U>(&self, offset: usize) -> Result<usize>360     fn io_addr<U>(&self, offset: usize) -> Result<usize> {
361         if !offset_valid::<U>(offset, self.maxsize()) {
362             return Err(EINVAL);
363         }
364 
365         // Probably no need to check, since the safety requirements of `Self::new` guarantee that
366         // this can't overflow.
367         self.addr().checked_add(offset).ok_or(EINVAL)
368     }
369 
370     /// Fallible 8-bit read with runtime bounds check.
371     #[inline(always)]
try_read8(&self, _offset: usize) -> Result<u8> where Self: IoCapable<u8>,372     fn try_read8(&self, _offset: usize) -> Result<u8>
373     where
374         Self: IoCapable<u8>,
375     {
376         build_error!("Backend does not support fallible 8-bit read")
377     }
378 
379     /// Fallible 16-bit read with runtime bounds check.
380     #[inline(always)]
try_read16(&self, _offset: usize) -> Result<u16> where Self: IoCapable<u16>,381     fn try_read16(&self, _offset: usize) -> Result<u16>
382     where
383         Self: IoCapable<u16>,
384     {
385         build_error!("Backend does not support fallible 16-bit read")
386     }
387 
388     /// Fallible 32-bit read with runtime bounds check.
389     #[inline(always)]
try_read32(&self, _offset: usize) -> Result<u32> where Self: IoCapable<u32>,390     fn try_read32(&self, _offset: usize) -> Result<u32>
391     where
392         Self: IoCapable<u32>,
393     {
394         build_error!("Backend does not support fallible 32-bit read")
395     }
396 
397     /// Fallible 64-bit read with runtime bounds check.
398     #[inline(always)]
try_read64(&self, _offset: usize) -> Result<u64> where Self: IoCapable<u64>,399     fn try_read64(&self, _offset: usize) -> Result<u64>
400     where
401         Self: IoCapable<u64>,
402     {
403         build_error!("Backend does not support fallible 64-bit read")
404     }
405 
406     /// Fallible 8-bit write with runtime bounds check.
407     #[inline(always)]
try_write8(&self, _value: u8, _offset: usize) -> Result where Self: IoCapable<u8>,408     fn try_write8(&self, _value: u8, _offset: usize) -> Result
409     where
410         Self: IoCapable<u8>,
411     {
412         build_error!("Backend does not support fallible 8-bit write")
413     }
414 
415     /// Fallible 16-bit write with runtime bounds check.
416     #[inline(always)]
try_write16(&self, _value: u16, _offset: usize) -> Result where Self: IoCapable<u16>,417     fn try_write16(&self, _value: u16, _offset: usize) -> Result
418     where
419         Self: IoCapable<u16>,
420     {
421         build_error!("Backend does not support fallible 16-bit write")
422     }
423 
424     /// Fallible 32-bit write with runtime bounds check.
425     #[inline(always)]
try_write32(&self, _value: u32, _offset: usize) -> Result where Self: IoCapable<u32>,426     fn try_write32(&self, _value: u32, _offset: usize) -> Result
427     where
428         Self: IoCapable<u32>,
429     {
430         build_error!("Backend does not support fallible 32-bit write")
431     }
432 
433     /// Fallible 64-bit write with runtime bounds check.
434     #[inline(always)]
try_write64(&self, _value: u64, _offset: usize) -> Result where Self: IoCapable<u64>,435     fn try_write64(&self, _value: u64, _offset: usize) -> Result
436     where
437         Self: IoCapable<u64>,
438     {
439         build_error!("Backend does not support fallible 64-bit write")
440     }
441 
442     /// Infallible 8-bit read with compile-time bounds check.
443     #[inline(always)]
read8(&self, _offset: usize) -> u8 where Self: IoKnownSize + IoCapable<u8>,444     fn read8(&self, _offset: usize) -> u8
445     where
446         Self: IoKnownSize + IoCapable<u8>,
447     {
448         build_error!("Backend does not support infallible 8-bit read")
449     }
450 
451     /// Infallible 16-bit read with compile-time bounds check.
452     #[inline(always)]
read16(&self, _offset: usize) -> u16 where Self: IoKnownSize + IoCapable<u16>,453     fn read16(&self, _offset: usize) -> u16
454     where
455         Self: IoKnownSize + IoCapable<u16>,
456     {
457         build_error!("Backend does not support infallible 16-bit read")
458     }
459 
460     /// Infallible 32-bit read with compile-time bounds check.
461     #[inline(always)]
read32(&self, _offset: usize) -> u32 where Self: IoKnownSize + IoCapable<u32>,462     fn read32(&self, _offset: usize) -> u32
463     where
464         Self: IoKnownSize + IoCapable<u32>,
465     {
466         build_error!("Backend does not support infallible 32-bit read")
467     }
468 
469     /// Infallible 64-bit read with compile-time bounds check.
470     #[inline(always)]
read64(&self, _offset: usize) -> u64 where Self: IoKnownSize + IoCapable<u64>,471     fn read64(&self, _offset: usize) -> u64
472     where
473         Self: IoKnownSize + IoCapable<u64>,
474     {
475         build_error!("Backend does not support infallible 64-bit read")
476     }
477 
478     /// Infallible 8-bit write with compile-time bounds check.
479     #[inline(always)]
write8(&self, _value: u8, _offset: usize) where Self: IoKnownSize + IoCapable<u8>,480     fn write8(&self, _value: u8, _offset: usize)
481     where
482         Self: IoKnownSize + IoCapable<u8>,
483     {
484         build_error!("Backend does not support infallible 8-bit write")
485     }
486 
487     /// Infallible 16-bit write with compile-time bounds check.
488     #[inline(always)]
write16(&self, _value: u16, _offset: usize) where Self: IoKnownSize + IoCapable<u16>,489     fn write16(&self, _value: u16, _offset: usize)
490     where
491         Self: IoKnownSize + IoCapable<u16>,
492     {
493         build_error!("Backend does not support infallible 16-bit write")
494     }
495 
496     /// Infallible 32-bit write with compile-time bounds check.
497     #[inline(always)]
write32(&self, _value: u32, _offset: usize) where Self: IoKnownSize + IoCapable<u32>,498     fn write32(&self, _value: u32, _offset: usize)
499     where
500         Self: IoKnownSize + IoCapable<u32>,
501     {
502         build_error!("Backend does not support infallible 32-bit write")
503     }
504 
505     /// Infallible 64-bit write with compile-time bounds check.
506     #[inline(always)]
write64(&self, _value: u64, _offset: usize) where Self: IoKnownSize + IoCapable<u64>,507     fn write64(&self, _value: u64, _offset: usize)
508     where
509         Self: IoKnownSize + IoCapable<u64>,
510     {
511         build_error!("Backend does not support infallible 64-bit write")
512     }
513 }
514 
515 /// Trait for types with a known size at compile time.
516 ///
517 /// This trait is implemented by I/O backends that have a compile-time known size,
518 /// enabling the use of infallible I/O accessors with compile-time bounds checking.
519 ///
520 /// Types implementing this trait can use the infallible methods in [`Io`] trait
521 /// (e.g., `read8`, `write32`), which require `Self: IoKnownSize` bound.
522 pub trait IoKnownSize: Io {
523     /// Minimum usable size of this region.
524     const MIN_SIZE: usize;
525 
526     /// Returns the absolute I/O address for a given `offset`,
527     /// performing compile-time bound checks.
528     // Always inline to optimize out error path of `build_assert`.
529     #[inline(always)]
io_addr_assert<U>(&self, offset: usize) -> usize530     fn io_addr_assert<U>(&self, offset: usize) -> usize {
531         build_assert!(offset_valid::<U>(offset, Self::MIN_SIZE));
532 
533         self.addr() + offset
534     }
535 }
536 
537 // MMIO regions support 8, 16, and 32-bit accesses.
538 impl<const SIZE: usize> IoCapable<u8> for Mmio<SIZE> {}
539 impl<const SIZE: usize> IoCapable<u16> for Mmio<SIZE> {}
540 impl<const SIZE: usize> IoCapable<u32> for Mmio<SIZE> {}
541 
542 // MMIO regions on 64-bit systems also support 64-bit accesses.
543 #[cfg(CONFIG_64BIT)]
544 impl<const SIZE: usize> IoCapable<u64> for Mmio<SIZE> {}
545 
546 impl<const SIZE: usize> Io for Mmio<SIZE> {
547     /// Returns the base address of this mapping.
548     #[inline]
addr(&self) -> usize549     fn addr(&self) -> usize {
550         self.0.addr()
551     }
552 
553     /// Returns the maximum size of this mapping.
554     #[inline]
maxsize(&self) -> usize555     fn maxsize(&self) -> usize {
556         self.0.maxsize()
557     }
558 
559     io_define_read!(fallible, try_read8, call_mmio_read(readb) -> u8);
560     io_define_read!(fallible, try_read16, call_mmio_read(readw) -> u16);
561     io_define_read!(fallible, try_read32, call_mmio_read(readl) -> u32);
562     io_define_read!(
563         fallible,
564         #[cfg(CONFIG_64BIT)]
565         try_read64,
566         call_mmio_read(readq) -> u64
567     );
568 
569     io_define_write!(fallible, try_write8, call_mmio_write(writeb) <- u8);
570     io_define_write!(fallible, try_write16, call_mmio_write(writew) <- u16);
571     io_define_write!(fallible, try_write32, call_mmio_write(writel) <- u32);
572     io_define_write!(
573         fallible,
574         #[cfg(CONFIG_64BIT)]
575         try_write64,
576         call_mmio_write(writeq) <- u64
577     );
578 
579     io_define_read!(infallible, read8, call_mmio_read(readb) -> u8);
580     io_define_read!(infallible, read16, call_mmio_read(readw) -> u16);
581     io_define_read!(infallible, read32, call_mmio_read(readl) -> u32);
582     io_define_read!(
583         infallible,
584         #[cfg(CONFIG_64BIT)]
585         read64,
586         call_mmio_read(readq) -> u64
587     );
588 
589     io_define_write!(infallible, write8, call_mmio_write(writeb) <- u8);
590     io_define_write!(infallible, write16, call_mmio_write(writew) <- u16);
591     io_define_write!(infallible, write32, call_mmio_write(writel) <- u32);
592     io_define_write!(
593         infallible,
594         #[cfg(CONFIG_64BIT)]
595         write64,
596         call_mmio_write(writeq) <- u64
597     );
598 }
599 
600 impl<const SIZE: usize> IoKnownSize for Mmio<SIZE> {
601     const MIN_SIZE: usize = SIZE;
602 }
603 
604 impl<const SIZE: usize> Mmio<SIZE> {
605     /// Converts an `MmioRaw` into an `Mmio` instance, providing the accessors to the MMIO mapping.
606     ///
607     /// # Safety
608     ///
609     /// Callers must ensure that `addr` is the start of a valid I/O mapped memory region of size
610     /// `maxsize`.
from_raw(raw: &MmioRaw<SIZE>) -> &Self611     pub unsafe fn from_raw(raw: &MmioRaw<SIZE>) -> &Self {
612         // SAFETY: `Mmio` is a transparent wrapper around `MmioRaw`.
613         unsafe { &*core::ptr::from_ref(raw).cast() }
614     }
615 
616     io_define_read!(infallible, pub read8_relaxed, call_mmio_read(readb_relaxed) -> u8);
617     io_define_read!(infallible, pub read16_relaxed, call_mmio_read(readw_relaxed) -> u16);
618     io_define_read!(infallible, pub read32_relaxed, call_mmio_read(readl_relaxed) -> u32);
619     io_define_read!(
620         infallible,
621         #[cfg(CONFIG_64BIT)]
622         pub read64_relaxed,
623         call_mmio_read(readq_relaxed) -> u64
624     );
625 
626     io_define_read!(fallible, pub try_read8_relaxed, call_mmio_read(readb_relaxed) -> u8);
627     io_define_read!(fallible, pub try_read16_relaxed, call_mmio_read(readw_relaxed) -> u16);
628     io_define_read!(fallible, pub try_read32_relaxed, call_mmio_read(readl_relaxed) -> u32);
629     io_define_read!(
630         fallible,
631         #[cfg(CONFIG_64BIT)]
632         pub try_read64_relaxed,
633         call_mmio_read(readq_relaxed) -> u64
634     );
635 
636     io_define_write!(infallible, pub write8_relaxed, call_mmio_write(writeb_relaxed) <- u8);
637     io_define_write!(infallible, pub write16_relaxed, call_mmio_write(writew_relaxed) <- u16);
638     io_define_write!(infallible, pub write32_relaxed, call_mmio_write(writel_relaxed) <- u32);
639     io_define_write!(
640         infallible,
641         #[cfg(CONFIG_64BIT)]
642         pub write64_relaxed,
643         call_mmio_write(writeq_relaxed) <- u64
644     );
645 
646     io_define_write!(fallible, pub try_write8_relaxed, call_mmio_write(writeb_relaxed) <- u8);
647     io_define_write!(fallible, pub try_write16_relaxed, call_mmio_write(writew_relaxed) <- u16);
648     io_define_write!(fallible, pub try_write32_relaxed, call_mmio_write(writel_relaxed) <- u32);
649     io_define_write!(
650         fallible,
651         #[cfg(CONFIG_64BIT)]
652         pub try_write64_relaxed,
653         call_mmio_write(writeq_relaxed) <- u64
654     );
655 }
656