From 3153d6aa665ab8fb54f1464b51047c8e601a22bc Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 28 Apr 2021 04:15:25 +0800 Subject: [PATCH] intermediate progress on suspend/resume --- Cargo.lock | 1 + emulation/renode.svd | 33 +++-- services/susres/Cargo.toml | 1 + services/susres/src/api.rs | 26 +++- services/susres/src/main.rs | 273 ++++++++++++++++++++++++++++++++++-- svd2utra/examples/soc.svd | 33 +++-- 6 files changed, 336 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7f2b25860..b37c958be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1339,6 +1339,7 @@ dependencies = [ "rkyv 0.4.3", "utralib", "xous", + "xous-ipc", "xous-names", ] diff --git a/emulation/renode.svd b/emulation/renode.svd index 1fa59b27e..03bb1e588 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -2275,10 +2275,25 @@ indicates we are on the resume half of a suspend/resume.]]> - INTERRUPT + POWERDOWN 0x001c 0x00 32 + + + powerdown + 0 + [0:0] + 0 + + + + + + INTERRUPT + 0x0020 + 0x00 + 32 interrupt @@ -2294,7 +2309,7 @@ suspend/resume from an interrupt context.]]> EV_STATUS - 0x0020 + 0x0024 0x00 32 @@ -2311,7 +2326,7 @@ Writes to this register have no effect.]]> EV_PENDING - 0x0024 + 0x0028 0x00 32 @@ -2329,7 +2344,7 @@ edge.]]> EV_ENABLE - 0x0028 + 0x002c 0x00 32 @@ -2345,7 +2360,7 @@ register to disable individual events.]]> 0 - 0x2c + 0x30 registers @@ -3086,7 +3101,7 @@ inject too fast.]]> SEED1 0x0000 - 0x54a7f2ac + 0x4a3e2334 32 @@ -3101,7 +3116,7 @@ inject too fast.]]> SEED0 0x0004 - 0x7c67acbc + 0x2fa94a6a 32 diff --git a/services/susres/Cargo.toml b/services/susres/Cargo.toml index 0d92c8cb5..817b28e04 100644 --- a/services/susres/Cargo.toml +++ b/services/susres/Cargo.toml @@ -11,6 +11,7 @@ log-server = { path = "../log-server" } xous-names = { path = "../xous-names" } log = "0.4" +xous-ipc = {path="../../xous-ipc"} num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} diff --git a/services/susres/src/api.rs b/services/susres/src/api.rs index 26acc7836..f7b0dc9c8 100644 --- a/services/susres/src/api.rs +++ b/services/susres/src/api.rs @@ -2,6 +2,7 @@ use utralib::generated::*; pub(crate) const SERVER_NAME_SUSRES: &str = "_Suspend/resume manager_"; +pub(crate) const SERVER_NAME_EXEC_GATE: &str = "_Suspend/resume execution gate_"; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { @@ -14,13 +15,36 @@ pub(crate) enum Opcode { /// indicate we're ready to suspend SuspendReady, + + /// from the timeout thread + SuspendTimeout, + + /// exit the server + Quit, +} + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub(crate) struct ScalarHook { + pub sid: (u32, u32, u32, u32), + pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API) + pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking. +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum SuspendEventCallback { + Event, // this contains a token as well which must be returned to indicate you're ready for the suspend + Drop, } #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum ExecGateOpcode { - SuspendNow, + SuspendingNow, + Drop, } +///////////////////////////////////////////////////////////////////////// +/////////////////////////// suspend-resume hardware management primitives +///////////////////////////////////////////////////////////////////////// #[cfg(target_os = "none")] #[derive(Debug, Copy, Clone)] pub enum RegOrField { diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index 293a043f5..1e0432595 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -4,18 +4,37 @@ mod api; use api::*; -use num_traits::FromPrimitive; +use num_traits::{ToPrimitive, FromPrimitive}; +use xous_ipc::Buffer; +use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack}; +use core::sync::atomic::{AtomicU32, AtomicBool, Ordering}; #[cfg(target_os = "none")] mod implementation { use utralib::generated::*; use crate::api::*; + use crate::SHOULD_RESUME; + use core::sync::atomic::{AtomicBool, Ordering}; - fn handle_irq(_irq_no: usize, arg: *mut usize) { + // we do all the suspend/resume coordination in an interrupt context + // the process state is pushed to main memory prior to entering this routine, so it's staged for a resume + // on a clean suspend then resume, the loader will queue up this interrupt to be the first thing to + // run on Xous resume, with a bit in the RESUME register set. + fn susres_handler(_irq_no: usize, arg: *mut usize) { let sr = unsafe{ &mut *(arg as *mut SusResHw) }; - // dummy routine for now + // clear the pending interrupt sr.csr.wfo(utra::susres::EV_PENDING_SOFT_INT, 1); - } + + if sr.csr.rf(utra::susres::STATE_RESUME) == 0 { + // power the system down - this should result in an almost immediate loss of power + sr.csr.wfo(utra::susres::POWERDOWN_POWERDOWN, 1); + + loop {} // block forever here + } else { + // this unblocks the threads waiting on the resume + SHOULD_RESUME.store(true, Ordering::Relaxed); + } + } // leaving this context will initiate the resume code pub struct SusResHw { /// our CSR @@ -56,17 +75,45 @@ mod implementation { loader_stack, }; - // check that the marker has been zerod by map_memory - // TODO + // check that the marker has been zero'd by map_memory. Once this passes we can probably get rid of this code. + let check_marker = marker.as_ptr() as *const [u32; 1024]; + for words in 0..1024 { + if unsafe{(*check_marker)[words]} != 0 { + log::error!("marker had non-zero entry: 0x{:x} @ 0x{:x}", unsafe{(*check_marker)[words]}, words); + } + } xous::claim_interrupt( utra::susres::SUSRES_IRQ, - handle_irq, + susres_handler, (&mut sr) as *mut SusResHw as *mut usize, ).expect("couldn't claim IRQ"); sr } + + pub fn do_suspend(&mut self, _forced: bool) { + // make sure we're able to handle a soft interrupt + self.csr.wfo(utra::susres::EV_ENABLE_SOFT_INT, 1); + + // ensure that the resume bit is not set + self.csr.wfo(utra::susres::STATE_RESUME, 0); + + // setup the clean suspend marker, note if things were forced + + // set a wakeup alarm + + // trigger an interrupt to process the final suspend bits + self.csr.wfo(utra::susres::INTERRUPT_INTERRUPT, 1); + + // SHOULD_RESUME will be set true by the interrupt context when it re-enters from resume + while !SHOULD_RESUME.load(Ordering::Relaxed) { + xous::yield_slice(); + } + } + pub fn do_resume(&mut self, _forced: bool) { + // do the resume stuffs here + } } } @@ -79,9 +126,81 @@ mod implementation { pub fn new() -> Self { SusResHw {} } + pub fn do_suspend(&mut self, _forced: bool) { + } + pub fn do_resume(&mut self, _forced: bool) { + } } } +#[derive(Copy, Clone, Debug)] +struct ScalarCallback { + server_to_cb_cid: CID, + cb_to_client_cid: CID, + cb_to_client_id: u32, + ready_to_suspend: bool, + token: u32, +} + +enum TimeoutOpcode { + Run, + Drop, +} + +static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(250); +static TIMEOUT_CONN: AtomicU32 = AtomicU32::new(0); +pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { + let sid = xous::SID::from_u32(sid0, sid1, sid2, sid3); + let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + TimeoutOpcode::Run => { + ticktimer.sleep_ms(TIMEOUT_TIME.load(Ordering::Relaxed) as u64).unwrap(); + match send_message(conn as u32, + Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0) + ) { + Err(xous::Error::ServerNotFound) => break, + Ok(xous::Result::Ok) => {}, + _ => panic!("unhandled error in status pump thread") + } + } + TimeoutOpcode::Drop => { + break + } + } + } + unsafe{xous::disconnect(TIMEOUT_CONN.load(Ordering::Relaxed))}; + TIMEOUT_CONN.store(0, Ordering::Relaxed); + xous::destroy_server(sid); +} + +static SHOULD_RESUME: AtomicBool = AtomicBool::new(false); +pub fn execution_gate() { + let xns = xous_names::XousNames::new().unwrap(); + let execgate_sid = xns.register_name(api::SERVER_NAME_EXEC_GATE).expect("can't register execution gate"); + log::trace!("execution_gate registered with NS -- {:?}", execgate_sid); + + loop { + let msg = xous::recieve_message(execgate_sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + // the entire purpose of SupendingNow is to block the thread that sent the message, until we're ready to resume. + ExecGateOpcode::SuspendingNow => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + while !SHOULD_RESUME.load(Ordering::Relaxed) { + xous::yield_slice(); + } + }), + ExecGateOpcode::Drop => { + break; + } + None => { + log::error!("received unknown opcode in execution_gate"); + } + } + } + xous::destroy_server(execgate_sid); +} + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -90,24 +209,154 @@ fn xmain() -> ! { let xns = xous_names::XousNames::new().unwrap(); let susres_sid = xns.register_name(api::SERVER_NAME_SUSRES).expect("can't register server"); - log::trace!("registered with NS -- {:?}", susres_sid); + log::trace!("main loop registered with NS -- {:?}", susres_sid); + + // make a connection for the timeout thread to wake us up + let timeout_incoming_conn = xous::connect(susres_sid).unwrap(); + TIMEOUT_CONN.store(timeout_incoming_conn, Ordering::Relaxed); + // allocate a private server ID for the timeout thread, it's not registered with the name server + let timeout_sid = xous::create_server().unwrap(); + let (sid0, sid1, sid2, sid3) = timeout_sid.to_u32(); + xous::create_thread_4(timeout_thread, sid0, sid1, sid2, sid3).expect("couldn't create timeout thread"); + let timeout_outgoing_conn = xous::connect(timeout_sid).expect("couldn't connect to our timeout thread"); let susres_hw = implementation::SusResHw::new(); + let mut suspend_requested = false; + let mut timeout_pending = false; + let mut suspend_subscribers: [Option; 32] = [None; 32]; loop { let msg = xous::receive_message(susres_sid).unwrap(); - /* match FromPrimitive::from_usize(msg.body.id()) { - None => { - log::error!("couldn't convert opcode"); + Opcode::SuspendEventSubscribe => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let hookdata = buffer.to_original::().unwrap(); + do_hook(hookdata, &mut suspend_subscribers); + }, + Opcode::SuspendReady => msg_scalar_unpack!(msg, token, _, _, _, { + if !suspend_requested { + log::error!("received a SuspendReady message when a suspend wasn't pending. Ignoring."); + continue; + } + if token >= suspend_subscribers.len() { + panic!("received a SuspendReady token that's out of range"); + } + if let Some(scb) = suspend_subscribers[token] { + if scb.ready_to_suspend { + log::error!("received a duplicate SuspendReady token: {} from {:?}", token, scb); + } + scb.ready_to_suspend = true; + + let mut all_ready = true; + for maybe_sub in suspend_subscribers.iter() { + if Some(sub) = maybe_sub { + if sub.ready_to_suspend == false { + all_ready = false; + break; + } + } + } + if all_ready { + susres_hw.do_suspend(false); + // when do_suspend() returns, it means we've resumed + suspend_requested = false; + + //// QUESTION: should we interlock the exec thread until after the do_resume() hardware callback is done? i think so... + + susres_hw.do_resume(false); + } + } else { + panic!("received an invalid token that does not map to a registered suspend listener"); + } + }), + Opcode::SuspendRequest => { + suspend_requested = true; + // clear the resume gate + SHOULD_RESUME.store(false, Ordering::Relaxed); + // clear the ready to suspend flag + for maybe_sub in suspend_subscribers.iter_mut() { + if Some(sub) = maybe_sub { + *sub.ready_to_suspend = false; + } + } + // do we want to start the timeout before or after sending the notifications? hmm. 🤔 + timeout_pending = true; + send_message(timeout_outgoing_conn, + Message::new_scalar(TimeoutOpcode::Run.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't initiate timeout before suspend!"); + + send_event(&suspend_subscribers); + }, + Opcode::SuspendTimeout => { + if timeout_pending { + timeout_pending = false; + // force a suspend + susres_hw.do_suspend(true); + // when do_suspend() returns, it means we've resumed + suspend_requested = false; + susres_hw.do_resume(true); + } else { + // this means we did a clean suspend, we've resumed, and the timeout came back after the resume + // just ignore the message. + } + } + Opcode::Quit => { break } - }*/ + None => { + log::error!("couldn't convert opcode"); + } + } } // clean up our program + unhook(&mut suspend_subscribers); log::trace!("main loop exit, destroying servers"); xns.unregister_server(susres_sid).unwrap(); xous::destroy_server(susres_sid).unwrap(); log::trace!("quitting"); xous::terminate_process(); loop {} } + +fn do_hook(hookdata: ScalarHook, cb_conns: &mut [Option; 32]) { + let (s0, s1, s2, s3) = hookdata.sid; + let sid = xous::SID::from_u32(s0, s1, s2, s3); + let server_to_cb_cid = xous::connect(sid).unwrap(); + let mut cb_dat = ScalarCallback { + server_to_cb_cid, + cb_to_client_cid: hookdata.cid, + cb_to_client_id: hookdata.id, + ready_to_suspend: false, + token: 0, + }; + for i in 0..cb_conns.len() { + if cb_conns[i].is_none() { + cb_dat.token = i as u32; + cb_conns[i] = Some(cb_dat); + break; + } + } + if i >= cb_conns.len() { + log::error!("ran out of space registering callback"); + } +} +fn unhook(cb_conns: &mut [Option; 32]) { + for entry in cb_conns.iter_mut() { + if let Some(scb) = entry { + xous::send_message(scb.server_to_cb_cid, + xous::Message::new_blocking_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); + unsafe{xous::disconnect(scb.server_to_cb_cid).unwrap();} + } + *entry = None; + } +} +fn send_event(cb_conns: &[Option; 32]) { + for entry in cb_conns.iter() { + if let Some(scb) = entry { + xous::send_message(scb.server_to_cb_cid, + xous::Message::new_scalar(SuspendEventCallback::Event.to_usize().unwrap(), + scb.cb_to_client_cid as usize, scb.cb_to_client_id as usize, scb.token as usize, 0) + ).unwrap(); + }; + } +} diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 1fa59b27e..03bb1e588 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -2275,10 +2275,25 @@ indicates we are on the resume half of a suspend/resume.]]> - INTERRUPT + POWERDOWN 0x001c 0x00 32 + + + powerdown + 0 + [0:0] + 0 + + + + + + INTERRUPT + 0x0020 + 0x00 + 32 interrupt @@ -2294,7 +2309,7 @@ suspend/resume from an interrupt context.]]> EV_STATUS - 0x0020 + 0x0024 0x00 32 @@ -2311,7 +2326,7 @@ Writes to this register have no effect.]]> EV_PENDING - 0x0024 + 0x0028 0x00 32 @@ -2329,7 +2344,7 @@ edge.]]> EV_ENABLE - 0x0028 + 0x002c 0x00 32 @@ -2345,7 +2360,7 @@ register to disable individual events.]]> 0 - 0x2c + 0x30 registers @@ -3086,7 +3101,7 @@ inject too fast.]]> SEED1 0x0000 - 0x54a7f2ac + 0x4a3e2334 32 @@ -3101,7 +3116,7 @@ inject too fast.]]> SEED0 0x0004 - 0x7c67acbc + 0x2fa94a6a 32