//! One owned SPI mask/readback operation. No ACK, EOI, deactivation, pending //! clear, allocator operation or fabricated handler completion is performed. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Invalid, Io, Capacity, Trigger, Clock, Timeout, Repeated } pub type Result = core::result::Result; pub trait Io { fn read(&mut self, offset: u64) -> Result; fn write(&mut self, offset: u64, value: u32) -> Result<()>; fn barrier(&mut self); fn now(&mut self) -> u64; } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Snapshot { pub intid: u32, pub typer: u32, pub control: u32, pub enable_word: u32, pub active_word: u32, pub pending_word: u32, pub config_word: u32, pub counter: u64, } impl Snapshot { pub fn masked(&self) -> bool { self.enable_word & (1 << (self.intid % 32)) == 0 } pub fn active(&self) -> bool { self.active_word & (1 << (self.intid % 32)) != 0 } pub fn pending(&self) -> bool { self.pending_word & (1 << (self.intid % 32)) != 0 } pub fn settled(&self) -> bool { self.control & (1 << 31) == 0 } } pub struct Spi { io: B, intid: u32, trigger: u32, attempted: bool, } impl Spi { pub fn new(io: B, intid: u32, trigger: u32) -> Result { if !(32..=1019).contains(&intid) || !matches!(trigger, 1 | 4) { return Err(Error::Invalid); } Ok(Self { io, intid, trigger, attempted: false }) } pub fn into_io(self) -> B { self.io } fn time(&mut self, deadline: u64, previous: u64) -> Result { let now = self.io.now(); if now == 0 || now < previous { return Err(Error::Clock); } if now >= deadline { return Err(Error::Timeout); } Ok(now) } fn read_at(&mut self, counter: u64) -> Result { self.io.barrier(); let typer = self.io.read(4)?; let capacity = ((typer & 31) + 1) * 32; if self.intid >= capacity { return Err(Error::Capacity); } let word = 4 * u64::from(self.intid / 32); let config_word = self.io.read(0xc00 + 4 * u64::from(self.intid / 16))?; let config = (config_word >> (2 * (self.intid % 16))) & 3; if config != if self.trigger == 1 { 2 } else { 0 } { return Err(Error::Trigger); } let value = Snapshot { intid: self.intid, typer, control: self.io.read(0)?, enable_word: self.io.read(0x100 + word)?, active_word: self.io.read(0x300 + word)?, pending_word: self.io.read(0x200 + word)?, config_word, counter }; self.io.barrier(); Ok(value) } /// The caller owns this route exclusively and retains all device, handler /// and worker resources. A masked GIC source is not itself proof that every /// software handler has returned. The larger drain must verify that too. pub fn mask(&mut self, deadline: u64, polls: u32) -> Result<(Snapshot, Snapshot)> { if self.attempted { return Err(Error::Repeated); } self.attempted = true; if deadline == 0 || polls == 0 || polls > 1_000_000 { return Err(Error::Invalid); } let mut time = self.time(deadline, 0)?; let before = self.read_at(time)?; time = self.time(deadline, time)?; // Only the requested write-one-to-clear enable bit is written. self.io.write(0x180 + 4 * u64::from(self.intid / 32), 1 << (self.intid % 32))?; self.io.barrier(); for _ in 0..polls { time = self.time(deadline, time)?; let after = self.read_at(time)?; time = self.time(deadline, time)?; if after.typer != before.typer { return Err(Error::Capacity); } if after.settled() && after.masked() && !after.active() { return Ok((before, after)); } } Err(Error::Timeout) } } /// The owner must retain the Device mapping for the lifetime of this object. /// Construction is unsafe because a numeric DTB address is not an MMIO lease. #[cfg(target_arch = "aarch64")] pub struct Native { base: u64 } #[cfg(target_arch = "aarch64")] impl Native { /// # Safety /// `base..base+bytes` must be the exclusively owned, mapped GIC distributor /// Device region from the accepted platform. The caller must retain it and /// the SPI registration, prevent concurrent reconfiguration, and separately /// drain software handlers. The physical counter must be available at EL1. pub unsafe fn new(base: u64, bytes: u64) -> Result { if base == 0 || base % 65536 != 0 || bytes < 65536 || base.checked_add(bytes).is_none() { return Err(Error::Invalid); } Ok(Self { base }) } } #[cfg(target_arch = "aarch64")] impl Io for Native { fn read(&mut self, offset: u64) -> Result { if offset % 4 != 0 || !(offset <= 4 || (0x100..0x180).contains(&offset) || (0x200..0x280).contains(&offset) || (0x300..0x380).contains(&offset) || (0xc00..0xd00).contains(&offset)) { return Err(Error::Invalid); } Ok(unsafe { core::ptr::read_volatile((self.base + offset) as *const u32) }) } fn write(&mut self, offset: u64, value: u32) -> Result<()> { if offset % 4 != 0 || !(0x180..0x200).contains(&offset) || value.count_ones() != 1 { return Err(Error::Invalid); } unsafe { core::ptr::write_volatile((self.base + offset) as *mut u32, value) }; Ok(()) } fn barrier(&mut self) { unsafe { core::arch::asm!("dsb sy", options(nostack, preserves_flags)) }; } fn now(&mut self) -> u64 { let value: u64; unsafe { core::arch::asm!("mrs {value}, CNTPCT_EL0", value=out(reg)value, options(nomem, nostack, preserves_flags)) }; value } }