1*** Variables ***
2${PLATFORM}                         @platforms/cpus/cortex-a53-gicv2.repl
3${LOG_WFI_ENTER}                    WFI_ENTER
4${LOG_WFI_END}                      WFI_EXIT
5
6*** Keywords ***
7Create Platform
8    Execute Command                 mach create
9    Execute Command                 machine LoadPlatformDescription ${PLATFORM}
10    Execute Command                 using sysbus
11
12    # Create infinite loop with WFI
13    Execute Command                 sysbus WriteDoubleWord 0x1000 0xD503207F  # wfi
14    Execute Command                 sysbus WriteDoubleWord 0x1004 0xD503201F  # nop
15    Execute Command                 sysbus WriteDoubleWord 0x1008 0xD503201F  # nop
16    Execute Command                 sysbus WriteDoubleWord 0x100C 0xD503201F  # nop
17    Execute Command                 sysbus WriteDoubleWord 0x1010 0x17FFFFFC  # b to 0x1000
18    Execute Command                 cpu PC 0x1000
19
20Assert SysReg Equals
21    [Arguments]                     ${name}  ${expected}
22    ${pc}=                          Execute Command  cpu GetSystemRegisterValue ${name}
23    Should Be Equal As Integers     ${pc}  ${expected}
24
25Assert Enter In Logs
26    Wait For Log Entry              ${LOG_WFI_ENTER}
27    # Should not show duplicate Enter message
28    Should Not Be In Log            ${LOG_WFI_ENTER}
29
30Assert End In Logs
31    Wait For Log Entry              ${LOG_WFI_END}
32    # Should not show duplicate Exit message
33    Should Not Be In Log            ${LOG_WFI_END}
34
35*** Test Cases ***
36Should Invoke Interrupt Hooks
37    Create Platform
38
39    Execute Command                 cpu AddHookAtWfiStateChange 'self.Log(LogLevel.Info, "${LOG_WFI_ENTER}" if isInWfi else "${LOG_WFI_END}" )'
40
41    Create Log Tester               1
42    Start Emulation
43
44    Assert Enter In Logs
45    # Trigger interrupt in CPU
46    Execute Command                 cpu OnGPIO 0 true
47    Assert End In Logs
48
49    # Deactivate interrupt line, so the CPU enters WFI state again
50    Execute Command                 cpu OnGPIO 0 false
51    Assert Enter In Logs
52
53    # Ensure that the hook triggers after Reset
54    Execute Command                 cpu Reset
55    # After reset time doesn't flow
56    Wait For Log Entry              ${LOG_WFI_END}
57
58Should Reset Cpu From WFI Hook
59    Create Platform
60
61    # Preload TTBR0 with a nonsensical value, to check if it is cleared after reset
62    Execute Command                 cpu SetSystemRegisterValue "TTBR0_EL1" 0xDEADBEEF
63    Execute Command                 cpu AddHookAtWfiStateChange 'self.Log(LogLevel.Info, "${LOG_WFI_ENTER}" if isInWfi else "${LOG_WFI_END}" )'
64    Execute Command                 cpu AddHookAtWfiStateChange 'self.Reset()'
65
66    Create Log Tester               1
67    Start Emulation
68
69    # Can't use Assert X In Logs, since the Reset happens inside the hook
70    Wait For Log Entry              ${LOG_WFI_ENTER}
71    Wait For Log Entry              ${LOG_WFI_END}
72
73    # CPU has been reset, so TTBR0 is set to 0
74    Assert SysReg Equals            "TTBR0_EL1"  0x0
75