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/kernel/hal/smmu_qemu_test.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 / kernel / hal / smmu_qemu_test.om` 529 lines ยท 12331 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/kernel/hal/smmu_qemu_test.om: target a64efi QEMU proof of the mode-1 (memory-backed) SMMUv3 3 note back end in smmuv3.om. Runs under qemu-system-aarch64 -M virt,iommu=smmuv3 with -device edu: 4 note 1. IORT -> SMMUv3 base; smmu_init_instance (zeroed 2-level STRTAB, CR1/CR2, GBPA.ABORT, CMDQ/EVENTQ, 5 note CFGI_ALL + TLBI_NSNH_ALL + CMD_SYNC completion, SMMUEN) -> SYNC must complete (CONS == PROD, ERR == 0). 6 note 2. smmu_open_stream for the edu RID (CD + L1STD + L2 STE in memory, CFGI_STE/CFGI_CD_ALL/TLBI/SYNC). 7 note 3. smmu_map_span one 8 KiB Normal-NC buffer (VA == PA stage-1 tables) and run the edu DMA engine 8 note RAM -> device -> RAM through it: the bytes must round-trip (mapped DMA succeeds). 9 note 4. edu DMA device -> an UNMAPPED page: the page must stay untouched and the EVENTQ must hold an 10 note F_TRANSLATION (0x10) record whose StreamID and InputAddr match (unmapped DMA faults + is recorded). 11 note 5. smmu_map_span_wb on frames_alloc (CPU Normal-WB) RAM: refused under a TEST-ONLY mock CCA=0, then 12 note mapped with the real IORT CCA=1 and an edu device -> RAM DMA must land in it. 13 note 6. CMDQ in-flight limit: a 15th non-SYNC command is refused, the SYNC fails closed, the queue recovers. 14 note 7. Failed open (mode 1): CMDQ pre-filled so the open's CFGI is refused -> SMMU_ECMDERR; the STE written 15 note to memory must be invalidated (word 0 == 0), the slot not live, and a retry of the same RID succeeds. 16 note Prints SMMU_QEMU_PASS or SMMU_QEMU_FAIL step=N, then PSCI SYSTEM_RESET (QEMU -no-reboot exits). 17 note QEMU-only: never run on Spark silicon (no edu device; refuses anything but vendor 0x1234/0x11e8). 18 19 const SQ_EFI_MMAP_CAP ulong 65536 20 const SQ_EDU_VID ulong 0x1234 21 const SQ_EDU_DID ulong 0x11e8 22 const SQ_EDU_BAR_SZ ulong `1048576` 23 const SQ_EDU_BUF ulong 0x40000 24 const SQ_DMA_LEN ulong 256 25 const SQ_SENT ulong 0x5a5a5a5a5a5a5a5a 26 27 global efi_mmap uchar 65536 28 global efi_mm_out uchar 64 29 note sq_st: 0=edu dev, 1=edu bar0, 2=smmu base, 3=sid, 4=stream handle, 5=buf pa, 6=unmapped pa 30 global sq_st ulong 8 31 32 text sq_hdr "\n[SMMUQ] mode-1 SMMUv3 on QEMU virt iommu=smmuv3 + edu DMA\n" 33 text sq_up "SMMUQ_UP base=0x" 34 text sq_cmds " cmds=" 35 text sq_sid "SMMUQ_STREAM sid=0x" 36 text sq_okdma "SMMUQ_MAPPED_DMA_OK bytes=" 37 text sq_evt "SMMUQ_FAULT type=0x" 38 text sq_esid " sid=0x" 39 text sq_eaddr " addr=0x" 40 text sq_pass "SMMU_QEMU_PASS mapped_dma=ok unmapped_dma=F_TRANSLATION evtq=recorded cmd_sync=complete wb=ok cmdq_limit=ok failopen=teardown\n" 41 text sq_wbmock "SMMUQ_WB_MOCK_CCA0_REFUSED (test-only: stream CCA forced to 0)\n" 42 text sq_wbok "SMMUQ_WB_DMA_OK frames_alloc WB page, IORT CCA=1, bytes=" 43 text sq_qlim "SMMUQ_CMDQ_LIMIT_OK refused=15th recovered=yes\n" 44 text sq_fo "SMMUQ_FAILOPEN_TEARDOWN_OK rid=0x10 ecmderr=yes ste_invalidated=yes retry=ok\n" 45 text sq_fail "SMMU_QEMU_FAIL step=" 46 text sq_nl "\n" 47 48 fn efi_find_acpi2_rsdp to ulong 49 ret efcfg 0x11d3e4f18868e871 0x81883cc7800022bc 50 end 51 52 fn efi_exit_boot_services to bool 53 bs = efbs 54 ih = fwsys 0 55 x = fwcall fwld add bs 256 8 0 0 0 0 0 0 56 outp = fwadr efi_mm_out 0 64 57 x = fwst add outp 40 8 efi_find_acpi2_rsdp 58 x = fwst add outp 48 8 arm64_current_el 59 gmm = fwld add bs 56 8 60 ebs = fwld add bs 232 8 61 mp = fwadr efi_mmap 0 SQ_EFI_MMAP_CAP 62 att = ulong 0 63 while lt att 8 bound 8 64 x = fwst outp 8 SQ_EFI_MMAP_CAP 65 s = fwcall gmm outp mp add outp 8 add outp 16 add outp 24 0 66 if ne s 0 67 ret false 68 end 69 s2 = fwcall ebs ih fwld add outp 8 8 0 0 0 0 70 att = add att 1 71 if eq s2 0 72 ret true 73 end 74 end 75 ret false 76 end 77 78 fn sq_halt to void 79 arm64_psci_reset get acpi_info 7 80 i = ulong 0 81 while lt i `9223372036854775807` bound `9223372036854775807` 82 arm64_wfe 83 end 84 end 85 86 fn sq_die step ulong v ulong to void 87 uart_puts sq_fail 88 uart_putdec step 89 uart_puts sq_esid 90 uart_puthex v 91 uart_puts sq_nl 92 sq_halt 93 end 94 95 note Same ECAM registration as spark_main.om spk_register_mcfg_segs (Bus 0..1, 2 MiB Device-nGnRnE window). 96 fn sq_mcfg to ulong 97 nm = get acpi_info 12 98 if gt nm PCIE_MAX_SEGS 99 nm = PCIE_MAX_SEGS 100 end 101 mapped = ulong 0 102 i = ulong 0 103 while lt i nm bound 16 104 base = get acpi_mcfg_base i 105 if ne base 0 106 if mmio_map_device base SPARK_ECAM_WIN_SZ 107 put pcie_seg_base mapped base 108 put pcie_seg_id mapped uint get acpi_mcfg_seg i 109 put pcie_seg_bus0 mapped uchar and get acpi_mcfg_sbus i 255 110 put pcie_seg_bus1 mapped uchar 1 111 mapped = add mapped 1 112 end 113 end 114 i = add i 1 115 end 116 put pcie_nsegs 0 mapped 117 j = ulong 0 118 while lt j mapped bound 16 119 h = pcie_map_seg_ecam get pcie_seg_id j 120 j = add j 1 121 end 122 ret mapped 123 end 124 125 note Find edu on segment 0 bus 0; BAR0 is a 32-bit non-prefetchable memory BAR (type bits [2:1] = 0). 126 fn sq_find_edu to bool 127 d = ulong 0 128 while lt d 32 bound 32 129 if eq pcie_probe_dev 0 0 d 0 PCIE_OK 130 if and eq get pcie_dev_info 0 SQ_EDU_VID eq get pcie_dev_info 1 SQ_EDU_DID 131 b = get pcie_dev_info 5 132 bar = and b 0xfffffff0 133 if eq and b 6 4 134 bar = or bar shl get pcie_dev_info 6 32 135 end 136 if eq bar 0 137 ret false 138 end 139 put sq_st 0 d 140 put sq_st 1 bar 141 ret true 142 end 143 end 144 d = add d 1 145 end 146 ret false 147 end 148 149 fn sq_iort to bool 150 ib = new uchar 4096 0 151 ipa = get acpi_info 10 152 ilen = get acpi_info 11 153 if or eq ipa 0 or lt ilen 48 gt ilen 4096 154 ret false 155 end 156 k = ulong 0 157 while lt k ilen bound 4096 158 put ib k uchar and fwld add ipa k 1 255 159 k = add k 1 160 end 161 ret eq iort_parse part ib 0 ilen 1 162 end 163 164 fn sq_smmu_up to long 165 base = get iort_smmu_base 0 166 put sq_st 2 base 167 if not mmio_map_device base SPARK_SMMU_SZ 168 ret -100 169 end 170 st = dma_alloc_nc 2 8192 171 cq = dma_alloc_nc 1 4096 172 eq0 = dma_alloc_nc 1 4096 173 pool = dma_alloc_nc 64 4096 174 if or or eq st 0 eq cq 0 or eq eq0 0 eq pool 0 175 ret -101 176 end 177 if ne smmu_set_pt_pool pool 64 SMMU_OK 178 ret -102 179 end 180 ret smmu_init_instance 0 base 16 st cq eq0 181 end 182 183 fn sq_edu_w64 off ulong v ulong to void 184 arm64_mmio_w64 add get sq_st 1 off v 185 end 186 187 fn sq_edu_r32 off ulong to ulong 188 ret arm64_mmio_r32 add get sq_st 1 off 189 end 190 191 note edu DMA (QEMU hw/misc/edu.c): 0x80 src, 0x88 dst, 0x90 count, 0x98 cmd (bit0 RUN, bit1 1 = device->RAM). 192 fn sq_edu_dma src ulong dst ulong to_ram ulong to bool 193 sq_edu_w64 0x80 src 194 sq_edu_w64 0x88 dst 195 sq_edu_w64 0x90 SQ_DMA_LEN 196 cmd = ulong 1 197 if ne to_ram 0 198 cmd = ulong 3 199 end 200 arm64_mmio_w32 add get sq_st 1 0x98 cmd 201 i = ulong 0 202 while lt i `200000000` bound `200000000` 203 if eq and sq_edu_r32 0x98 1 0 204 ret true 205 end 206 cpurelax 207 i = add i 1 208 end 209 ret false 210 end 211 212 fn sq_fill pa ulong v ulong n ulong to void 213 o = ulong 0 214 while lt o n bound 1024 215 arm64_mmio_w64 add pa o add v o 216 o = add o 8 217 end 218 end 219 220 fn sq_same pa ulong v ulong n ulong to bool 221 o = ulong 0 222 while lt o n bound 1024 223 if ne arm64_mmio_r64 add pa o add v o 224 ret false 225 end 226 o = add o 8 227 end 228 ret true 229 end 230 231 fn sq_untouched pa ulong n ulong to bool 232 o = ulong 0 233 while lt o n bound 1024 234 if ne arm64_mmio_r64 add pa o SQ_SENT 235 ret false 236 end 237 o = add o 8 238 end 239 ret true 240 end 241 242 fn sq_stream to bool 243 d = get sq_st 0 244 rid = uint shl d 3 245 cd = dma_alloc_nc 1 4096 246 tt = dma_alloc_nc 1 4096 247 if or eq cd 0 eq tt 0 248 ret false 249 end 250 sh = smmu_open_stream 0 rid cd tt 251 if le sh 0 252 ret false 253 end 254 put sq_st 3 get iort_qres 3 255 put sq_st 4 ulong sh 256 uart_puts sq_sid 257 uart_puthex get sq_st 3 258 uart_puts sq_nl 259 ret true 260 end 261 262 fn sq_dma_tests to ulong 263 buf = dma_alloc_nc 2 4096 264 bad = dma_alloc_nc 1 4096 265 if or eq buf 0 eq bad 0 266 ret 30 267 end 268 put sq_st 5 buf 269 put sq_st 6 bad 270 if ne smmu_map_span long get sq_st 4 buf 8192 0 SMMU_OK 271 ret 31 272 end 273 sq_fill buf 0x0123456789abcdef SQ_DMA_LEN 274 for z ulong 32 275 arm64_mmio_w64 add add buf 4096 mul z 8 SQ_SENT 276 arm64_mmio_w64 add bad mul z 8 SQ_SENT 277 end 278 if ne pcie_enable_endpoint 0 0 get sq_st 0 0 PCIE_OK 279 ret 32 280 end 281 if not mmio_map_device get sq_st 1 SQ_EDU_BAR_SZ 282 ret 33 283 end 284 if not sq_edu_dma buf SQ_EDU_BUF 0 285 ret 34 286 end 287 if not sq_edu_dma SQ_EDU_BUF add buf 4096 1 288 ret 35 289 end 290 if not sq_same add buf 4096 0x0123456789abcdef SQ_DMA_LEN 291 ret 36 292 end 293 uart_puts sq_okdma 294 uart_putdec SQ_DMA_LEN 295 uart_puts sq_nl 296 if not sq_edu_dma SQ_EDU_BUF bad 1 297 ret 37 298 end 299 if not sq_untouched bad SQ_DMA_LEN 300 ret 38 301 end 302 ret 0 303 end 304 305 fn sq_check_fault to ulong 306 n = smmu_drain_events 0 307 if le n 0 308 ret 40 309 end 310 w0 = get smmu_inst_last_evt 0 311 uart_puts sq_evt 312 uart_puthex and w0 255 313 uart_puts sq_esid 314 uart_puthex shr w0 32 315 uart_puts sq_eaddr 316 uart_puthex get smmu_inst_last_addr 0 317 uart_puts sq_nl 318 if ne and w0 255 0x10 319 ret 41 320 end 321 if ne shr w0 32 get sq_st 3 322 ret 42 323 end 324 if ne and get smmu_inst_last_addr 0 0xfffffffffffff000 get sq_st 6 325 ret 43 326 end 327 ret 0 328 end 329 330 note WB attribute check on memory that really is CPU Normal-WB (frames_alloc RAM, mapped WB by mmu.om): 331 note (a) TEST-ONLY mock: force this stream's recorded CCA to 0 -> smmu_map_span_wb must refuse (SMMU_EATTR); 332 note (b) real IORT CCA (QEMU virt RC reports CCA=1) -> WB map, edu device->RAM DMA lands, CPU reads it back. 333 fn sq_wb_tests to ulong 334 wb = frames_alloc 1 4096 SMMU_PA_40_LIMIT 335 if eq wb 0 336 ret 50 337 end 338 sh = long get sq_st 4 339 slot = sub get sq_st 4 1 340 cca = get smmu_str_cca slot 341 put smmu_str_cca slot 0 342 r = smmu_map_span_wb sh wb 4096 343 put smmu_str_cca slot cca 344 if ne r SMMU_EATTR 345 ret 51 346 end 347 uart_puts sq_wbmock 348 if ne cca 1 349 ret 52 350 end 351 for z ulong 32 352 arm64_mmio_w64 add wb mul z 8 SQ_SENT 353 end 354 if ne smmu_map_span_wb sh wb 4096 SMMU_OK 355 ret 53 356 end 357 if not sq_edu_dma SQ_EDU_BUF wb 1 358 ret 54 359 end 360 if not sq_same wb 0x0123456789abcdef SQ_DMA_LEN 361 ret 55 362 end 363 uart_puts sq_wbok 364 uart_putdec SQ_DMA_LEN 365 uart_puts sq_nl 366 ret 0 367 end 368 369 note CMDQ in-flight limit (mode 1): 14 commands + SYNC is the maximum batch; a 15th non-SYNC command is 370 note refused by smmu_cmd_push, the next SYNC fails closed (batch dropped), and the queue then recovers. 371 fn sq_qlimit to ulong 372 for k ulong 15 373 smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0 374 end 375 if ne smmu_cmd_sync 0 SMMU_ECMDERR 376 ret 60 377 end 378 if ne smmu_cmd_sync 0 SMMU_OK 379 ret 61 380 end 381 for j ulong 14 382 smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0 383 end 384 if ne smmu_cmd_sync 0 SMMU_OK 385 ret 62 386 end 387 uart_puts sq_qlim 388 ret 0 389 end 390 391 note Word 0 of the in-memory STE for sid on instance 0 (0 when no L2 table exists yet). 392 fn sq_ste_w0 sid ulong to ulong 393 d = arm64_mmio_r64 add get smmu_inst_strtab 0 mul shr sid SMMU_STRTAB_SPLIT 8 394 if eq d 0 395 ret 0 396 end 397 ret arm64_mmio_r64 add and d SMMU_L2PTR_MASK mul and sid 63 64 398 end 399 400 note Mode-1 failed open: 14 un-issued commands make the open's CFGI_STE over-limit, so its CMD_SYNC fails 401 note (SMMU_ECMDERR) after the STE was already written to memory. The teardown must invalidate that STE, 402 note leave the slot unused and the instance up, and a retry of the same RID must succeed. 403 fn sq_failopen to ulong 404 cd = dma_alloc_nc 1 4096 405 tt = dma_alloc_nc 1 4096 406 if or eq cd 0 eq tt 0 407 ret 70 408 end 409 n0 = get smmu_str_n 0 410 for k ulong 14 411 smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0 412 end 413 r = smmu_open_stream 0 0x0010 cd tt 414 if ne r SMMU_ECMDERR 415 sq_die 71 ulong sub 0 r 416 end 417 sid = get iort_qres 3 418 if or ne get smmu_str_n 0 n0 ne get smmu_str_live n0 0 419 ret 72 420 end 421 if ne sq_ste_w0 sid 0 422 ret 73 423 end 424 if ne get smmu_inst_state 0 1 425 ret 74 426 end 427 if le smmu_open_stream 0 0x0010 cd tt 0 428 ret 75 429 end 430 if eq and sq_ste_w0 sid 1 0 431 ret 76 432 end 433 uart_puts sq_fo 434 ret 0 435 end 436 437 fn el1main el1_arg ulong to void 438 uart_puts sq_hdr 439 bmmio_reset 440 pcie_reset 441 smmu_reset 442 put bmmio_mode 0 1 443 if eq sq_mcfg 0 444 sq_die 1 0 445 end 446 if not sq_find_edu 447 sq_die 2 0 448 end 449 if lt pcie_quiesce_unused 0 450 sq_die 3 0 451 end 452 if not sq_iort 453 sq_die 4 get acpi_info 11 454 end 455 r = sq_smmu_up 456 if ne r SMMU_OK 457 sq_die 5 ulong r 458 end 459 uart_puts sq_up 460 uart_puthex get sq_st 2 461 uart_puts sq_cmds 462 uart_putdec get smmu_cmd_count 0 463 uart_puts sq_nl 464 if not sq_stream 465 sq_die 6 0 466 end 467 t = sq_dma_tests 468 if ne t 0 469 sq_die t 0 470 end 471 f = sq_check_fault 472 if ne f 0 473 sq_die f 0 474 end 475 w = sq_wb_tests 476 if ne w 0 477 sq_die w get iort_qres 8 478 end 479 q = sq_qlimit 480 if ne q 0 481 sq_die q 0 482 end 483 fo = sq_failopen 484 if ne fo 0 485 sq_die fo 0 486 end 487 uart_puts sq_pass 488 sq_halt 489 end 490 491 fn main to int 492 if not efi_exit_boot_services 493 ret 1 494 end 495 arm64_daif_mask 496 arm64_post_ebs_set UART_SPARK_16550_PA PSCI_CONDUIT_NONE 497 outp = fwadr efi_mm_out 0 64 498 rsdp = fwld add outp 40 8 499 ok_acpi = acpi_parse_rsdp rsdp 500 conduit = get acpi_info 7 501 if ne get acpi_info 8 0 502 uart_init get acpi_info 8 get acpi_info 9 conduit 503 else 504 uart_init UART_SPARK_16550_PA UART_KIND_16550_32 conduit 505 end 506 arm64_post_ebs_set uart_base conduit 507 frames_init 508 mp = fwadr efi_mmap 0 SQ_EFI_MMAP_CAP 509 cnt = frames_ingest_efi_raw mp fwld outp 8 fwld add outp 16 8 510 if eq cnt 0 511 trap 8 512 end 513 ib = arm64_meta 0 514 bss = arm64_meta 1 515 ie = arm64_meta 2 516 frames_reserve ib ie 517 if ne rsdp 0 518 frames_reserve rsdp add rsdp 4096 519 end 520 if not mmu_build_initial ib bss ie 521 trap 8 522 end 523 e1 = arm64_meta 4 524 if eq e1 0 525 trap 8 526 end 527 mmu_enter_el1 e1 rsdp 528 ret 0 529 end