intermediate progress on suspend/resume

This commit is contained in:
bunnie 2021-04-28 04:15:25 +08:00
parent 46741b5162
commit 3153d6aa66
6 changed files with 336 additions and 31 deletions

1
Cargo.lock generated
View file

@ -1339,6 +1339,7 @@ dependencies = [
"rkyv 0.4.3",
"utralib",
"xous",
"xous-ipc",
"xous-names",
]

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-04-27 19:37:49]]></description>
<description><![CDATA[Litex SoC 2021-04-28 03:19:07]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<register>
<name>GIT_DIRTY</name>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<resetValue>0x01</resetValue>
<size>32</size>
<fields>
<field>
@ -2275,10 +2275,25 @@ indicates we are on the resume half of a suspend/resume.]]></description>
</fields>
</register>
<register>
<name>INTERRUPT</name>
<name>POWERDOWN</name>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>powerdown</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Write a `1` to force an immediate powerdown. Use with care.]]></description>
</field>
</fields>
</register>
<register>
<name>INTERRUPT</name>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>interrupt</name>
@ -2294,7 +2309,7 @@ suspend/resume from an interrupt context.]]></description>
<name>EV_STATUS</name>
<description><![CDATA[This register contains the current raw level of the soft_int event trigger.
Writes to this register have no effect.]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2311,7 +2326,7 @@ Writes to this register have no effect.]]></description>
<name>EV_PENDING</name>
<description><![CDATA[When a soft_int event occurs, the corresponding bit will be set in this
register. To clear the Event, set the corresponding bit in this register.]]></description>
<addressOffset>0x0024</addressOffset>
<addressOffset>0x0028</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2329,7 +2344,7 @@ edge.]]></description>
<name>EV_ENABLE</name>
<description><![CDATA[This register enables the corresponding soft_int events. Write a ``0`` to this
register to disable individual events.]]></description>
<addressOffset>0x0028</addressOffset>
<addressOffset>0x002c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2345,7 +2360,7 @@ register to disable individual events.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x2c</size>
<size>0x30</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -3086,7 +3101,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x54a7f2ac</resetValue>
<resetValue>0x4a3e2334</resetValue>
<size>32</size>
<fields>
<field>
@ -3101,7 +3116,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x7c67acbc</resetValue>
<resetValue>0x2fa94a6a</resetValue>
<size>32</size>
<fields>
<field>

View file

@ -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"]}

View file

@ -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 {

View file

@ -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<ScalarCallback>; 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::<ScalarHook, _>().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<ScalarCallback>; 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<ScalarCallback>; 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<ScalarCallback>; 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();
};
}
}

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-04-27 19:37:49]]></description>
<description><![CDATA[Litex SoC 2021-04-28 03:19:07]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<register>
<name>GIT_DIRTY</name>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<resetValue>0x01</resetValue>
<size>32</size>
<fields>
<field>
@ -2275,10 +2275,25 @@ indicates we are on the resume half of a suspend/resume.]]></description>
</fields>
</register>
<register>
<name>INTERRUPT</name>
<name>POWERDOWN</name>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>powerdown</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Write a `1` to force an immediate powerdown. Use with care.]]></description>
</field>
</fields>
</register>
<register>
<name>INTERRUPT</name>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>interrupt</name>
@ -2294,7 +2309,7 @@ suspend/resume from an interrupt context.]]></description>
<name>EV_STATUS</name>
<description><![CDATA[This register contains the current raw level of the soft_int event trigger.
Writes to this register have no effect.]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2311,7 +2326,7 @@ Writes to this register have no effect.]]></description>
<name>EV_PENDING</name>
<description><![CDATA[When a soft_int event occurs, the corresponding bit will be set in this
register. To clear the Event, set the corresponding bit in this register.]]></description>
<addressOffset>0x0024</addressOffset>
<addressOffset>0x0028</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2329,7 +2344,7 @@ edge.]]></description>
<name>EV_ENABLE</name>
<description><![CDATA[This register enables the corresponding soft_int events. Write a ``0`` to this
register to disable individual events.]]></description>
<addressOffset>0x0028</addressOffset>
<addressOffset>0x002c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -2345,7 +2360,7 @@ register to disable individual events.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x2c</size>
<size>0x30</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -3086,7 +3101,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x54a7f2ac</resetValue>
<resetValue>0x4a3e2334</resetValue>
<size>32</size>
<fields>
<field>
@ -3101,7 +3116,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x7c67acbc</resetValue>
<resetValue>0x2fa94a6a</resetValue>
<size>32</size>
<fields>
<field>