Home/Instructions/Wait for Interrupt
WFI

RISC-V WFI Instruction Details

Instruction ManualI-type

Wait-for-interrupt instruction: an implementation may stall the hart until an interrupt might need service, or legally avoid stalling and continue as a NOP.

Instruction Syntax

wfi
Operand Breakdown
No explicit operands; 32-bit SYSTEM encoding 0x10500073, rs1/rd=00000 and funct3=000.
Available in privileged modes; U-mode support is optional. TW and the S-mode bounded-time rule still apply.
May stall or act as a NOP. A locally enabled pending interrupt must resume the normal wait path regardless of global enables or delegation.
Resume need not take a trap. Without a trap, continue at PC+4 and recheck conditions. H adds rules such as VTW.
PrivilegedSystem & Privileged

Extension condition: Available in implemented privileged modes; U-mode support is optional and subject to TW and the S-mode bounded-time rule. H adds VTW rules.

Instruction Behavior

WFI informs the implementation that this hart may stall until an interrupt might need service; a legal implementation may instead treat it as a NOP. On the normal wait path, a locally enabled pending interrupt must resume execution regardless of global interrupt-enable bits or mideleg; resumption does not necessarily take an interrupt trap. If a deliverable enabled interrupt appears while stalled, the trap is taken on the following instruction. If the resume event does not cause a trap, execution continues at WFI PC+4 and software must recheck conditions. With TW=1, lower-than-M WFI must raise an illegal-instruction exception if it fails to complete within an implementation-specific bounded time; an implementation may always trap immediately. When S-mode exists, U-mode has this bounded-completion condition regardless of TW.

WFI Decode And Wait Animation

Shows the fixed SYSTEM encoding, TW restriction, and ISA-visible WFI behavior: it may stall or may return as a NOP.

no explicit operands
wfiwfi
31..20
19..15
14..12
11..7
6..0
000100000101
funct12
00000
rs1=x0
000
funct3
00000
rd=x0
1110011
SYSTEM
Wait State
instruction
0x10500073
opcode
1110011 -> SYSTEM
funct3
000 -> privileged non-CSR form
fixed
funct12=000100000101, rs1=x0, rd=x0
permission
M-mode example may reach WFI semantics
wait
implementation may stall the hart
resume
local enabled interrupt pending -> resume
next
must resume; interrupt trap only if deliverable

This V=0 model assumes U-mode WFI support when S-mode is absent and excludes H/VTW. The result shows possible paths for the selected conditions, not committed state; exceeding the TW bounded limit must trap. This animation does not promise sleep duration, power reduction, wake latency, or scheduling policy; those are not portable WFI ISA semantics.

Quick Understanding & Search Notes

Wait-for-interrupt instruction: an implementation may stall the hart until an interrupt might need service, or legally avoid stalling and continue as a NOP.

In the SYSTEM encoding, funct12=000100000101, rs1=x0, rd=x0, and funct3=000.
An implementation may resume for any reason or treat WFI as a NOP; the specification does not guarantee sleep duration or power behavior.
A locally enabled pending interrupt must resume the normal wait path. Lower-privilege TW=1 execution, and U-mode when S-mode exists, must trap if the implementation-specific bounded completion limit is exceeded.

Common Usage Scenarios

Idle loop power saving

With V=0, execution in M, no deliverable interrupt and PC=0x2000, an implementation choosing the NOP path continues after wfi at 0x2004. Software must still recheck its wait condition; this does not imply sleep or power savings.

Low-power waiting

With V=0, execution in M, no deliverable interrupt and PC=0x2000, an implementation choosing the NOP path continues after wfi at 0x2004. Software must still recheck its wait condition; this does not imply sleep or power savings.

Pre-Use Checklist

Syntax Check
  • No explicit operands; 32-bit SYSTEM encoding 0x10500073, rs1/rd=00000 and funct3=000.
  • Available in privileged modes; U-mode support is optional. TW and the S-mode bounded-time rule still apply.
Semantic Check
  • May stall or act as a NOP. A locally enabled pending interrupt must resume the normal wait path regardless of global enables or delegation.
  • Resume need not take a trap. Without a trap, continue at PC+4 and recheck conditions. H adds rules such as VTW.

Pitfalls / Common Confusions

WFI is not a HINT instruction; an implementation may still avoid stalling and return as a NOP.
Do not use WFI as a fixed-delay primitive, portable power guarantee, or scheduler mechanism.
WFI must resume for locally enabled pending interrupts even when global interrupt-enable bits are clear.
When mstatus.TW=1, lower-privilege WFI must raise an illegal-instruction exception if it fails to complete within an implementation-specific bounded time, and may be implemented to always trap immediately.
When S-mode is implemented, U-mode WFI raises an illegal-instruction exception regardless of TW unless it completes within the implementation-specific bounded time.
If the resume event does not cause a trap, execution continues after WFI; software usually rechecks conditions and loops.

FAQ

Does WFI always put the processor into low-power sleep?

No. WFI is not a HINT instruction, but its official semantics still allow an implementation to stall or to legally continue as a NOP.

What should software do after WFI returns?

Software should recheck its wait condition or pending state, because WFI may resume for any reason and resumption alone does not guarantee the intended event has been handled.