Merge pull request #719 from betrusted-io/maybe-fix-usb-lockup

Maybe fix usb lockup
This commit is contained in:
bunnie 2025-11-03 18:35:48 +08:00 committed by GitHub
commit ef0f7848e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 56 additions and 31 deletions

View file

@ -77,9 +77,10 @@ loader-baosec = [
"ux-api/loader-baosec", "ux-api/loader-baosec",
"sigcheck", "sigcheck",
] ]
kernel-baosec = ["utralib/bao1x"] kernel = []
kernel-baosor = ["utralib/bao1x"] kernel-baosec = ["utralib/bao1x", "kernel"]
kernel-dabao = ["utralib/bao1x"] kernel-baosor = ["utralib/bao1x", "kernel"]
kernel-dabao = ["utralib/bao1x", "kernel"]
test-baosec = ["ux-api/loader-baosec", "camera-gc2145"] test-baosec = ["ux-api/loader-baosec", "camera-gc2145"]
hosted-baosec = ["std"] # emulated hardware on x86 platform hosted-baosec = ["std"] # emulated hardware on x86 platform
board-baosor = [ board-baosor = [

View file

@ -37,12 +37,7 @@ pub mod mbox;
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
pub mod rram; pub mod rram;
// no signature checking in the kernel // no signature checking in the kernel
#[cfg(all( #[cfg(all(not(feature = "hosted-baosec"), not(feature = "kernel"),))]
not(feature = "hosted-baosec"),
not(feature = "kernel-baosec"),
not(feature = "kernel-dabao"),
not(feature = "kernel-baosor")
))]
pub mod sigcheck; pub mod sigcheck;
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
pub mod sram_trim; pub mod sram_trim;

View file

@ -2251,10 +2251,12 @@ pub struct CorigineWrapper {
pub ep_out_ready: Box<[Arc<AtomicBool>]>, pub ep_out_ready: Box<[Arc<AtomicBool>]>,
pub address_is_set: Arc<AtomicBool>, pub address_is_set: Arc<AtomicBool>,
pub event: Option<CrgEvent>, pub event: Option<CrgEvent>,
pub irq_csr: AtomicCsr<u32>,
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl CorigineWrapper { impl CorigineWrapper {
pub fn new(obj: CorigineUsb) -> Self { pub fn new(obj: CorigineUsb) -> Self {
let irq_csr = obj.irq_csr.clone();
let c = Self { let c = Self {
hw: Arc::new(Mutex::new(obj)), hw: Arc::new(Mutex::new(obj)),
ep_meta: [None; CRG_EP_NUM], ep_meta: [None; CRG_EP_NUM],
@ -2264,6 +2266,7 @@ impl CorigineWrapper {
.into_boxed_slice(), .into_boxed_slice(),
event: None, event: None,
address_is_set: Arc::new(AtomicBool::new(false)), address_is_set: Arc::new(AtomicBool::new(false)),
irq_csr,
}; };
c c
} }
@ -2275,6 +2278,7 @@ impl CorigineWrapper {
ep_out_ready: self.ep_out_ready.clone(), ep_out_ready: self.ep_out_ready.clone(),
event: None, event: None,
address_is_set: self.address_is_set.clone(), address_is_set: self.address_is_set.clone(),
irq_csr: self.irq_csr.clone(),
}; };
c.ep_meta.copy_from_slice(&self.ep_meta); c.ep_meta.copy_from_slice(&self.ep_meta);
for (dst, src) in c.ep_out_ready.iter().zip(self.ep_out_ready.iter()) { for (dst, src) in c.ep_out_ready.iter().zip(self.ep_out_ready.iter()) {
@ -2290,6 +2294,13 @@ impl CorigineWrapper {
*/ */
self.hw.lock().unwrap() self.hw.lock().unwrap()
} }
pub fn disable_interrupts(&self) { self.irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 0); }
pub fn enable_interrupts(&self) {
self.irq_csr.wo(utralib::utra::irqarray1::EV_PENDING, 0xFFFF_FFFF);
self.irq_csr.wo(utralib::utra::irqarray1::EV_ENABLE, 3);
}
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -2535,6 +2546,7 @@ impl UsbBus for CorigineWrapper {
buf.len(), buf.len(),
&buf[..8.min(buf.len())] &buf[..8.min(buf.len())]
); );
self.disable_interrupts();
let addr = if let Some(addr) = self.core().get_app_buf_ptr(ep_addr.index() as u8, CRG_IN) { let addr = if let Some(addr) = self.core().get_app_buf_ptr(ep_addr.index() as u8, CRG_IN) {
// crate::println!("addr {:x}", addr); // crate::println!("addr {:x}", addr);
addr addr
@ -2574,6 +2586,7 @@ impl UsbBus for CorigineWrapper {
} }
#[cfg(feature = "verbose-debug")] #[cfg(feature = "verbose-debug")]
crate::println!("ep{} initiated {}", ep_addr.index(), buf.len()); crate::println!("ep{} initiated {}", ep_addr.index(), buf.len());
self.enable_interrupts();
Ok(buf.len()) Ok(buf.len())
} }
} }

