diff --git a/emulation/soc/renode.svd b/emulation/soc/renode.svd index 0342eb314..a061d8e91 100644 --- a/emulation/soc/renode.svd +++ b/emulation/soc/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1185,10 +1185,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xbb8f0968 + 0x7393e50d 32 @@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x17 + 0x18 32 @@ -3284,7 +3284,7 @@ inject too fast.]]> SEED1 0x0000 - 0x4673469d + 0x4588f3a0 32 @@ -3299,7 +3299,7 @@ inject too fast.]]> SEED0 0x0004 - 0x60ad02ab + 0x5eea589 32 @@ -5529,8 +5529,51 @@ register to disable individual events.]]> - IDENTIFIER_MEM + WFI 0xF0023000 + WFI + + + WFI + 0x0000 + 0x00 + 32 + + + wfi + 0 + [0:0] + 0 + + + + + + IGNORE_LOCKED + 0x0004 + 0x00 + 32 + + + ignore_locked + 0 + [0:0] + 0 + + + + + + + 0 + 0x8 + registers + + + + IDENTIFIER_MEM + 0xF0024000 IDENTIFIER_MEM diff --git a/kernel/src/arch/riscv.rs b/kernel/src/arch/riscv.rs index df6885888..713e3d189 100644 --- a/kernel/src/arch/riscv.rs +++ b/kernel/src/arch/riscv.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 use riscv::register::{satp, sie, sstatus}; -use xous_kernel::PID; pub mod exception; pub mod irq; @@ -13,11 +12,39 @@ pub mod syscall; pub use process::Thread; +use utralib::generated::*; +use xous_kernel::{MemoryFlags, MemoryType, PID}; +use crate::mem::MemoryManager; + +pub const WFI_KERNEL: Wfi = Wfi { + // the manually chosen virtual address has to be in the top 4MiB as it is the only page shared among all processes + base: 0xffcd_0000 as *mut usize, // see https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md +}; + +pub struct Wfi { + pub base: *mut usize, +} + pub fn current_pid() -> PID { PID::new(satp::read().asid() as _).unwrap() } pub fn init() { + MemoryManager::with_mut(|memory_manager| { + memory_manager + .map_range( + utra::wfi::HW_WFI_BASE as *mut u8, + ((WFI_KERNEL.base as u32) & !4095) as *mut u8, + 4096, + PID::new(1).unwrap(), + MemoryFlags::R | MemoryFlags::W, + MemoryType::Default, + ) + .expect("unable to map WFI") + }); + let mut wfi_kernel_csr = CSR::new(WFI_KERNEL.base as *mut u32); + wfi_kernel_csr.wfo(utra::wfi::IGNORE_LOCKED_IGNORE_LOCKED, 1); + unsafe { sie::set_ssoft(); sie::set_sext(); @@ -28,10 +55,16 @@ pub fn init() { /// Put the core to sleep until an interrupt hits. Returns `true` /// to indicate the kernel should not exit. pub fn idle() -> bool { + let mut wfi_kernel_csr = CSR::new(WFI_KERNEL.base as *mut u32); // Issue `wfi`. This will return as soon as an external interrupt // is available. - unsafe { riscv::asm::wfi() }; + if true { + unsafe { riscv::asm::wfi() }; + } else { + wfi_kernel_csr.wfo(utra::wfi::IGNORE_LOCKED_IGNORE_LOCKED, 1); + wfi_kernel_csr.wfo(utra::wfi::WFI_WFI, 1); + } // Enable interrupts temporarily in Supervisor mode, allowing them // to drain. Aside from this brief instance, interrupts are diff --git a/precursors/soc.svd b/precursors/soc.svd index 0342eb314..a061d8e91 100644 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1185,10 +1185,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xbb8f0968 + 0x7393e50d 32 @@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x17 + 0x18 32 @@ -3284,7 +3284,7 @@ inject too fast.]]> SEED1 0x0000 - 0x4673469d + 0x4588f3a0 32 @@ -3299,7 +3299,7 @@ inject too fast.]]> SEED0 0x0004 - 0x60ad02ab + 0x5eea589 32 @@ -5529,8 +5529,51 @@ register to disable individual events.]]> - IDENTIFIER_MEM + WFI 0xF0023000 + WFI + + + WFI + 0x0000 + 0x00 + 32 + + + wfi + 0 + [0:0] + 0 + + + + + + IGNORE_LOCKED + 0x0004 + 0x00 + 32 + + + ignore_locked + 0 + [0:0] + 0 + + + + + + + 0 + 0x8 + registers + + + + IDENTIFIER_MEM + 0xF0024000 IDENTIFIER_MEM diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index 610c64d00..60b24fde3 100644 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/engine-sha512/src/main.rs b/services/engine-sha512/src/main.rs index 0d238b438..ac51b0767 100644 --- a/services/engine-sha512/src/main.rs +++ b/services/engine-sha512/src/main.rs @@ -72,7 +72,6 @@ mod implementation { let mut engine512 = Engine512 { csr: CSR::new(csr.as_mut_ptr() as *mut u32), fifo, - #[cfg(feature = "event_wait")] done: false, }; xous::claim_interrupt( @@ -81,9 +80,10 @@ mod implementation { (&mut engine512) as *mut Engine512 as *mut usize, ) .expect("couldn't claim irq"); + // note: we can't use a "susres" manager here because this block has un-suspendable state + // instead, we rely on every usage of this mechanism to explicitly set this enable bit before relying upon in engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1); } - engine512 } @@ -153,6 +153,7 @@ mod implementation { self.csr.wfo(utra::sha512::POWER_ON, 1); #[cfg(feature = "event_wait")] { + engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1); self.done = false; self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1); while !self.done { @@ -208,6 +209,7 @@ mod implementation { unsafe { sha.write_volatile(0x0); } // stuff a dummy byte, in case the hash was empty #[cfg(feature = "event_wait")] { + engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1); self.done = false; self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1); while !self.done { diff --git a/services/ticktimer-server/Cargo.toml b/services/ticktimer-server/Cargo.toml index fa27ccdf9..8febf5438 100644 --- a/services/ticktimer-server/Cargo.toml +++ b/services/ticktimer-server/Cargo.toml @@ -24,6 +24,6 @@ utralib = {path = "../../utralib"} debug-print = [] watchdog = [] # generally you want to disable watchdog if you're doing development -# default = ["watchdog"] -default = [] +default = ["watchdog"] +# default = [] # default = ["debug-print"] diff --git a/services/ticktimer-server/src/main.rs b/services/ticktimer-server/src/main.rs index bcdcffcfe..fac31cbb0 100644 --- a/services/ticktimer-server/src/main.rs +++ b/services/ticktimer-server/src/main.rs @@ -58,15 +58,13 @@ mod implementation { pub struct XousTickTimer { csr: utralib::CSR, - #[cfg(feature = "watchdog")] - wdt: utralib::CSR, current_response: Option, connection: xous::CID, ticktimer_sr_manager: RegManager::<{utra::ticktimer::TICKTIMER_NUMREGS}>, wdt_sr_manager: RegManager::<{utra::wdt::WDT_NUMREGS}>, + wdt: utralib::CSR, } - #[cfg(feature = "watchdog")] fn handle_wdt(_irq_no: usize, arg: *mut usize) { let xtt = unsafe { &mut *(arg as *mut XousTickTimer) }; // disarm the WDT -- do it in an interrupt context, to make sure we aren't interrupted while doing this. @@ -78,6 +76,7 @@ mod implementation { // - the ring oscillator has a tolerance band of 65MHz +/- 50% // - the CPU runs at 100MHz with a tight tolerance // - thus it is impossible to guarantee sync between the domains, so we do a two-step query/response interlock + #[cfg(feature = "watchdog")] if xtt.wdt.rf(utra::wdt::STATE_ENABLED) == 1 { if xtt.wdt.rf(utra::wdt::STATE_ARMED1) != 0 { xtt.wdt.wfo(utra::wdt::WATCHDOG_RESET_CODE, 0x600d); @@ -138,12 +137,11 @@ mod implementation { let mut xtt = XousTickTimer { csr: CSR::new(csr.as_mut_ptr() as *mut u32), - #[cfg(feature = "watchdog")] - wdt: CSR::new(wdt.as_mut_ptr() as *mut u32), current_response: None, connection, ticktimer_sr_manager, wdt_sr_manager, + wdt: CSR::new(wdt.as_mut_ptr() as *mut u32), }; #[cfg(feature = "watchdog")] @@ -160,7 +158,6 @@ mod implementation { ) .expect("couldn't claim irq"); - #[cfg(feature = "watchdog")] xous::claim_interrupt( utra::wdt::WDT_IRQ, handle_wdt, @@ -173,6 +170,11 @@ mod implementation { xtt.wdt.wfo(utra::wdt::EV_ENABLE_SOFT_INT, 1); xtt.wdt_sr_manager.push(RegOrField::Reg(utra::wdt::EV_ENABLE), None); } + #[cfg(not(feature = "watchdog"))] + { + xtt.wdt.wfo(utra::wdt::EV_ENABLE_SOFT_INT, 0); + xtt.wdt_sr_manager.push(RegOrField::Reg(utra::wdt::EV_ENABLE), None); + } xtt.ticktimer_sr_manager.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET0), None); xtt.ticktimer_sr_manager.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET1), None); @@ -248,7 +250,7 @@ mod implementation { self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 1); } - #[cfg(feature = "watchdog")] + #[allow(dead_code)] pub fn reset_wdt(&mut self) { // this triggers an interrupt, and the handler of the interrupt does the actual reset // this is done because we don't want the WDT reset to be interrupted @@ -256,7 +258,6 @@ mod implementation { } #[allow(dead_code)] - #[cfg(feature = "watchdog")] pub fn check_wdt(&mut self) { let state = self.wdt.r(utra::wdt::STATE); if state & self.wdt.ms(utra::wdt::STATE_DISARMED, 1) == 0 { @@ -427,7 +428,7 @@ mod implementation { .unwrap(); } - #[cfg(feature = "watchdog")] + #[allow(dead_code)] pub fn reset_wdt(&self) { // dummy function, does nothing } diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 0342eb314..a061d8e91 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1185,10 +1185,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xbb8f0968 + 0x7393e50d 32 @@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x17 + 0x18 32 @@ -3284,7 +3284,7 @@ inject too fast.]]> SEED1 0x0000 - 0x4673469d + 0x4588f3a0 32 @@ -3299,7 +3299,7 @@ inject too fast.]]> SEED0 0x0004 - 0x60ad02ab + 0x5eea589 32 @@ -5529,8 +5529,51 @@ register to disable individual events.]]> - IDENTIFIER_MEM + WFI 0xF0023000 + WFI + + + WFI + 0x0000 + 0x00 + 32 + + + wfi + 0 + [0:0] + 0 + + + + + + IGNORE_LOCKED + 0x0004 + 0x00 + 32 + + + ignore_locked + 0 + [0:0] + 0 + + + + + + + 0 + 0x8 + registers + + + + IDENTIFIER_MEM + 0xF0024000 IDENTIFIER_MEM diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 273b6d4e3..1f183e111 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -951,10 +951,12 @@ impl SysCall { /// Returns `true` if the given syscall may be called from an IRQ context pub fn can_call_from_interrupt(&self) -> bool { + if let SysCall::TrySendMessage(_cid, msg) = self { + return ! msg.is_blocking(); + } matches!( self, - SysCall::TrySendMessage(_, _) - | SysCall::TryConnect(_) + SysCall::TryConnect(_) | SysCall::TryReceiveMessage(_) | SysCall::ReturnToParent(_, _) | SysCall::ReturnScalar2(_, _, _) @@ -1344,12 +1346,12 @@ pub fn terminate_process() { /// Return execution to the kernel. This function may return at any time, /// including immediately pub fn yield_slice() { - rsyscall(SysCall::Yield).expect("yield_slice returned an error"); + rsyscall(SysCall::Yield).ok(); } /// Return execution to the kernel and wait for a message or an interrupt. pub fn wait_event() { - rsyscall(SysCall::WaitEvent).expect("wait_event returned an error"); + rsyscall(SysCall::WaitEvent).ok(); } #[deprecated(