Merge pull request #872 from betrusted-io/susres

suspend/resume improvements
This commit is contained in:
bunnie 2026-05-08 03:20:33 +08:00 committed by GitHub
commit 166e61df54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 81 additions and 48 deletions

View file

@ -9,7 +9,7 @@
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateSet("baremetal", "dabao", "baosec", "bootloader", "kernel", "loader", "swap", "apps", "boot1", "alt-boot1", "baosec-lite")] [ValidateSet("baremetal", "dabao", "baosec", "bootloader", "kernel", "loader", "swap", "apps", "boot1", "alt-boot1", "baosec-lite", "os")]
[string]$Config, [string]$Config,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
@ -54,6 +54,10 @@ $configurations = @{
@{ Image = "bao1x-boot1.img"; FunctionCode = "boot1" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" }, @{ Image = "bao1x-boot1.img"; FunctionCode = "boot1" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" },
@{ Image = "bao1x-alt-boot1.img"; FunctionCode = "loader" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" } @{ Image = "bao1x-alt-boot1.img"; FunctionCode = "loader" ; TargetDir = "target\riscv32imac-unknown-none-elf\release" }
) )
"os" = @(
@{ Image = "xous.img"; FunctionCode = "kernel" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" }
@{ Image = "swap.img"; FunctionCode = "swap" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" }
)
"kernel" = @( "kernel" = @(
@{ Image = "xous.img"; FunctionCode = "kernel" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" } @{ Image = "xous.img"; FunctionCode = "kernel" ; TargetDir = "target\riscv32imac-unknown-xous-elf\release" }
) )

View file

@ -52,8 +52,8 @@ pub trait BioApi<'a> {
fn update_bio_freq(&mut self, freq: u32) -> u32; fn update_bio_freq(&mut self, freq: u32) -> u32;
/// Called before frequency changes to activate any application-specific callbacks /// Called before frequency changes to activate any application-specific callbacks
/// to avoid glitches. /// to avoid glitches. `into_wfi` specifies if we're about to enter a WFI state.
fn prep_freq_change(&mut self); fn prep_freq_change(&mut self, into_wfi: bool);
/// Returns the frequency, in Hz, of the incoming clock to the BIO cores. /// Returns the frequency, in Hz, of the incoming clock to the BIO cores.
fn get_bio_freq(&self) -> u32; fn get_bio_freq(&self) -> u32;

View file

@ -134,10 +134,16 @@ impl<'a> BioApi<'a> for Bio {
} }
} }
fn prep_freq_change(&mut self) { fn prep_freq_change(&mut self, into_wfi: bool) {
send_message( send_message(
self.conn, self.conn,
Message::new_blocking_scalar(BioOp::PrepFreqChange.to_usize().unwrap(), 0, 0, 0, 0), Message::new_blocking_scalar(
BioOp::PrepFreqChange.to_usize().unwrap(),
if into_wfi { 1 } else { 0 },
0,
0,
0,
),
) )
.ok(); .ok();
} }

View file

