From 24b6d4cb42c9b46044f02866abf6c748ebb8d136 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 31 Dec 2025 23:34:35 +0800 Subject: [PATCH] fix bugs in implementation --- libs/bao1x-hal/src/bio.rs | 39 ++++++++++++++++++------------------ libs/bao1x-hal/src/bio_hw.rs | 25 ++++++++++++++++++++--- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/libs/bao1x-hal/src/bio.rs b/libs/bao1x-hal/src/bio.rs index 246efe1c1..fe4ea09e0 100644 --- a/libs/bao1x-hal/src/bio.rs +++ b/libs/bao1x-hal/src/bio.rs @@ -66,7 +66,7 @@ impl<'a> BioApi<'a> for Bio { ArchivedBioError::Uninit => panic!("Error in message passing"), ArchivedBioError::InvalidCore => Err(BioError::InvalidCore), ArchivedBioError::NoFreeMachines => Err(BioError::NoFreeMachines), - ArchivedBioError::CoreInUse => Err(BioError::CoreInUse), + ArchivedBioError::ResourceInUse => Err(BioError::ResourceInUse), ArchivedBioError::Oom => Err(BioError::Oom), _ => Err(BioError::InternalError), } @@ -81,39 +81,40 @@ impl<'a> BioApi<'a> for Bio { Ok(()) } - unsafe fn get_core_handle(&'a mut self) -> Result>, BioError> { + unsafe fn get_core_handle(&'a self, fifo: Fifo) -> Result>, BioError> { match send_message( self.conn, - Message::new_blocking_scalar(BioOp::GetCoreHandle.to_usize().unwrap(), 0, 0, 0, 0), + Message::new_blocking_scalar(BioOp::GetCoreHandle.to_usize().unwrap(), fifo as usize, 0, 0, 0), ) .map_err(|e| >::into(e))? { - xous::Result::Scalar5(_, bank, valid, _, _) => { + xous::Result::Scalar5(_, valid, _, _, _) => { if valid == 0 { // no handles available return Ok(None); } - let base = match bank { - 0 => utralib::HW_BIO_FIFO0_BASE, - 1 => utralib::HW_BIO_FIFO1_BASE, - 2 => utralib::HW_BIO_FIFO2_BASE, - 3 => utralib::HW_BIO_FIFO3_BASE, - _ => panic!("Incorrect bank value"), + let base = match fifo { + Fifo::Fifo0 => utralib::HW_BIO_FIFO0_BASE, + Fifo::Fifo1 => utralib::HW_BIO_FIFO1_BASE, + Fifo::Fifo2 => utralib::HW_BIO_FIFO2_BASE, + Fifo::Fifo3 => utralib::HW_BIO_FIFO3_BASE, }; // the actual mapping is requested in the process space of the caller: this allows // us to directly interact with the BIO without having to use a syscall. - let virtual_page = xous::map_memory( + if let Ok(virtual_page) = xous::map_memory( MemoryAddress::new(base), None, - utralib::HW_BIO_FIFO1_MEM_LEN, + utralib::HW_BIO_FIFO0_MEM_LEN, xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map BIO memory handle"); - Ok(Some(CoreHandle::new( - self.conn, - virtual_page.as_ptr() as usize, - arbitrary_int::u2::from_u32(bank as u32), - ))) + ) { + Ok(Some(CoreHandle::new( + self.conn, + virtual_page.as_ptr() as usize, + arbitrary_int::u2::from_u32(fifo as u32), + ))) + } else { + Err(BioError::Oom) + } } _ => unimplemented!("Unhandled return type on get_core_handle()"), } diff --git a/libs/bao1x-hal/src/bio_hw.rs b/libs/bao1x-hal/src/bio_hw.rs index 475343490..8519b5a5c 100644 --- a/libs/bao1x-hal/src/bio_hw.rs +++ b/libs/bao1x-hal/src/bio_hw.rs @@ -403,8 +403,8 @@ impl<'a> BioApi<'a> for BioSharedState { offset: usize, config: CoreConfig, ) -> Result, BioError> { - if self.core_config[core as usize].is_none() { - return Err(BioError::CoreInUse); + if self.core_config[core as usize].is_some() { + return Err(BioError::ResourceInUse); } self.core_config[core as usize] = Some(config); self.load_code(code, offset, core); @@ -453,7 +453,7 @@ impl<'a> BioApi<'a> for BioSharedState { prev_freq } - unsafe fn get_core_handle(&'a mut self) -> Result>, BioError> { + unsafe fn get_core_handle(&'a self, _fifo: Fifo) -> Result>, BioError> { unimplemented!("This is managed by the main loop server, not the hardware interface"); } @@ -467,11 +467,30 @@ impl<'a> BioApi<'a> for BioSharedState { CoreRunSetting::Unchanged => (), } } + let core_code = start_mask; start_mask = start_mask | start_mask << 4 | start_mask << 8; stop_mask = stop_mask | stop_mask << 4 | stop_mask << 8; let ctrl = self.bio.r(bio_bdma::SFR_CTRL); self.bio.wo(bio_bdma::SFR_CTRL, (ctrl & !stop_mask) | start_mask); + + let mut timeout = 0; + loop { + let ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL) & 0xFF0; + if ctrl == 0 { + break; + } + timeout += 1; + if timeout > 100000 { + crate::println!("Timeout on set_core_run_states: req {:x} != rbk {:x}", core_code, ctrl); + break; + } + } + let check = self.bio.r(utra::bio_bdma::SFR_CTRL); + if check != core_code { + crate::println!("run-state check failed: {:x}", check); + } + Ok(()) }