diff --git a/services/spinor/Cargo.toml b/services/spinor/Cargo.toml index 8194548e1..f0112561a 100644 --- a/services/spinor/Cargo.toml +++ b/services/spinor/Cargo.toml @@ -34,6 +34,7 @@ rand = "0.8.5" [features] precursor = ["utralib/precursor"] +vexii-test = [] hosted = ["utralib/hosted"] renode = ["utralib/renode"] extra_flush = [] diff --git a/services/spinor/src/lib.rs b/services/spinor/src/lib.rs index 575d91e91..cd2a8d984 100644 --- a/services/spinor/src/lib.rs +++ b/services/spinor/src/lib.rs @@ -18,6 +18,15 @@ use num_traits::*; use xous::{CID, Message, send_message}; use xous_ipc::Buffer; +pub unsafe fn flush_block(addr: usize) { + core::arch::asm!( + "mv a0, {addr}", + ".word 0x0025200f", /* cbo.flush 0(a0) */ + addr = in(reg) addr, + out("a0") _, // clobber a0; without this, the compiler can't know that a0 actually "mattered" + ); +} + #[derive(Debug)] pub struct Spinor { conn: CID, @@ -100,10 +109,45 @@ impl Spinor { } #[cfg(not(test))] - fn send_write_region(&self, wr: &WriteRegion) -> Result<(), SpinorError> { + fn send_write_region(&self, wr: &WriteRegion, _sector: &[u8]) -> Result<(), SpinorError> { let mut buf = Buffer::into_buf(*wr).or(Err(SpinorError::IpcError))?; buf.lend_mut(self.conn, Opcode::WriteRegion.to_u32().unwrap()).or(Err(SpinorError::IpcError))?; + // vexii requires virtually addressed region to be flushed. Sector contains the virtual address range + // to be flushed. Therefore, the cache flush routine needs to end up in the lib.rs side of things, + // to be called upon return from the spinor server's successful write of the region. + #[cfg(feature = "vexii-test")] + { + const VEXII_CACHE_LINE_WIDTH_BYTES: usize = 64; + let unaligned_start = _sector.as_ptr() as usize; + let aligned_start = unaligned_start & !(VEXII_CACHE_LINE_WIDTH_BYTES - 1); + let unaligned_len = _sector.len(); + // add in the extra bytes we need at the "start" to round down to a cache line on the start + let partially_aligned_len = unaligned_len + (unaligned_start - aligned_start); + // add in the extra bytes we need at the "end" to round up to end at a cache line + let aligned_len = (partially_aligned_len + (VEXII_CACHE_LINE_WIDTH_BYTES - 1)) + & !(VEXII_CACHE_LINE_WIDTH_BYTES - 1); + log::debug!( + "Flushing from {:x}-{:x} -> {:x}-{:x}", + unaligned_start, + unaligned_start + unaligned_len, + aligned_start, + aligned_start + aligned_len + ); + // now use cbo.flush on each line - this is coded as binary because Rust still requires + // nightly to understand cbo.flush. + for i in (aligned_start..aligned_start + aligned_len).step_by(VEXII_CACHE_LINE_WIDTH_BYTES) { + // in any case, don't run past the length of the allocated area - this will cause a page fault + if i < unaligned_start + unaligned_len { + unsafe { + flush_block(i); + } + } else { + log::info!("Ignoring flush overrun @ {:x}", i); + } + } + } + match buf.to_original::() { Ok(wr) => { if let Some(res) = wr.result { @@ -120,7 +164,7 @@ impl Spinor { } #[cfg(test)] - fn send_write_region(&self, wr: &WriteRegion) -> Result<(), SpinorError> { + fn send_write_region(&self, wr: &WriteRegion, _sector_dummy: &[u8]) -> Result<(), SpinorError> { let mut i = 0; if !wr.clean_patch { assert!( @@ -241,7 +285,7 @@ impl Spinor { /// lazy and you just send a large amount of unchanged data with a couple small changes scattered /// about, this will not be "smart" about it and do a diff and optimally patch sub-regions. It /// will erase and re-write the entire region included in the patch. It must also be a multiple of - /// two u8s in length, because the DDR interface can only tranfer even multiples of bytes. + /// two u8s in length, because the DDR interface can only transfer even multiples of bytes. /// `patch_index` is the offset relative to the base address of `region` to patch. It is *not* relative to /// the base of FLASH, it is relative to the base of `region`. You can think of it as the index into /// the `region` slice where the patch data should go. patch_index /can/ have an odd byte offset, @@ -365,7 +409,7 @@ impl Spinor { wr.start = patch_start .expect("check region did not intersect patch region; this shouldn't be possible."); wr.len = data_index as u32; - ret = self.send_write_region(&wr); + ret = self.send_write_region(&wr, sector); if ret.is_err() { break; } @@ -403,7 +447,7 @@ impl Spinor { // if the requested patch data happens to be identical to the existing data already, don't // even send the request. if dirty { - ret = self.send_write_region(&wr); + ret = self.send_write_region(&wr, sector); if ret.is_err() { break; // abort fast if we encounter an error } @@ -509,7 +553,7 @@ mod tests { wr.data[2] = 0xCC; wr.data[3] = 0xDD; let mut spinor = Spinor::new(); - let ret = spinor.send_write_region(&wr); + let ret = spinor.send_write_region(&wr, &[0]); assert!(ret.is_ok(), "spinor write returned an error"); for (addr, &byte) in EMU_FLASH.lock().unwrap().iter().enumerate() { @@ -538,7 +582,7 @@ mod tests { wr.data[8] = 0x3; wr.data[9] = 0x4; wr.len = 10; - let ret = spinor.send_write_region(&wr); + let ret = spinor.send_write_region(&wr, &[0]); assert!(ret.is_ok(), "spinor write returned an error"); for (addr, &byte) in EMU_FLASH.lock().unwrap().iter().enumerate() { diff --git a/services/spinor/src/main.rs b/services/spinor/src/main.rs index bb1d954a5..9799a56f5 100644 --- a/services/spinor/src/main.rs +++ b/services/spinor/src/main.rs @@ -570,6 +570,7 @@ mod implementation { /// Not sure why :-/, but, I stopped caring because ".word 0x500F" works! #[inline] fn flush_dcache(&self, _start: u32, _len: u32) { + #[cfg(not(feature = "vexii-test"))] unsafe { #[rustfmt::skip] core::arch::asm!( @@ -585,6 +586,22 @@ mod implementation { "nop", ); } + #[cfg(feature = "vexii-test")] + unsafe { + // fence.i / fence invalidates only i-cache, and syncs write-back of d-cache but + // d-cache is not invalidated. We need to explicitly invalidate d-cache range, which + // means we need to know what addresses we're updating. The arguments to the flush + // instruction need to be virtual addresses in the process space of the process + // that might be reading the data. Thus, we do not implement the invalidation here, + // but instead we do it in the lib.rs file of the spinor crate, so that the caller + // is doing the invalidation within their address space after interactions with the + // spinor server have occurred. + #[rustfmt::skip] + core::arch::asm!( + "fence.i", + "fence", + ); + } // augment with manual flushing, because the above instruction didn't seem to do the trick?? #[cfg(feature = "extra_flush")] {