move the os_timer into the susres server, so we can suspend it

also clean up various syntax errors
This commit is contained in:
bunnie 2021-04-28 15:38:56 +08:00
parent 3153d6aa66
commit 3043769170
3 changed files with 93 additions and 40 deletions

View file

@ -1,12 +1,14 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
mod os_timer;
mod api;
use api::*;
use num_traits::{ToPrimitive, FromPrimitive};
use xous_ipc::Buffer;
use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack};
use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack, send_message, Message};
use core::sync::atomic::{AtomicU32, AtomicBool, Ordering};
#[cfg(target_os = "none")]
@ -44,7 +46,7 @@ mod implementation {
/// loader stack region -- this data is dirtied on every resume; claim it in this process so no others accidentally use it
loader_stack: xous::MemoryRange,
/// backing store for the ticktimer value
time_backing: Option<[u32; 2]>,
stored_time: Option<u64>,
}
impl SusResHw {
pub fn new() -> Self {
@ -70,7 +72,7 @@ mod implementation {
).expect("couldn't map clean suspend page");
let mut sr = SusResHw {
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
time_backing: None,
stored_time: None,
marker,
loader_stack,
};
@ -99,6 +101,14 @@ mod implementation {
// ensure that the resume bit is not set
self.csr.wfo(utra::susres::STATE_RESUME, 0);
// stop thread scheduling, by stopping the ticktimer
self.csr.wfo(utra::susres::CONTROL_PAUSE, 1);
self.stored_time = Some(
(self.csr.r(utra::susres::TIME0) as u64 |
(self.csr.r(utra::susres::TIME1) as u64) << 32)
+ 1 // advance by one tick, to ensure time goes up monotonically at the next resume
);
// setup the clean suspend marker, note if things were forced
// set a wakeup alarm
@ -112,7 +122,20 @@ mod implementation {
}
}
pub fn do_resume(&mut self, _forced: bool) {
// do the resume stuffs here
if let Some(time)= self.stored_time.take() {
// restore the ticktimer
self.csr.wo(utra::susres::RESUME_TIME0, time as u32);
self.csr.wo(utra::susres::RESUME_TIME1, (time >> 32) as u32);
// load the saved value -- can only be done while the timer is paused
self.csr.wo(utra::susres::CONTROL,
self.csr.ms(utra::susres::CONTROL_PAUSE, 1) |
self.csr.ms(utra::susres::CONTROL_LOAD, 1)
);
// start the timer running
self.csr.wo(utra::susres::CONTROL, 0);
} else {
panic!("Can't resume because the ticktimer value was not saved properly before suspend!")
}
}
}
@ -142,7 +165,9 @@ struct ScalarCallback {
token: u32,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
enum TimeoutOpcode {
SetCsr,
Run,
Drop,
}
@ -150,14 +175,41 @@ enum TimeoutOpcode {
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();
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
#[cfg(target_os = "none")]
use utralib::generated::*;
#[cfg(target_os = "none")]
let mut csr: Option<CSR::<u32>> = None;
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,
#[cfg(target_os = "none")]
Some(TimeoutOpcode::SetCsr) => msg_scalar_unpack!(msg, base, _, _, _, {
csr = Some(CSR::new(base as *mut u32));
}),
#[cfg(not(target_os = "none"))]
Some(TimeoutOpcode::SetCsr) => msg_scalar_unpack!(msg, base, _, _, _, {
// ignore the opcode in hosted mode
}),
Some(TimeoutOpcode::Run) => {
#[cfg(target_os = "none")]
{
// we have to re-implement the ticktimer time reading here because as we wait for the timeout,
// the ticktimer goes away! so we use the susres local copy with direct hardware ops to keep track of time in this phase
fn get_hw_time(hw: CSR::<u32>) -> u64 {
hw.r(utra::susres::TIME0) as u64 | ((hw.r(utra::susres::TIME1) as u64) << 32)
}
if let Some(hw) = csr {
let now = get_hw_time(hw);
let timeout = TIMEOUT_TIME.load(Ordering::Relaxed); // ignore updates to timeout once we're waiting
while ((get_hw_time(hw) - now) as u32) < timeout {
xous::yield_slice();
}
} else {
panic!("hardware CSR not sent to timeout_thread before it was instructed to run");
}
}
match send_message(TIMEOUT_CONN.load(Ordering::Relaxed),
Message::new_scalar(Opcode::SuspendTimeout.to_usize().unwrap(), 0, 0, 0, 0)
) {
Err(xous::Error::ServerNotFound) => break,
@ -165,9 +217,12 @@ pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
_ => panic!("unhandled error in status pump thread")
}
}
TimeoutOpcode::Drop => {
Some(TimeoutOpcode::Drop) => {
break
}
None => {
log::error!("received unknown opcode in timeout_thread!");
}
}
}
unsafe{xous::disconnect(TIMEOUT_CONN.load(Ordering::Relaxed))};
@ -176,21 +231,22 @@ pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
}
static SHOULD_RESUME: AtomicBool = AtomicBool::new(false);
static RESUME_EXEC: 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();
let msg = xous::receive_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) {
Some(ExecGateOpcode::SuspendingNow) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
while !RESUME_EXEC.load(Ordering::Relaxed) {
xous::yield_slice();
}
}),
ExecGateOpcode::Drop => {
Some(ExecGateOpcode::Drop) => {
break;
}
None => {
@ -203,6 +259,9 @@ pub fn execution_gate() {
#[xous::xous_main]
fn xmain() -> ! {
// Start the OS timer which is responsible for setting up preemption.
os_timer::init();
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Trace);
log::info!("my PID is {}", xous::process::id());
@ -217,10 +276,10 @@ fn xmain() -> ! {
// 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");
xous::create_thread_4(timeout_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).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 susres_hw = implementation::SusResHw::new();
let mut suspend_requested = false;
let mut timeout_pending = false;
@ -228,12 +287,12 @@ fn xmain() -> ! {
loop {
let msg = xous::receive_message(susres_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Opcode::SuspendEventSubscribe => {
Some(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, _, _, _, {
Some(Opcode::SuspendReady) => msg_scalar_unpack!(msg, token, _, _, _, {
if !suspend_requested {
log::error!("received a SuspendReady message when a suspend wasn't pending. Ignoring.");
continue;
@ -241,7 +300,7 @@ fn xmain() -> ! {
if token >= suspend_subscribers.len() {
panic!("received a SuspendReady token that's out of range");
}
if let Some(scb) = suspend_subscribers[token] {
if let Some(mut scb) = suspend_subscribers[token] {
if scb.ready_to_suspend {
log::error!("received a duplicate SuspendReady token: {} from {:?}", token, scb);
}
@ -249,35 +308,35 @@ fn xmain() -> ! {
let mut all_ready = true;
for maybe_sub in suspend_subscribers.iter() {
if Some(sub) = maybe_sub {
if let 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);
// this now allows all other threads to commence
RESUME_EXEC.store(true, Ordering::Relaxed);
}
} else {
panic!("received an invalid token that does not map to a registered suspend listener");
}
}),
Opcode::SuspendRequest => {
Some(Opcode::SuspendRequest) => {
suspend_requested = true;
// clear the resume gate
SHOULD_RESUME.store(false, Ordering::Relaxed);
RESUME_EXEC.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;
}
if let 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;
@ -287,7 +346,7 @@ fn xmain() -> ! {
send_event(&suspend_subscribers);
},
Opcode::SuspendTimeout => {
Some(Opcode::SuspendTimeout) => {
if timeout_pending {
timeout_pending = false;
// force a suspend
@ -295,12 +354,13 @@ fn xmain() -> ! {
// when do_suspend() returns, it means we've resumed
suspend_requested = false;
susres_hw.do_resume(true);
RESUME_EXEC.store(true, Ordering::Relaxed);
} 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 => {
Some(Opcode::Quit) => {
break
}
None => {
@ -332,18 +392,16 @@ fn do_hook(hookdata: ScalarHook, cb_conns: &mut [Option<ScalarCallback>; 32]) {
if cb_conns[i].is_none() {
cb_dat.token = i as u32;
cb_conns[i] = Some(cb_dat);
break;
return;
}
}
if i >= cb_conns.len() {
log::error!("ran out of space registering callback");
}
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)
xous::Message::new_blocking_scalar(SuspendEventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)
).unwrap();
unsafe{xous::disconnect(scb.server_to_cb_cid).unwrap();}
}

View file

@ -3,8 +3,6 @@
mod api;
mod os_timer;
use heapless::binary_heap::{BinaryHeap, Min};
use heapless::consts::*;
@ -444,9 +442,6 @@ fn recalculate_sleep(
#[xous::xous_main]
fn xmain() -> ! {
// Start the OS timer which is responsible for setting up preemption.
os_timer::init();
let mut sleep_heap: BinaryHeap<SleepRequest, U32, Min> = BinaryHeap::new();
log_server::init_wait().unwrap();