interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / spark / spark_main.om

1536 lines · 35828 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/spark/spark_main.om: Unified Bare-Metal Aienux SPARK.EFI Entry Point (Phase 3).
3note Wires all 6 pure-Omega slices (A -> B -> D -> C -> E -> F) into one compact target a64efi PE32+:
4note Stage 1 (Slice A): UEFI ExitBootServices -> EL2->EL1h -> MMU (MAIR 0x440004ff, 512 MiB Normal-NC)
5note -> GICv3 + ITS (dedicated boot LPI slot 64) -> 20-core Grace SMP (arm64_ldar)
6note Stage 2 (Slice B): Multi-segment PCIe MCFG/ECAM (Bus 0..1) -> GB20B/NVMe/RTL8127 probe
7note -> ACPI IORT + SMMUv3 A/B/C (per-stream disjoint Normal-NC DMA + 3 RMRs)
8note -> BAR0 MMIO mode 1 (fwld/fwst+dsb sy) -> irqbm iqmode=2
9note Stage 3 (Slice D): Strictly RO NVMe -> GPT -> 64-bit ext4 -> 80 MiB OMFWBND1 bundle load
10note + e4_toc parse + per-entry SHA-256 verification -> RTL8127 NIC + TCP MAC bind
11note Stage 4 (Slice C): GB20B GSP bring-up (sireset/siseti/siencode -> fbparseelf(entry1) + fbbindblobs
12note + fbbindsig -> 80 MiB abbuild -> sccfg(abpa 1, fbappver) -> gpkt/gin/s2boot
13note -> tbattach(abcmdpa, abstatpa) -> breset/bsys/breg/bstart -> bpoll/dspauto/
14note bguest/bstatic to bstate==9 -> gmmu_bm identity VA==PA + mminval + rmchan
15note 0xca6f/0xcec0 + RPC 54 bsetpd + cmbind(marker_va + 16))
16note Stage 5 (Slice E): compute.om + sm_121 pure-.om SASS codegen (cg_dec->bc_regalloc->bw_enc_sm121)
17note -> q38_run.om (q3_init + q3_set_code + fail-closed weight/kernel check)
18note Stage 6 (Slice F): wire_loop.om OMTK v1 on TCP port 18100 (completion poll wired to marker_va + 16)
19note Zero Linux syscalls, zero 184 MiB static .bss, zero firmware bytes embedded.
20note Hardware status: HOST_AND_QEMU_VERIFIED (Spark silicon NOT_RUN).
21
22const EFI_MMAP_CAP ulong 65536
23const SPK_GB20B_VID ulong 0x10de
24const SPK_RTL_VID ulong 0x10ec
25const SPK_RTL_DID ulong 0x8127
26const SPK_OMTK_PORT ulong 18100
27const SPK_MAX_CTX ulong 4096
28const SPK_VOCAB ulong 248320
29const SPK_STATIC_IP ulong 3232235884
30const SPK_GATEWAY ulong 3232235777
31const SPK_NETMASK ulong 4294967040
32const SPK_ISN ulong 12345
33const SPK_BND_CAP ulong 83886080
34const SPK_ARENA_CAP ulong 83886080
35const SPK_GB20B_BDF ulong 64424509696
36const SPK_FMC_HASH_SZ ulong 48
37const SPK_FMC_PKEY_SZ ulong 384
38const SPK_FMC_SIG_SZ ulong 384
39
40note Error codes emitted by spk_fail(stage, code):
41const SPK_E_GIC ulong 101
42const SPK_E_ITS ulong 102
43const SPK_E_SMP ulong 103
44const SPK_E_NO_MCFG ulong 200
45const SPK_E_NO_GB20B ulong 201
46const SPK_E_BAR0_MAP ulong 202
47const SPK_E_GB20B_ID ulong 203
48const SPK_E_QUIESCE ulong 204
49const SPK_E_SMMU ulong 205
50const SPK_E_IRQ_BIND ulong 206
51const SPK_E_PCIE_EN ulong 207
52const SPK_E_SMMU_BASE ulong 208
53const SPK_E_NVME_INIT ulong 301
54const SPK_E_GPT ulong 302
55const SPK_E_EXT4 ulong 303
56const SPK_E_BUNDLE ulong 304
57const SPK_E_RTL ulong 305
58const SPK_E_BMHAL ulong 401
59const SPK_E_FWELF ulong 402
60const SPK_E_ARENA ulong 403
61const SPK_E_SEC2 ulong 404
62const SPK_E_GSPBOOT ulong 405
63const SPK_E_GMMU ulong 406
64const SPK_E_RMCHAN ulong 407
65const SPK_E_CMBIND ulong 408
66const SPK_E_COMPUTE ulong 501
67const SPK_E_SASS ulong 502
68const SPK_E_Q3INIT ulong 503
69const SPK_E_WEIGHTS ulong 504
70
71global efi_mmap uchar 65536
72global efi_mm_out uchar 64
73
74note spk_st:
75note 0=gb20b_bar_h, 1=sgpu_sh, 2=snvme_sh, 3=nvme_dma_pa, 4=rtl_dma_pa,
76note 5=bnd_pa, 6=bnd_cap, 7=bnd_len, 8=arena_pa, 9=heap_pa,
77note 10=pb_pa, 11=marker_pa, 12=nc_base, 13=wb_base, 14=chan_gen,
78note 15=snic_sh, 16=code_pa, 17=gpput, 18=nvme_sec_pa, 19=sass_bytes
79global spk_st ulong 24
80global spk_nvme_bar ulong 1
81global spk_rtl_bar ulong 1
82global spk_gb20b_bar ulong 1
83note Bundle entry PA/size indexed by kind 1..7: spk_kpa[kind], spk_ksz[kind]
84global spk_kpa ulong 8
85global spk_ksz ulong 8
86
87text spk_s1 "\n[SPARK 1/6] UEFI->EL1h MMU GICv3+ITS 20-core SMP\n"
88text spk_s2 "[SPARK 2/6] PCIe MCFG/ECAM(Bus 0..1) SMMUv3 A/B/C BAR0-MMIO(mode=1) irqbm(iqmode=2)\n"
89text spk_s3 "[SPARK 3/6] RO-NVMe GPT ext4 80MiB OMFWBND1(TOC+SHA256) RTL8127/TCP\n"
90text spk_s4 "[SPARK 4/6] GB20B GSP(sysinfo+fw_bm+80MiB abbuild+sccfg+SEC2+boot->9) GMMU(VA==PA)+bsetpd rmchan\n"
91text spk_s5 "[SPARK 5/6] compute.om sm_121-SASS q38_run.om(marker+16)\n"
92text spk_s6 "[SPARK 6/6] OMTK v1 wire_loop READY tcp_port=18100\n"
93text spk_el1 "EL1_ALIVE current_el="
94text spk_mair " mair=0x"
95text spk_sctlr " sctlr=0x"
96text spk_tcr " tcr=0x"
97text spk_ram " free_mib="
98text spk_nc " nc_pool_pa=0x"
99text spk_ncsz " nc_pool_mib="
100text spk_mcfg " mcfg_segs="
101text spk_gic " GICV3_OK rev="
102text spk_its " ITS_OK cmds="
103text spk_smp " SMP_ONLINE online="
104text spk_exp " expected="
105text spk_fl " failed="
106text spk_ms " boot_ms="
107text spk_fail_a "[SPARK FAIL stage="
108text spk_fail_b " code="
109text spk_fail_c "]\n"
110text spk_bnd_path "/aienos/spark-bundle.img"
111text spk_q_s3net "[SPARK QEMU] stage 3 bundle verified; RTL8127/TCP STUB (no QEMU model)\n"
112text spk_q_stop "SPARK_QEMU_E2E_PASS real=stage1,stage2(pcie+smmuv3+streams+bme),stage3(nvme+gpt+ext4+omfwbnd1-sha256) stubbed=gb20b-regs,stage3-net,stage4,stage5,stage6\n"
113
114note ========================================================================
115note Cross-Slice Bare-Metal Seam Implementations (Pieces 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6)
116note ========================================================================
117
118fn a64_dsb_sy to void
119arm64_dsb_sy
120end
121
122fn a64_dc_civac_range pa ulong nbytes ulong to void
123arm64_dc_civac_range pa nbytes
124end
125
126fn q3_dc_civac pa ulong nbytes ulong to void
127arm64_dc_civac_range pa nbytes
128end
129
130note Piece 3 dead-simulator-branch guards (never called in gspmode=2 / bare-metal)
131fn s2simrd off ulong to ulong
132trap 9
133end
134
135fn s2simwr off ulong v ulong to void
136trap 9
137end
138
139fn gsbind chid uint tok uint ent uint gpva ulong upa ulong ipa ulong to long
140trap 9
141end
142
143fn gsdisable chid uint to long
144trap 9
145end
146
147fn gsidle chid uint to long
148trap 9
149end
150
151note Piece 3 (bm_hal.om) BAR0/Normal-NC/CNTVCT callbacks -> Piece 2 bar0_mmio (mode=1) & Piece 1 fwops
152fn bmrbarrd off ulong to ulong
153ret bmmio_r32 long get spk_st 0 off
154end
155
156fn bmrbarwr off ulong v ulong to void
157r = bmmio_w32 long get spk_st 0 off v
158end
159
160fn bmrmemrd8 pa ulong to uchar
161ret uchar and fwld pa 1 255
162end
163
164fn bmrmemwr8 pa ulong v uchar to void
165x = fwst pa 1 ulong v
166end
167
168fn bmrcntvct to ulong
169ret fwsys 2
170end
171
172note Piece 4 (gpt_ro.om / ext4_ro.om) sector reader -> nvme_ro_bare.om (strictly read-only)
173fn spk_nvme_read1 lba ulong to int
174sec_pa = get spk_st 18
175if eq sec_pa 0
176ret -1
177end
178ret nvme_read_direct lba 1 sec_pa 0
179end
180
181fn gpt_read_lba lba ulong to int
182rc = spk_nvme_read1 lba
183if ne rc 0
184ret rc
185end
186sec_pa = get spk_st 18
187i = ulong 0
188while lt i 512 bound 512
189put gpt_sec i uchar and fwld add sec_pa i 1 255
190i = add i 1
191end
192ret 0
193end
194
195fn ext4_read_lba lba ulong to int
196rc = spk_nvme_read1 lba
197if ne rc 0
198ret rc
199end
200sec_pa = get spk_st 18
201i = ulong 0
202while lt i 512 bound 512
203put e4_sec i uchar and fwld add sec_pa i 1 255
204i = add i 1
205end
206ret 0
207end
208
209note Stream bundle bytes into 80 MiB Normal-NC bnd_pa and mirror [0..4096) into e4_toc[].
210fn ext4_sink off ulong b uchar to void
211bpa = get spk_st 5
212bcap = get spk_st 6
213if and ne bpa 0 lt off bcap
214x = fwst add bpa off 1 ulong b
215if lt off 4096
216put e4_toc off b
217end
218if ge add off 1 get spk_st 7
219put spk_st 7 add off 1
220end
221end
222end
223
224note Piece 6 (wire_loop.om) platform/NIC/GPU seams -> Pieces 1, 2, 3, 4, 5
225fn wl_cntvct to ulong
226ret fwsys 2
227end
228
229fn wl_cntfrq to ulong
230ret fwsys 3
231end
232
233fn wl_dsb_ish to void
234arm64_dsb_ish
235end
236
237fn wl_nic_rx to ulong
238e = rtlrx
239if ne e 0
240ret 0
241end
242n = get rs S_RXLEN
243if or eq n 0 gt n 2048
244ret 0
245end
246i = ulong 0
247while lt i n bound 2048
248put Trx i get obuf i
249i = add i 1
250end
251ret n
252end
253
254fn wl_nic_tx n ulong to long
255if or lt n 14 gt n FMAX
256ret -1
257end
258e = rtltxrec
259if ne e 0
260ret -2
261end
262i = ulong 0
263while lt i n bound 1514
264put fbuf i get Ttx i
265i = add i 1
266end
267r = rtltx n
268if ne r 0
269ret -3
270end
271ret 0
272end
273
274fn wl_nic_reclaim to void
275e = rtltxrec
276end
277
278fn wl_q38_reset_kv keep_pos ulong to long
279ret q38_reset_kv keep_pos
280end
281
282fn wl_q38_stage_step tok uint kv uint mode uchar to long
283ret q38_stage_step tok kv mode
284end
285
286fn wl_q38_verify_step to long
287md = uchar and get wl_st WL_S_CURMODE 255
288rc = q38_verify_step md
289if lt rc 0
290ret rc
291end
292ret 0
293end
294
295fn wl_q38_read_tok to uint
296ret uint mmrd32 q3_ncva 6
297end
298
299note Piece 6 -> Piece 3 submission: PB already flushed with DSB SY by Piece 5;
300note chpush executes DSB SY -> GPFIFO -> DSB SY -> USERD.GPPut -> DSB SY -> BAR0 doorbell (0x00BB0090),
301note then cmsub accounts the in-flight entry for cmpoll(marker_va + 16).
302fn wl_gpu_kick pb_words uint to long
303pbva = get spk_st 10
304rc = chpush 0 pbva pb_words
305if lt rc 0
306ret rc
307end
308ret cmsub 0 1
309end
310
311note Piece 6 -> Piece 3/5 completion poll: polls marker_va + 16 via cmbind(0, semslot, 16, gen)
312note matching Piece 5's C3 marker1(WFI) -> L2_FLUSH -> marker2(WFI) completion tail!
313fn wl_gpu_poll to long
314ret cmpoll 0
315end
316
317fn wl_gpu_irq_check to long
318ret iqservice get spk_st 14 16
319end
320
321fn wl_gpu_quarantine to void
322r = cmquar 0 get spk_st 14
323end
324
325fn wl_should_stop to ulong
326ret 0
327end
328
329note ========================================================================
330note Fail-Closed Helper (NEVER touches dead UEFI Boot Services post-EBS)
331note ========================================================================
332
333fn spk_fail stage ulong code ulong to void
334uart_puts spk_fail_a
335uart_putdec stage
336uart_puts spk_fail_b
337uart_putdec code
338uart_puts spk_fail_c
339arm64_psci_reset get acpi_info 7
340i = ulong 0
341while lt i 9223372036854775807 bound 9223372036854775807
342arm64_wfe
343end
344end
345
346note ========================================================================
347note Stage 1: UEFI ExitBootServices + EL1h + GICv3/ITS + 20-core SMP
348note ========================================================================
349
350fn efi_find_acpi2_rsdp to ulong
351ret efcfg 0x11d3e4f18868e871 0x81883cc7800022bc
352end
353
354fn efi_exit_boot_services to bool
355bs = efbs
356ih = fwsys 0
357x = fwcall fwld add bs 256 8 0 0 0 0 0 0
358outp = fwadr efi_mm_out 0 64
359x = fwst add outp 40 8 efi_find_acpi2_rsdp
360x = fwst add outp 48 8 arm64_current_el
361gmm = fwld add bs 56 8
362ebs = fwld add bs 232 8
363mp = fwadr efi_mmap 0 EFI_MMAP_CAP
364att = ulong 0
365while lt att 8 bound 8
366x = fwst outp 8 EFI_MMAP_CAP
367s = fwcall gmm outp mp add outp 8 add outp 16 add outp 24 0
368if ne s 0
369ret false
370end
371s2 = fwcall ebs ih fwld add outp 8 8 0 0 0 0
372att = add att 1
373if eq s2 0
374x = fwst add outp 32 8 att
375ret true
376end
377end
378ret false
379end
380
381fn reserve_gb10_rmrs to void
382frames_reserve 0x00000000a1600000 0x00000000b9800000
383frames_reserve 0x0000000280000000 0x0000000303000000
384end
385
386fn spk_stage1 to bool
387lpi_out = new ulong 1 0
388uart_puts spk_s1
389uart_puts spk_el1
390uart_putdec arm64_current_el
391uart_puts spk_mair
392uart_puthex arm64_sysrd SYSREG_MAIR_EL1
393uart_puts spk_sctlr
394uart_puthex arm64_sysrd SYSREG_SCTLR_EL1
395uart_puts spk_tcr
396uart_puthex arm64_sysrd SYSREG_TCR_EL1
397uart_puts u_nl
398uart_puts spk_ram
399uart_putdec div frames_free_bytes 1048576
400uart_puts spk_nc
401uart_puthex dma_pool_base
402uart_puts spk_ncsz
403uart_putdec div dma_pool_size 1048576
404uart_puts spk_mcfg
405uart_putdec get acpi_info 12
406uart_puts u_nl
407if not gicv3_init get acpi_info 2 get acpi_info 3 get acpi_info 4 get acpi_info 5
408spk_fail 1 SPK_E_GIC
409ret false
410end
411uart_puts spk_gic
412uart_putdec get gic_state 4
413uart_puts u_nl
414if ne get acpi_info 5 0
415if not gic_its_init
416spk_fail 1 SPK_E_ITS
417ret false
418end
419db = gic_its_map_msix 0x100 8 lpi_out
420if or ne db add get acpi_info 5 GITS_TRANSLATER ne get lpi_out 0 8192
421spk_fail 1 SPK_E_ITS
422ret false
423end
424uart_puts spk_its
425uart_putdec get gic_state 11
426uart_puts u_nl
427end
428online = smp_bringup
429uart_puts spk_smp
430uart_putdec online
431uart_puts spk_exp
432uart_putdec get smp_stats 1
433uart_puts spk_fl
434uart_putdec get smp_stats 2
435uart_puts spk_ms
436uart_putdec arm64_time_ms
437uart_puts u_nl
438if ne get smp_stats 2 0
439spk_fail 1 SPK_E_SMP
440ret false
441end
442ret true
443end
444
445note ========================================================================
446note Stage 2: PCIe Multi-Segment ECAM (Bus 0..1) + IORT/SMMUv3 A/B/C + BAR0 + IRQ
447note ========================================================================
448
449fn spk_register_mcfg_segs to ulong
450nm = get acpi_info 12
451if eq nm 0
452ret 0
453end
454if gt nm PCIE_MAX_SEGS
455nm = PCIE_MAX_SEGS
456end
457mapped = ulong 0
458i = ulong 0
459while lt i nm bound 16
460base = get acpi_mcfg_base i
461seg = uint get acpi_mcfg_seg i
462b0 = uchar and get acpi_mcfg_sbus i 255
463b1 = uchar and get acpi_mcfg_ebus i 255
464note Map 2 MiB ECAM window (Bus 0..1) as Device-nGnRnE XN for Spark endpoint probing
465if ne base 0
466if mmio_map_device base SPARK_ECAM_WIN_SZ
467put pcie_seg_base mapped base
468put pcie_seg_id mapped seg
469put pcie_seg_bus0 mapped b0
470if gt b1 1
471put pcie_seg_bus1 mapped uchar 1
472else
473put pcie_seg_bus1 mapped b1
474end
475mapped = add mapped 1
476end
477end
478i = add i 1
479end
480put pcie_nsegs 0 mapped
481j = ulong 0
482while lt j mapped bound 16
483h = pcie_map_seg_ecam get pcie_seg_id j
484j = add j 1
485end
486ret mapped
487end
488
489note spk_dev: 0=gpu seg, 1=gpu rid, 2=nvme seg, 3=nvme rid, 4=rtl seg, 5=rtl rid,
490note 6=found mask (1 = GPU, 2 = NVMe, 4 = RTL8127). Streams and endpoint enables use the probed BDFs.
491global spk_dev ulong 8
492
493note Decode a memory BAR at pcie_dev_info[lo] (+ upper half at [lo+1] only when it is a 64-bit BAR,
494note type bits [2:1] == 0b10, PCIe Base r6.0 ยง7.5.1.2.1).
495fn spk_bar lo ulong to ulong
496b = get pcie_dev_info lo
497bar = and b 0xfffffff0
498if eq and b 6 4
499bar = or bar shl get pcie_dev_info add lo 1 32
500end
501ret bar
502end
503
504fn spk_mark k ulong seg uint bus ulong d ulong to void
505put spk_dev mul k 2 ulong seg
506put spk_dev add mul k 2 1 or shl bus 8 shl d 3
507put spk_dev 6 or get spk_dev 6 shl ulong 1 k
508end
509
510fn spk_record_gpu seg uint bus ulong d ulong to bool
511bar0 = spk_bar 5
512if eq bar0 0
513bar0 = SPK_PLAT_GPU_BAR_FALLBACK
514end
515if eq bar0 0
516ret false
517end
518put spk_gb20b_bar 0 bar0
519spk_mark 0 seg bus d
520ret true
521end
522
523fn spk_record_dev seg uint bus ulong d ulong to bool
524vid = get pcie_dev_info 0
525did = get pcie_dev_info 1
526if spk_plat_is_gpu vid did
527ret spk_record_gpu seg bus d
528end
529if and eq vid SPK_RTL_VID eq did SPK_RTL_DID
530bar2 = spk_bar 7
531if eq bar2 0
532bar2 = SPARK_RTL8127_BAR2_PA
533end
534put spk_rtl_bar 0 bar2
535spk_mark 2 seg bus d
536end
537if eq shr get pcie_dev_info 4 8 0x010802
538if eq and get spk_dev 6 2 0
539nbar = spk_bar 5
540if eq nbar 0
541nbar = SPARK_NVME_BAR0_PA
542end
543put spk_nvme_bar 0 nbar
544spk_mark 1 seg bus d
545end
546end
547ret false
548end
549
550note Scan Bus 0..1 (pcie_seg_bus0..pcie_seg_bus1) across all MCFG segments:
551note on DGX Spark GB20B=000f:01:00.0, NVMe=0004:01:00.0, RTL8127=0007:01:00.0 (all Bus 1!).
552fn spk_probe_seg_bus seg uint bus ulong to bool
553found = false
554d = ulong 0
555while lt d 32 bound 32
556if eq pcie_probe_dev seg bus d 0 PCIE_OK
557if spk_record_dev seg bus d
558found = true
559end
560end
561d = add d 1
562end
563ret found
564end
565
566fn spk_probe_gb20b to bool
567ns = get pcie_nsegs 0
568found = false
569i = ulong 0
570while lt i ns bound 16
571seg = get pcie_seg_id i
572b = ulong get pcie_seg_bus0 i
573b1 = ulong get pcie_seg_bus1 i
574while le b b1 bound 2
575if spk_probe_seg_bus seg b
576found = true
577end
578b = add b 1
579end
580i = add i 1
581end
582ret found
583end
584
585note Parse ACPI IORT and bring up SMMUv3 A/B (128 KiB aligned STRTAB) + C (ABORT_ONLY)
586note + open disjoint streams for GB20B (000f:01:00.0, auto-maps 3 RMRs), NVMe, RTL8127.
587fn spk_init_iort to bool
588ib = new uchar 4096 0
589ipa = get acpi_info 10
590ilen = get acpi_info 11
591if or eq ipa 0 or lt ilen 48 gt ilen 4096
592ret false
593end
594k = ulong 0
595while lt k ilen bound 4096
596put ib k uchar and fwld add ipa k 1 255
597k = add k 1
598end
599ret eq iort_parse part ib 0 ilen 1
600end
601
602fn spk_init_smmu_one inst ulong base ulong min_bits ulong to bool
603if not mmio_map_device base SPARK_SMMU_SZ
604ret false
605end
606st_p = dma_alloc_nc 32 131072
607cq_p = dma_alloc_nc 1 4096
608eq_p = dma_alloc_nc 1 4096
609if or eq st_p 0 or eq cq_p 0 eq eq_p 0
610ret false
611end
612ret eq smmu_init_instance inst base min_bits st_p cq_p eq_p SMMU_OK
613end
614
615note Stage-1 table / L2-STE pool for the mode-1 SMMUv3 back end (4 MiB Normal-NC; GB10 RMRs use 2 MiB blocks).
616const SPK_SMMU_PT_PAGES ulong 1024
617
618note IORT is the source of truth for SMMUv3 bases, but only after it agrees with the platform map
619note (plat_*.om): exactly SPK_PLAT_NSMMU SMMUv3 nodes, node i at spk_plat_smmu_base i (GB10: A/B/C). An
620note extra node would be an SMMU left unprogrammed (GBPA unknown); any mismatch means the A/B/C role
621note assumption is wrong -> fail closed (SPK_E_SMMU_BASE 208).
622fn spk_check_smmu_bases to bool
623ns = get iort_meta 0
624if ne ns SPK_PLAT_NSMMU
625ret false
626end
627i = ulong 0
628while lt i ns bound 3
629if ne get iort_smmu_base i spk_plat_smmu_base i
630ret false
631end
632i = add i 1
633end
634ret true
635end
636
637note Requires spk_init_iort + spk_check_smmu_bases to have passed; bases come from the (checked) IORT.
638fn spk_init_smmu_abc to bool
639pool = dma_alloc_nc SPK_SMMU_PT_PAGES 4096
640if ne smmu_set_pt_pool pool SPK_SMMU_PT_PAGES SMMU_OK
641ret false
642end
643if eq SPK_PLAT_QEMU 1
644note QEMU virt iommu=smmuv3: a single SMMUv3 from IORT node 0 (IDR1.SIDSIZE = 16).
645ret spk_init_smmu_one 0 get iort_smmu_base 0 16
646end
647if not spk_init_smmu_one 0 get iort_smmu_base 0 20
648ret false
649end
650if not spk_init_smmu_one 1 get iort_smmu_base 1 9
651ret false
652end
653cb = get iort_smmu_base 2
654if not mmio_map_device cb SPARK_SMMU_SZ
655ret false
656end
657ret eq smmu_abort_only 2 cb SMMU_OK
658end
659
660fn spk_open_one_stream seg uint rid uint st_slot ulong to bool
661cd = dma_alloc_nc 1 4096
662tt = dma_alloc_nc 1 4096
663if or eq cd 0 eq tt 0
664ret false
665end
666sh = smmu_open_stream seg rid cd tt
667if le sh 0
668ret false
669end
670put spk_st st_slot ulong sh
671ret true
672end
673
674fn spk_open_dev_stream k ulong st_slot ulong to bool
675ret spk_open_one_stream uint get spk_dev mul k 2 uint get spk_dev add mul k 2 1 st_slot
676end
677
678note Streams for the probed GPU (slot 1), NVMe (slot 2) and RTL8127 (slot 15, when present).
679fn spk_open_smmu_streams to bool
680if not spk_open_dev_stream 0 1
681ret false
682end
683if not spk_open_dev_stream 1 2
684ret false
685end
686if eq and get spk_dev 6 4 0
687ret true
688end
689ret spk_open_dev_stream 2 15
690end
691
692note Enable MemSpace + BusMaster (+ DisINTx, MSI-X masked) on each used endpoint only AFTER its SMMUv3
693note stream is live: pcie_quiesce_unused may have cleared BME, and firmware may leave it clear post-EBS.
694fn spk_enable_dev k ulong to bool
695seg = uint get spk_dev mul k 2
696rid = get spk_dev add mul k 2 1
697ret eq pcie_enable_endpoint seg shr rid 8 and shr rid 3 31 and rid 7 PCIE_OK
698end
699
700fn spk_enable_endpoints to bool
701if not spk_enable_dev 0
702ret false
703end
704if not spk_enable_dev 1
705ret false
706end
707if eq and get spk_dev 6 4 0
708ret true
709end
710ret spk_enable_dev 2
711end
712
713fn spk_stage2 to bool
714uart_puts spk_s2
715bmmio_reset
716pcie_reset
717smmu_reset
718ibm_reset
719note Switch bar0_mmio.om to bare-metal mode 1 (fwld/fwst + dsb sy via fwops.om).
720put bmmio_mode 0 1
721put spk_gb20b_bar 0 0
722put spk_nvme_bar 0 0
723put spk_rtl_bar 0 0
724for di ulong 8
725put spk_dev di 0
726end
727nsegs = spk_register_mcfg_segs
728if eq nsegs 0
729spk_fail 2 SPK_E_NO_MCFG
730ret false
731end
732note Probe PCIe ECAM Bus 0..1 for GB20B (0x10de). Fails closed cleanly on QEMU virt (absent GPU).
733if not spk_probe_gb20b
734spk_fail 2 SPK_E_NO_GB20B
735ret false
736end
737gbar = get spk_gb20b_bar 0
738if not mmio_map_device gbar SPK_PLAT_GPU_BAR_SZ
739spk_fail 2 SPK_E_BAR0_MAP
740ret false
741end
742hb = bmmio_register gbar gbar SPK_PLAT_GPU_BAR_SZ BMMIO_ATTR_NGNRNE
743if le hb 0
744spk_fail 2 SPK_E_BAR0_MAP
745ret false
746end
747put spk_st 0 ulong hb
748if not spk_plat_verify_gpu hb
749spk_fail 2 SPK_E_GB20B_ID
750ret false
751end
752note NVMe is mandatory everywhere; RTL8127 is mandatory on GB10 (SPK_PLAT_NEED_RTL). No device found ->
753note fail closed instead of touching a hard-coded BAR.
754if eq and get spk_dev 6 2 0
755spk_fail 2 SPK_E_BAR0_MAP
756ret false
757end
758if not mmio_map_device get spk_nvme_bar 0 SPARK_NVME_BAR0_SZ
759spk_fail 2 SPK_E_BAR0_MAP
760ret false
761end
762if eq and get spk_dev 6 4 0
763if eq SPK_PLAT_NEED_RTL 1
764spk_fail 2 SPK_E_BAR0_MAP
765ret false
766end
767else
768if not mmio_map_device get spk_rtl_bar 0 SPARK_RTL8127_BAR2_SZ
769spk_fail 2 SPK_E_BAR0_MAP
770ret false
771end
772end
773if lt pcie_quiesce_unused 0
774spk_fail 2 SPK_E_QUIESCE
775ret false
776end
777note Initialize IORT + SMMUv3 A/B/C + open disjoint streams (GB20B + 3 RMRs, NVMe, RTL8127)
778note BEFORE any NVMe/RTL8127/GB20B DMA is enabled.
779if not spk_init_iort
780spk_fail 2 SPK_E_SMMU
781ret false
782end
783if not spk_check_smmu_bases
784spk_fail 2 SPK_E_SMMU_BASE
785ret false
786end
787if not spk_init_smmu_abc
788spk_fail 2 SPK_E_SMMU
789ret false
790end
791if not spk_open_smmu_streams
792spk_fail 2 SPK_E_SMMU
793ret false
794end
795spk_plat_note_stage2
796if not spk_enable_endpoints
797spk_fail 2 SPK_E_PCIE_EN
798ret false
799end
800if eq SPK_PLAT_QEMU 0
801if ne ibm_bind hb 1 0
802spk_fail 2 SPK_E_IRQ_BIND
803ret false
804end
805end
806put iqmode 0 2
807ret true
808end
809
810note ========================================================================
811note Stage 3: Strictly RO NVMe + GPT + 64-bit ext4 + 80 MiB OMFWBND1 + RTL8127
812note ========================================================================
813
814fn spk_stage3_disk to bool
815spk_nvdma = dma_alloc_nc 16 4096
816sec_pa = dma_alloc_nc 1 4096
817bnd_pa = dma_alloc_nc 20480 65536
818if or eq spk_nvdma 0 or eq sec_pa 0 eq bnd_pa 0
819spk_fail 3 SPK_E_NVME_INIT
820ret false
821end
822put spk_st 3 spk_nvdma
823put spk_st 18 sec_pa
824put spk_st 5 bnd_pa
825put spk_st 6 SPK_BND_CAP
826put spk_st 7 0
827note Map NVMe DMA queues + bounce page on NVMe SMMUv3 stream before nvme_init
828snvme = long get spk_st 2
829if ne smmu_map_span snvme spk_nvdma 65536 0 SMMU_OK
830spk_fail 3 SPK_E_NVME_INIT
831ret false
832end
833if ne smmu_map_span snvme sec_pa 4096 0 SMMU_OK
834spk_fail 3 SPK_E_NVME_INIT
835ret false
836end
837if ne nvme_init get spk_nvme_bar 0 spk_nvdma 1 0
838spk_fail 3 SPK_E_NVME_INIT
839ret false
840end
841if ne gpt_ro_parse nvme_ns_blocks 0
842spk_fail 3 SPK_E_GPT
843ret false
844end
845if ne gpt_ro_select_linux_root 0
846spk_fail 3 SPK_E_GPT
847ret false
848end
849p_lba = gpt_ro_first_lba
850p_secs = gpt_ro_sectors
851if or eq p_lba 0 eq p_secs 0
852spk_fail 3 SPK_E_GPT
853ret false
854end
855if ne ext4_ro_mount p_lba p_secs 0
856spk_fail 3 SPK_E_EXT4
857ret false
858end
859ret true
860end
861
862note Verify SHA-256 of one bundle entry [pa, pa + sz) against e4_bnd_sha[idx*32..+32).
863fn spk_verify_bnd_entry idx ulong pa ulong sz ulong to bool
864s256_init
865remn = sz
866cur = pa
867while gt remn 0 bound 20480
868ch = remn
869if gt ch 4096
870ch = ulong 4096
871end
872j = ulong 0
873while lt j ch bound 4096
874s256_byte uchar and fwld add cur j 1 255
875j = add j 1
876end
877cur = add cur ch
878remn = sub remn ch
879end
880s256_fin
881base = mul idx 32
882for b ulong 32
883if ne get s2_out b get e4_bnd_sha add base b
884ret false
885end
886end
887ret true
888end
889
890fn spk_stage3_bundle to bool
891for k ulong 8
892put spk_kpa k 0
893put spk_ksz k 0
894end
895plen = len spk_bnd_path
896pi = ulong 0
897while lt pi plen bound 256
898put e4_path pi get spk_bnd_path pi
899pi = add pi 1
900end
901if ne ext4_ro_lookup plen 0
902spk_fail 3 SPK_E_BUNDLE
903ret false
904end
905fsz = ext4_ro_file_size
906if or lt fsz 4096 gt fsz SPK_BND_CAP
907spk_fail 3 SPK_E_BUNDLE
908ret false
909end
910if ne ext4_ro_stream 0 fsz 0 0
911spk_fail 3 SPK_E_BUNDLE
912ret false
913end
914if ne bundle_parse_toc fsz 0
915spk_fail 3 SPK_E_BUNDLE
916ret false
917end
918cnt = get e4_bnd_meta 2
919bpa = get spk_st 5
920if or eq cnt 0 gt cnt 8
921spk_fail 3 SPK_E_BUNDLE
922ret false
923end
924i = ulong 0
925while lt i cnt bound 8
926ek = get e4_bnd_kind i
927eo = get e4_bnd_off i
928es = get e4_bnd_size i
929if not spk_verify_bnd_entry i add bpa eo es
930spk_fail 3 SPK_E_BUNDLE
931ret false
932end
933if and ge ek 1 le ek 7
934put spk_kpa ek add bpa eo
935put spk_ksz ek es
936end
937i = add i 1
938end
939note Require kinds 1..6 (gsp.bin, bl.img, fmc.img, fmc.hash, fmc.pubkey, fmc.sig) present & verified
940for req ulong 6
941if or eq get spk_kpa add req 1 0 eq get spk_ksz add req 1 0
942spk_fail 3 SPK_E_BUNDLE
943ret false
944end
945end
946note abcotmk (arena_bm.om) reads fixed 48 B fmc.hash, 384 B fmc.pubkey, 384 B fmc.sig from these PAs:
947note refuse undersized entries so the COT packet never reads past a verified entry.
948if lt get spk_ksz 4 SPK_FMC_HASH_SZ
949spk_fail 3 SPK_E_BUNDLE
950ret false
951end
952if lt get spk_ksz 5 SPK_FMC_PKEY_SZ
953spk_fail 3 SPK_E_BUNDLE
954ret false
955end
956if lt get spk_ksz 6 SPK_FMC_SIG_SZ
957spk_fail 3 SPK_E_BUNDLE
958ret false
959end
960ret true
961end
962
963fn spk_stage3_net to bool
964rtl_dma = dma_alloc_nc 32 4096
965if eq rtl_dma 0
966spk_fail 3 SPK_E_RTL
967ret false
968end
969put spk_st 4 rtl_dma
970snic = long get spk_st 15
971if ne smmu_map_span snic rtl_dma 131072 0 SMMU_OK
972spk_fail 3 SPK_E_RTL
973ret false
974end
975if ne rtl_bare_setup get spk_rtl_bar 0 rtl_dma 0
976spk_fail 3 SPK_E_RTL
977ret false
978end
979if ne rtlinit rtl_dma 0
980spk_fail 3 SPK_E_RTL
981ret false
982end
983for mi ulong 6
984put Tmac mi uchar and get rs add S_MAC0 mi 255
985end
986ret true
987end
988
989fn spk_stage3 to bool
990uart_puts spk_s3
991if not spk_stage3_disk
992ret false
993end
994if not spk_stage3_bundle
995ret false
996end
997if eq SPK_PLAT_QEMU 1
998uart_puts spk_q_s3net
999ret true
1000end
1001ret spk_stage3_net
1002end
1003
1004note ========================================================================
1005note Stage 4: GB20B GSP Bring-Up + GMMU(VA==PA) + bsetpd + rmchan(0xca6f/0xcec0)
1006note ========================================================================
1007
1008fn spk_stage4_sysinfo to bool
1009sireset
1010gbar = get spk_gb20b_bar 0
1011if ne siseti 0 gbar SIEOK
1012ret false
1013end
1014if ne siseti 2 add gbar 7340032 SIEOK
1015ret false
1016end
1017if ne siseti 4 SPK_GB20B_BDF SIEOK
1018ret false
1019end
1020siencode
1021ret true
1022end
1023
1024fn spk_stage4_bootloop to bool
1025dspinit
1026dspperms DSPMALL
1027put sqcore 0 1
1028put sqbud 0 100000
1029it = ulong 0
1030while lt it 50000 bound 50000
1031st = get bstate 0
1032if eq st 9
1033ret true
1034end
1035if eq st 11
1036if ne dspauto 0
1037ret false
1038end
1039else
1040if eq st 5
1041rqclear 792
1042rqput32 4 0 580
1043rqput32 4 4 173
1044if ne bguest 0
1045ret false
1046end
1047else
1048if eq st 7
1049if ne bstatic 0
1050ret false
1051end
1052else
1053pr = bpoll
1054if lt pr 0
1055ret false
1056end
1057end
1058end
1059end
1060it = add it 1
1061end
1062ret eq get bstate 0 9
1063end
1064
1065fn spk_stage4_gsp to bool
1066frq = fwsys 3
1067if ne bminit 1 get spk_gb20b_bar 0 SPARK_GB20B_BAR0_SZ dma_pool_base dma_pool_size frq BMOK
1068spk_fail 4 SPK_E_BMHAL
1069ret false
1070end
1071put gspmode 0 2
1072put gspd0ok 0 1
1073put gsppns 0 10000
1074if not spk_stage4_sysinfo
1075spk_fail 4 SPK_E_FWELF
1076ret false
1077end
1078note Parse verified bundle entry 1 (gsp.bin ELF64) and bind entries 2..6 (bl, fmc, hash, pubkey, sig)
1079if ne fbparseelf get spk_kpa 1 get spk_ksz 1 FBOK
1080spk_fail 4 SPK_E_FWELF
1081ret false
1082end
1083if ne fbbindblobs get spk_kpa 2 get spk_ksz 2 get spk_kpa 3 get spk_ksz 3 get spk_kpa 4 get spk_kpa 5 FBOK
1084spk_fail 4 SPK_E_FWELF
1085ret false
1086end
1087if ne fbbindsig get spk_kpa 6 get e4_bnd_meta 1 FBOK
1088spk_fail 4 SPK_E_FWELF
1089ret false
1090end
1091note Allocate 80 MiB GSP arena + 133 MiB WPR2 heap inside Normal-NC pool & map on GB20B SMMU stream
1092arena_pa = dma_alloc_nc 20480 65536
1093heap_pa = dma_alloc_nc 34048 65536
1094if or eq arena_pa 0 eq heap_pa 0
1095spk_fail 4 SPK_E_ARENA
1096ret false
1097end
1098put spk_st 8 arena_pa
1099put spk_st 9 heap_pa
1100sgpu = long get spk_st 1
1101if or ne smmu_map_span sgpu arena_pa SPK_ARENA_CAP 0 SMMU_OK ne smmu_map_span sgpu heap_pa 139460608 0 SMMU_OK
1102spk_fail 4 SPK_E_ARENA
1103ret false
1104end
1105if ne abbuild arena_pa SPK_ARENA_CAP heap_pa 139460608 ABOK
1106spk_fail 4 SPK_E_ARENA
1107ret false
1108end
1109note Phase 3 #77 reminder: configure seqcore CORE_RESUME libosArgsPA + appVersion after abbuild!
1110sccfg abpa 1 get fbappver 0
1111for ci ulong 1024
1112put gpkt ci get abcot ci
1113end
1114put gin 0 get abclen 0
1115put gin 1 abpa 15
1116put gin 2 get fbappver 0
1117for bi ulong 4
1118put gin add bi 3 100000
1119end
1120for si ulong 7
1121put s2cfg si 100000
1122end
1123if ne s2boot uint GB20B_VAL_BOOT42 EOK
1124spk_fail 4 SPK_E_SEC2
1125ret false
1126end
1127note abbuild already pre-queued RPC 72 (siout) into rqcmd & abcmdpa via abpre;
1128note arm boot state machine at bstate==4, attach cmdq/statq via abcmdpa/abstatpa, and poll to bstate==9.
1129put bstate 0 4
1130put bbudget 0 100000
1131put bstaticn 0 64
1132put bgen 0 1
1133if ne tbattach get abcmdpa 0 get abstatpa 0 uint GB20B_VAL_BOOT42 0
1134spk_fail 4 SPK_E_GSPBOOT
1135ret false
1136end
1137if not spk_stage4_bootloop
1138spk_fail 4 SPK_E_GSPBOOT
1139ret false
1140end
1141ret true
1142end
1143
1144fn spk_idmap_2m pa ulong sz ulong to bool
1145if eq pa 0
1146ret false
1147end
1148ret eq mmmap pa pa sz MMSZ2M MMAPSYS 0 MMOK
1149end
1150
1151note GB20B DMA is Stage-1 confined by its SMMUv3 stream (spk_st 1): every sysmem range the GPU walks,
1152note fetches or writes (GMMU page tables, semaphore/marker buffer, PB, Normal-NC, WB, SASS code) must be
1153note mapped there as a disjoint span, or the first GMMU walk / PB fetch faults. Fail closed on any error.
1154fn spk_gpu_span pa ulong sz ulong to bool
1155if eq pa 0
1156ret false
1157end
1158sg = long get spk_st 1
1159ret eq smmu_map_span sg pa sz 0 SMMU_OK
1160end
1161
1162fn spk_gpu_span_wb pa ulong sz ulong to bool
1163if eq pa 0
1164ret false
1165end
1166sg = long get spk_st 1
1167ret eq smmu_map_span_wb sg pa sz SMMU_OK
1168end
1169
1170fn spk_stage4_gmmu_init to bool
1171gmmu_pool = dma_alloc_nc 512 4096
1172gmbuf_pa = dma_alloc_nc 64 2097152
1173if or eq gmmu_pool 0 eq gmbuf_pa 0
1174ret false
1175end
1176if not spk_gpu_span gmmu_pool 2097152
1177ret false
1178end
1179if not spk_gpu_span gmbuf_pa 262144
1180ret false
1181end
1182if ne mmwinit dma_pool_base dma_pool_size MMOK
1183ret false
1184end
1185if ne mmasinit gmmu_pool 512 MMOK
1186ret false
1187end
1188pb_pa = dma_alloc_nc 512 2097152
1189nc_base = dma_alloc_nc 2048 2097152
1190wb_base = frames_alloc 512 2097152 FR_PA_40BIT_LIMIT
1191code_pa = dma_alloc_nc 512 2097152
1192put spk_st 10 pb_pa
1193put spk_st 12 nc_base
1194put spk_st 13 wb_base
1195put spk_st 16 code_pa
1196if not spk_gpu_span pb_pa 2097152
1197ret false
1198end
1199if not spk_gpu_span nc_base 8388608
1200ret false
1201end
1202note wb_base comes from frames_alloc (CPU Normal-WB, ISH): map it Normal-WB on the SMMU too, and only on an
1203note IO-coherent (IORT CCA=1) path (smmu_map_span_wb refuses otherwise) -- never an NC alias of WB memory.
1204if not spk_gpu_span_wb wb_base 2097152
1205ret false
1206end
1207if not spk_gpu_span code_pa 2097152
1208ret false
1209end
1210note Identity-map PB, Normal-NC, WB, and SASS code spans into GMMU at VA == PA (2 MiB pages)
1211if not spk_idmap_2m pb_pa 2097152
1212ret false
1213end
1214if not spk_idmap_2m nc_base 8388608
1215ret false
1216end
1217if not spk_idmap_2m wb_base 2097152
1218ret false
1219end
1220if not spk_idmap_2m code_pa 2097152
1221ret false
1222end
1223note 2 MiB-aligned gmbuf_pa -> gminit VA == PA so mk_va == mk_pa!
1224ret eq gminit 8 gmbuf_pa 64 gmbuf_pa GMOK
1225end
1226
1227fn spk_stage4_chan to bool
1228if not spk_stage4_gmmu_init
1229spk_fail 4 SPK_E_GMMU
1230ret false
1231end
1232chinit
1233if ne chprep 0 42 3 64 CHOK
1234spk_fail 4 SPK_E_RMCHAN
1235ret false
1236end
1237if ne cminit CMOK
1238spk_fail 4 SPK_E_CMBIND
1239ret false
1240end
1241if ne gmalloc 4096 1 GMHPOLL GMOK
1242spk_fail 4 SPK_E_CMBIND
1243ret false
1244end
1245semslot = get gmout 0
1246mk_pa = get gmbpa semslot
1247put spk_st 11 mk_pa
1248gen = get mmwgen 0
1249put spk_st 14 gen
1250put iqstate 0 0
1251put iqgen 0 0
1252put iqhalt 0 0
1253put iqmode 0 2
1254if ne iqinit gen 0
1255spk_fail 4 SPK_E_CMBIND
1256ret false
1257end
1258note Bind completion semaphore to marker_va + 16 (VA == PA, matching Piece 5 C3 marker2 tail)
1259if ne cmbind 0 semslot 16 gen CMOK
1260spk_fail 4 SPK_E_CMBIND
1261ret false
1262end
1263if ne rminit RMOK
1264spk_fail 4 SPK_E_RMCHAN
1265ret false
1266end
1267rmcfg 1001 1002 1003 1004 1005 1006
1268rmcfgch 1007 42 1 99
1269rmcfg20 true 1008
1270if ne rmbind 0 gen RMOK
1271spk_fail 4 SPK_E_RMCHAN
1272ret false
1273end
1274if ne rmstart 100000 RMOK
1275spk_fail 4 SPK_E_RMCHAN
1276ret false
1277end
1278s1 = ulong 0
1279while and ne get rmst 0 RMSREADY lt s1 64 bound 64
1280if lt rmstep 0
1281spk_fail 4 SPK_E_RMCHAN
1282ret false
1283end
1284s1 = add s1 1
1285end
1286if ne get rmst 0 RMSREADY
1287spk_fail 4 SPK_E_RMCHAN
1288ret false
1289end
1290note RPC 54 SET_PAGE_DIRECTORY: bind GMMU root PDB to VAS 1004 before scheduling channel
1291for pi ulong 11
1292put pain pi 0
1293end
1294put pain 0 1001
1295put pain 1 1002
1296put pain 3 get mmroot 0
1297put pain 4 512
1298put pain 5 2
1299put pain 7 1004
1300if ne pasetpd 48
1301spk_fail 4 SPK_E_GMMU
1302ret false
1303end
1304if ne bsetpd 100000 0
1305spk_fail 4 SPK_E_GMMU
1306ret false
1307end
1308sp = ulong 0
1309while and ne get bstate 0 9 lt sp 50000 bound 50000
1310if lt bpoll 0
1311spk_fail 4 SPK_E_GMMU
1312ret false
1313end
1314sp = add sp 1
1315end
1316if ne get bstate 0 9
1317spk_fail 4 SPK_E_GMMU
1318ret false
1319end
1320if ne rmsched 100000 RMOK
1321spk_fail 4 SPK_E_RMCHAN
1322ret false
1323end
1324s2 = ulong 0
1325while and ne get rmst 0 RMSACTV lt s2 32 bound 32
1326if lt rmstep 0
1327spk_fail 4 SPK_E_RMCHAN
1328ret false
1329end
1330s2 = add s2 1
1331end
1332if ne get rmst 0 RMSACTV
1333spk_fail 4 SPK_E_RMCHAN
1334ret false
1335end
1336if ne rmpub gen RMOK
1337spk_fail 4 SPK_E_RMCHAN
1338ret false
1339end
1340if ne mminval 10000 MMOK
1341spk_fail 4 SPK_E_GMMU
1342ret false
1343end
1344ret true
1345end
1346
1347fn spk_stage4 to bool
1348uart_puts spk_s4
1349if not spk_stage4_gsp
1350ret false
1351end
1352ret spk_stage4_chan
1353end
1354
1355note ========================================================================
1356note Stage 5: Compute + sm_121 Pure-.om SASS Codegen + q38_run.om
1357note ========================================================================
1358
1359fn spk_emit_one_sass qk ulong dkind ulong a ulong b ulong c ulong to bool
1360ir = new ulong 8192 0
1361meta = new ulong 512 0
1362scratch = new ulong 2048 0
1363phys = new ulong 288 0
1364stats = new ulong 4 0
1365if ne cg_dec dkind a b c ir meta 0
1366ret false
1367end
1368nins = get meta 0
1369if eq nins 0
1370ret false
1371end
1372if ne bc_regalloc ir nins scratch phys stats 246 0
1373ret false
1374end
1375ret spk_enc_and_bind qk ir phys nins uint get stats 0
1376end
1377
1378fn spk_enc_and_bind qk ulong ir view ulong 8192 phys view ulong 288 nins ulong pg uint to bool
1379code = new uchar 16384 0
1380nb = bw_enc_sm121 ir phys nins 0 code
1381if or eq nb 0 gt nb 16384
1382ret false
1383end
1384dst = add get spk_st 16 mul qk 16384
1385i = ulong 0
1386while lt i nb bound 16384
1387x = fwst add dst i 1 ulong get code i
1388i = add i 1
1389end
1390arm64_dc_civac_range dst nb
1391put spk_st 19 add get spk_st 19 nb
1392if eq pg 0
1393pg = uint 32
1394end
1395ret eq q3_set_code qk dst pg 0 Q3OK
1396end
1397
1398fn spk_stage5 to bool
1399uart_puts spk_s5
1400cbpbinit
1401if ne cbpbsetup18 uint CBCCMP CBOK
1402spk_fail 5 SPK_E_COMPUTE
1403ret false
1404end
1405if ne q3_init get spk_st 12 get spk_st 13 get spk_st 10 get spk_st 11 SPK_MAX_CTX Q3OK
1406spk_fail 5 SPK_E_Q3INIT
1407ret false
1408end
1409note Emit, regalloc, encode, flush, and bind the 7 pure-.om sm_121 glue kernels
1410if not spk_emit_one_sass Q3KNORM 0 1 5120 0
1411spk_fail 5 SPK_E_SASS
1412ret false
1413end
1414if not spk_emit_one_sass Q3KADDNORM 0 3 5120 0
1415spk_fail 5 SPK_E_SASS
1416ret false
1417end
1418if not spk_emit_one_sass Q3KSWIGLU 1 0 17408 0
1419spk_fail 5 SPK_E_SASS
1420ret false
1421end
1422if not spk_emit_one_sass Q3KGATHER 2 0 14336 14336
1423spk_fail 5 SPK_E_SASS
1424ret false
1425end
1426if not spk_emit_one_sass Q3KGATE 3 0 0 0
1427spk_fail 5 SPK_E_SASS
1428ret false
1429end
1430if not spk_emit_one_sass Q3KAM0 4 0 0 0
1431spk_fail 5 SPK_E_SASS
1432ret false
1433end
1434if not spk_emit_one_sass Q3KAM1 4 1 0 0
1435spk_fail 5 SPK_E_SASS
1436ret false
1437end
1438note Fail closed cleanly if full 14-kernel + 64-layer Qwen3.8-27B weight set is not yet bound
1439for k ulong Q3KCOUNT
1440if eq get q3kset k 0
1441spk_fail 5 SPK_E_WEIGHTS
1442ret false
1443end
1444end
1445for l ulong Q3LAYERS
1446if eq get q3lset l 0
1447spk_fail 5 SPK_E_WEIGHTS
1448ret false
1449end
1450end
1451ret true
1452end
1453
1454note ========================================================================
1455note Stage 6: OMTK v1 Wire Loop on TCP Port 18100
1456note ========================================================================
1457
1458fn spk_stage6 to void
1459uart_puts spk_s6
1460wl_init SPK_OMTK_PORT SPK_MAX_CTX SPK_VOCAB
1461put Tcf 1 SPK_STATIC_IP
1462put Tcf 2 SPK_GATEWAY
1463put Tcf 3 SPK_NETMASK
1464put Tcf 5 SPK_ISN
1465wl_run
1466end
1467
1468note ========================================================================
1469note EL1h Entry (from Piece 1 mmu_enter_el1) and UEFI Entry (main)
1470note ========================================================================
1471
1472fn el1main el1_arg ulong to void
1473if not spk_stage1
1474ret
1475end
1476if not spk_stage2
1477ret
1478end
1479if not spk_stage3
1480ret
1481end
1482if eq SPK_PLAT_QEMU 1
1483note QEMU has no GB20B: stop after the last stage QEMU can model and exit via PSCI SYSTEM_RESET.
1484uart_puts spk_q_stop
1485arm64_psci_reset get acpi_info 7
1486ret
1487end
1488if not spk_stage4
1489ret
1490end
1491if not spk_stage5
1492ret
1493end
1494spk_stage6
1495end
1496
1497fn main to int
1498if not efi_exit_boot_services
1499ret 1
1500end
1501arm64_daif_mask
1502arm64_post_ebs_set UART_SPARK_16550_PA PSCI_CONDUIT_NONE
1503outp = fwadr efi_mm_out 0 64
1504rsdp = fwld add outp 40 8
1505ok_acpi = acpi_parse_rsdp rsdp
1506conduit = get acpi_info 7
1507if ne get acpi_info 8 0
1508uart_init get acpi_info 8 get acpi_info 9 conduit
1509else
1510uart_init UART_SPARK_16550_PA UART_KIND_16550_32 conduit
1511end
1512arm64_post_ebs_set uart_base conduit
1513frames_init
1514mp = fwadr efi_mmap 0 EFI_MMAP_CAP
1515cnt = frames_ingest_efi_raw mp fwld outp 8 fwld add outp 16 8
1516if eq cnt 0
1517trap 8
1518end
1519ib = arm64_meta 0
1520bss = arm64_meta 1
1521ie = arm64_meta 2
1522frames_reserve ib ie
1523reserve_gb10_rmrs
1524if ne rsdp 0
1525frames_reserve rsdp add rsdp 4096
1526end
1527if not mmu_build_initial ib bss ie
1528trap 8
1529end
1530e1 = arm64_meta 4
1531if eq e1 0
1532trap 8
1533end
1534mmu_enter_el1 e1 rsdp
1535ret 0
1536end