intermediate check-in on wfi

This commit is contained in:
bunnie 2021-05-30 23:07:16 +08:00
parent 1e2d46366a
commit 7c55925926
9 changed files with 207 additions and 40 deletions

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-05-30 03:54:04]]></description>
<description><![CDATA[Litex SoC 2021-05-30 22:41:49]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1185,10 +1185,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``bb8f0968``, so this value is 3146713448, which should be enough to check out
``7393e50d``, so this value is 1939072269, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0xbb8f0968</resetValue>
<resetValue>0x7393e50d</resetValue>
<size>32</size>
<fields>
<field>
@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x17</resetValue>
<resetValue>0x18</resetValue>
<size>32</size>
<fields>
<field>
@ -3284,7 +3284,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>0x4673469d</resetValue>
<resetValue>0x4588f3a0</resetValue>
<size>32</size>
<fields>
<field>
@ -3299,7 +3299,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x60ad02ab</resetValue>
<resetValue>0x5eea589</resetValue>
<size>32</size>
<fields>
<field>
@ -5529,8 +5529,51 @@ register to disable individual events.]]></description>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<name>WFI</name>
<baseAddress>0xF0023000</baseAddress>
<groupName>WFI</groupName>
<registers>
<register>
<name>WFI</name>
<addressOffset>0x0000</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>wfi</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` triggers an attempt to sleep clocks until an event happens]]></description>
</field>
</fields>
</register>
<register>
<name>IGNORE_LOCKED</name>
<addressOffset>0x0004</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>ignore_locked</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` causes the reset condition for the SoC to ignore the locked state
of the PLL]]></description>
</field>
</fields>
</register>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x8</size>
<usage>registers</usage>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<baseAddress>0xF0024000</baseAddress>
<groupName>IDENTIFIER_MEM</groupName>
<registers>
<register>

View file

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

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-05-30 03:54:04]]></description>
<description><![CDATA[Litex SoC 2021-05-30 22:41:49]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1185,10 +1185,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``bb8f0968``, so this value is 3146713448, which should be enough to check out
``7393e50d``, so this value is 1939072269, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0xbb8f0968</resetValue>
<resetValue>0x7393e50d</resetValue>
<size>32</size>
<fields>
<field>
@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x17</resetValue>
<resetValue>0x18</resetValue>
<size>32</size>
<fields>
<field>
@ -3284,7 +3284,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>0x4673469d</resetValue>
<resetValue>0x4588f3a0</resetValue>
<size>32</size>
<fields>
<field>
@ -3299,7 +3299,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x60ad02ab</resetValue>
<resetValue>0x5eea589</resetValue>
<size>32</size>
<fields>
<field>
@ -5529,8 +5529,51 @@ register to disable individual events.]]></description>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<name>WFI</name>
<baseAddress>0xF0023000</baseAddress>
<groupName>WFI</groupName>
<registers>
<register>
<name>WFI</name>
<addressOffset>0x0000</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>wfi</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` triggers an attempt to sleep clocks until an event happens]]></description>
</field>
</fields>
</register>
<register>
<name>IGNORE_LOCKED</name>
<addressOffset>0x0004</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>ignore_locked</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` causes the reset condition for the SoC to ignore the locked state
of the PLL]]></description>
</field>
</fields>
</register>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x8</size>
<usage>registers</usage>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<baseAddress>0xF0024000</baseAddress>
<groupName>IDENTIFIER_MEM</groupName>
<registers>
<register>

Binary file not shown.

View file

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

View file

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

View file

@ -58,15 +58,13 @@ mod implementation {
pub struct XousTickTimer {
csr: utralib::CSR<u32>,
#[cfg(feature = "watchdog")]
wdt: utralib::CSR<u32>,
current_response: Option<SleepRequest>,
connection: xous::CID,
ticktimer_sr_manager: RegManager::<{utra::ticktimer::TICKTIMER_NUMREGS}>,
wdt_sr_manager: RegManager::<{utra::wdt::WDT_NUMREGS}>,
wdt: utralib::CSR<u32>,
}
#[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
}

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-05-30 03:54:04]]></description>
<description><![CDATA[Litex SoC 2021-05-30 22:41:49]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1185,10 +1185,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``bb8f0968``, so this value is 3146713448, which should be enough to check out
``7393e50d``, so this value is 1939072269, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0xbb8f0968</resetValue>
<resetValue>0x7393e50d</resetValue>
<size>32</size>
<fields>
<field>
@ -1205,7 +1205,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x17</resetValue>
<resetValue>0x18</resetValue>
<size>32</size>
<fields>
<field>
@ -3284,7 +3284,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>0x4673469d</resetValue>
<resetValue>0x4588f3a0</resetValue>
<size>32</size>
<fields>
<field>
@ -3299,7 +3299,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x60ad02ab</resetValue>
<resetValue>0x5eea589</resetValue>
<size>32</size>
<fields>
<field>
@ -5529,8 +5529,51 @@ register to disable individual events.]]></description>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<name>WFI</name>
<baseAddress>0xF0023000</baseAddress>
<groupName>WFI</groupName>
<registers>
<register>
<name>WFI</name>
<addressOffset>0x0000</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>wfi</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` triggers an attempt to sleep clocks until an event happens]]></description>
</field>
</fields>
</register>
<register>
<name>IGNORE_LOCKED</name>
<addressOffset>0x0004</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>ignore_locked</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[Writing a `1` causes the reset condition for the SoC to ignore the locked state
of the PLL]]></description>
</field>
</fields>
</register>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x8</size>
<usage>registers</usage>
</addressBlock>
</peripheral>
<peripheral>
<name>IDENTIFIER_MEM</name>
<baseAddress>0xF0024000</baseAddress>
<groupName>IDENTIFIER_MEM</groupName>
<registers>
<register>

View file

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