diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index 24cfd3597..d003c99d5 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -1,7 +1,7 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] -mod os_timer; +//mod os_timer; mod api; use api::*; @@ -18,6 +18,20 @@ mod implementation { use crate::SHOULD_RESUME; use core::sync::atomic::{AtomicBool, Ordering}; + const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000; + + fn timer_tick(_irq_no: usize, arg: *mut usize) { + let mut timer = CSR::new(arg as *mut u32); + // this call forces pre-emption every timer tick + // rsyscalls are "raw syscalls" -- used for syscalls that don't have a friendly wrapper around them + // since ReturnToParent is only used here, we haven't wrapped it, so we use an rsyscall + xous::rsyscall(xous::SysCall::ReturnToParent(xous::PID::new(1).unwrap(), 0)) + .expect("couldn't return to parent"); + + // acknowledge the timer + timer.wfo(utra::timer0::EV_PENDING_ZERO, 0b1); + } + // 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 @@ -41,6 +55,8 @@ mod implementation { pub struct SusResHw { /// our CSR csr: utralib::CSR, + /// OS timer CSR + os_timer: utralib::CSR, /// memory region for the "clean suspend" marker marker: xous::MemoryRange, /// loader stack region -- this data is dirtied on every resume; claim it in this process so no others accidentally use it @@ -50,6 +66,21 @@ mod implementation { } impl SusResHw { pub fn new() -> Self { + // os timer initializations + let ostimer_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::timer0::HW_TIMER0_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map Ticktimer CSR range"); + xous::claim_interrupt( + utra::timer0::TIMER0_IRQ, + timer_tick, + ostimer_csr.as_mut_ptr() as *mut usize, + ).expect("couldn't claim IRQ"); + + // everything else let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::susres::HW_SUSRES_BASE), None, @@ -72,17 +103,28 @@ mod implementation { ).expect("couldn't map clean suspend page"); let mut sr = SusResHw { csr: CSR::new(csr.as_mut_ptr() as *mut u32), + os_timer: CSR::new(ostimer_csr.as_mut_ptr() as *mut u32), stored_time: None, marker, loader_stack, }; + // start the OS timer running + let ms = 10; // tick every 10 ms + sr.os_timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer + // load its values + sr.os_timer.wfo(utra::timer0::LOAD_LOAD, (SYSTEM_CLOCK_FREQUENCY / 1_000) * ms); + sr.os_timer.wfo(utra::timer0::RELOAD_RELOAD, (SYSTEM_CLOCK_FREQUENCY / 1_000) * ms); + // enable the timer + sr.os_timer.wfo(utra::timer0::EN_EN, 0b1); + + // Set EV_ENABLE, this starts pre-emption + sr.os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, 0b1); // 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); - } + // note to self: don't use log:: here because we're upstream of logging being initialized + assert!(unsafe{(*check_marker)[words]} == 0, "marker had non-zero entry!"); } xous::claim_interrupt( @@ -260,7 +302,8 @@ pub fn execution_gate() { #[xous::xous_main] fn xmain() -> ! { // Start the OS timer which is responsible for setting up preemption. - os_timer::init(); + // os_timer::init(); + let mut susres_hw = implementation::SusResHw::new(); log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Trace); @@ -279,7 +322,6 @@ fn xmain() -> ! { 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 mut susres_hw = implementation::SusResHw::new(); let mut suspend_requested = false; let mut timeout_pending = false; diff --git a/services/susres/src/os_timer.rs b/services/susres/src/os_timer.rs deleted file mode 100644 index 1adfdfbbb..000000000 --- a/services/susres/src/os_timer.rs +++ /dev/null @@ -1,75 +0,0 @@ -#[cfg(baremetal)] -use utralib::generated::*; - -#[cfg(baremetal)] -const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000; -#[cfg(baremetal)] -const TIMER_BASE: usize = utra::timer0::HW_TIMER0_BASE; // claim the same address in virt as in phys - -#[cfg(baremetal)] -fn timer_tick(_irq_no: usize, _arg: *mut usize) { - //println!(">>> Timer tick"); - let mut timer = CSR::new(TIMER_BASE as *mut u32); - - xous::rsyscall(xous::SysCall::ReturnToParent(xous::PID::new(1).unwrap(), 0)) - .expect("couldn't return to parent"); - - // acknowledge the timer - timer.wfo(utra::timer0::EV_PENDING_ZERO, 0b1); - //println!("<<< Returning from timer_tick()"); -} - -#[cfg(baremetal)] -pub fn init() { - use xous::{MemoryAddress, MemorySize}; - println!("Allocating timer..."); - xous::rsyscall(xous::SysCall::MapMemory( - MemoryAddress::new(utra::timer0::HW_TIMER0_BASE), - MemoryAddress::new(TIMER_BASE), - MemorySize::new(4096).unwrap(), - xous::MemoryFlags::R | xous::MemoryFlags::W, - )) - .expect("timer: couldn't map timer"); - - xous::rsyscall(xous::SysCall::ClaimInterrupt( - utra::timer0::TIMER0_IRQ, - MemoryAddress::new(timer_tick as *mut usize as usize).unwrap(), - None, - )) - .expect("timer: couldn't claim interrupt"); - - let ms = 10; // tick every 10 ms - en(false); - load((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms); - reload((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms); - en(true); - - // Set EV_ENABLE - let mut timer = CSR::new(TIMER_BASE as *mut u32); - timer.wfo(utra::timer0::EV_ENABLE_ZERO, 0b1); -} - -#[cfg(not(baremetal))] -pub fn init() {} - -#[cfg(baremetal)] -pub fn load(value: u32) { - let mut timer = CSR::new(TIMER_BASE as *mut u32); - timer.wfo(utra::timer0::LOAD_LOAD, value); -} - -#[cfg(baremetal)] -pub fn reload(value: u32) { - let mut timer = CSR::new(TIMER_BASE as *mut u32); - timer.wfo(utra::timer0::RELOAD_RELOAD, value); -} - -#[cfg(baremetal)] -pub fn en(en: bool) { - let mut timer = CSR::new(TIMER_BASE as *mut u32); - if en { - timer.wfo(utra::timer0::EN_EN, 0b1); - } else { - timer.wfo(utra::timer0::EN_EN, 0b0); - } -} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f98e77f4e..464ecc811 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -50,6 +50,7 @@ fn try_main() -> Result<(), DynError> { "trng", "llio", "rtc", + "susres", ]; let fcc_pkgs = [ "fcc-agent", @@ -60,6 +61,7 @@ fn try_main() -> Result<(), DynError> { "xous-names", "keyboard", "trng", + "susres", ]; let benchmark_pkgs = [ "benchmark", @@ -70,6 +72,7 @@ fn try_main() -> Result<(), DynError> { "xous-names", "keyboard", "trng", + "susres", ]; let minimal_pkgs = [ "ticktimer-server", @@ -79,6 +82,7 @@ fn try_main() -> Result<(), DynError> { "llio", "rkyv-test-client", "rkyv-test-server", + "susres", ]; let cbtest_pkgs = [ "ticktimer-server", @@ -89,6 +93,7 @@ fn try_main() -> Result<(), DynError> { "cb-test-srv", "cb-test-c1", "cb-test-c2", + "susres", ]; let sr_pkgs = [ "ticktimer-server",