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