|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +import sys, pathlib, time |
| 4 | +sys.path.append(str(pathlib.Path(__file__).resolve().parents[1])) |
| 5 | + |
| 6 | +from m1n1.proxy import REGION_RX_EL1 |
| 7 | +from m1n1.setup import * |
| 8 | +from m1n1 import asm |
| 9 | + |
| 10 | +p.smp_start_secondaries() |
| 11 | +for i in range(1, 10): |
| 12 | + p.mmu_init_secondary(i) |
| 13 | + |
| 14 | +aic = u.adt["arm-io/aic"].get_reg(0)[0] |
| 15 | + |
| 16 | +mon.add(aic, 0xc000) |
| 17 | + |
| 18 | +hacr = 0x1f000000056c01#0#xffffffff_ffffffff |
| 19 | + |
| 20 | +hcr = HCR(u.mrs(HCR_EL2)) |
| 21 | +hcr.TIDCP = 0 |
| 22 | +hcr.TGE = 0 |
| 23 | +hcr.AMO = 1 |
| 24 | +hcr.IMO = 1 |
| 25 | +hcr.FMO = 1 |
| 26 | +print(hcr) |
| 27 | +u.msr(HCR_EL2, hcr.value) |
| 28 | +u.msr(HACR_EL2, hacr) |
| 29 | +u.inst(0xd5033fdf) # isb |
| 30 | + |
| 31 | +AIC_NR_IRQ = aic + 0x04 |
| 32 | +AIC_RST = aic + 0x10 |
| 33 | +AIC_CFG = aic + 0x14 |
| 34 | + |
| 35 | +AIC_CFG_ENABLE = 1 << 0 |
| 36 | +AIC_CFG_PREFER_PCORES = 1 << 28 |
| 37 | + |
| 38 | +AIC_RR_DELAY = aic + 0x28 |
| 39 | +AIC_CLUSTER_ENABLE_CFG = aic + 0x30 |
| 40 | +AIC_SOME_CNT = aic + 0x3c |
| 41 | + |
| 42 | +AIC_DELAYS = aic + 0x100 |
| 43 | + |
| 44 | +AIC_IDLE_CLUSTERS = aic + 0x340 |
| 45 | + |
| 46 | +AIC_TIMESTAMPS = 0x28e101800 |
| 47 | + |
| 48 | +AIC_IRQ_CFG = aic + 0x2000 |
| 49 | +# AIC_IRQ_ROUTE: bits 3:0 ? only 0 works. |
| 50 | +# AIC_IRQ_CFG_DELAY: bits 7:5 |
| 51 | + |
| 52 | +AIC_SW_GEN_SET = aic + 0x6000 |
| 53 | +AIC_SW_GEN_CLR = aic + 0x6200 |
| 54 | +AIC_MASK_SET = aic + 0x6400 |
| 55 | +AIC_MASK_CLR = aic + 0x6600 |
| 56 | +AIC_HW_STATE = aic + 0x6800 |
| 57 | + |
| 58 | +AIC_IRQ_CFG_2 = aic + 0x6a00 |
| 59 | +AIC_MASK2_SET = aic + 0xae00 |
| 60 | +AIC_MASK2_CLR = aic + 0xb000 |
| 61 | +AIC_HW2_STATE = aic + 0xb200 |
| 62 | + |
| 63 | +AIC_INTERRUPT_ACK = aic + 0xc000 |
| 64 | + |
| 65 | +num_irq = p.read32(AIC_NR_IRQ) & 0xffff |
| 66 | + |
| 67 | +code = u.malloc(0x1000) |
| 68 | + |
| 69 | +c = asm.ARMAsm(""" |
| 70 | +write32_ts: |
| 71 | + isb |
| 72 | + mrs x2, CNTPCT_EL0 |
| 73 | + str w1, [x0] |
| 74 | + mov x0, x2 |
| 75 | + isb |
| 76 | + ret |
| 77 | +
|
| 78 | +en_and_spin: |
| 79 | + msr DAIFSet, 7 |
| 80 | + msr DAIFClr, 2 |
| 81 | + isb |
| 82 | + b 1f |
| 83 | +
|
| 84 | +en_and_spin_sec: |
| 85 | + msr DAIFSet, 7 |
| 86 | + #msr DAIFClr, 2 |
| 87 | + isb |
| 88 | + b 1f |
| 89 | +
|
| 90 | +1: |
| 91 | + cmp x0, #0 |
| 92 | + beq 2f |
| 93 | + sub x0, x0, #1 |
| 94 | + b 1b |
| 95 | +2: |
| 96 | + msr DAIFSet, 7 |
| 97 | + msr DAIFClr, 2 |
| 98 | + isb |
| 99 | + ret |
| 100 | +
|
| 101 | +""", code) |
| 102 | + |
| 103 | +iface.writemem(code, c.data) |
| 104 | +p.dc_cvau(code, len(c.data)) |
| 105 | +p.ic_ivau(code, len(c.data)) |
| 106 | + |
| 107 | +def cpoll(): |
| 108 | + mon.poll() |
| 109 | + mon.poll() |
| 110 | + |
| 111 | +def init(): |
| 112 | + cpoll() |
| 113 | + |
| 114 | + p.set32(AIC_RST, 1) |
| 115 | + p.set32(AIC_CFG, 1 | AIC_CFG_PREFER_PCORES) |
| 116 | + |
| 117 | + cpoll() |
| 118 | + |
| 119 | +def test_irq_routing(): |
| 120 | + irq = 24 |
| 121 | + |
| 122 | + p.write32(AIC_SW_GEN_CLR, 1 << irq) |
| 123 | + p.write32(AIC_MASK_CLR, 1 << irq) |
| 124 | + |
| 125 | + cpoll() |
| 126 | + ts = p.call(c.write32_ts, AIC_SW_GEN_SET, 1 << irq) |
| 127 | + print(f"IRQ triggered at time {ts:#x}") |
| 128 | + p.nop() |
| 129 | + |
| 130 | + print("w") |
| 131 | + cpoll() |
| 132 | + cpoll() |
| 133 | + time.sleep(0.1) |
| 134 | + |
| 135 | + #p.write32(AIC_SW_GEN_CLR, 1 << irq) |
| 136 | + |
| 137 | + cpoll() |
| 138 | + |
| 139 | +def get_irq_state(irq): |
| 140 | + v = p.read32(AIC_HW_STATE + 4* (irq//32)) |
| 141 | + return bool(v & 1<<(irq%32)) |
| 142 | + |
| 143 | + |
| 144 | +TEST_CPU = 2 |
| 145 | + |
| 146 | +u.msr(DAIF, 0) |
| 147 | +for i in range(1, 10): |
| 148 | + u.msr(DAIF, 0x140, call=lambda x, *args: p.smp_call_sync(i, x | REGION_RX_EL1, *args)) |
| 149 | + u.msr(DAIF, 0x1c0, call=lambda x, *args: p.smp_call_sync(i, x | REGION_RX_EL1, *args)) |
| 150 | + u.msr((3,4,15,10,4), 0, call=lambda x, *args: p.smp_call_sync(i, x | REGION_RX_EL1, *args)); |
| 151 | + mpidr = u.mrs(MIDR_EL1, call=lambda x, *args: p.smp_call_sync(i, x | REGION_RX_EL1, args[0], args[1])); |
| 152 | + print(i, hex(mpidr)) |
| 153 | + |
| 154 | +init() |
| 155 | + |
| 156 | +def sec_call(x, *args): |
| 157 | + return p.smp_call_sync(TEST_CPU, x | REGION_RX_EL1, *args) |
| 158 | + |
| 159 | +def cpu_call(cpu, x, *args): |
| 160 | + return p.smp_call_sync(cpu, x | REGION_RX_EL1, *args) |
| 161 | + |
| 162 | +u.msr(HCR_EL2, hcr.value, call=sec_call) |
| 163 | +u.msr(HACR_EL2, hacr, call=sec_call) |
| 164 | + |
| 165 | +daif = u.mrs(DAIF) |
| 166 | +print("DAIF: %x" % daif) |
| 167 | +daif |= 0x1c0 |
| 168 | +print("DAIF: %x" % daif) |
| 169 | +u.msr(DAIF, daif) |
| 170 | + |
| 171 | +p.smp_call(TEST_CPU, c.en_and_spin, 0x10000000) |
| 172 | +test_irq_routing() |
| 173 | +p.smp_wait(TEST_CPU) |
| 174 | + |
| 175 | +p.smp_call(TEST_CPU, c.en_and_spin, 0x10000000) |
| 176 | +test_irq_routing() |
| 177 | +p.smp_wait(TEST_CPU) |
| 178 | + |
| 179 | +print(hex(c.en_and_spin)) |
| 180 | + |
| 181 | +daif = u.mrs(DAIF) |
| 182 | +print("DAIF: %x" % daif) |
| 183 | +daif &= ~0x1c0 |
| 184 | +print("DAIF: %x" % daif) |
| 185 | +u.msr(DAIF, daif) |
| 186 | + |
| 187 | +print("DAIF: %x" % daif) |
| 188 | +daif &= ~0x1c0 |
| 189 | +u.msr(DAIF, daif) |
| 190 | +print("DAIF: %x" % daif) |
| 191 | +print("ISR", hex(u.mrs(ISR_EL1))) |
| 192 | +print("ISR #2", hex(u.mrs(ISR_EL1, call=sec_call))) |
| 193 | +u.msr(DAIF, daif) |
| 194 | +print("test") |
| 195 | +# test_irq_routing() |
| 196 | +daif = u.mrs(DAIF) |
| 197 | +print("ISR", hex(u.mrs(ISR_EL1))) |
| 198 | +print("ISR #2", hex(u.mrs(ISR_EL1, call=sec_call))) |
| 199 | +print("DAIF: %x" % daif) |
| 200 | +daif &= ~0x1c0 |
| 201 | +u.msr(DAIF, daif) |
| 202 | +print("DAIF: %x" % daif) |
| 203 | +print("ISR", hex(u.mrs(ISR_EL1))) |
| 204 | +print("ISR #2", hex(u.mrs(ISR_EL1, call=sec_call))) |
| 205 | + |
| 206 | +for i in range(0, 10): |
| 207 | + if i == 0: |
| 208 | + isr = u.mrs(ISR_EL1) |
| 209 | + else: |
| 210 | + isr = u.mrs(ISR_EL1, call=lambda x, *args: cpu_call(i, x, *args)) |
| 211 | + print(f"{i}: {isr:#x}") |
| 212 | + |
| 213 | +u.msr(DAIF, 0x1c0) |
| 214 | +print("call") |
| 215 | +p.smp_call_el1(TEST_CPU, c.en_and_spin_sec, 0x20000000) |
| 216 | +print("test") |
| 217 | +print(time.time()) |
| 218 | +test_irq_routing() |
| 219 | +print(time.time()) |
| 220 | +print("wait") |
| 221 | +p.smp_wait(TEST_CPU) |
| 222 | +print("-") |
| 223 | + |
| 224 | +for i in range(1, 10): |
| 225 | + u.msr(DAIF, 0x140, call=lambda x, *args: p.smp_call_sync(i, x | REGION_RX_EL1, *args)) |
| 226 | + |
| 227 | +for i in range(0, 10): |
| 228 | + if i == 0: |
| 229 | + isr = u.mrs(ISR_EL1) |
| 230 | + else: |
| 231 | + isr = u.mrs(ISR_EL1, call=lambda x, *args: cpu_call(i, x, *args)) |
| 232 | + print(f"{i}: {isr:#x}") |
| 233 | + |
| 234 | +cpoll() |
| 235 | +p.set32(AIC_RST, 1) |
| 236 | +cpoll() |
0 commit comments