4

How does a WFE instruction work ? What I have read is that it makes processor wait for an IRQ /FIQ/event/....

But what happens when you get an IRQ, does the irq_fault_handler vector executes on getting an interrupt or instruction subsequent to the WFE is executed ?

user970251
  • 345
  • 1
  • 5
  • 15

2 Answers2

9

WFE is conceptually equivalent to

while (!event_has_occurred) /*do nothing*/;

except that it turns the CPU off instead of running a tight loop.

Several things that can interrupt a WFE, including not only an interrupt but also an explicit wake up event from another CPU (in a multicore processor).

If an interrupt happens during WFE, the usual things happens. The processor switches to IRQ or FIQ mode, jumps to the IRQ or FIQ handler, and the address of the WFE instruction (plus the usual offset of 8) is placed in lr.

If the CPU was waken up by an explicit wake up event, the execution proceeds with the next instruction after the WFE.

Think of WFE as a very long NOP which only completes when some external event happens.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • You may also mask interrupts before executing the `WFE`, in which case your next instruction will execute. This is often needed for *sleep* code. The `WFE` will not fetch from external SDRAM, etc and will save power; many designs use this feature to enter different *sleep states*. Many programmer's will care about power consumption; but this aspect depends on the SOC. From an execution stand-point, the `WFE` is like an extended `NOP`. (+1) – artless noise Sep 16 '13 at 14:04
  • Ok. What if the IRQ handler is defined as _irq: .word _irq. http://git.denx.de/?p=u-boot.git;a=blob;f=arch/arm/cpu/armv7/start.S;h=ef62fc83270a327bc7df970f598540f1d7ce0fe2;hb=HEAD see under SPL BUILD – user970251 Sep 17 '13 at 07:31
  • @user970251 I have no idea what your comment is asking. The behavior of WFE doesn't depend on how the IRQ handler is defined in the source code. Are you trying to understand what that IRQ handler does? That's a completely different question, unrelated to WFE. – Gilles 'SO- stop being evil' Sep 17 '13 at 08:21
  • what I am asking here is the code I am referencing has the irq handler defined to nothing, what would happen in this case – user970251 Sep 17 '13 at 09:38
  • 1
    @user970251 That line isn't the IRQ handler that will be used, it's a placeholder until the address of the IRQ handler is placed into the interrupt vector. If an IRQ happens in the meantime and interrupts are unblocked, then the temporary handler will be called, and it's a tight loop (i.e. it does nothing, forever or until the battery runs out). If you still don't understand, [ask another question](http://stackoverflow.com/questions/ask); this has nothing to do with WFE. – Gilles 'SO- stop being evil' Sep 17 '13 at 11:26
1

It should also be stressed that WFE is "just" a hint instruction, i.e.: a compliant implementation could treat it as a NOP rather than going to sleep.

Therefore, you almost always need to surround it with a loop.

The following answer provides a minimal bare-metal assembly example that illustrates how WFE is typically used to wait for an event from another CPU: What does multicore assembly language look like?

Ciro Santilli
  • 3,693
  • 1
  • 18
  • 44