wfi
Wait for interrupt
- Extension
- Privileged base (mret, wfi)
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
wfi- Length
- 32-bit
Encoding
| 3120 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 000100000101 | 00000 | 000 | 00000 | 11100 | 11 | ||||||||||||||||||||||||||
Match 0x10500073, mask 0xffffffff. Source: wfi 11..7=0 19..15=0 31..20=0x105 14..12=0 6..2=0x1C 1..0=3 in riscv-opcodes/extensions/rv_system.
Description
Can causes the processor to enter a low-power state until the next interrupt occurs.
When H is implemented:
The behavior of wfi is affected by the mstatus.TW
and hstatus.VTW bits, as summarized below.
mstatus.TW | hstatus.VTW | wfi behavior | |||
| HS-mode | U-mode | VS-mode | in VU-mode | ||
| 0 | 0 | Wait | Trap (I) | Wait | Trap (V) |
| 0 | 1 | Wait | Trap (I) | Trap (V) | Trap (V) |
| 1 | - | Trap (I) | Trap (I) | Trap (I) | Trap (I) |
Trap (I) - Trap with Illegal Instruction code + Trap (V) - Trap with Virtual Instruction code |
Otherwise:
The wfi instruction is also affected by mstatus.TW, as shown below:
mstatus.TW | wfi behavior | |
| S-mode | U-mode | |
| 0 | Wait | Trap (I) |
| 1 | Trap (I) | Trap (I) |
Trap (I) - Trap with Illegal Instruction code |
When wfi is marked as causing a trap above, the implementation is allowed to wait
for an unspecified period of time to see if an interrupt occurs before raising the trap.
That period of time can be zero (i.e., wfi always causes a trap in the cases identified
above).
Description from riscv-unified-db, reformatted.
Operation
# first, perform all the access checks
if (mode() == PrivilegeMode::U && !WFI_U_MODE) {
raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
}
if ((CSR[misa].S == 1) && (CSR[mstatus].TW == 1'b1)) {
if (mode() != PrivilegeMode::M) {
raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
}
}
if (CSR[misa].H == 1) {
if (CSR[hstatus].VTW == 1'b0) {
if (mode() == PrivilegeMode::VU) {
raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
}
} else if (CSR[hstatus].VTW == 1'b1) {
if ((mode() == PrivilegeMode::VS) || (mode() == PrivilegeMode::VU)) {
raise (ExceptionCode::VirtualInstruction, mode(), $encoding);
}
}
}
# passed, so now do the wait
wfi();Pseudocode in riscv-unified-db’s IDL, quoted unchanged.