Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_gsp_mmio.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_gsp_mmio.c` 254 lines ยท 9564 bytes Raw Blame History 1 /* test_gsp_mmio: the GB10B GSP start-up sequence (gpu/gsp_mmio.c) against the simulated SEC2 + GSP falcon 2 * (gpu/gsp_sim.c). Host only; nothing here has run on the chip. */ 3 #include "gsp_mmio.h" 4 #include "gsp_sim.h" 5 #include "ck_test.h" 6 #include 7 8 #define ARGS_PA 0x0000000123456000ull 9 #define APP_VERSION 0x0005f3a1u 10 11 static uint8_t pkt[868]; 12 static struct ck_gspsim sim; 13 static struct ck_gpu_regs regs; 14 static unsigned pauses; 15 16 static void count_pause(void *ctx) 17 { 18 (void)ctx; 19 pauses++; 20 } 21 22 /* A well-formed SEC2 boot packet, laid out like L192 gsp_boot_build: MCTP dword, NVDM COT dword, then 23 * NVDM_PAYLOAD_COT (version 1, size 860, ...). Payload bytes past the header are a pattern. */ 24 static void make_packet(void) 25 { 26 static const uint32_t hdr[3] = {0x00000001u, 0x1410DE7Eu, 1u | (860u << 16)}; 27 memcpy(pkt, hdr, sizeof(hdr)); 28 for (unsigned i = sizeof(hdr); i < sizeof(pkt); i++) 29 pkt[i] = (uint8_t)(i * 7u + 3u); 30 } 31 32 static struct ck_gsp_mmio_in base_in(void) 33 { 34 struct ck_gsp_mmio_in in = {pkt, sizeof(pkt), ARGS_PA, APP_VERSION, 100, 100, 100, 100, count_pause, 0}; 35 return in; 36 } 37 38 static struct ck_gspsim_cfg base_cfg(void) 39 { 40 struct ck_gspsim_cfg c = {0, 3, 4, 2, 5, 0, 0, 0, ARGS_PA, 0}; 41 return c; 42 } 43 44 static int run(const struct ck_gspsim_cfg *c, const struct ck_gsp_mmio_in *in, struct ck_gsp_mmio_out *o) 45 { 46 ck_gspsim_init(&sim, c); 47 regs = ck_gspsim_regs(&sim); 48 pauses = 0; 49 return ck_gsp_mmio_boot(®s, in, o); 50 } 51 52 static uint32_t writes_to(uint32_t off) 53 { 54 uint32_t n = 0; 55 for (uint32_t i = 0; i < sim.nlog && i < CK_GSPSIM_LOG_MAX; i++) 56 n += sim.log[i].write && sim.log[i].off == off; 57 return n; 58 } 59 60 static int index_of_write(uint32_t off) 61 { 62 for (uint32_t i = 0; i < sim.nlog && i < CK_GSPSIM_LOG_MAX; i++) 63 if (sim.log[i].write && sim.log[i].off == off) 64 return (int)i; 65 return -1; 66 } 67 68 static void test_happy(void) 69 { 70 struct ck_gspsim_cfg c = base_cfg(); 71 struct ck_gsp_mmio_in in = base_in(); 72 struct ck_gsp_mmio_out o; 73 CHECK(run(&c, &in, &o) == CK_GSP_OK); 74 CHECK(o.stage == CK_GSP_OK); 75 CHECK(sim.violations == 0); 76 CHECK(memcmp(sim.emem, pkt, sizeof(pkt)) == 0); /* EMEM holds the packet byte for byte */ 77 CHECK(o.dwords == 217u); 78 CHECK(o.ememc_after == (CK_GSP_EMEMC_AINCW | 3u << 8 | 25u << 2)); /* 217 = 3 blocks of 64 + 25 */ 79 CHECK(sim.qtail == 864u); /* padded size minus one dword */ 80 CHECK(index_of_write(CK_GSP_SEC2_QTAIL0) >= 0); 81 CHECK(index_of_write(CK_GSP_SEC2_QTAIL0) < index_of_write(CK_GSP_SEC2_QHEAD0)); /* TAIL then HEAD */ 82 CHECK(writes_to(CK_GSP_SEC2_QHEAD0) == 1 && writes_to(CK_GSP_SEC2_QTAIL0) == 1); 83 CHECK(writes_to(CK_GSP_SEC2_EMEMC0) == 1 && writes_to(CK_GSP_SEC2_EMEMD0) == 217u); 84 /* the first accesses, in order: HEAD, TAIL reads, EMEMC config, EMEMC read */ 85 CHECK(sim.log[0].off == CK_GSP_SEC2_QHEAD0 && !sim.log[0].write); 86 CHECK(sim.log[1].off == CK_GSP_SEC2_QTAIL0 && !sim.log[1].write); 87 CHECK(sim.log[2].off == CK_GSP_SEC2_EMEMC0 && sim.log[2].write && sim.log[2].val == CK_GSP_EMEMC_AINCW); 88 CHECK(sim.log[3].off == CK_GSP_SEC2_EMEMC0 && !sim.log[3].write); 89 CHECK(sim.os == APP_VERSION && writes_to(CK_GSP_FLCN_OS) == 1); 90 CHECK(o.cpuctl & CK_GSP_CPUCTL_ACTIVE); 91 CHECK(o.polls[0] == 0); 92 CHECK(o.polls[1] == 3); /* cmd_reads busy reads, then handled */ 93 CHECK(o.polls[2] == 4); /* mask_reads locked reads, then released */ 94 CHECK(o.polls[3] >= 5); /* args twice, then lockdown_reads */ 95 CHECK(pauses == o.polls[0] + o.polls[1] + o.polls[2] + o.polls[3]); 96 CHECK(o.gsp_mb0 == 0 && o.sec2_mb0 == 0); 97 /* the last two accesses: FALCON_OS write, CPUCTL read */ 98 uint32_t n = sim.nlog; 99 CHECK(n >= 2 && sim.log[n - 2].off == CK_GSP_FLCN_OS && sim.log[n - 2].write); 100 CHECK(sim.log[n - 1].off == CK_GSP_RISCV_CPUCTL && !sim.log[n - 1].write); 101 } 102 103 static void test_busy(void) 104 { 105 struct ck_gspsim_cfg c = base_cfg(); 106 struct ck_gsp_mmio_in in = base_in(); 107 struct ck_gsp_mmio_out o; 108 c.busy_reads = 3; 109 CHECK(run(&c, &in, &o) == CK_GSP_OK); 110 CHECK(o.polls[0] == 3 && sim.violations == 0); 111 112 c.busy_reads = CK_GSPSIM_NO_END; 113 CHECK(run(&c, &in, &o) == CK_GSP_E_BUSY); 114 CHECK(o.polls[0] == 100); 115 CHECK(writes_to(CK_GSP_SEC2_EMEMC0) == 0 && writes_to(CK_GSP_SEC2_EMEMD0) == 0); /* nothing written */ 116 CHECK(writes_to(CK_GSP_SEC2_QHEAD0) == 0); 117 118 in.polls_can_send = 0; /* budget 0: check once */ 119 CHECK(run(&c, &in, &o) == CK_GSP_E_BUSY && o.polls[0] == 0 && pauses == 0); 120 } 121 122 static void test_args(void) 123 { 124 struct ck_gsp_mmio_out o; 125 struct ck_gsp_mmio_in in = base_in(); 126 struct ck_gspsim_cfg c = base_cfg(); 127 in.packet = 0; 128 CHECK(run(&c, &in, &o) == CK_GSP_E_ARG && sim.nlog == 0); 129 in = base_in(); 130 in.len = 0; 131 CHECK(run(&c, &in, &o) == CK_GSP_E_ARG && sim.nlog == 0); 132 in.len = CK_GSP_EMEM_CHANNEL_BYTES + 1; 133 CHECK(run(&c, &in, &o) == CK_GSP_E_ARG && sim.nlog == 0); 134 CHECK(ck_gsp_mmio_boot(0, &in, &o) == CK_GSP_E_ARG); 135 CHECK(ck_gsp_mmio_boot(®s, 0, &o) == CK_GSP_E_ARG); 136 /* 1024 bytes exactly fits the channel */ 137 static uint8_t big[1024]; 138 memcpy(big, pkt, sizeof(pkt)); 139 in = base_in(); 140 in.packet = big; 141 in.len = sizeof(big); 142 c.mask_reads = CK_GSPSIM_NO_END; /* the sim rejects a non-868 packet; we only check the send here */ 143 in.polls_mask = 2; 144 CHECK(run(&c, &in, &o) == CK_GSP_E_MASK); 145 CHECK(o.dwords == 256u && sim.qtail == 1020u && sim.violations == 0); 146 } 147 148 static void test_odd_length(void) 149 { 150 struct ck_gspsim_cfg c = base_cfg(); 151 struct ck_gsp_mmio_in in = base_in(); 152 struct ck_gsp_mmio_out o; 153 in.len = 7; /* one full dword and a 3-byte tail, zero-padded */ 154 in.polls_mask = 1; 155 CHECK(run(&c, &in, &o) == CK_GSP_E_MASK); /* the sim refuses a short packet: mask never released */ 156 CHECK(o.dwords == 2u && sim.qtail == 4u); 157 CHECK(sim.emem[0] == 0x00000001u); 158 CHECK(sim.emem[1] == (0x1410DE7Eu & 0x00FFFFFFu)); /* bytes 4..6, top byte zero */ 159 CHECK(sim.violations == 0); 160 } 161 162 static void test_failures(void) 163 { 164 struct ck_gsp_mmio_out o; 165 struct ck_gsp_mmio_in in = base_in(); 166 struct ck_gspsim_cfg c; 167 168 c = base_cfg(); c.cmd_reads = CK_GSPSIM_NO_END; 169 CHECK(run(&c, &in, &o) == CK_GSP_E_SEC2); 170 CHECK(o.polls[1] == 100 && o.sec2_mb0 == CK_GSP_SEC2_CMD_BUSY); 171 CHECK(writes_to(CK_GSP_FLCN_OS) == 0); 172 173 c = base_cfg(); c.mask_reads = CK_GSPSIM_NO_END; 174 CHECK(run(&c, &in, &o) == CK_GSP_E_MASK); 175 CHECK((o.hwcfg2 & CK_GSP_PRIV_LOCKED_MASK) == CK_GSP_PRIV_LOCKED && writes_to(CK_GSP_FLCN_OS) == 0); 176 177 c = base_cfg(); c.args_reads = CK_GSPSIM_NO_END; /* the FMC never takes its arguments */ 178 CHECK(run(&c, &in, &o) == CK_GSP_E_LOCKDOWN); 179 CHECK(o.gsp_mb0 == (uint32_t)ARGS_PA && o.gsp_mb1 == (uint32_t)(ARGS_PA >> 32)); 180 CHECK(writes_to(CK_GSP_FLCN_OS) == 0); 181 182 c = base_cfg(); c.lockdown_reads = CK_GSPSIM_NO_END; 183 CHECK(run(&c, &in, &o) == CK_GSP_E_LOCKDOWN && writes_to(CK_GSP_FLCN_OS) == 0); 184 185 c = base_cfg(); c.fmc_error = 0x23; 186 CHECK(run(&c, &in, &o) == CK_GSP_E_FMC); 187 CHECK(o.gsp_mb0 == 0x23 && writes_to(CK_GSP_FLCN_OS) == 0 && sim.violations == 0); 188 189 c = base_cfg(); c.riscv_dead = 1; 190 CHECK(run(&c, &in, &o) == CK_GSP_E_RISCV); 191 CHECK(writes_to(CK_GSP_FLCN_OS) == 1 && !(o.cpuctl & CK_GSP_CPUCTL_ACTIVE)); 192 193 c = base_cfg(); c.emem_no_inc = 1; 194 CHECK(run(&c, &in, &o) == CK_GSP_E_EMEM); 195 CHECK(writes_to(CK_GSP_SEC2_QHEAD0) == 0 && writes_to(CK_GSP_SEC2_QTAIL0) == 0); /* no doorbell */ 196 197 /* a wrong packet (bad NVDM type): SEC2 consumes it and the target mask is never released */ 198 make_packet(); 199 pkt[7] = 0x15; 200 c = base_cfg(); 201 CHECK(run(&c, &in, &o) == CK_GSP_E_MASK && sim.phase == 9u); 202 make_packet(); 203 } 204 205 /* MAILBOX0 equal to the low half of the boot-argument address but MAILBOX1 not the high half: the 206 * driver treats that as an FMC code (kernel_gsp_gh100.c:521-528), so must we. */ 207 static void test_mailbox_half_match(void) 208 { 209 struct ck_gsp_mmio_out o; 210 struct ck_gsp_mmio_in in = base_in(); 211 struct ck_gspsim_cfg c = base_cfg(); 212 c.args_reads = CK_GSPSIM_NO_END; 213 c.fmc_args_pa = ARGS_PA + (1ull << 32); /* sim stuffs a different high half */ 214 CHECK(run(&c, &in, &o) == CK_GSP_E_FMC); 215 CHECK(o.gsp_mb0 == (uint32_t)ARGS_PA && writes_to(CK_GSP_FLCN_OS) == 0); 216 } 217 218 static void test_notify(void) 219 { 220 struct ck_gspsim_cfg c = base_cfg(); 221 ck_gspsim_init(&sim, &c); 222 regs = ck_gspsim_regs(&sim); 223 ck_gsp_mmio_notify(®s); 224 CHECK(sim.nlog == 1 && sim.log[0].off == CK_GSP_QHEAD0 && sim.log[0].val == 0 && sim.log[0].write); 225 CHECK(sim.gsp_qhead_writes == 1 && sim.violations == 0); 226 } 227 228 static void test_zero_hwcfg2(void) 229 { 230 /* HWCFG2 reading 0 is not a released mask (kernel_sec2_gb10b.c:282): keep waiting */ 231 struct ck_gspsim_cfg c = base_cfg(); 232 struct ck_gsp_mmio_in in = base_in(); 233 struct ck_gsp_mmio_out o; 234 c.hwcfg2_zero_reads = 2; 235 c.mask_reads = 0; 236 CHECK(run(&c, &in, &o) == CK_GSP_OK && sim.violations == 0); 237 CHECK(o.polls[2] == 2); 238 c.hwcfg2_zero_reads = CK_GSPSIM_NO_END; 239 CHECK(run(&c, &in, &o) == CK_GSP_E_MASK && o.hwcfg2 == 0 && writes_to(CK_GSP_FLCN_OS) == 0); 240 } 241 242 int main(void) 243 { 244 make_packet(); 245 test_happy(); 246 test_busy(); 247 test_args(); 248 test_odd_length(); 249 test_failures(); 250 test_mailbox_half_match(); 251 test_notify(); 252 test_zero_hwcfg2(); 253 return ck_t_verdict("test_gsp_mmio"); 254 }