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 / kernel / hal / pcie_smmu_test.om

739 lines · 20432 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/hal/pcie_smmu_test.om: Pure-Omega host/model verification suite for Piece 2
3note (bar0_mmio.om, pcie.om, smmuv3.om, irqbm.om, irq.om iqmode=2, irqcc.om).
4note Compose with:
5note cat si/directives/om2.om aienux/kernel/hal/iort.om aienux/kernel/hal/bar0_mmio.om \
6note aienux/kernel/hal/pcie.om aienux/kernel/hal/smmuv3.om gpu/driver/gsp/logdec.om \
7note gpu/driver/gsp/irqbm.om gpu/driver/gsp/irq.om gpu/driver/gsp/irqcc.om \
8note aienux/kernel/hal/pcie_smmu_test.om | seed/out/omc > pcie_smmu_test
9
10text ps_pass "PASS "
11text ps_fail "FAIL "
12text ps_nl "\n"
13text ps_t1 "t01_bar0_mmio_exclusive_and_gb20b_id"
14text ps_t2 "t02_pcie_mcfg_and_ecam_ngnrne"
15text ps_t3 "t03_pcie_caps_quiesce_and_flr_guard"
16text ps_t4 "t04_smmu_preconditions_and_fail_closed"
17text ps_t5 "t05_smmu_abc_deterministic_bringup"
18text ps_t6 "t06_smmu_disjoint_dma_and_rmr"
19text ps_t7 "t07_smmu_eventq_fault_drain"
20text ps_t8 "t08_irq_iqmode2_bar0_and_crashcat"
21text ps_t9 "t09_smmu_open_teardown_and_iort_base_check"
22text ps_sum "PCIE_SMMU_TEST_PASS total=9 failed=0\n"
23text ps_fsum "PCIE_SMMU_TEST_FAIL\n"
24
25global ps_mcfg uchar 128
26global ps_iort uchar 512
27global ps_mmio_stub ulong 4
28
29note Host-side stubs for bar0_mmio.om mode 1 (bare-metal uses fwops.om fwld/fwst + dsb sy).
30fn arm64_mmio_r32 addr ulong to ulong
31put ps_mmio_stub 0 addr
32ret and get ps_mmio_stub 1 4294967295
33end
34
35fn arm64_mmio_w32 addr ulong val ulong to void
36put ps_mmio_stub 0 addr
37put ps_mmio_stub 1 and val 4294967295
38end
39
40fn arm64_mmio_r64 addr ulong to ulong
41put ps_mmio_stub 2 addr
42ret get ps_mmio_stub 3
43end
44
45fn arm64_mmio_w64 addr ulong val ulong to void
46put ps_mmio_stub 2 addr
47put ps_mmio_stub 3 val
48end
49
50fn ps_wstr s view uchar * to void
51n = len s
52r = write 1 s 0 n
53end
54
55fn ps_rep name view uchar * ok bool to ulong
56if ok
57ps_wstr ps_pass
58ps_wstr name
59ps_wstr ps_nl
60ret ulong 0
61end
62ps_wstr ps_fail
63ps_wstr name
64ps_wstr ps_nl
65ret ulong 1
66end
67
68fn ps_mput16 off ulong v ulong to void
69put ps_mcfg off uchar and v 0xff
70put ps_mcfg add off 1 uchar and shr v 8 0xff
71end
72
73fn ps_mput32 off ulong v ulong to void
74ps_mput16 off and v 0xffff
75ps_mput16 add off 2 and shr v 16 0xffff
76end
77
78fn ps_mput64 off ulong v ulong to void
79ps_mput32 off and v 0xffffffff
80ps_mput32 add off 4 and shr v 32 0xffffffff
81end
82
83fn ps_mcfg_checksum n ulong to void
84put ps_mcfg 9 0
85acc = ulong 0
86for i ulong n bound 128
87acc = and add acc ulong get ps_mcfg i 0xff
88end
89put ps_mcfg 9 uchar and sub 256 acc 0xff
90end
91
92fn ps_build_spark_mcfg to ulong
93for i ulong 128
94put ps_mcfg i 0
95end
96put ps_mcfg 0 77
97put ps_mcfg 1 67
98put ps_mcfg 2 70
99put ps_mcfg 3 71
100tlen = ulong 108
101ps_mput32 4 tlen
102note Entry 0: seg 4 (NVMe) base 0xF340000000 bus 0..1
103ps_mput64 44 SPARK_ECAM_SEG4_PA
104ps_mput16 52 4
105put ps_mcfg 54 0
106put ps_mcfg 55 1
107note Entry 1: seg 7 (RTL8127) base 0xF370000000 bus 0..1
108ps_mput64 60 SPARK_ECAM_SEG7_PA
109ps_mput16 68 7
110put ps_mcfg 70 0
111put ps_mcfg 71 1
112note Entry 2: seg 9 (unused Wi-Fi) base 0xF390000000 bus 0..1
113ps_mput64 76 0xf390000000
114ps_mput16 84 9
115put ps_mcfg 86 0
116put ps_mcfg 87 1
117note Entry 3: seg 15 (GB20B) base 0x29000000 bus 0..1
118ps_mput64 92 SPARK_ECAM_SEG15_PA
119ps_mput16 100 15
120put ps_mcfg 102 0
121put ps_mcfg 103 1
122ps_mcfg_checksum tlen
123ret tlen
124end
125
126fn ps_iput16 off ulong v ulong to void
127put ps_iort off uchar and v 0xff
128put ps_iort add off 1 uchar and shr v 8 0xff
129end
130
131fn ps_iput32 off ulong v ulong to void
132ps_iput16 off and v 0xffff
133ps_iput16 add off 2 and shr v 16 0xffff
134end
135
136fn ps_iput64 off ulong v ulong to void
137ps_iput32 off and v 0xffffffff
138ps_iput32 add off 4 and shr v 32 0xffffffff
139end
140
141fn ps_iort_checksum n ulong to void
142put ps_iort 9 0
143acc = ulong 0
144for i ulong n bound 512
145acc = and add acc ulong get ps_iort i 0xff
146end
147put ps_iort 9 uchar and sub 256 acc 0xff
148end
149
150note Build Spark-shape IORT: 3 SMMUs (A=0x13800000, B=0x13000000, C=0x14900000),
151note 3 RCs (seg 4 -> A out=0x50000, seg 7 -> A out=0x80000, seg 15 -> B out=0),
152note 1 RMR on B (SID 0..0x100, 3 Spark ranges). Total 7 nodes.
153fn ps_build_spark_iort to ulong
154for i ulong 512
155put ps_iort i 0
156end
157put ps_iort 0 73
158put ps_iort 1 79
159put ps_iort 2 82
160put ps_iort 3 84
161note Header: 48 bytes, 7 nodes starting at offset 48
162ps_iput32 36 7
163ps_iput32 40 48
164note Node 0 @ 48 (len 24): SMMUv3 A = 0x13800000
165put ps_iort 48 IORT_NODE_SMMUV3
166ps_iput16 49 24
167ps_iput64 64 SPARK_SMMU_A_PA
168note Node 1 @ 72 (len 24): SMMUv3 B = 0x13000000
169put ps_iort 72 IORT_NODE_SMMUV3
170ps_iput16 73 24
171ps_iput64 88 SPARK_SMMU_B_PA
172note Node 2 @ 96 (len 24): SMMUv3 C = 0x14900000
173put ps_iort 96 IORT_NODE_SMMUV3
174ps_iput16 97 24
175ps_iput64 112 SPARK_SMMU_C_PA
176note Node 3 @ 120 (len 52 = 32 hdr + 20 map): RC seg 4 -> SMMUv3 A (off 48), in 0..0xffff -> out 0x50000
177put ps_iort 120 IORT_NODE_RC
178ps_iput16 121 52
179ps_iput32 128 1
180ps_iput32 132 32
181ps_iput32 148 4
182ps_iput32 152 0
183ps_iput32 156 0xffff
184ps_iput32 160 0x50000
185ps_iput32 164 48
186note Node 4 @ 172 (len 52): RC seg 7 -> SMMUv3 A (off 48), in 0..0xffff -> out 0x80000
187put ps_iort 172 IORT_NODE_RC
188ps_iput16 173 52
189ps_iput32 180 1
190ps_iput32 184 32
191ps_iput32 200 7
192ps_iput32 204 0
193ps_iput32 208 0xffff
194ps_iput32 212 0x80000
195ps_iput32 216 48
196note Node 5 @ 224 (len 52): RC seg 15 -> SMMUv3 B (off 72), in 0..0xffff -> out 0
197put ps_iort 224 IORT_NODE_RC
198ps_iput16 225 52
199ps_iput32 232 1
200ps_iput32 236 32
201ps_iput32 252 15
202ps_iput32 256 0
203ps_iput32 260 0xffff
204ps_iput32 264 0
205ps_iput32 268 72
206note Node 6 @ 276 (len 108 = 28 hdr + 60 descs + 20 map): RMR on B (off 72), SID 0..0x100
207put ps_iort 276 IORT_NODE_RMR
208ps_iput16 277 108
209ps_iput32 284 1
210ps_iput32 288 88
211ps_iput32 292 0x10
212ps_iput32 296 3
213ps_iput32 300 28
214note Desc 0 @ 304: 0xa1600000 + 386 MiB (0x18200000)
215ps_iput64 304 0xa1600000
216ps_iput64 312 0x18200000
217note Desc 1 @ 324: 0x280000000 + 2 GiB (0x80000000)
218ps_iput64 324 0x280000000
219ps_iput64 332 0x80000000
220note Desc 2 @ 344: 0x300000000 + 48 MiB (0x3000000)
221ps_iput64 344 0x300000000
222ps_iput64 352 0x3000000
223note Map @ 364: in=0, cnt=0x100, out=0, ref=72 (SMMUv3 B)
224ps_iput32 364 0
225ps_iput32 368 0x100
226ps_iput32 372 0
227ps_iput32 376 72
228tlen = ulong 384
229ps_iput32 4 tlen
230ps_iort_checksum tlen
231ret tlen
232end
233
234fn t01_bar0 to bool
235bmmio_reset
236note Reject non-Device-nGnRnE (attr=1 Device-nGnRE), va==0, unaligned va, va+sz wrap
237if ne bmmio_register SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_SZ 1 BMMIO_EATTR
238ret false
239end
240if ne bmmio_register SPARK_GB20B_BAR0_PA 0 SPARK_GB20B_BAR0_SZ BMMIO_ATTR_NGNRNE BMMIO_EARG
241ret false
242end
243if ne bmmio_register SPARK_GB20B_BAR0_PA add SPARK_GB20B_BAR0_PA 1 SPARK_GB20B_BAR0_SZ BMMIO_ATTR_NGNRNE BMMIO_EALIGN
244ret false
245end
246if ne bmmio_register SPARK_GB20B_BAR0_PA 0xfffffffffffff000 0x2000 BMMIO_ATTR_NGNRNE BMMIO_EARG
247ret false
248end
249hb = bmmio_register SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_SZ BMMIO_ATTR_NGNRNE
250if le hb 0
251ret false
252end
253note Reject overlapping BAR
254if ne bmmio_register add SPARK_GB20B_BAR0_PA 4096 add SPARK_GB20B_BAR0_PA 4096 4096 BMMIO_ATTR_NGNRNE BMMIO_EOVERLAP
255ret false
256end
257hn = bmmio_register SPARK_NVME_BAR0_PA SPARK_NVME_BAR0_PA SPARK_NVME_BAR0_SZ BMMIO_ATTR_NGNRNE
258hr = bmmio_register SPARK_RTL8127_BAR2_PA SPARK_RTL8127_BAR2_PA SPARK_RTL8127_BAR2_SZ BMMIO_ATTR_NGNRNE
259if or le hn 0 le hr 0
260ret false
261end
262note Reject unaligned & OOB reads
263v1 = bmmio_r32 hb 2
264if ne get bmmio_err 0 BMMIO_EALIGN
265ret false
266end
267v2 = bmmio_r32 hb SPARK_GB20B_BAR0_SZ
268if ne get bmmio_err 0 BMMIO_EBOUNDS
269ret false
270end
271note Verify GB20B BOOT_0 / BOOT_42 identity check
272z1 = bmmio_w32 hb GB20B_OFF_BOOT0 GB20B_VAL_BOOT0
273z2 = bmmio_w32 hb GB20B_OFF_BOOT42 GB20B_VAL_BOOT42
274if not bmmio_verify_gb20b hb
275ret false
276end
277z3 = bmmio_w32 hb GB20B_OFF_BOOT42 0x1ba00000
278if bmmio_verify_gb20b hb
279ret false
280end
281ret true
282end
283
284fn t02_mcfg to bool
285bmmio_reset
286pcie_reset
287n = ps_build_spark_mcfg
288loc = new uchar 128 0
289for i ulong 128
290put loc i get ps_mcfg i
291end
292if ne pcie_mcfg_parse part loc 0 n PCIE_OK
293ret false
294end
295if ne get pcie_nsegs 0 4
296ret false
297end
298note Corrupt checksum must fail closed
299put loc 9 xor get loc 9 1
300if eq pcie_mcfg_parse part loc 0 n PCIE_OK
301ret false
302end
303put loc 9 xor get loc 9 1
304if ne pcie_mcfg_parse part loc 0 n PCIE_OK
305ret false
306end
307note Map ECAM windows for segs 4, 7, 9, 15 as Device-nGnRnE XN
308h4 = pcie_map_seg_ecam 4
309h7 = pcie_map_seg_ecam 7
310h9 = pcie_map_seg_ecam 9
311h15 = pcie_map_seg_ecam 15
312if or le h4 0 or le h7 0 or le h9 0 le h15 0
313ret false
314end
315ret true
316end
317
318fn t03_caps_quiesce to bool
319note Populate GB20B 000f:01:00.0 (PM D0 @ 0x60, MSI-X 9 vecs @ 0x70 BAR0+0x00b90000/0x00ba0000)
320note Seed STATUS=0x0010 (CapList) + RW1C error bit 0x8000 -> must NOT be written back as 1 when updating COMMAND!
321z = pcie_cfg_w32 15 1 0 0 0 0x2e1210de
322z = pcie_cfg_w32 15 1 0 0 4 0x80100003
323z = pcie_cfg_w32 15 1 0 0 52 0x60
324z = pcie_cfg_w32 15 1 0 0 0x60 0x00007001
325z = pcie_cfg_w32 15 1 0 0 0x64 0x00000000
326z = pcie_cfg_w32 15 1 0 0 0x70 0x80080011
327z = pcie_cfg_w32 15 1 0 0 0x74 0x00b90000
328z = pcie_cfg_w32 15 1 0 0 0x78 0x00ba0000
329note Populate unused Wi-Fi 0009:01:00.0 (BME=1) + multi-function 0009:01:02.0 (HTYPE=0x80) / 0009:01:02.1 (BME=1)
330z = pcie_cfg_w32 9 1 0 0 0 0x792514c3
331z = pcie_cfg_w32 9 1 0 0 4 0x00100006
332z = pcie_cfg_w32 9 1 2 0 0 0x123410de
333z = pcie_cfg_w32 9 1 2 0 4 0x00000006
334z = pcie_cfg_w32 9 1 2 0 12 0x00800000
335z = pcie_cfg_w32 9 1 2 1 0 0x123510de
336z = pcie_cfg_w32 9 1 2 1 4 0x00000006
337note off>=4096 must fail (no wrap to 0)
338if ne pcie_cfg_r16 15 1 0 0 4096 0xffff
339ret false
340end
341if ne pcie_cfg_w16 15 1 0 0 4096 0 PCIE_EARG
342ret false
343end
344note Quiesce unused endpoints before GBPA.ABORT (must quiesce 3: 0009:01:00.0, 0009:01:02.0, 0009:01:02.1)
345if ne pcie_quiesce_unused PCIE_OK
346ret false
347end
348if or ne get pcie_quiesced 0 1 ne get pcie_quiesced_cnt 0 3
349ret false
350end
351if ne and pcie_cfg_r16 9 1 0 0 4 PCIE_CMD_BME 0
352ret false
353end
354if ne and pcie_cfg_r16 9 1 2 1 4 PCIE_CMD_BME 0
355ret false
356end
357note Enable GB20B: verifies D0, masks MSI-X, sets Mem+BME+DisINTx, writes 0 to RW1C STATUS upper 16 bits
358if ne pcie_enable_endpoint 15 1 0 0 PCIE_OK
359ret false
360end
361if or ne get pcie_cap_info 3 9 or ne get pcie_cap_info 5 0x00b90000 ne get pcie_cap_info 7 0x00ba0000
362ret false
363end
364mctl = pcie_cfg_r16 15 1 0 0 0x72
365if or ne and mctl PCIE_MSIX_ENABLE 0 eq and mctl PCIE_MSIX_FMASK 0
366ret false
367end
368note Verify RW1C STATUS upper 16 bits were written as 0 (not 0x8010 echoed back)
369cs_after = pcie_cfg_r32 15 1 0 0 4
370if ne shr cs_after 16 0
371ret false
372end
373if ne and cs_after 0x0406 0x0406
374ret false
375end
376note Out-of-bounds cap pointer (cp=252 for PM cap needs cp+4=256) must fail PCIE_ECAP
377z = pcie_cfg_w32 7 1 0 0 0 0x812710ec
378z = pcie_cfg_w32 7 1 0 0 4 0x00100006
379z = pcie_cfg_w32 7 1 0 0 52 252
380z = pcie_cfg_w32 7 1 0 0 252 0x00000001
381if ne pcie_walk_caps 7 1 0 0 PCIE_ECAP
382ret false
383end
384note D3 endpoint must be refused
385z = pcie_cfg_w32 4 1 0 0 0 0xa80d144d
386z = pcie_cfg_w32 4 1 0 0 4 0x00100006
387z = pcie_cfg_w32 4 1 0 0 52 0x60
388z = pcie_cfg_w32 4 1 0 0 0x60 0x00000001
389z = pcie_cfg_w32 4 1 0 0 0x64 0x00000003
390if ne pcie_enable_endpoint 4 1 0 0 PCIE_ED3
391ret false
392end
393note GB20B FLR hard refusal (Trap 18)
394if ne pcie_flr_refuse 15 1 0 0 PCIE_EFLR
395ret false
396end
397ret true
398end
399
400fn t04_smmu_neg to bool
401smmu_reset
402note Must refuse before pcie_quiesced==1
403put pcie_quiesced 0 0
404if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10000000 0x10100000 0x10200000 SMMU_ENOQUIESCE
405ret false
406end
407put pcie_quiesced 0 1
408note Misaligned L1 STRTAB_BASE (4 KiB aligned but not 128 KiB aligned for log2sz=20) -> SMMU_EARG
409if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10001000 0x10100000 0x10200000 SMMU_EARG
410ret false
411end
412note Pre-register SMMUv3 A with missing S1P (bit 1==0) -> SMMU_EIDR
413ha = bmmio_register SPARK_SMMU_A_PA SPARK_SMMU_A_PA SPARK_SMMU_SZ BMMIO_ATTR_NGNRNE
414put smmu_inst_h 0 ha
415put smmu_inst_base 0 SPARK_SMMU_A_PA
416z = bmmio_w32 ha SMMU_REG_IDR0 0x08000000
417z = bmmio_w32 ha SMMU_REG_IDR1 0x00840019
418if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10000000 0x10100000 0x10200000 SMMU_EIDR
419ret false
420end
421note Good IDR0 (ST_LEVEL=1 | S1P=1 = 0x08000002), bad SIDSIZE=16 (< 20 for A) -> SMMU_EIDR
422z = bmmio_w32 ha SMMU_REG_IDR0 0x08000002
423z = bmmio_w32 ha SMMU_REG_IDR1 0x00840010
424if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10000000 0x10100000 0x10200000 SMMU_EIDR
425ret false
426end
427note Inject CMDQ_CONS.ERR[30:24] = 1 (CERROR_ILL) -> SMMU_ECMDERR and CR0 rolled back to 0
428z = bmmio_w32 ha SMMU_REG_IDR1 0x00840019
429put smmu_sim_cmd_err 0 1
430if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10000000 0x10100000 0x10200000 SMMU_ECMDERR
431ret false
432end
433put smmu_sim_cmd_err 0 0
434if ne bmmio_r32 ha SMMU_REG_CR0 0
435ret false
436end
437ret true
438end
439
440fn t05_smmu_abc to bool
441bmmio_reset
442smmu_reset
443put pcie_quiesced 0 1
444ilen = ps_build_spark_iort
445ib = new uchar 512 0
446for i ulong 512
447put ib i get ps_iort i
448end
449if ne iort_parse part ib 0 ilen 1
450ret false
451end
452note Pre-seed IDR0 (ST_LEVEL=1 | S1P=1 = 0x08000002) and IDR1 (CMDQS=4 | EVENTQS=4 | SIDSIZE=25 = 0x00840019) on A and B
453ha = bmmio_register SPARK_SMMU_A_PA SPARK_SMMU_A_PA SPARK_SMMU_SZ BMMIO_ATTR_NGNRNE
454hb = bmmio_register SPARK_SMMU_B_PA SPARK_SMMU_B_PA SPARK_SMMU_SZ BMMIO_ATTR_NGNRNE
455put smmu_inst_h 0 ha
456put smmu_inst_base 0 SPARK_SMMU_A_PA
457put smmu_inst_h 1 hb
458put smmu_inst_base 1 SPARK_SMMU_B_PA
459z = bmmio_w32 ha SMMU_REG_IDR0 0x08000002
460z = bmmio_w32 ha SMMU_REG_IDR1 0x00840019
461z = bmmio_w32 hb SMMU_REG_IDR0 0x08000002
462z = bmmio_w32 hb SMMU_REG_IDR1 0x00840019
463if ne smmu_init_instance 0 SPARK_SMMU_A_PA 20 0x10000000 0x10100000 0x10200000 SMMU_OK
464ret false
465end
466if ne smmu_init_instance 1 SPARK_SMMU_B_PA 9 0x10300000 0x10400000 0x10500000 SMMU_OK
467ret false
468end
469if ne smmu_abort_only 2 SPARK_SMMU_C_PA SMMU_OK
470ret false
471end
472note Verify all 7 deterministic bring-up stages (0x7f) and CR0==0x0d on A and B, ABORT_ONLY on C
473if or ne get smmu_trace 0 0x7f ne get smmu_trace 1 0x7f
474ret false
475end
476if or ne bmmio_r32 ha SMMU_REG_CR0 SMMU_CR0_ALL ne bmmio_r32 hb SMMU_REG_CR0 SMMU_CR0_ALL
477ret false
478end
479if ne get smmu_inst_state 2 2
480ret false
481end
482ret true
483end
484
485fn t06_smmu_dma to bool
486note Open GB20B SID 0x100 on SMMUv3 B -> auto-maps 3 mandatory Spark RMRs
487sgpu = smmu_open_stream 15 0x0100 0x11000000 0x11001000
488if le sgpu 0
489ret false
490end
491note Open NVMe SID 0x50100 and RTL8127 SID 0x80100 on SMMUv3 A
492snvme = smmu_open_stream 4 0x0100 0x11002000 0x11003000
493snic = smmu_open_stream 7 0x0100 0x11004000 0x11005000
494if or le snvme 0 le snic 0
495ret false
496end
497note Verify exact CD word 0 (ENDI==0, AA64==1, R==1, A==1, T0SZ==16, NC walk), STE word 1 == 0 (S1CIR/COR/CSH==0), and PTE SH=2
498if ne get smmu_cd_words 0 0x00006202c0000010
499ret false
500end
501if or ne and get smmu_ste_words 0 0xf 0xb ne get smmu_ste_words 1 0
502ret false
503end
504if ne get smmu_cd_words 3 SMMU_CD_MAIR0
505ret false
506end
507if ne get smmu_sp_pte 0 0x006000000000064c
508ret false
509end
510note Map disjoint spans: GB20B GSP arena 80 MiB, NVMe weights dest 64 MiB, RTL8127 rdma 72 KiB
511if ne smmu_map_span sgpu 0x340000000 0x5000000 0 SMMU_OK
512ret false
513end
514if ne smmu_map_span snvme 0x400000000 0x4000000 0 SMMU_OK
515ret false
516end
517if ne smmu_map_span snic 0x380000000 0x12000 0 SMMU_OK
518ret false
519end
520note Cross-stream overlap (GB20B trying to map NVMe's weights dest before unmap) MUST FAIL
521if ne smmu_map_span sgpu 0x400000000 0x4000000 0 SMMU_EOVERLAP
522ret false
523end
524note Wildcard RAM (>32 GiB) and >=40-bit PA MUST FAIL
525if ne smmu_map_span sgpu 0x500000000 0x1000000000 0 SMMU_EWILDCARD
526ret false
527end
528if ne smmu_map_span sgpu 0x10000000000 0x1000 0 SMMU_EWILDCARD
529ret false
530end
531note Unmap RMR MUST FAIL
532if ne smmu_unmap_span sgpu 0xa1600000 0x18200000 SMMU_EARG
533ret false
534end
535note Unmap weights dest from NVMe -> then GB20B maps it -> SUCCEEDS!
536if ne smmu_unmap_span snvme 0x400000000 0x4000000 SMMU_OK
537ret false
538end
539if ne smmu_map_span sgpu 0x400000000 0x4000000 0 SMMU_OK
540ret false
541end
542note Regression: CMDQ wrap past 32 commands (mask 0x1f) must succeed cleanly
543for w ulong 35
544if ne smmu_cmd_sync 1 SMMU_OK
545ret false
546end
547end
548ret true
549end
550
551fn t07_eventq to bool
552hb = get smmu_inst_h 1
553note Inject F_TRANSLATION (code=0x10, SID=0x100) at SMMUv3 B eventq slot 0 (Page 1 0x100a8/0x100ac, 32 B/record stride=4 u64 -> inst*64=64)
554note Also set OVFLG (bit 31 = 0x80000000) in PROD to verify OVACKFLG preservation/ack in CONS
555put smmu_evtq_buf 64 or ulong 0x10 shl ulong 0x100 32
556z = bmmio_w32 hb SMMU_REG_EVTQ_PROD 0x80000001
557n = smmu_drain_events 1
558if or ne n 1 ne get smmu_inst_evt_cnt 1 1
559ret false
560end
561if ne and get smmu_inst_last_evt 1 0xff 0x10
562ret false
563end
564ret eq bmmio_r32 hb SMMU_REG_EVTQ_CONS 0x80000001
565end
566
567fn ps_ccput64 o ulong v ulong to void
568for i ulong 8
569put ccbuf add o i uchar and shr v mul i 8 0xff
570end
571end
572
573fn t08_irq_bm to bool
574bmmio_reset
575ibm_reset
576hg = bmmio_register SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_PA SPARK_GB20B_BAR0_SZ BMMIO_ATTR_NGNRNE
577if ne ibm_bind hg 1 0
578ret false
579end
580note Reset irq.om state
581put iqmode 0 1
582put iqstate 0 0
583put iqgen 0 0
584put iqdone 0 0
585put iqpend 0 0
586put iqhalt 0 0
587if ne iqinit 1 0
588ret false
589end
590note iqmode=1 must still return -9
591if ne iqservice 1 8 -9
592ret false
593end
594note Switch to bare-metal GB20B polling mode (iqmode=2)
595put iqmode 0 2
596note Seed verified GB20B IRQ regs: RISCV_IRQMASK=0x50 (0x111528), RISCV_IRQDEST=0x50 (0x11152c), FALCON_IRQSTAT=0x40 (0x110008)
597z = bmmio_w32 hg IBM_RVMASK 0x50
598z = bmmio_w32 hg IBM_RVDEST 0x50
599z = bmmio_w32 hg IBM_IRQSTAT 0x40
600put ibm_rpc_avail 0 2
601r1 = iqservicecc 1 8 -1 8
602if or ne r1 2 or ne get ibm_clears 0 1 ne get ibm_order_err 0 0
603ret false
604end
605if ne bmmio_r32 hg IBM_IRQSTAT 0
606ret false
607end
608note Regression: 2nd iqservicecc call with SWGEN0 must keep ibm_order_err == 0
609z = bmmio_w32 hg IBM_IRQSTAT 0x40
610put ibm_rpc_avail 0 1
611if ne iqservicecc 1 8 -1 8 1
612ret false
613end
614if or ne get ibm_clears 0 2 ne get ibm_order_err 0 0
615ret false
616end
617note Inject HALT (0x10) + valid CrashCat V1 report packet (48 bytes) in ccbuf
618for i ulong 48
619put ccbuf i 0
620end
621ps_ccput64 0 14587176806713922100
622ps_ccput64 8 0x1234
623ps_ccput64 16 0x6f
624ps_ccput64 24 0xde
625ps_ccput64 32 0x14d
626ps_ccput64 40 0x1bc
627if ne iqccbind 1 0x340000000 48 0
628ret false
629end
630z = bmmio_w32 hg IBM_IRQSTAT 0x10
631if ne iqservicecc 1 8 -1 8 -13
632ret false
633end
634if ne get iqhalt 0 1
635ret false
636end
637if ne get ibm_masks 0 1
638ret false
639end
640if ne get iqccpkts 0 1
641ret false
642end
643note Verify sticky lockdown stops ALL further BAR0 accesses
644put iqstate 0 0
645put iqgen 0 0
646put iqhalt 0 0
647put ibm_locked 0 0
648if ne iqinit 2 0
649ret false
650end
651z = bmmio_w32 hg IBM_IRQSTAT 0x40
652put ibm_rpc_avail 0 2
653put ibm_rpc_lock_at 0 1
654put ibm_rpc_done 0 0
655if ne iqservicecc 2 8 -1 8 1
656ret false
657end
658if ne get iqstate 0 3
659ret false
660end
661if ne get ibm_locked 0 1
662ret false
663end
664rd_before = get bmmio_rd_ops 0
665wr_before = get bmmio_wr_ops 0
666if ne iqservicecc 2 8 -1 8 -14
667ret false
668end
669if ne get bmmio_rd_ops 0 rd_before
670ret false
671end
672ret eq get bmmio_wr_ops 0 wr_before
673end
674
675fn ps_live_spans to ulong
676c = ulong 0
677for k ulong SMMU_MAX_SPANS
678if ne get smmu_sp_live k 0
679c = add c 1
680end
681end
682ret c
683end
684
685note Error-path teardown: a GB20B open whose CMD_SYNC fails must leave no stream, no live span (RMRs
686note included) and zeroed STE/CD words, so a retry reuses slot 0 and re-maps the RMRs cleanly.
687note IORT base cross-check: an instance brought up at a base other than the IORT node's -> SMMU_EBASE.
688fn t09_teardown to bool
689if not t05_smmu_abc
690ret false
691end
692put smmu_sim_cmd_err 1 1
693if ne smmu_open_stream 15 0x0100 0x11000000 0x11001000 SMMU_ECMDERR
694ret false
695end
696put smmu_sim_cmd_err 1 0
697if or ne get smmu_str_n 0 0 ne get smmu_str_live 0 0
698ret false
699end
700if or ne ps_live_spans 0 ne get smmu_ste_words 0 0
701ret false
702end
703if ne get smmu_cd_words 0 0
704ret false
705end
706note Model hardware recovery: clear CONS.ERR (real HW: GERROR/GERRORN ack + CONS resume).
707z = bmmio_w32 get smmu_inst_h 1 SMMU_REG_CMDQ_CONS get smmu_inst_cmd_prod 1
708if ne smmu_open_stream 15 0x0100 0x11000000 0x11001000 1
709ret false
710end
711if eq ps_live_spans 0
712ret false
713end
714put smmu_inst_base 0 0x12345000
715if ne smmu_open_stream 4 0x0100 0x11002000 0x11003000 SMMU_EBASE
716ret false
717end
718put smmu_inst_base 0 SPARK_SMMU_A_PA
719ret gt smmu_open_stream 4 0x0100 0x11002000 0x11003000 0
720end
721
722fn main to int
723fail = ulong 0
724fail = add fail ps_rep ps_t1 t01_bar0
725fail = add fail ps_rep ps_t2 t02_mcfg
726fail = add fail ps_rep ps_t3 t03_caps_quiesce
727fail = add fail ps_rep ps_t4 t04_smmu_neg
728fail = add fail ps_rep ps_t5 t05_smmu_abc
729fail = add fail ps_rep ps_t6 t06_smmu_dma
730fail = add fail ps_rep ps_t7 t07_eventq
731fail = add fail ps_rep ps_t8 t08_irq_bm
732fail = add fail ps_rep ps_t9 t09_teardown
733if eq fail 0
734ps_wstr ps_sum
735ret 0
736end
737ps_wstr ps_fsum
738ret 1
739end