Webcentive RISC-V Reference

wfi

Wait for interrupt

Extension
Privileged base (mret, wfi)
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
wfi
Length
32-bit

Encoding

3120191514121176210
00010000010100000000000001110011

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.TWhstatus.VTWwfi behavior
HS-modeU-modeVS-modein VU-mode
00WaitTrap (I)WaitTrap (V)
01WaitTrap (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.TWwfi behavior
S-modeU-mode
0WaitTrap (I)
1Trap (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.

Specification and related pages