twizzler_driver/device/
mmio.rsuse twizzler_abi::{
device::{MmioInfo, SubObjectType, MMIO_OFFSET},
object::NULLPAGE_SIZE,
};
use twizzler_object::{ObjID, Object, ObjectInitError, ObjectInitFlags, Protections};
use volatile::access::{ReadOnly, ReadWrite};
use super::Device;
pub struct MmioObject {
obj: Object<MmioInfo>,
}
impl MmioObject {
pub(crate) fn new(id: ObjID) -> Result<Self, ObjectInitError> {
Ok(Self {
obj: Object::init_id(
id,
Protections::READ | Protections::WRITE,
ObjectInitFlags::empty(),
)?,
})
}
pub fn get_info(&self) -> &MmioInfo {
self.obj.base().unwrap()
}
pub unsafe fn get_mmio_offset<T>(
&self,
offset: usize,
) -> volatile::VolatileRef<'_, T, ReadOnly> {
let ptr = self.obj.base().unwrap() as *const MmioInfo as *const u8;
volatile::VolatileRef::from_ref(
(ptr.add(MMIO_OFFSET + offset).sub(NULLPAGE_SIZE) as *mut T)
.as_mut()
.unwrap(),
)
}
pub unsafe fn get_mmio_offset_mut<T>(
&self,
offset: usize,
) -> volatile::VolatileRef<'_, T, ReadWrite> {
let ptr = self.obj.base().unwrap() as *const MmioInfo as *const u8;
volatile::VolatileRef::from_mut_ref(
(ptr.add(MMIO_OFFSET + offset).sub(NULLPAGE_SIZE) as *mut T)
.as_mut()
.unwrap(),
)
}
}
impl Device {
pub fn get_mmio(&self, idx: u8) -> Option<MmioObject> {
let id = self.get_subobj(SubObjectType::Mmio.into(), idx)?;
MmioObject::new(id).ok()
}
}