@ -405,10 +405,12 @@ impl BioSharedState {
// mask in the core that is now listening to the clock // mask in the core that is now listening to the clock
let existing_extclk = self.bio.rf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK); let existing_extclk = self.bio.rf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK);
self.bio.rmwf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK, (1 << core as u32) | existing_extclk); self.bio.rmwf(bio_bdma::SFR_EXTCLOCK_USE_EXTCLK, (1 << core as u32) | existing_extclk);
crate::println!("core: {:?} - extclock {:x}", core, self.bio.r(bio_bdma::SFR_EXTCLOCK)); // crate::println!("core: {:?} - extclock {:x}", core, self.bio.r(bio_bdma::SFR_EXTCLOCK));
return None; return None;
} }
}; };
#[cfg(feature = "std")]
log::debug!("config: {:?}, int {}, fraq {}, actual {}", config, div_int, div_frac, actual_freq);
let sfr_value = (div_int as u32) << 16 | (div_frac as u32) << 8; let sfr_value = (div_int as u32) << 16 | (div_frac as u32) << 8;
match core { match core {
BioCore::Core0 => { BioCore::Core0 => {
@ -494,6 +496,8 @@ impl<'a> BioApi<'a> for BioSharedState {
fn update_bio_freq(&mut self, freq: u32) -> u32 { fn update_bio_freq(&mut self, freq: u32) -> u32 {
let prev_freq = self.fclk_freq_hz; let prev_freq = self.fclk_freq_hz;
self.fclk_freq_hz = freq; self.fclk_freq_hz = freq;
#[cfg(feature = "std")]
log::debug!("new freq: {}, old freq: {}", freq, prev_freq);
for (i, config) in self.core_config.clone().iter().enumerate() { for (i, config) in self.core_config.clone().iter().enumerate() {
if let Some(config) = config { if let Some(config) = config {
self.apply_config(config, i.into()); self.apply_config(config, i.into());
@ -502,7 +506,7 @@ impl<'a> BioApi<'a> for BioSharedState {
prev_freq prev_freq
} }
fn prep_freq_change(&mut self) { fn prep_freq_change(&mut self, _wfi: bool) {
// at the hw level, there's nothing to do; a handler at the `std` level is invoked instead // at the hw level, there's nothing to do; a handler at the `std` level is invoked instead
} }

View file

@ -637,7 +637,9 @@ impl ClockManagerImpl {
self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32); self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32);
wfi_debug("internal osc"); wfi_debug("internal osc");
// don't divide fclk -- allows BIO to keep operating at 48MHz rate // don't divide fclk -- allows BIO to keep operating at 48MHz rate
self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0700_01ff); self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0000_ffff);
self.sysctrl.wo(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0, 0x0000_ffff);
self.sysctrl.wo(utra::sysctrl::SFR_CGUSET, 0x32);
// lower core voltage to 0.7v // lower core voltage to 0.7v
self.ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420002); self.ao_sysctrl.wo(utra::ao_sysctrl::SFR_PMUTRM0CSR, 0x08420002);

View file

@ -95,7 +95,7 @@ impl Hal {
0, 0,
), ),
) )
.expect("Couldn't setup preemption state"); .ok();
} }
} }

View file

@ -132,7 +132,11 @@ fn bio_service(clk_freq: u32) {
if let Some(_scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() { if let Some(_scalar) = msg_opt.as_mut().unwrap().body.scalar_message_mut() {
// pause rendering while the clock transitions, avoiding 'bright glitches' // pause rendering while the clock transitions, avoiding 'bright glitches'
#[cfg(feature = "oem-baosec-lite")] #[cfg(feature = "oem-baosec-lite")]
xous::send_message(led_conn, xous::Message::new_blocking_scalar(128, 0, 0, 0, 0)).ok(); xous::send_message(
led_conn,
xous::Message::new_blocking_scalar(128, _scalar.arg1, 0, 0, 0),
)
.ok();
} }
} }

View file