View file

@ -74,7 +74,7 @@ verbose-debug = ["bao1x-hal/verbose-debug"]
# mutually exclusive with kernel "print-panics"; will get a DUART allocation error on boot if kernel panics are on # mutually exclusive with kernel "print-panics"; will get a DUART allocation error on boot if kernel panics are on
# sticks the DUART into the deep stack; necessary because we can't have external dependencies in usbd-device # sticks the DUART into the deep stack; necessary because we can't have external dependencies in usbd-device
# and the DUART can be banged with simple hard-coded memory offsets. # and the DUART can be banged with simple hard-coded memory offsets.
usbd-debug = ["usb-device/baochip-debug"] usbd-debug = ["usb-device/baochip-debug", "debug-print-usb"]
prototyping = [ prototyping = [
] # temporary feature-out for figuring out the code; remove this if you don't recognize it anymore ] # temporary feature-out for figuring out the code; remove this if you don't recognize it anymore

View file

@ -1,7 +1,7 @@
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::atomic::compiler_fence; use std::sync::atomic::{AtomicBool, compiler_fence};
use std::sync::atomic::{AtomicPtr, Ordering}; use std::sync::atomic::{AtomicPtr, Ordering};
use bao1x_hal::usb::driver::*; use bao1x_hal::usb::driver::*;
@ -51,6 +51,10 @@ pub struct Bao1xUsb<'a> {
// holds one HS packet - must be statically allocated in IRQ handler. Valid length is // holds one HS packet - must be statically allocated in IRQ handler. Valid length is
// passed as part of the interrupt recovery message. // passed as part of the interrupt recovery message.
pub serial_rx: [u8; SERIAL_MAX_PACKET_SIZE], pub serial_rx: [u8; SERIAL_MAX_PACKET_SIZE],
// an error reporter for the double lock condition, which we need to figure out how to handle still.
// used for debugging, the idea is to query this in userspace to try and pick up the double-lock problem
// from the interrupt handler.
pub double_lock: AtomicBool,
} }
impl<'a> Bao1xUsb<'a> { impl<'a> Bao1xUsb<'a> {
@ -104,9 +108,14 @@ impl<'a> Bao1xUsb<'a> {
hid_packet: None, hid_packet: None,
serial_port, serial_port,
serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE], serial_rx: [0u8; SERIAL_MAX_PACKET_SIZE],
double_lock: AtomicBool::new(false),
} }
} }
#[allow(dead_code)]
/// Used only when debugging the double lock problem
pub fn double_lock_detected(&self) -> bool { self.double_lock.swap(false, Ordering::SeqCst) }
pub fn init(&mut self) { pub fn init(&mut self) {
// this has to be done in `main` because we're passing a pointer to the Box'd structure, which // this has to be done in `main` because we're passing a pointer to the Box'd structure, which
// the IRQ handler can freely and safely manipulate // the IRQ handler can freely and safely manipulate
@ -124,9 +133,9 @@ impl<'a> Bao1xUsb<'a> {
self.irq_csr.wo(utra::irqarray1::EV_EDGE_TRIGGERED, 0); self.irq_csr.wo(utra::irqarray1::EV_EDGE_TRIGGERED, 0);
self.irq_csr.wo(utra::irqarray1::EV_POLARITY, 0); self.irq_csr.wo(utra::irqarray1::EV_POLARITY, 0);
self.wrapper.hw.lock().unwrap().init(); self.wrapper.core().init();
self.wrapper.hw.lock().unwrap().start(); self.wrapper.core().start();
self.wrapper.hw.lock().unwrap().update_current_speed(); self.wrapper.core().update_current_speed();
// irq must me enabled without dependency on the hw lock // irq must me enabled without dependency on the hw lock
self.irq_csr.wo(utra::irqarray1::EV_PENDING, 0xFFFF_FFFF); self.irq_csr.wo(utra::irqarray1::EV_PENDING, 0xFFFF_FFFF);
@ -145,10 +154,10 @@ impl<'a> Bao1xUsb<'a> {
// disable all interrupts so we can safely go through initialization routines // disable all interrupts so we can safely go through initialization routines
self.irq_csr.wo(utra::irqarray1::EV_ENABLE, 0); self.irq_csr.wo(utra::irqarray1::EV_ENABLE, 0);
self.wrapper.hw.lock().unwrap().reset(); self.wrapper.core().reset();
self.wrapper.hw.lock().unwrap().init(); self.wrapper.core().init();
self.wrapper.hw.lock().unwrap().start(); self.wrapper.core().start();
self.wrapper.hw.lock().unwrap().update_current_speed(); self.wrapper.core().update_current_speed();
// reset all shared data structures // reset all shared data structures
self.device.force_reset().ok(); self.device.force_reset().ok();
@ -211,7 +220,14 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) {
// #[cfg(feature = "verbose-debug")] // #[cfg(feature = "verbose-debug")]
// crate::println!("getting event"); // crate::println!("getting event");
// scoping on the hardware lock to manipulate pointer states // scoping on the hardware lock to manipulate pointer states
let mut corigine_usb = usb.wrapper.core(); let mut corigine_usb = match usb.wrapper.hw.try_lock() {
Ok(lock) => lock,
_ => {
crate::println!("double lock - this case is actually broken, stack will crash");
usb.double_lock.store(true, Ordering::SeqCst);
return;
}
};
let mut event = { let mut event = {
if corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst).is_null() { if corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst).is_null() {
// break; // break;
@ -219,7 +235,12 @@ pub(crate) fn composite_handler(_irq_no: usize, arg: *mut usize) {
break; break;
} }
let event_ptr = corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst) as usize; let event_ptr = corigine_usb.udc_event.evt_dq_pt.load(Ordering::SeqCst) as usize;
unsafe { (event_ptr as *mut EventTrbS).as_mut().expect("couldn't deref pointer") } match unsafe { (event_ptr as *mut EventTrbS).as_mut() } {
Some(ptr) => ptr,
None => {
break;
}
}
}; };
if event.dw3.cycle_bit() != corigine_usb.udc_event.ccs { if event.dw3.cycle_bit() != corigine_usb.udc_event.ccs {
break; break;

View file

@ -62,16 +62,7 @@ fn main() -> ! {
pub(crate) fn main_hw() -> ! { pub(crate) fn main_hw() -> ! {
#[cfg(feature = "usbd-debug")] #[cfg(feature = "usbd-debug")]
// bind the duart bao1x_hal::claim_duart();
let duart_mapping = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::duart::HW_DUART_BASE),
xous::MemoryAddress::new(0x3000_0000),
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map DUART");
#[cfg(feature = "usbd-debug")]
crate::println!("duart mapping: {:x?}", duart_mapping);
#[cfg(feature = "debug-print-usb")] #[cfg(feature = "debug-print-usb")]
panic::set_hook(Box::new(|info| { panic::set_hook(Box::new(|info| {
@ -288,6 +279,11 @@ pub(crate) fn main_hw() -> ! {
let msg = msg_opt.as_mut().unwrap(); let msg = msg_opt.as_mut().unwrap();
let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidCall); let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidCall);
log::debug!("{:?}", opcode); log::debug!("{:?}", opcode);
if cu.double_lock_detected() {
log::warn!(
"Double lock error detected in USB stack. Meditations: services/usb-bao1x/src/hw.rs@226 (composite_handler inner loop) and consider adding more IRQ enable/disable similar to libs/bao1x-hal/src/usb/driver.rs@2549 (write impl)"
);
}
match opcode { match opcode {
#[cfg(feature = "board-baosec")] #[cfg(feature = "board-baosec")]
Opcode::PmicIrq => match pmic.get_vbus_irq_status(&mut i2c).unwrap() { Opcode::PmicIrq => match pmic.get_vbus_irq_status(&mut i2c).unwrap() {
@ -750,7 +746,6 @@ pub(crate) fn main_hw() -> ! {
usb_send.s.as_bytes().chunks(bao1x_hal::usb::driver::CRG_UDC_APP_BUFSIZE) usb_send.s.as_bytes().chunks(bao1x_hal::usb::driver::CRG_UDC_APP_BUFSIZE)
{ {
cu.serial_port.write(&chunk).ok(); cu.serial_port.write(&chunk).ok();
cu.serial_port.flush().ok(); // just drop characters on error
} }
} }
_ => {} // silent errors _ => {} // silent errors