interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / linux / aien-gpu / aien_gsp_boot.c

534 lines · 22591 bytes Raw Blame History
1/* aien_gsp_boot: the GB10 GSP start-up from user space over /dev/aien-gpu (L332 rung 2, window 1).
2 *
3 * Our own code end to end, NVIDIA's signed firmware on loan:
4 * 1. reads the boot package (rung2/window1-package.sh): GSP-RM .fwimage + its gb10y signature, GspRmBoot_GB10B image,
5 * GspRmFmcGfwProdSigned_GB10B image + hash + public key + signature, the 936-byte GspSystemInfo;
6 * 2. builds every block into the module's DMA arena with L192's gsp_boot_build (native/kernel/dev/gb10_gsp_boot.c),
7 * the arena's IOVA as the chip address and the module's heap buffer as the sysmem heap;
8 * 3. runs the 11 register steps of native/kernel/gpu/gsp_mmio.c over AIEN_GPU_REG_RD / AIEN_GPU_REG_WR (the module
9 * lets only the start-up contract's registers through and rings the SEC2 doorbell once per load);
10 * 4. if GSP-RM's core runs, reads the status queue (gsp_mq_recv) until GSP_INIT_DONE, a sequencer request (not
11 * handled in window 1: recorded, then stop) or the time limit;
12 * 5. writes every register access with its time, every status message, the GSP log buffers and a one-line verdict
13 * (AIEN_GSP_BOOT: ...) into the output directory. Pass or fail, all of it is evidence.
14 *
15 * Built with -DAIEN_GSP_BOOT_SIM it runs the same code against the simulated SEC2 + GSP (gpu/gsp_sim.c) and a fake GSP
16 * that answers on the status queue, so every path is exercised on a host with no GPU (rung2/window1_sim_test.sh).
17 *
18 * aien_gsp_boot --pkg DIR --app-version N --out DIR [--polls N] [--pause-us N] [--wait-ms N] [--plan-only]
19 * sim build only: [--sim-events init|seq|none|junk] [--sim-cfg happy|busy|fmc-error|riscv-dead] [--sim-arena-mib N]
20 * Exit 0 = INIT-DONE (or PLAN-ONLY), 1 = the chip stopped somewhere (verdict says where), 2 = refused before the chip.
21 */
22#include <errno.h>
23#include <fcntl.h>
24#include <stdarg.h>
25#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <sys/stat.h>
32#include <time.h>
33#include <unistd.h>
34
35#include "gb10_gsp_boot.h"
36#include "gsp_mmio.h"
37#include "gsp_seq.h"
38#ifdef AIEN_GSP_BOOT_SIM
39#include "gsp_sim.h"
40static uint8_t *volatile g_host_arena; /* the simulated arena; volatile so the store survives -O and the leak check sees it */
41#else
42#include "aien_gpu_uapi.h"
43#endif
44
45#define MSG_CAP (GSP_ELEM_SIZE_MAX + 8u)
46
47static const char *g_out;
48static FILE *g_trace;
49static struct timespec g_t0;
50
51static uint64_t now_us(void)
52{
53 struct timespec t;
54 clock_gettime(CLOCK_MONOTONIC, &t);
55 return (uint64_t)((t.tv_sec - g_t0.tv_sec) * 1000000 + (t.tv_nsec - g_t0.tv_nsec) / 1000);
56}
57
58static void die(const char *verdict, const char *fmt, ...)
59{
60 va_list ap;
61 va_start(ap, fmt);
62 fprintf(stderr, "aien_gsp_boot: ");
63 vfprintf(stderr, fmt, ap);
64 fprintf(stderr, "\n");
65 va_end(ap);
66 printf("AIEN_GSP_BOOT: %s\n", verdict);
67 exit(2);
68}
69
70static FILE *out_file(const char *name, const char *mode)
71{
72 char p[4096];
73 snprintf(p, sizeof p, "%s/%s", g_out, name);
74 FILE *f = fopen(p, mode);
75 if (!f)
76 die("TOOL-ERROR", "%s: %s", p, strerror(errno));
77 return f;
78}
79
80static void out_bytes(const char *name, const void *p, size_t n)
81{
82 FILE *f = out_file(name, "wb");
83 if (n && fwrite(p, 1, n, f) != n)
84 die("TOOL-ERROR", "write %s", name);
85 fclose(f);
86}
87
88/* every loaded package file stays reachable until exit (one-shot tool; the sanitized sim build checks for leaks) */
89static uint8_t *volatile g_loaded[8];
90static unsigned g_nloaded;
91
92static uint8_t *load(const char *dir, const char *name, size_t *n, size_t want)
93{
94 char p[4096];
95 snprintf(p, sizeof p, "%s/%s", dir, name);
96 FILE *f = fopen(p, "rb");
97 if (!f)
98 die("PACKAGE-REFUSED", "%s: %s", p, strerror(errno));
99 struct stat st;
100 if (fstat(fileno(f), &st) || st.st_size <= 0)
101 die("PACKAGE-REFUSED", "%s: empty", p);
102 if (want && (size_t)st.st_size != want)
103 die("PACKAGE-REFUSED", "%s: %lld bytes, want %zu", p, (long long)st.st_size, want);
104 uint8_t *b = malloc((size_t)st.st_size);
105 if (!b || fread(b, 1, (size_t)st.st_size, f) != (size_t)st.st_size)
106 die("PACKAGE-REFUSED", "%s: read", p);
107 fclose(f);
108 if (g_nloaded < sizeof g_loaded / sizeof g_loaded[0])
109 g_loaded[g_nloaded++] = b;
110 *n = (size_t)st.st_size;
111 return b;
112}
113
114/* ---- the register path: the device through the module, or the simulator ---- */
115struct regs_ctx {
116 int fd;
117 int err; /* first refused access (errno); after it nothing more reaches the chip */
118 uint32_t err_off;
119 unsigned long n;
120#ifdef AIEN_GSP_BOOT_SIM
121 struct ck_gpu_regs sim;
122#endif
123};
124
125static uint32_t rd(void *c, uint32_t off)
126{
127 struct regs_ctx *x = c;
128 uint32_t v = 0xffffffffu;
129 if (!x->err) {
130#ifdef AIEN_GSP_BOOT_SIM
131 v = x->sim.rd(x->sim.ctx, off);
132#else
133 struct aien_gpu_reg r = {off, 0};
134 if (ioctl(x->fd, AIEN_GPU_REG_RD, &r)) {
135 x->err = errno;
136 x->err_off = off;
137 } else {
138 v = r.val;
139 }
140#endif
141 }
142 x->n++;
143 fprintf(g_trace, "R %06x %08x t=%llu%s\n", off, v, (unsigned long long)now_us(), x->err ? " REFUSED" : "");
144 return v;
145}
146
147static void wr(void *c, uint32_t off, uint32_t v)
148{
149 struct regs_ctx *x = c;
150 if (!x->err) {
151#ifdef AIEN_GSP_BOOT_SIM
152 x->sim.wr(x->sim.ctx, off, v);
153#else
154 struct aien_gpu_reg r = {off, v};
155 if (ioctl(x->fd, AIEN_GPU_REG_WR, &r)) {
156 x->err = errno;
157 x->err_off = off;
158 }
159#endif
160 }
161 x->n++;
162 fprintf(g_trace, "W %06x %08x t=%llu%s\n", off, v, (unsigned long long)now_us(), x->err ? " REFUSED" : "");
163}
164
165static unsigned g_pause_us = 100;
166
167static void pause_fn(void *c)
168{
169 struct regs_ctx *x = c;
170 if (x->err || !g_pause_us)
171 return; /* refused: let the budget run out fast */
172 struct timespec t = {0, (long)g_pause_us * 1000};
173 nanosleep(&t, 0);
174}
175
176static const char *stage_name(int s)
177{
178 switch (s) {
179 case CK_GSP_OK: return "OK";
180 case CK_GSP_E_ARG: return "E_ARG";
181 case CK_GSP_E_BUSY: return "E_BUSY (SEC2 channel never emptied)";
182 case CK_GSP_E_EMEM: return "E_EMEM (EMEM write pointer did not move)";
183 case CK_GSP_E_SEC2: return "E_SEC2 (SEC2 never handled the packet)";
184 case CK_GSP_E_MASK: return "E_MASK (GSP target mask never released)";
185 case CK_GSP_E_LOCKDOWN: return "E_LOCKDOWN (no lockdown release, no FMC code)";
186 case CK_GSP_E_FMC: return "E_FMC (FMC error code in GSP MAILBOX0)";
187 case CK_GSP_E_RISCV: return "E_RISCV (GSP RISC-V core not active)";
188 }
189 return "?";
190}
191
192static void region(FILE *f, const char *name, gsp_region r)
193{
194 fprintf(f, "%-12s pa=%#llx size=%#llx\n", name, (unsigned long long)r.pa, (unsigned long long)r.size);
195}
196
197static const char *log_names[GSP_GB10B_LOG_COUNT] = {"LOGINIT", "LOGINTR", "LOGRM", "LOGMNOC", "LOGDBG", "LOGROOT", "LOGKRNL"};
198
199static void dump_logs(const gsp_arena *a, const gsp_boot_plan *p)
200{
201 char n[64];
202 for (unsigned i = 0; i < GSP_GB10B_LOG_COUNT; i++) {
203 snprintf(n, sizeof n, "log-%s.bin", log_names[i]);
204 out_bytes(n, a->base + (p->log[i].pa - a->pa), p->log[i].size);
205 }
206 out_bytes("rpc-shared.bin", a->base + (p->rpc_shared.pa - a->pa), p->rpc_shared.size);
207}
208
209#ifdef AIEN_GSP_BOOT_SIM
210/* The fake GSP: creates the status queue the way GSP-RM's msgqTxCreate would and posts messages on it. With the two
211 * queue pointers swapped, gsp_mq_send writes the status queue and reads our readPtr (SWAP_RX), as GSP-RM does. */
212static void sim_gsp_answer(const gsp_arena *a, const gsp_boot_plan *p, const char *events)
213{
214 uint8_t *statq = a->base + (p->statq_pa - a->pa), *cmdq = a->base + (p->cmdq_pa - a->pa);
215 const uint32_t h[8] = {0, GSP_MQ_QUEUE_SIZE, GSP_MQ_MSG_SIZE, GSP_MQ_MSG_COUNT, 0, GSP_MQ_FLAGS_SWAP_RX,
216 GSP_MQ_RXHDR_OFF, GSP_MQ_ENTRY_OFF};
217 if (!strcmp(events, "none"))
218 return;
219 memcpy(statq, h, sizeof h);
220 gsp_mq gsp = {.cmdq = statq, .statq = cmdq};
221 const char *why;
222 static const char hello[] = "fake LIBOS print";
223 gsp_seg s = {hello, sizeof hello};
224 if (gsp_mq_send(&gsp, 0x1007u, &s, 1, sizeof hello, 0, 0, &why)) /* an event the tool does not act on */
225 die("TOOL-ERROR", "sim send: %s", why);
226 if (!strcmp(events, "junk")) {
227 uint32_t two = 2; /* writePtr past an empty slot: element count 0, a corrupt queue */
228 memcpy(statq + 16, &two, 4);
229 return;
230 }
231 if (!strcmp(events, "seq-regs")) {
232 /* rpc_run_cpu_sequencer_v17_00: size, cmdIndex, save[8], then commands.
233 * One REG_WRITE of falcon mailbox0, then a following INIT_DONE. */
234 uint32_t seqb[14] = {0};
235 seqb[0] = 4;
236 seqb[1] = 3;
237 seqb[10] = 0;
238 seqb[11] = 0x00110c00; /* GSP queue head, on the module write allowlist */
239 seqb[12] = 0x7;
240 gsp_seg se = {seqb, sizeof seqb};
241 if (gsp_mq_send(&gsp, GSP_EV_RUN_CPU_SEQUENCER, &se, 1, sizeof seqb, 0, 0, &why))
242 die("TOOL-ERROR", "sim send: %s", why);
243 uint8_t done[8] = {0};
244 gsp_seg de = {done, sizeof done};
245 if (gsp_mq_send(&gsp, GSP_EV_INIT_DONE, &de, 1, sizeof done, 0, 0, &why))
246 die("TOOL-ERROR", "sim send: %s", why);
247 return;
248 }
249 uint8_t body[64] = {0};
250 gsp_seg e = {body, sizeof body};
251 uint32_t fn = !strcmp(events, "seq") ? GSP_EV_RUN_CPU_SEQUENCER : GSP_EV_INIT_DONE;
252 if (gsp_mq_send(&gsp, fn, &e, 1, sizeof body, 0, 0, &why))
253 die("TOOL-ERROR", "sim send: %s", why);
254}
255#endif
256
257int main(int argc, char **argv)
258{
259 const char *pkg = 0;
260 uint32_t app_version = 0, polls = 50000, wait_ms = 30000;
261 int have_app = 0, plan_only = 0;
262#ifdef AIEN_GSP_BOOT_SIM
263 const char *sim_events = "init", *sim_cfg = "happy";
264 unsigned sim_arena_mib = 192;
265#endif
266 for (int i = 1; i < argc; i++) {
267 const char *a = argv[i], *v = i + 1 < argc ? argv[i + 1] : 0;
268 if (!strcmp(a, "--plan-only")) { plan_only = 1; continue; }
269 if (!v) die("USAGE", "%s needs a value", a);
270 i++;
271 if (!strcmp(a, "--pkg")) pkg = v;
272 else if (!strcmp(a, "--out")) g_out = v;
273 else if (!strcmp(a, "--app-version")) { app_version = (uint32_t)strtoul(v, 0, 0); have_app = 1; }
274 else if (!strcmp(a, "--polls")) polls = (uint32_t)strtoul(v, 0, 0);
275 else if (!strcmp(a, "--pause-us")) g_pause_us = (unsigned)strtoul(v, 0, 0);
276 else if (!strcmp(a, "--wait-ms")) wait_ms = (uint32_t)strtoul(v, 0, 0);
277#ifdef AIEN_GSP_BOOT_SIM
278 else if (!strcmp(a, "--sim-events")) sim_events = v;
279 else if (!strcmp(a, "--sim-cfg")) sim_cfg = v;
280 else if (!strcmp(a, "--sim-arena-mib")) sim_arena_mib = (unsigned)strtoul(v, 0, 0);
281#endif
282 else die("USAGE", "unknown option %s", a);
283 }
284 if (!pkg || !g_out || !have_app)
285 die("USAGE", "need --pkg DIR --app-version N --out DIR");
286 if (g_pause_us > 100000)
287 die("USAGE", "--pause-us at most 100000");
288 if (mkdir(g_out, 0755) && errno != EEXIST)
289 die("TOOL-ERROR", "%s: %s", g_out, strerror(errno));
290 clock_gettime(CLOCK_MONOTONIC, &g_t0);
291 g_trace = out_file("regs.txt", "w");
292 setvbuf(g_trace, 0, _IOLBF, 0);
293
294 /* 1. the package */
295 size_t n_elf, n_sig, n_bl, n_fmc, n_h, n_k, n_s, n_si;
296 gsp_boot_in in = {0};
297 in.rm_elf = load(pkg, "rm_elf.bin", &n_elf, 0);
298 in.rm_sig = load(pkg, "rm_sig.bin", &n_sig, 0);
299 in.bootloader = load(pkg, "bootloader.bin", &n_bl, GSP_GB10B_BOOTLOADER_SIZE);
300 in.fmc = load(pkg, "fmc.bin", &n_fmc, 0);
301 in.fmc_hash = load(pkg, "fmc_hash.bin", &n_h, GSP_COT_HASH_SIZE);
302 in.fmc_pubkey = load(pkg, "fmc_pkey.bin", &n_k, GSP_COT_KEY_SIZE);
303 in.fmc_sig = load(pkg, "fmc_sig.bin", &n_s, GSP_COT_SIG_SIZE);
304 in.system_info = load(pkg, "system_info.bin", &n_si, GSP_SYSTEM_INFO_SIZE);
305 in.rm_elf_size = n_elf;
306 in.rm_sig_size = (uint32_t)n_sig;
307 in.bootloader_size = (uint32_t)n_bl;
308 in.fmc_size = (uint32_t)n_fmc;
309 in.bl_code_off = GSP_GB10B_BL_CODE_OFF;
310 in.bl_data_off = GSP_GB10B_BL_DATA_OFF;
311 in.bl_manifest_off = GSP_GB10B_BL_MANIFEST_OFF;
312
313 /* 2. the memory: the module's arena and heap, or host memory standing in for them */
314 struct regs_ctx rc = {.fd = -1};
315 uint8_t *base;
316 uint64_t arena_pa, arena_bytes, heap_pa, heap_bytes;
317 unsigned dma_bits;
318#ifdef AIEN_GSP_BOOT_SIM
319 arena_bytes = (uint64_t)sim_arena_mib << 20;
320 arena_pa = 0x0000004000000000ull; /* SIM-CHOICE: an IOVA-looking address below 40 bits */
321 heap_pa = 0x0000005000000000ull;
322 heap_bytes = 512ull << 20;
323 dma_bits = 40;
324 base = aligned_alloc(GSP_PAGE, arena_bytes);
325 g_host_arena = base; /* reachable until exit */
326 if (!base)
327 die("TOOL-ERROR", "no memory for the host arena");
328#else
329 rc.fd = open("/dev/aien-gpu", O_RDWR);
330 if (rc.fd < 0)
331 die("NO-DEVICE", "/dev/aien-gpu: %s", strerror(errno));
332 struct aien_gpu_info info;
333 if (ioctl(rc.fd, AIEN_GPU_INFO, &info))
334 die("NO-DEVICE", "AIEN_GPU_INFO: %s", strerror(errno));
335 FILE *fi = out_file("info.txt", "w");
336 fprintf(fi, "boot0=%08x boot42=%08x bar0_pa=%#llx bar0_bytes=%llu arena=%#llx@%#llx heap=%#llx@%#llx dma_bits=%u allow_boot=%u doorbells=%u\n",
337 info.boot0, info.boot42, (unsigned long long)info.bar0_pa, (unsigned long long)info.bar0_bytes,
338 (unsigned long long)info.arena_bytes, (unsigned long long)info.arena_iova,
339 (unsigned long long)info.heap_bytes, (unsigned long long)info.heap_iova, info.dma_bits, info.allow_boot,
340 info.doorbells);
341 fclose(fi);
342 if (!plan_only && (!info.allow_boot || info.doorbells))
343 die("REFUSED-BY-SHIM", "allow_boot=%u doorbells=%u: load with allow_boot=1, one attempt per load",
344 info.allow_boot, info.doorbells);
345 if (!info.heap_bytes)
346 die("REFUSED-BY-SHIM", "no heap: load with heap_mib=512");
347 arena_bytes = info.arena_bytes;
348 arena_pa = info.arena_iova;
349 heap_pa = info.heap_iova;
350 heap_bytes = info.heap_bytes;
351 dma_bits = info.dma_bits;
352 base = mmap(0, arena_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, rc.fd, 0);
353 if (base == MAP_FAILED)
354 die("NO-DEVICE", "mmap arena: %s", strerror(errno));
355#endif
356 in.sysmem_heap_pa = heap_pa;
357 in.sysmem_heap_size = heap_bytes;
358 in.dma_bits = dma_bits;
359
360 /* The first arena page is the zeroed 4 KiB "notify op" surface NVIDIA's driver allocates before
361 GSP_SET_SYSTEM_INFO (kernel_gsp.c:3489-3526 _kgspAllocNotifyOpSharedSurface); its address goes into
362 GspSystemInfo.notifyOpSharedSurfacePhysAddr (offset 48, gsp_static_config.h:180) unless the package set one. */
363 if (arena_bytes <= GSP_PAGE)
364 die("BUILD-REFUSED", "arena too small for the notify page");
365 memset(base, 0, GSP_PAGE);
366 static uint8_t si_sent[GSP_SYSTEM_INFO_SIZE];
367 memcpy(si_sent, in.system_info, GSP_SYSTEM_INFO_SIZE);
368 uint64_t notify_pa = 0;
369 for (int i = 0; i < 8; i++)
370 notify_pa |= (uint64_t)si_sent[48 + i] << (8 * i);
371 if (!notify_pa) {
372 notify_pa = arena_pa;
373 for (int i = 0; i < 8; i++)
374 si_sent[48 + i] = (uint8_t)(notify_pa >> (8 * i));
375 }
376 in.system_info = si_sent;
377 FILE *fs = out_file("system_info.sent.bin", "wb");
378 if (fwrite(si_sent, 1, sizeof si_sent, fs) != sizeof si_sent)
379 die("TOOL-ERROR", "write system_info.sent.bin");
380 fclose(fs);
381
382 gsp_arena arena;
383 if (gsp_arena_init(&arena, base + GSP_PAGE, arena_pa + GSP_PAGE, arena_bytes - GSP_PAGE))
384 die("BUILD-REFUSED", "arena not 4 KiB aligned");
385 static gsp_boot_plan plan;
386 const char *why = 0;
387 if (gsp_boot_build(&arena, &in, &plan, &why))
388 die("BUILD-REFUSED", "gsp_boot_build: %s (arena %#llx bytes)", why, (unsigned long long)arena_bytes);
389 FILE *fp = out_file("plan.txt", "w");
390 fprintf(fp, "arena pa=%#llx cap=%#llx used=%#llx heap pa=%#llx size=%#llx dma_bits=%u app_version=%u\n",
391 (unsigned long long)arena.pa, (unsigned long long)arena.cap, (unsigned long long)arena.used,
392 (unsigned long long)heap_pa, (unsigned long long)heap_bytes, dma_bits, app_version);
393 fprintf(fp, "notify_page pa=%#llx\n", (unsigned long long)notify_pa);
394 region(fp, "wpr_meta", plan.wpr_meta);
395 region(fp, "libos_args", plan.libos_args);
396 for (unsigned i = 0; i < GSP_GB10B_LOG_COUNT; i++)
397 region(fp, log_names[i], plan.log[i]);
398 region(fp, "args_cached", plan.args_cached);
399 region(fp, "rpc_shared", plan.rpc_shared);
400 region(fp, "radix3", plan.radix3);
401 region(fp, "bootloader", plan.bootloader);
402 region(fp, "signature", plan.signature);
403 region(fp, "fmc", plan.fmc);
404 region(fp, "fmc_params", plan.fmc_params);
405 fprintf(fp, "radix3_levels %u %u %u %u elf_data_pa=%#llx cmdq_write_ptr=%u tx_seq=%u cot_packet_len=%u emem_queue_tail=%u\n",
406 plan.radix3_level_pages[0], plan.radix3_level_pages[1], plan.radix3_level_pages[2],
407 plan.radix3_level_pages[3], (unsigned long long)plan.elf_data_pa, plan.cmdq_write_ptr, plan.tx_seq,
408 plan.cot_packet_len, plan.emem_queue_tail);
409 fclose(fp);
410 out_bytes("cot_packet.bin", plan.cot_packet, plan.cot_packet_len);
411 if (plan_only) {
412 printf("AIEN_GSP_BOOT: PLAN-ONLY (arena used %#llx of %#llx)\n", (unsigned long long)arena.used,
413 (unsigned long long)arena.cap);
414 return 0;
415 }
416
417 /* 3. the 11 register steps */
418#ifdef AIEN_GSP_BOOT_SIM
419 static struct ck_gspsim sim;
420 struct ck_gspsim_cfg cfg = {0, 3, 4, 2, 5, 0, 0, 0, plan.fmc_params.pa, 0};
421 if (!strcmp(sim_cfg, "busy")) cfg.busy_reads = CK_GSPSIM_NO_END;
422 else if (!strcmp(sim_cfg, "fmc-error")) cfg.fmc_error = 0x23;
423 else if (!strcmp(sim_cfg, "riscv-dead")) cfg.riscv_dead = 1;
424 else if (strcmp(sim_cfg, "happy")) die("USAGE", "unknown --sim-cfg %s", sim_cfg);
425 ck_gspsim_init(&sim, &cfg);
426 rc.sim = ck_gspsim_regs(&sim);
427#endif
428 struct ck_gpu_regs regs = {rd, wr, &rc};
429 struct ck_gsp_mmio_in mi = {plan.cot_packet, plan.cot_packet_len, plan.fmc_params.pa, app_version,
430 polls, polls, polls, polls, pause_fn, &rc};
431 struct ck_gsp_mmio_out mo;
432 uint64_t t_boot = now_us();
433 int stage = ck_gsp_mmio_boot(&regs, &mi, &mo);
434 uint64_t t_core = now_us();
435 FILE *fr = out_file("result.txt", "w");
436 fprintf(fr, "stage=%d %s boot_us=%llu polls=%u,%u,%u,%u dwords=%u ememc=%#x sec2_mb=%#x,%#x hwcfg2=%#x gsp_mb=%#x,%#x cpuctl=%#x accesses=%lu\n",
437 stage, stage_name(stage), (unsigned long long)(t_core - t_boot), mo.polls[0], mo.polls[1], mo.polls[2],
438 mo.polls[3], mo.dwords, mo.ememc_after, mo.sec2_mb0, mo.sec2_mb1, mo.hwcfg2, mo.gsp_mb0, mo.gsp_mb1,
439 mo.cpuctl, rc.n);
440 if (rc.err) {
441 fprintf(fr, "shim refused offset %#x: %s\n", rc.err_off, strerror(rc.err));
442 fclose(fr);
443 dump_logs(&arena, &plan);
444 die("REFUSED-BY-SHIM", "register %#x: %s", rc.err_off, strerror(rc.err));
445 }
446 if (stage != CK_GSP_OK) {
447 fclose(fr);
448 dump_logs(&arena, &plan);
449 printf("AIEN_GSP_BOOT: STOPPED-AT %s\n", stage_name(stage));
450 return 1;
451 }
452
453 /* 4. GSP-RM's core runs: wait on the status queue */
454#ifdef AIEN_GSP_BOOT_SIM
455 sim_gsp_answer(&arena, &plan, sim_events);
456#endif
457 gsp_mq q;
458 if (gsp_mq_attach(&q, &arena, &plan))
459 die("TOOL-ERROR", "gsp_mq_attach");
460 static uint8_t msg[MSG_CAP];
461 const char *verdict = "NO-INIT-DONE (time limit)";
462 int ret = 1, seen_queue = 0;
463 unsigned nmsg = 0;
464 FILE *fm = out_file("messages.txt", "w");
465 while (now_us() - t_core < (uint64_t)wait_ms * 1000u) {
466 uint32_t fn = 0, res = 0;
467 int r = gsp_mq_recv(&q, msg, sizeof msg, &fn, &res, &why);
468 if (r < 0 && !seen_queue && why && !strcmp(why, "status queue header not the driver's shape")) {
469 struct timespec t = {0, 1000000};
470 nanosleep(&t, 0); /* GSP-RM has not created its queue yet */
471 continue;
472 }
473 if (r < 0) {
474 fprintf(fm, "t=%llu RECV-ERROR %s\n", (unsigned long long)now_us(), why);
475 verdict = "STATUS-QUEUE-ERROR";
476 break;
477 }
478 seen_queue = 1;
479 if (r == 0) {
480 struct timespec t = {0, 1000000};
481 nanosleep(&t, 0);
482 continue;
483 }
484 uint32_t len;
485 memcpy(&len, msg + GSP_ELEM_HDR_SIZE + 8, 4); /* rpc.length */
486 fprintf(fm, "t=%llu msg %u function=%#x rpc_result=%#x length=%u\n", (unsigned long long)now_us(), nmsg, fn,
487 res, len);
488 char name[64];
489 snprintf(name, sizeof name, "msg-%03u-%#x.bin", nmsg, fn);
490 out_bytes(name, msg, GSP_ELEM_HDR_SIZE + len);
491 nmsg++;
492 if (fn == GSP_EV_INIT_DONE) {
493 verdict = "INIT-DONE";
494 ret = 0;
495 break;
496 }
497 if (fn == GSP_EV_RUN_CPU_SEQUENCER) {
498 /* Replaces kgspExecuteSequencerBuffer for register opcodes. Core opcodes
499 * still refuse (they restart the chip). A buffer we cannot run keeps the
500 * old stop, with the payload saved. */
501 uint32_t pay = len > GSP_RPC_HDR_SIZE ? len - GSP_RPC_HDR_SIZE : 0;
502 uint32_t words[64];
503 const char *sw = 0;
504 int ran = 0;
505 if (pay >= 40 && pay / 4 <= 64 && (pay % 4) == 0) {
506 memcpy(words, msg + GSP_ELEM_HDR_SIZE + GSP_RPC_HDR_SIZE, pay);
507 gsp_seq_io sio;
508 memset(&sio, 0, sizeof sio);
509 sio.rd = rd;
510 sio.wr = wr;
511 sio.ctx = &rc;
512 sio.poll_budget = 8;
513 if (words[0] <= pay / 4 - 10 &&
514 gsp_seq_run(words + 10, words[0], words[1], &sio, &sw) == 0 && !rc.err)
515 ran = 1;
516 }
517 if (ran) {
518 fprintf(fm, "sequencer ran\n");
519 continue;
520 }
521 verdict = "SEQUENCER-REQUESTED (not handled in window 1; payload saved)";
522 break;
523 }
524 }
525 uint64_t t_end = now_us();
526 fclose(fm);
527 uint32_t mb0 = rd(&rc, CK_GSP_FLCN_MAILBOX0), mb1 = rd(&rc, CK_GSP_FLCN_MAILBOX1);
528 fprintf(fr, "wait_us=%llu messages=%u verdict=%s gsp_mb=%#x,%#x\n", (unsigned long long)(t_end - t_core), nmsg,
529 verdict, mb0, mb1);
530 fclose(fr);
531 dump_logs(&arena, &plan);
532 printf("AIEN_GSP_BOOT: %s\n", verdict);
533 return ret;
534}