safe_mmio/
physical.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 core::{
6    fmt::{self, Debug, Formatter},
7    marker::PhantomData,
8};
9
10/// The physical instance of some device's MMIO space.
11pub struct PhysicalInstance<T> {
12    pa: usize,
13    _phantom: PhantomData<T>,
14}
15
16impl<T> Debug for PhysicalInstance<T> {
17    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
18        f.debug_struct("PhysicalInstance")
19            .field("pa", &self.pa)
20            .field("size", &size_of::<T>())
21            .finish()
22    }
23}
24
25impl<T> PhysicalInstance<T> {
26    /// # Safety
27    ///
28    /// This must refer to the physical address of a real set of device registers of type `T`, and
29    /// there must only ever be a single `PhysicalInstance` created for those device registers.
30    pub const unsafe fn new(pa: usize) -> Self {
31        Self {
32            pa,
33            _phantom: PhantomData,
34        }
35    }
36
37    /// Returns the physical base address of the device's registers.
38    pub const fn pa(&self) -> usize {
39        self.pa
40    }
41}