safe_mmio/
volatile_mmio.rs

1// Copyright 2025 The safe-mmio Authors.
2// This project is dual-licensed under Apache 2.0 and MIT terms.
3// See LICENSE-APACHE and LICENSE-MIT for details.
4
5use crate::{SharedMmioPointer, UniqueMmioPointer};
6
7impl<T> UniqueMmioPointer<'_, T> {
8    /// Performs an MMIO read of the entire `T`.
9    ///
10    /// # Safety
11    ///
12    /// This field must be safe to perform an MMIO read from.
13    pub unsafe fn read_unsafe(&mut self) -> T {
14        // SAFETY: self.regs is always a valid and unique pointer to MMIO address space.
15        unsafe { self.regs.read_volatile() }
16    }
17
18    /// Performs an MMIO write of the entire `T`.
19    ///
20    /// Note that this takes `&mut self` rather than `&self` because an MMIO read may cause
21    /// side-effects that change the state of the device.
22    ///
23    /// # Safety
24    ///
25    /// This field must be safe to perform an MMIO write to.
26    pub unsafe fn write_unsafe(&mut self, value: T) {
27        // SAFETY: self.regs is always a valid and unique pointer to MMIO address space.
28        unsafe {
29            self.regs.write_volatile(value);
30        }
31    }
32}
33
34impl<T> SharedMmioPointer<'_, T> {
35    /// Performs an MMIO read of the entire `T`.
36    ///
37    /// # Safety
38    ///
39    /// This field must be safe to perform an MMIO read from, and doing so must not cause any
40    /// side-effects.
41    pub unsafe fn read_unsafe(&self) -> T {
42        // SAFETY: self.regs is always a valid and unique pointer to MMIO address space.
43        unsafe { self.regs.read_volatile() }
44    }
45}