@ -1,4 +1,5 @@
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, atomic::AtomicBool};
use num_traits::*; use num_traits::*;
use utralib::*; use utralib::*;
@ -51,7 +52,9 @@ fn susres_service() {
let timeout_sid = xous::create_server().unwrap(); let timeout_sid = xous::create_server().unwrap();
let timeout_outgoing_conn = xous::connect(timeout_sid).unwrap(); let timeout_outgoing_conn = xous::connect(timeout_sid).unwrap();
let timeout_pending = Arc::new(AtomicBool::new(false));
std::thread::spawn({ std::thread::spawn({
let timeout_pending = timeout_pending.clone();
let timeout_sid = timeout_sid.clone(); let timeout_sid = timeout_sid.clone();
// safety: this thread will read-only from the susres timer fields, and thus its operations // safety: this thread will read-only from the susres timer fields, and thus its operations
// are thread-safe // are thread-safe
@ -70,21 +73,30 @@ fn susres_service() {
} }
let start = get_hw_time(&hw); let start = get_hw_time(&hw);
let timeout = TIMEOUT_TIME.load(Ordering::Relaxed); // ignore updates to timeout once we're waiting let timeout = TIMEOUT_TIME.load(Ordering::Relaxed); // ignore updates to timeout once we're waiting
let mut reached_timeout = true;
while ((get_hw_time(&hw) - start) as u32) < timeout { while ((get_hw_time(&hw) - start) as u32) < timeout {
// log::info!("delta t: {}", (get_hw_time(hw) - start) as u32); // log::info!("delta t: {}", (get_hw_time(hw) - start) as u32);
xous::yield_slice(); xous::yield_slice();
if !timeout_pending.load(Ordering::SeqCst) {
// remember the check here so we don't have inconsistent state with the send_message
// later
reached_timeout = false;
break;
}
}
if reached_timeout {
log::trace!("HW timeout reached");
send_message(
susres_conn,
Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0),
)
.ok();
} }
log::trace!("HW timeout reached");
send_message(
susres_conn,
Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0),
)
.ok();
} }
} }
}); });
let mut suspend_requested: Option<Sender> = None; let mut suspend_requested: Option<Sender> = None;
let mut timeout_pending = false;
let mut reboot_requested: bool = false; let mut reboot_requested: bool = false;
let mut allow_suspend = true; let mut allow_suspend = true;
@ -173,10 +185,14 @@ fn susres_service() {
} }
// note: we must have at least one `Last` subscriber for this logic to work! // note: we must have at least one `Last` subscriber for this logic to work!
if all_ready && current_op_order == xous_api_susres::SuspendOrder::Last { if all_ready && current_op_order == xous_api_susres::SuspendOrder::Last {
// this stops the timeout timer from going
timeout_pending.store(false, Ordering::SeqCst);
// allow the state to propagate to the timer
xous::yield_slice();
// turn off preemption // turn off preemption
hal.set_preemption(false); hal.set_preemption(false);
log::info!("all callbacks reporting in, doing suspend"); log::info!("all callbacks reporting in, doing suspend");
timeout_pending = false;
use bao1x_api::bio::BioApi; use bao1x_api::bio::BioApi;
let mut bio = bao1x_hal::bio::Bio::new(); let mut bio = bao1x_hal::bio::Bio::new();
@ -210,35 +226,29 @@ fn susres_service() {
// - The update_bio_freq routine is being "too clever" and compensating for the fd // - The update_bio_freq routine is being "too clever" and compensating for the fd
// setting. But it seems like the routine just takes the putative fclk setting as // setting. But it seems like the routine just takes the putative fclk setting as
// the argument to update_bio_freq(), I don't see any compensation happening... // the argument to update_bio_freq(), I don't see any compensation happening...
bio.prep_freq_change(); bio.prep_freq_change(true);
if fclk_fd_backup == 0xff { bio.update_bio_freq(48_000_000);
bio.update_bio_freq(48_000_000);
} else {
bio.update_bio_freq(24_000_000);
}
// clk_mgr.set_fclk_fd(0xff); // set divider to 1:1 ratio
clk_mgr.wfi(); clk_mgr.wfi();
// ~~~ time passes, but we're on carbonite so we don't notice ~~~ // ~~~ time passes, but we're on carbonite so we don't notice ~~~
// clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD before restoring potentially bio.prep_freq_change(false);
clk_mgr.set_fclk_fd(fclk_fd_backup); // restore the FD for fclk, as it is side-effected by WFI
// fast PLL setting // fast PLL setting
bio.prep_freq_change();
clk_mgr.restore_wfi(); clk_mgr.restore_wfi();
bio.update_bio_freq(clk_mgr.fclk); bio.update_bio_freq(clk_mgr.fclk);
// when wfi() returns, it means we've resumed // when wfi() returns, it means we've resumed
let sender = suspend_requested let sender = suspend_requested
.take() .take()
.expect("suspend was requested, but no requestor is on record!"); .expect("suspend_requested was checked to be Some() at entry to the loop, but now somehow it's now None!");
log_server::resume(); // log server is a special case, in order to avoid circular dependencies log_server::resume(); // log server is a special case, in order to avoid circular dependencies
// this now allows all other threads to commence // this now allows all other threads to commence
log::info!("low-level resume done, restoring execution"); log::info!("low-level resume done, restoring execution");
for pid in gated_pids.drain(..) { for pid in gated_pids.drain(..) {
xous::return_scalar(pid, 0) xous::return_scalar(pid, 0).ok();
.expect("couldn't return dummy message to unblock execution");
} }
// restore preemption // restore preemption
hal.set_preemption(true); hal.set_preemption(true);
@ -265,13 +275,16 @@ fn susres_service() {
} }
}), }),
Some(Opcode::SuspendRequest) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { Some(Opcode::SuspendRequest) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
log::info!("registered suspend listeners:"); log::debug!("registered suspend listeners:");
for sub in suspend_subscribers.iter() { for sub in suspend_subscribers.iter() {
log::info!("{:?}", sub); log::debug!("{:?}", sub);
} }
// if the 2-second timeout is still pending from a previous suspend, deny the suspend // if the timeout is still pending from a previous suspend, deny the suspend
// request. ...just don't suspend that quickly after resuming??? // request. ...just don't suspend that quickly after resuming???
if allow_suspend && !timeout_pending { //
// note that short-circuit eval means timeout_pending is not side-effected if
// allow_spend is false. Do not change the order of these operations!
if allow_suspend && !timeout_pending.swap(true, Ordering::SeqCst) {
suspend_requested = Some(msg.sender); suspend_requested = Some(msg.sender);
// clear the ready to suspend flag and failed to suspend flag // clear the ready to suspend flag and failed to suspend flag
@ -279,11 +292,10 @@ fn susres_service() {
sub.ready_to_suspend = false; sub.ready_to_suspend = false;
sub.failed_to_suspend = false; sub.failed_to_suspend = false;
} }
// do we want to start the timeout before or after sending the notifications? hmm. 🤔
timeout_pending = true; // any message to the timeout server will trigger it - it only has one function
// any message to this server will trigger it - it only has one function xous::try_send_message(timeout_outgoing_conn, Message::new_scalar(0, 0, 0, 0, 0))
send_message(timeout_outgoing_conn, Message::new_scalar(0, 0, 0, 0, 0)) .ok();
.expect("couldn't initiate timeout before suspend!");
current_op_order = xous_api_susres::SuspendOrder::Early; current_op_order = xous_api_susres::SuspendOrder::Early;
let mut at_least_one_event_sent = false; let mut at_least_one_event_sent = false;
@ -305,12 +317,11 @@ fn susres_service() {
} }
}), }),
Some(Opcode::SuspendTimeout) => { Some(Opcode::SuspendTimeout) => {
if timeout_pending { if timeout_pending.swap(false, Ordering::SeqCst) {
// record which tokens had not reported in // record which tokens had not reported in
for sub in suspend_subscribers.iter_mut() { for sub in suspend_subscribers.iter_mut() {
sub.failed_to_suspend = !sub.ready_to_suspend; sub.failed_to_suspend = !sub.ready_to_suspend;
} }
timeout_pending = false;
log::warn!( log::warn!(
"Suspend timed out, forcing an unclean suspend at stage {:?}", "Suspend timed out, forcing an unclean suspend at stage {:?}",
current_op_order current_op_order
@ -330,20 +341,22 @@ fn susres_service() {
} }
} }
let sender = suspend_requested
.take()
.expect("suspend was requested, but no requestor is on record!");
for pid in gated_pids.drain(..) { for pid in gated_pids.drain(..) {
xous::return_scalar(pid, 0) xous::return_scalar(pid, 0)
.expect("couldn't return dummy message to unblock execution"); .expect("couldn't return dummy message to unblock execution");
} }
// this unblocks the requestor of the suspend // this unblocks the requestor of the suspend
xous::return_scalar(sender, 0).ok(); if let Some(sender) = suspend_requested.take() {
xous::return_scalar(sender, 0).ok();
} else {
log::error!("Suspend requester disappeared - race condition somewhere!");
}
} else { } else {
log::info!("clean suspend timeout received, ignoring"); log::warn!("clean suspend timeout received, ignoring");
// this means we did a clean suspend, we've resumed, and the timeout came back after // this means we did a clean suspend, we've resumed, and the timeout came back after
// the resume just ignore the message. // the resume just ignore the message. It's a warning because this message should,
// in practice, never be generated due to the timeout_pending semaphore.
} }
} }
Some(Opcode::WasSuspendClean) => msg_blocking_scalar_unpack!(msg, token, _, _, _, { Some(Opcode::WasSuspendClean) => msg_blocking_scalar_unpack!(msg, token, _, _, _, {
@ -448,7 +461,7 @@ fn send_event(
order: xous_api_susres::SuspendOrder, order: xous_api_susres::SuspendOrder,
) -> (bool, xous_api_susres::SuspendOrder) { ) -> (bool, xous_api_susres::SuspendOrder) {
let mut at_least_one_event_sent = false; let mut at_least_one_event_sent = false;
log::info!("Sending suspend to {:?} stage", order); log::debug!("Sending suspend to {:?} stage", order);
/* /*
// abortive attempt to get suspend to shut down the system. Doesn't work, results in a panic because too many messages are still moving around. // abortive attempt to get suspend to shut down the system. Doesn't work, results in a panic because too many messages are still moving around.
#[cfg(not(target_os = "xous"))] #[cfg(not(target_os = "xous"))]