From 82a47602c277bd4324dedd5110ba0765090983af Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Oct 2025 18:01:17 +0800 Subject: [PATCH] improve std interface for bio - imem accessors are static-mapped slices - AtomicCsr is used so that the bio structure is Sync allowing it to be shared across threads --- libs/xous-bio-bdma/src/lib.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index e41e818b7..126e251a4 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -99,7 +99,10 @@ pub fn lfsr_next_u32(state: u32) -> u32 { pub const BIO_PRIVATE_MEM_LEN: usize = 4096; pub struct BioSharedState { + #[cfg(feature = "baremetal")] pub bio: CSR, + #[cfg(not(feature = "baremetal"))] + pub bio: AtomicCsr, pub imem_slice: [&'static mut [u32]; 4], } impl BioSharedState { @@ -170,9 +173,30 @@ impl BioSharedState { .unwrap(); BioSharedState { - bio: CSR::new(csr.as_mut_ptr() as *mut u32), + bio: AtomicCsr::new(csr.as_mut_ptr() as *mut u32), + // safety: MemoryRange does not de-allocate the mapping on Drop. So the maps live the + // lifetime of the process (or until `unmap_memory` is called: please don't do that). + // Since the range maps to some underlying hardware and it's aligned for the data types, + // it's as safe as any naked slice can be (i.e. you still have to think about concurrency etc.). imem_slice: unsafe { - [imem0.as_slice_mut(), imem1.as_slice_mut(), imem2.as_slice_mut(), imem3.as_slice_mut()] + [ + core::slice::from_raw_parts_mut( + imem0.as_mut_ptr() as *mut u32, + utralib::HW_BIO_IMEM0_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + imem1.as_mut_ptr() as *mut u32, + utralib::HW_BIO_IMEM1_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + imem2.as_mut_ptr() as *mut u32, + utralib::HW_BIO_IMEM2_MEM_LEN / size_of::(), + ), + core::slice::from_raw_parts_mut( + imem3.as_mut_ptr() as *mut u32, + utralib::HW_BIO_IMEM3_MEM_LEN / size_of::(), + ), + ] }, } }