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 / smmuv3.om

1158 lines · 33729 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/hal/smmuv3.om: Pure-Omega multi-instance ARM SMMUv3 A/B/C driver
3note for bare-metal DGX Spark (consumes iort.om, bar0_mmio.om, pcie.om).
4note Enforces: pcie_quiesced before GBPA.ABORT, IDR0.ST_LEVEL==1, IDR1.SIDSIZE>=min_bits,
5note deterministic CR0/GBPA/STRTAB/CMDQ/EVENTQ/CFGI_ALL/TLBI/CMD_SYNC/ERR==0/SMMUEN bring-up,
6note SMMUv3 C abort-only, strict per-stream disjoint Normal-NC DMA (<40-bit PA, zero wildcard RAM),
7note and auto-mapping of the 3 mandatory GB20B IORT RMR ranges on SMMUv3 B SID 0x100.
8note
9note Two back ends share one API (bmmio_mode, bar0_mmio.om):
10note mode 0 (host model): commands/STEs/CDs/events live in globals; bmmio_sim registers (unit tests).
11note mode 1 (bare metal): everything the SMMU reads is materialised in physically contiguous,
12note aligned Normal-NC memory (VA == PA): 2-level stream table (L1STD + 64-STE L2 tables),
13note 64-byte STE + CD, VMSAv8-64 4 KiB-granule stage-1 tables (VA == PA, 2 MiB blocks where
14note aligned), 16-entry CMDQ and EVENTQ with real PROD/CONS, CMD_SYNC completion polled with a
15note bounded fail-closed timeout plus CMDQ_CONS.ERR and GERROR.CMDQ_ERR checks.
16note Spec references are Arm IHI 0070 (SMMUv3) section numbers; page tables follow Arm ARM D8 (VMSAv8-64).
17note Compose with: si/directives/om2.om aienux/kernel/hal/iort.om aienux/kernel/hal/bar0_mmio.om aienux/kernel/hal/pcie.om aienux/kernel/hal/smmuv3.om ...
18
19const SMMU_MAX_INST ulong 3
20const SMMU_MAX_STREAMS ulong 8
21const SMMU_MAX_SPANS ulong 32
22const SMMU_PA_40_LIMIT ulong 0x10000000000
23const SMMU_MAX_SPAN_BYTES ulong 0x800000000
24const SMMU_POLL_MAX ulong 1024
25note Mode 1 poll budget (~seconds of Device-nGnRnE reads); exceeding it fails closed.
26const SMMU_POLL_HW ulong 4000000
27
28note ARM SMMUv3 register offsets (IHI 0070H.a)
29const SMMU_REG_IDR0 ulong 0x0000
30const SMMU_REG_IDR1 ulong 0x0004
31const SMMU_REG_CR0 ulong 0x0020
32const SMMU_REG_CR0ACK ulong 0x0024
33note IHI 0070 §6.3.10 SMMU_CR1, §6.3.12 SMMU_CR2, §6.3.14/§6.3.15 SMMU_GERROR/GERRORN
34const SMMU_REG_CR1 ulong 0x0028
35const SMMU_REG_CR2 ulong 0x002c
36const SMMU_REG_GERROR ulong 0x0060
37const SMMU_REG_GERRORN ulong 0x0064
38const SMMU_REG_GBPA ulong 0x0044
39const SMMU_REG_STRTAB_BASE ulong 0x0080
40const SMMU_REG_STRTAB_CFG ulong 0x0088
41const SMMU_REG_CMDQ_BASE ulong 0x0090
42const SMMU_REG_CMDQ_PROD ulong 0x0098
43const SMMU_REG_CMDQ_CONS ulong 0x009c
44const SMMU_REG_EVTQ_BASE ulong 0x00a0
45note ARM IHI 0070H.a §6.3: EVENTQ_PROD/CONS are on Page 1 (0x100a8 / 0x100ac), NOT Page 0!
46const SMMU_REG_EVTQ_PROD ulong 0x100a8
47const SMMU_REG_EVTQ_CONS ulong 0x100ac
48
49const SMMU_CR0_SMMUEN ulong 0x01
50const SMMU_CR0_EVTQEN ulong 0x04
51const SMMU_CR0_CMDQEN ulong 0x08
52const SMMU_CR0_QUEUES ulong 0x0c
53const SMMU_CR0_ALL ulong 0x0d
54const SMMU_GBPA_UPDATE ulong 0x80000000
55const SMMU_GBPA_ABORT ulong 0x00100000
56const SMMU_BASE_RA ulong 0x4000000000000000
57note CR1 (§6.3.10): QUEUE_IC/OC=0 (Normal-NC), QUEUE_SH[5:4]=2 (OSH), TABLE_IC/OC=0, TABLE_SH[11:10]=2.
58note Matches the Normal-NC pool the queues/tables live in (CPU MAIR attr3 = 0x44).
59const SMMU_CR1_NC_OSH ulong 0x820
60note CR2 (§6.3.12): E2H[0]=0, RECINVSID[1]=1 (record C_BAD_STREAMID), PTM[2]=1 (no broadcast TLBI).
61const SMMU_CR2_VAL ulong 0x6
62note GERROR.CMDQ_ERR is bit 0 (§6.3.14); active when GERROR[0] != GERRORN[0].
63const SMMU_GERR_CMDQ ulong 0x1
64note L1STD (§5.1.2): L2Ptr[51:6] | Span[4:0]; Span = SPLIT + 1 = 7 -> 2^(7-1) = 64 STEs per 4 KiB L2 table.
65const SMMU_STRTAB_SPLIT ulong 6
66const SMMU_L1_SPAN ulong 7
67const SMMU_L2PTR_MASK ulong 0x000fffffffffffc0
68note VMSAv8-64 output-address mask for 4 KiB granule table/page descriptors (Arm ARM D8.3).
69const SMMU_PT_OA_MASK ulong 0x0000fffffffff000
70const SMMU_BLK_2M ulong 0x200000
71const SMMU_Q_LOG2SIZE ulong 4
72const SMMU_Q_ENTRIES ulong 16
73note For LOG2SIZE=4, index bits [3:0] + wrap bit 4 -> mask = (1 << 5) - 1 = 0x1f (IHI 0070H.a §3.5.1)
74const SMMU_Q_IDX_MASK ulong 0x1f
75const SMMU_EVTQ_OVFLG ulong 0x80000000
76
77note SMMUv3 command opcodes
78const SMMU_CMD_CFGI_STE ulong 0x03
79const SMMU_CMD_CFGI_ALL ulong 0x04
80const SMMU_CMD_CFGI_CD_ALL ulong 0x06
81const SMMU_CMD_TLBI_NSNH_ALL ulong 0x30
82const SMMU_CMD_SYNC ulong 0x46
83
84note Stage-1 PTE leaf attributes:
85note Normal-NC (MAIR slot 3 = 0x44): AttrIdx=3<<2 (0xc) | AP[1]=1 (0x40) | SH=2 Outer-Shareable (0x200) | AF (0x400) | PXN|UXN
86const SMMU_PTE_NORMAL_NC ulong 0x006000000000064c
87note Device-nGnRnE (MAIR slot 2 = 0x00) for ITS MSI doorbell only: AttrIdx=2<<2 (0x8) | AP[1]=1 (0x40) | AF (0x400) | PXN|UXN
88const SMMU_PTE_DEV_ITS ulong 0x0060000000000448
89note Normal WB (MAIR slot 0 = 0xff): AttrIdx=0 | AP[1]=1 (0x40) | SH=3 Inner-Shareable (0x300) | AF (0x400) | PXN|UXN.
90note Only for streams whose IORT root-complex node reports CCA=1 (IO-coherent); see smmu_map_span_wb.
91const SMMU_PTE_NORMAL_WB ulong 0x0060000000000740
92const SMMU_ITS_DB_BASE ulong 0x08000000
93const SMMU_ITS_DB_END ulong 0x08100000
94const SMMU_CD_MAIR0 ulong 0x440004ff
95
96note Return codes
97const SMMU_OK long 0
98const SMMU_EARG long -1
99const SMMU_EIDR long -2
100const SMMU_ECR0 long -3
101const SMMU_EGBPA long -4
102const SMMU_ECMDERR long -5
103const SMMU_EABORTONLY long -6
104const SMMU_ERMRATTR long -7
105const SMMU_EOVERLAP long -8
106const SMMU_EFULL long -9
107const SMMU_ENOQUIESCE long -10
108const SMMU_ENOTFOUND long -11
109const SMMU_EWILDCARD long -12
110const SMMU_EATTR long -13
111note IORT-routed SMMU base does not match the base this instance was brought up at.
112const SMMU_EBASE long -14
113
114note Per-instance state (0=off, 1=up, 2=abort_only)
115global smmu_inst_state ulong 3
116global smmu_inst_h long 3
117global smmu_inst_base ulong 3
118global smmu_inst_sidbits ulong 3
119global smmu_inst_cmd_prod ulong 3
120global smmu_inst_evt_cnt ulong 3
121global smmu_inst_last_evt ulong 3
122
123note Host-model command queue buffer (3 instances * 16 cmds * 2 u64 words = 96 u64)
124global smmu_cmdq_buf ulong 96
125note Host-model event queue buffer (3 instances * 16 evts * 4 u64 words = 192 u64, 32 B/record per IHI 0070H.a §7.3)
126global smmu_evtq_buf ulong 192
127
128note Active stream table (up to 8 streams)
129global smmu_str_n ulong 1
130global smmu_str_live ulong 8
131global smmu_str_inst ulong 8
132global smmu_str_sid ulong 8
133global smmu_str_ttbr ulong 8
134note Encoded STE[0..7] and CD[0..7] for each stream (8 streams * 8 u64 = 64 u64)
135global smmu_ste_words ulong 64
136global smmu_cd_words ulong 64
137
138note Per-stream disjoint DMA span table (up to 32 active spans)
139global smmu_sp_live ulong 32
140global smmu_sp_stream ulong 32
141global smmu_sp_pa ulong 32
142global smmu_sp_len ulong 32
143global smmu_sp_pte ulong 32
144global smmu_sp_rmr ulong 32
145
146note Mode 1 per-instance memory: STRTAB PA, STRTAB LOG2SIZE, CMDQ PA, EVTQ PA, commands since last SYNC,
147note last event InputAddr.
148global smmu_inst_strtab ulong 3
149global smmu_inst_l2sz ulong 3
150global smmu_inst_cmdq ulong 3
151global smmu_inst_evtq ulong 3
152global smmu_inst_pend ulong 3
153global smmu_inst_last_addr ulong 3
154note Mode 1: sticky "a command was refused by smmu_cmd_push" flag; the next smmu_cmd_sync drops the batch
155note and fails closed.
156global smmu_inst_qerr ulong 3
157note Mode 1: last CMDQ PROD value actually written to SMMU_CMDQ_PROD (commands up to here are issued).
158global smmu_inst_hwprod ulong 3
159note Per-stream IORT CCA (1 = IO-coherent root complex)
160global smmu_str_cca ulong 8
161note Mode 1 page-table/L2-STE page pool: 0=base PA, 1=pages, 2=used
162global smmu_pt_pool ulong 3
163
164note Bring-up step trace (bitmask per instance for deterministic ordering verification)
165global smmu_trace ulong 3
166global smmu_cmd_count ulong 3
167note Host-model fault injector (mode 0 only): if nonzero, sets CMDQ_CONS.ERR[30:24] during CMD_SYNC
168global smmu_sim_cmd_err ulong 3
169
170fn smmu_reset to void
171put smmu_str_n 0 0
172for i ulong SMMU_MAX_INST
173put smmu_inst_state i 0
174put smmu_inst_h i 0
175put smmu_inst_base i 0
176put smmu_inst_sidbits i 0
177put smmu_inst_cmd_prod i 0
178put smmu_inst_evt_cnt i 0
179put smmu_inst_last_evt i 0
180put smmu_trace i 0
181put smmu_cmd_count i 0
182put smmu_sim_cmd_err i 0
183put smmu_inst_strtab i 0
184put smmu_inst_l2sz i 0
185put smmu_inst_cmdq i 0
186put smmu_inst_evtq i 0
187put smmu_inst_pend i 0
188put smmu_inst_last_addr i 0
189put smmu_inst_qerr i 0
190put smmu_inst_hwprod i 0
191end
192for p ulong 3
193put smmu_pt_pool p 0
194end
195for j ulong SMMU_MAX_STREAMS
196put smmu_str_live j 0
197put smmu_str_inst j 0
198put smmu_str_sid j 0
199put smmu_str_ttbr j 0
200put smmu_str_cca j 0
201end
202for k ulong SMMU_MAX_SPANS
203put smmu_sp_live k 0
204put smmu_sp_stream k 0
205put smmu_sp_pa k 0
206put smmu_sp_len k 0
207put smmu_sp_pte k 0
208put smmu_sp_rmr k 0
209end
210for m ulong 64
211put smmu_ste_words m 0
212put smmu_cd_words m 0
213end
214for q ulong 96
215put smmu_cmdq_buf q 0
216end
217for e ulong 192
218put smmu_evtq_buf e 0
219end
220end
221
222fn smmu_hw to bool
223ret eq get bmmio_mode 0 1
224end
225
226fn smmu_poll_eq h long reg ulong mask ulong want ulong to bool
227lim = SMMU_POLL_MAX
228if smmu_hw
229lim = SMMU_POLL_HW
230end
231i = ulong 0
232while lt i lim bound 4000000
233v = bmmio_r32 h reg
234if ne get bmmio_err 0 BMMIO_OK
235ret false
236end
237if eq and v mask want
238ret true
239end
240cpurelax
241i = add i 1
242end
243ret false
244end
245
246note ---- Mode 1 memory back end (Normal-NC, VA == PA; arm64_mmio_* = fwld/fwst bracketed by DSB SY) ----
247fn smmu_mw pa ulong v ulong to void
248arm64_mmio_w64 pa v
249end
250
251fn smmu_mr pa ulong to ulong
252ret arm64_mmio_r64 pa
253end
254
255fn smmu_zero pa ulong nbytes ulong to void
256o = ulong 0
257while lt o nbytes bound 131072
258smmu_mw add pa o 0
259o = add o 8
260end
261end
262
263note Register the page pool used for stage-1 tables and L2 stream tables (mode 1). 4 KiB aligned, < 40-bit.
264fn smmu_set_pt_pool pa ulong pages ulong to long
265if or eq pa 0 or ne and pa 4095 0 eq pages 0
266ret SMMU_EARG
267end
268if or gt pages 65536 gt add pa mul pages 4096 SMMU_PA_40_LIMIT
269ret SMMU_EARG
270end
271put smmu_pt_pool 0 pa
272put smmu_pt_pool 1 pages
273put smmu_pt_pool 2 0
274ret SMMU_OK
275end
276
277fn smmu_pt_alloc to ulong
278used = get smmu_pt_pool 2
279if or eq get smmu_pt_pool 0 0 ge used get smmu_pt_pool 1
280ret 0
281end
282pa = add get smmu_pt_pool 0 mul used 4096
283put smmu_pt_pool 2 add used 1
284smmu_zero pa 4096
285ret pa
286end
287
288note Walk a VMSAv8-64 4 KiB-granule, T0SZ=16 (4-level, start level 0) stage-1 table rooted at root
289note (Arm ARM D8.2: L0 index VA[47:39], L1 [38:30], L2 [29:21], L3 [20:12]) down to level lvl.
290note mk=true creates zeroed next-level tables (descriptor = table PA | 0b11, D8.3.1) from the pool;
291note mk=false (unmap) never allocates. Returns the PA of the level-lvl descriptor, or 0 when the pool is
292note exhausted, a block/invalid entry is in the way, or (mk=false) the table does not exist.
293fn smmu_pt_idx va ulong lvl ulong to ulong
294ret mul and shr va sub 39 mul lvl 9 511 8
295end
296
297fn smmu_pt_slot root ulong va ulong lvl ulong mk bool to ulong
298tbl = root
299l = ulong 0
300while lt l lvl bound 4
301dpa = add tbl smmu_pt_idx va l
302d = smmu_mr dpa
303if eq d 0
304if not mk
305ret 0
306end
307nt = smmu_pt_alloc
308if eq nt 0
309ret 0
310end
311d = or nt 3
312smmu_mw dpa d
313end
314if ne and d 3 3
315ret 0
316end
317tbl = and d SMMU_PT_OA_MASK
318l = add l 1
319end
320ret add tbl smmu_pt_idx va lvl
321end
322
323note Map: the descriptor must be empty (else SMMU_EOVERLAP, nothing written). Unmap (val 0): a missing
324note table means nothing is mapped there.
325fn smmu_pt_put root ulong va ulong lvl ulong val ulong to long
326mk = ne val 0
327s = smmu_pt_slot root va lvl mk
328if eq s 0
329if mk
330ret SMMU_EFULL
331end
332ret SMMU_OK
333end
334if mk
335if ne smmu_mr s 0
336ret SMMU_EOVERLAP
337end
338end
339smmu_mw s val
340ret SMMU_OK
341end
342
343note Bytes of the last smmu_pt_span_raw call that were processed successfully.
344global smmu_pt_done ulong 1
345
346note Map (pte != 0) or unmap (pte == 0) [pa, pa+sz) at VA == PA. 2 MiB-aligned 2 MiB chunks use L2 block
347note descriptors (bits[1:0]=0b01, OA[47:21]); everything else uses L3 page descriptors (bits[1:0]=0b11).
348note Unmap mirrors the identical block/page decision, so it clears exactly the descriptors map wrote.
349fn smmu_pt_span_raw root ulong pa ulong sz ulong pte ulong to long
350put smmu_pt_done 0 0
351off = ulong 0
352while lt off sz bound 1048576
353cur = add pa off
354lvl = ulong 3
355step = ulong 4096
356dt = ulong 3
357if eq and cur sub SMMU_BLK_2M 1 0
358if ge sub sz off SMMU_BLK_2M
359lvl = ulong 2
360step = SMMU_BLK_2M
361dt = ulong 1
362end
363end
364v = ulong 0
365if ne pte 0
366v = or or cur pte dt
367end
368r = smmu_pt_put root cur lvl v
369if ne r SMMU_OK
370ret r
371end
372off = add off step
373put smmu_pt_done 0 off
374end
375ret SMMU_OK
376end
377
378note A failed map rolls back only the descriptors it wrote ([pa, pa + done)), so an SMMU_EOVERLAP never
379note clears a descriptor that belongs to another span.
380fn smmu_pt_span root ulong pa ulong sz ulong pte ulong to long
381r = smmu_pt_span_raw root pa sz pte
382if and ne r SMMU_OK ne pte 0
383done = get smmu_pt_done 0
384if gt done 0
385z = smmu_pt_span_raw root pa done 0
386end
387end
388ret r
389end
390
391note Mode 1 in-flight limit: at most 15 of the 16 CMDQ entries between CMD_SYNCs (14 commands + the SYNC),
392note so PROD never laps CONS (§3.5: full = same index, wrap bits differ). An over-limit command is not
393note written; smmu_inst_qerr is set and the next smmu_cmd_sync fails closed.
394const SMMU_CMDQ_MAX_PEND ulong 15
395
396fn smmu_cmd_push inst ulong w0 ulong w1 ulong to void
397if smmu_hw
398lim = sub SMMU_CMDQ_MAX_PEND 1
399if eq and w0 0xff SMMU_CMD_SYNC
400lim = SMMU_CMDQ_MAX_PEND
401end
402if ge get smmu_inst_pend inst lim
403put smmu_inst_qerr inst 1
404ret
405end
406end
407prod = get smmu_inst_cmd_prod inst
408slot = and prod sub SMMU_Q_ENTRIES 1
409base_idx = add mul inst 32 mul slot 2
410put smmu_cmdq_buf base_idx w0
411put smmu_cmdq_buf add base_idx 1 w1
412if smmu_hw
413note §4.1: 16-byte command at CMDQ_BASE + (PROD.WR & (2^LOG2SIZE - 1)) * 16
414cpa = add get smmu_inst_cmdq inst mul slot 16
415smmu_mw cpa w0
416smmu_mw add cpa 8 w1
417put smmu_inst_pend inst add get smmu_inst_pend inst 1
418end
419put smmu_inst_cmd_prod inst and add prod 1 SMMU_Q_IDX_MASK
420put smmu_cmd_count inst add get smmu_cmd_count inst 1
421end
422
423note Mode 1: if smmu_cmd_push refused a command, drop the un-issued batch -- every command after the last
424note PROD actually written to the SMMU (smmu_inst_hwprod) -- and fail. smmu_inst_pend keeps counting the
425note issued-but-not-yet-confirmed commands (it only resets after a successful SYNC), so after a failed SYNC
426note the in-flight limit still covers entries the SMMU may not have consumed.
427fn smmu_q_ok inst ulong to bool
428if eq get smmu_inst_qerr inst 0
429ret true
430end
431hp = get smmu_inst_hwprod inst
432unissued = and sub add get smmu_inst_cmd_prod inst 32 hp SMMU_Q_IDX_MASK
433put smmu_inst_cmd_prod inst hp
434put smmu_inst_pend inst sub get smmu_inst_pend inst unissued
435put smmu_inst_qerr inst 0
436ret false
437end
438
439note Flush command queue with CMD_SYNC and enforce CMDQ_CONS.ERR[30:24] == 0.
440fn smmu_cmd_sync inst ulong to long
441h = get smmu_inst_h inst
442if smmu_hw
443if eq get smmu_inst_cmdq inst 0
444ret SMMU_ECMDERR
445end
446if not smmu_q_ok inst
447ret SMMU_ECMDERR
448end
449end
450note CMD_SYNC: op=0x46, CS=0 (no IRQ), MSH=3 (Inner Shareable, bits 23:22)
451smmu_cmd_push inst or SMMU_CMD_SYNC shl ulong 3 22 0
452if smmu_hw
453if not smmu_q_ok inst
454ret SMMU_ECMDERR
455end
456end
457cpufence
458prod = get smmu_inst_cmd_prod inst
459rc = bmmio_w32 h SMMU_REG_CMDQ_PROD prod
460if ne rc BMMIO_OK
461ret SMMU_ECMDERR
462end
463put smmu_inst_hwprod inst prod
464note In host model mode 0, model auto-advances CONS unless fault-injected in smmu_sim_cmd_err / bits 30:24.
465if eq get bmmio_mode 0 0
466inj = get smmu_sim_cmd_err inst
467if ne inj 0
468sr = bmmio_w32 h SMMU_REG_CMDQ_CONS or shl and inj 0x7f 24 prod
469else
470cur = bmmio_r32 h SMMU_REG_CMDQ_CONS
471err_bits = and shr cur 24 0x7f
472if eq err_bits 0
473sr = bmmio_w32 h SMMU_REG_CMDQ_CONS prod
474if ne sr BMMIO_OK
475ret SMMU_ECMDERR
476end
477end
478end
479end
480ok = smmu_poll_eq h SMMU_REG_CMDQ_CONS SMMU_Q_IDX_MASK prod
481if not ok
482ret SMMU_ECMDERR
483end
484cons = bmmio_r32 h SMMU_REG_CMDQ_CONS
485err = and shr cons 24 0x7f
486if ne err 0
487ret SMMU_ECMDERR
488end
489if smmu_hw
490put smmu_inst_pend inst 0
491ge0 = bmmio_r32 h SMMU_REG_GERROR
492ge1 = bmmio_r32 h SMMU_REG_GERRORN
493if ne and xor ge0 ge1 SMMU_GERR_CMDQ 0
494ret SMMU_ECMDERR
495end
496end
497ret SMMU_OK
498end
499
500note Bring up an SMMUv3 node into ABORT-only state (used for SMMUv3 C at 0x14900000).
501fn smmu_abort_only inst ulong base ulong to long
502if eq get pcie_quiesced 0 0
503ret SMMU_ENOQUIESCE
504end
505if or ge inst SMMU_MAX_INST eq base 0
506ret SMMU_EARG
507end
508h = get smmu_inst_h inst
509if le h 0
510h = bmmio_register base base SPARK_SMMU_SZ BMMIO_ATTR_NGNRNE
511if lt h 0
512ret SMMU_EARG
513end
514put smmu_inst_h inst h
515put smmu_inst_base inst base
516end
517rc = bmmio_w32 h SMMU_REG_CR0 0
518if ne rc BMMIO_OK
519ret SMMU_ECR0
520end
521if eq get bmmio_mode 0 0
522z = bmmio_w32 h SMMU_REG_CR0ACK 0
523end
524if not smmu_poll_eq h SMMU_REG_CR0ACK 0xffffffff 0
525ret SMMU_ECR0
526end
527gbpa = or SMMU_GBPA_UPDATE SMMU_GBPA_ABORT
528rc = bmmio_w32 h SMMU_REG_GBPA gbpa
529if ne rc BMMIO_OK
530ret SMMU_EGBPA
531end
532if eq get bmmio_mode 0 0
533z = bmmio_w32 h SMMU_REG_GBPA SMMU_GBPA_ABORT
534end
535if not smmu_poll_eq h SMMU_REG_GBPA SMMU_GBPA_UPDATE 0
536ret SMMU_EGBPA
537end
538put smmu_inst_state inst 2
539put smmu_trace inst or get smmu_trace inst 0x03
540ret SMMU_OK
541end
542
543note §6.3.24 STRTAB_BASE (ADDR[51:6], RA[62]); §6.3.25 STRTAB_BASE_CFG: FMT[17:16]=1 (2-level),
544note SPLIT[10:6]=6, LOG2SIZE[5:0]; §6.3.26/§6.3.29 CMDQ_BASE/EVENTQ_BASE: ADDR[51:5] | LOG2SIZE[4:0] | RA/WA[62].
545note CR1/CR2 are written while SMMUEN=0 (§6.3.10: CR1 is RO when CR0.SMMUEN or queues enabled).
546note Mode 1 zeroes the L1 stream table (all L1STD.Span=0 -> C_BAD_STREAMID), CMDQ and EVENTQ before enable.
547fn smmu_init_prog_queues inst ulong log2sz ulong strtab_pa ulong cmdq_pa ulong evtq_pa ulong to void
548h = get smmu_inst_h inst
549if smmu_hw
550smmu_zero strtab_pa mul shl ulong 1 sub log2sz SMMU_STRTAB_SPLIT 8
551smmu_zero cmdq_pa mul SMMU_Q_ENTRIES 16
552smmu_zero evtq_pa mul SMMU_Q_ENTRIES 32
553end
554put smmu_inst_strtab inst strtab_pa
555put smmu_inst_l2sz inst log2sz
556put smmu_inst_cmdq inst cmdq_pa
557put smmu_inst_evtq inst evtq_pa
558put smmu_inst_pend inst 0
559z = bmmio_w32 h SMMU_REG_CR1 SMMU_CR1_NC_OSH
560z = bmmio_w32 h SMMU_REG_CR2 SMMU_CR2_VAL
561cfg = or shl ulong 1 16 or shl SMMU_STRTAB_SPLIT 6 log2sz
562z = bmmio_w64 h SMMU_REG_STRTAB_BASE or strtab_pa SMMU_BASE_RA
563z = bmmio_w32 h SMMU_REG_STRTAB_CFG cfg
564z = bmmio_w64 h SMMU_REG_CMDQ_BASE or or cmdq_pa SMMU_BASE_RA SMMU_Q_LOG2SIZE
565z = bmmio_w32 h SMMU_REG_CMDQ_PROD 0
566z = bmmio_w32 h SMMU_REG_CMDQ_CONS 0
567z = bmmio_w64 h SMMU_REG_EVTQ_BASE or or evtq_pa SMMU_BASE_RA SMMU_Q_LOG2SIZE
568z = bmmio_w32 h SMMU_REG_EVTQ_PROD 0
569z = bmmio_w32 h SMMU_REG_EVTQ_CONS 0
570put smmu_inst_cmd_prod inst 0
571put smmu_inst_hwprod inst 0
572put smmu_inst_qerr inst 0
573cpufence
574end
575
576fn smmu_rollback_cr0 h long to void
577z = bmmio_w32 h SMMU_REG_CR0 0
578if eq get bmmio_mode 0 0
579z = bmmio_w32 h SMMU_REG_CR0ACK 0
580end
581ok = smmu_poll_eq h SMMU_REG_CR0ACK 0xffffffff 0
582end
583
584fn smmu_init_enable inst ulong log2sz ulong to long
585h = get smmu_inst_h inst
586rc = bmmio_w32 h SMMU_REG_CR0 SMMU_CR0_QUEUES
587if ne rc BMMIO_OK
588smmu_rollback_cr0 h
589ret SMMU_ECR0
590end
591if eq get bmmio_mode 0 0
592z = bmmio_w32 h SMMU_REG_CR0ACK SMMU_CR0_QUEUES
593end
594if not smmu_poll_eq h SMMU_REG_CR0ACK SMMU_CR0_QUEUES SMMU_CR0_QUEUES
595smmu_rollback_cr0 h
596ret SMMU_ECR0
597end
598put smmu_trace inst or get smmu_trace inst 0x10
599smmu_cmd_push inst SMMU_CMD_CFGI_ALL 31
600smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
601sr = smmu_cmd_sync inst
602if ne sr SMMU_OK
603smmu_rollback_cr0 h
604ret sr
605end
606put smmu_trace inst or get smmu_trace inst 0x20
607rc = bmmio_w32 h SMMU_REG_CR0 SMMU_CR0_ALL
608if ne rc BMMIO_OK
609smmu_rollback_cr0 h
610ret SMMU_ECR0
611end
612if eq get bmmio_mode 0 0
613z = bmmio_w32 h SMMU_REG_CR0ACK SMMU_CR0_ALL
614end
615if not smmu_poll_eq h SMMU_REG_CR0ACK SMMU_CR0_ALL SMMU_CR0_ALL
616smmu_rollback_cr0 h
617ret SMMU_ECR0
618end
619put smmu_inst_sidbits inst log2sz
620put smmu_inst_state inst 1
621put smmu_trace inst or get smmu_trace inst 0x40
622ret SMMU_OK
623end
624
625fn smmu_pa_valid pa ulong to bool
626if eq pa 0
627ret false
628end
629if ne and pa 4095 0
630ret false
631end
632ret lt pa SMMU_PA_40_LIMIT
633end
634
635note ARM IHI 0070H.a §6.3.24: L1 STRTAB_BASE alignment = max(64, (1 << (log2sz - 6)) * 8)
636note For log2sz=20 (SMMUv3 A): 131072 B (128 KiB).
637fn smmu_strtab_aligned pa ulong log2sz ulong to bool
638l1_sz = mul shl ulong 1 sub log2sz 6 8
639if lt l1_sz 64
640l1_sz = ulong 64
641end
642ret eq and pa sub l1_sz 1 0
643end
644
645fn smmu_idr_ok h long min_bits ulong to bool
646idr0 = bmmio_r32 h SMMU_REG_IDR0
647idr1 = bmmio_r32 h SMMU_REG_IDR1
648note Require IDR0.S1P (bit 1 == 1) and IDR0.ST_LEVEL == 1 (bits 28:27)
649if eq and idr0 2 0
650ret false
651end
652if ne and shr idr0 27 3 1
653ret false
654end
655note Require IDR1.SIDSIZE >= min_bits, IDR1.CMDQS[25:21] >= 4, IDR1.EVENTQS[20:16] >= 4
656if lt and idr1 0x3f min_bits
657ret false
658end
659if lt and shr idr1 21 0x1f SMMU_Q_LOG2SIZE
660ret false
661end
662ret ge and shr idr1 16 0x1f SMMU_Q_LOG2SIZE
663end
664
665note Deterministic SMMUv3 bring-up (instances A=0 and B=1; refuses C=2 which must stay abort-only).
666note Exact sequence: quiesce check -> IDR0.S1P==1 & ST_LEVEL==1 & IDR1.SIDSIZE>=min_bits & CMDQS/EVENTQS>=4 ->
667note CR0=0 -> CR0ACK==0 -> GBPA.ABORT|Update -> GBPA.Update==0 ->
668note 2-level aligned STRTAB/CMDQ/EVENTQ -> DSB SY -> CR0=CMDQEN|EVTQEN -> CR0ACK ->
669note CFGI_ALL + TLBI_NSNH_ALL + CMD_SYNC + CMDQ_CONS.ERR==0 -> CR0=SMMUEN|CMDQEN|EVTQEN -> CR0ACK.
670fn smmu_init_instance inst ulong base ulong min_bits ulong strtab_pa ulong cmdq_pa ulong evtq_pa ulong to long
671if eq get pcie_quiesced 0 0
672ret SMMU_ENOQUIESCE
673end
674if ge inst 2
675ret SMMU_EARG
676end
677if or eq base 0 lt min_bits 8
678ret SMMU_EARG
679end
680log2sz = min_bits
681if lt log2sz 12
682log2sz = ulong 12
683end
684if not smmu_pa_valid strtab_pa
685ret SMMU_EARG
686end
687if not smmu_strtab_aligned strtab_pa log2sz
688ret SMMU_EARG
689end
690if not smmu_pa_valid cmdq_pa
691ret SMMU_EARG
692end
693if not smmu_pa_valid evtq_pa
694ret SMMU_EARG
695end
696h = get smmu_inst_h inst
697if le h 0
698h = bmmio_register base base SPARK_SMMU_SZ BMMIO_ATTR_NGNRNE
699if lt h 0
700ret SMMU_EARG
701end
702put smmu_inst_h inst h
703put smmu_inst_base inst base
704end
705if not smmu_idr_ok h min_bits
706ret SMMU_EIDR
707end
708put smmu_trace inst 0x01
709rc = bmmio_w32 h SMMU_REG_CR0 0
710if ne rc BMMIO_OK
711ret SMMU_ECR0
712end
713if eq get bmmio_mode 0 0
714z = bmmio_w32 h SMMU_REG_CR0ACK 0
715end
716if not smmu_poll_eq h SMMU_REG_CR0ACK 0xffffffff 0
717ret SMMU_ECR0
718end
719put smmu_trace inst or get smmu_trace inst 0x02
720rc = bmmio_w32 h SMMU_REG_GBPA or SMMU_GBPA_UPDATE SMMU_GBPA_ABORT
721if ne rc BMMIO_OK
722ret SMMU_EGBPA
723end
724if eq get bmmio_mode 0 0
725z = bmmio_w32 h SMMU_REG_GBPA SMMU_GBPA_ABORT
726end
727if not smmu_poll_eq h SMMU_REG_GBPA SMMU_GBPA_UPDATE 0
728ret SMMU_EGBPA
729end
730put smmu_trace inst or get smmu_trace inst 0x04
731smmu_init_prog_queues inst log2sz strtab_pa cmdq_pa evtq_pa
732put smmu_trace inst or get smmu_trace inst 0x08
733ret smmu_init_enable inst log2sz
734end
735
736note Internal helper: add one disjoint span to stream slot (0..7) with explicit PTE attrs.
737fn smmu_add_span_raw slot ulong pa ulong sz ulong pte_attr ulong is_rmr ulong to long
738if or eq pa 0 eq sz 0
739ret SMMU_EARG
740end
741if or ne and pa 4095 0 ne and sz 4095 0
742ret SMMU_EARG
743end
744if gt sz SMMU_MAX_SPAN_BYTES
745ret SMMU_EWILDCARD
746end
747endpa = add pa sz
748if or lt endpa pa gt endpa SMMU_PA_40_LIMIT
749ret SMMU_EWILDCARD
750end
751note Enforce strict disjointness across ALL active spans of ALL streams
752free_idx = ulong SMMU_MAX_SPANS
753for i ulong SMMU_MAX_SPANS
754if ne get smmu_sp_live i 0
755if bmmio_ranges_overlap pa sz get smmu_sp_pa i get smmu_sp_len i
756ret SMMU_EOVERLAP
757end
758else
759if eq free_idx SMMU_MAX_SPANS
760free_idx = i
761end
762end
763end
764if eq free_idx SMMU_MAX_SPANS
765ret SMMU_EFULL
766end
767if smmu_hw
768pr = smmu_pt_span get smmu_str_ttbr slot pa sz pte_attr
769if ne pr SMMU_OK
770ret pr
771end
772end
773put smmu_sp_live free_idx 1
774put smmu_sp_stream free_idx slot
775put smmu_sp_pa free_idx pa
776put smmu_sp_len free_idx sz
777put smmu_sp_pte free_idx pte_attr
778put smmu_sp_rmr free_idx is_rmr
779ret SMMU_OK
780end
781
782note Encode 64-byte CD (IHI 0070H.a §5.4, smmu.c:142) and STE (IHI 0070H.a §5.2, smmu.c:130):
783note CD word 0: T0SZ=16 (4-level 48-bit matching pagetable.om) | TG0=0 (4K) | IRGN0=0 | ORGN0=0 | SH0=0 (NC) |
784note EPD1=1<<30 | V=1<<31 | IPS=2<<32 (40-bit) | ENDI=0 (little-endian!) | AA64=1<<41 | R=1<<45 | A=1<<46
785note = 0x00006202c0000010.
786note STE word 0: V=1 | Config=5<<1 (Stage-1 translate) | S1ContextPtr=(cd_pa & ~0x3f).
787note STE words 1..7: 0 (S1CIR=0, S1COR=0, S1CSH=0 -> Normal-NC CD, no WB attribute mismatch).
788fn smmu_encode_ste_cd slot ulong cd_pa ulong ttbr0_pa ulong to void
789base = mul slot 8
790put smmu_cd_words base 0x00006202c0000010
791put smmu_cd_words add base 1 ttbr0_pa
792put smmu_cd_words add base 2 0
793put smmu_cd_words add base 3 SMMU_CD_MAIR0
794ste0 = or ulong 11 and cd_pa 0x000fffffffffffc0
795put smmu_ste_words base ste0
796for w ulong 7
797put smmu_ste_words add base add w 1 0
798end
799end
800
801fn smmu_map_one_rmr slot ulong r ulong to long
802fl = ulong get iort_rmr_flags r
803mattr = and shr fl 2 0xff
804if ne mattr 4
805if ne mattr 5
806if ne fl 0x10
807ret SMMU_ERMRATTR
808end
809end
810end
811nrng = ulong get iort_rmr_nrng r
812for k ulong nrng bound 4
813ridx = add mul r IORT_MAX_RMR_RANGES k
814ar = smmu_add_span_raw slot get iort_rmr_rbase ridx get iort_rmr_rlen ridx SMMU_PTE_NORMAL_NC 1
815if ne ar SMMU_OK
816ret ar
817end
818end
819ret SMMU_OK
820end
821
822fn smmu_map_rmrs slot ulong inst ulong sid ulong to long
823nrmr = get iort_meta 3
824for r ulong nrmr bound 4
825if eq ulong get iort_rmr_sidx r inst
826if ge sid ulong get iort_rmr_slo r
827if le sid ulong get iort_rmr_shi r
828rc = smmu_map_one_rmr slot r
829if ne rc SMMU_OK
830ret rc
831end
832end
833end
834end
835end
836ret SMMU_OK
837end
838
839note Mode 1: write the 64-byte CD (§5.4) at cd_pa and the 64-byte STE (§5.2) into the 2-level stream
840note table. L1 index = SID >> SPLIT, L2 index = SID & (2^SPLIT - 1) (§3.3.3). A missing L2 table is
841note allocated (zeroed: every other STE stays V=0) and published with L1STD = L2Ptr | Span (§5.1.2).
842note Words 1..7 are written before word 0 (CD.V / STE.V), each fwst bracketed by DSB SY.
843fn smmu_hw_put_cd dst ulong base ulong to void
844for w ulong 7
845smmu_mw add dst mul add w 1 8 get smmu_cd_words add base add w 1
846end
847smmu_mw dst get smmu_cd_words base
848end
849
850fn smmu_hw_put_ste dst ulong base ulong to void
851for w ulong 7
852smmu_mw add dst mul add w 1 8 get smmu_ste_words add base add w 1
853end
854smmu_mw dst get smmu_ste_words base
855end
856
857fn smmu_hw_l2_for inst ulong sid ulong to ulong
858l1 = add get smmu_inst_strtab inst mul shr sid SMMU_STRTAB_SPLIT 8
859d = smmu_mr l1
860if eq d 0
861l2 = smmu_pt_alloc
862if eq l2 0
863ret 0
864end
865d = or l2 SMMU_L1_SPAN
866smmu_mw l1 d
867end
868ret and d SMMU_L2PTR_MASK
869end
870
871fn smmu_hw_write_ste inst ulong sid ulong n ulong cd_pa ulong to long
872base = mul n 8
873smmu_hw_put_cd cd_pa base
874l2 = smmu_hw_l2_for inst sid
875if eq l2 0
876ret SMMU_EFULL
877end
878ste = add l2 mul and sid sub shl ulong 1 SMMU_STRTAB_SPLIT 1 64
879smmu_hw_put_ste ste base
880ret SMMU_OK
881end
882
883note Mode 1: invalidate the STE for sid (word 0 / V first), then CFGI_STE + TLBI + SYNC.
884note Returns false only if the invalidation could not be confirmed by CMD_SYNC.
885fn smmu_hw_clear_ste inst ulong sid ulong to bool
886l1 = add get smmu_inst_strtab inst mul shr sid SMMU_STRTAB_SPLIT 8
887d = smmu_mr l1
888if eq d 0
889ret true
890end
891ste = add and d SMMU_L2PTR_MASK mul and sid sub shl ulong 1 SMMU_STRTAB_SPLIT 1 64
892for w ulong 8
893smmu_mw add ste mul w 8 0
894end
895cpufence
896smmu_cmd_push inst or SMMU_CMD_CFGI_STE shl sid 32 0
897smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
898ret eq smmu_cmd_sync inst SMMU_OK
899end
900
901note Error-path teardown for a partially opened stream slot n. Mode 1 first invalidates the STE (V=0,
902note CFGI_STE + TLBI + SYNC) and only then clears the CD and the slot's stage-1 descriptors; if that
903note invalidation cannot be confirmed the SMMU may still hold a cached valid STE, so the whole instance
904note is taken down (smmu_inst_state 0: every later open/bring-up on it is refused -> fail closed).
905note Then every span of the slot (RMR spans included) is dropped and the slot's CD/STE words are zeroed.
906note Page-table pool pages are not returned (bump allocator).
907fn smmu_abandon_stream n ulong inst ulong sid ulong cd_pa ulong to void
908if smmu_hw
909if not smmu_hw_clear_ste inst sid
910put smmu_inst_state inst 0
911end
912smmu_zero cd_pa 64
913end
914for k ulong SMMU_MAX_SPANS
915if ne get smmu_sp_live k 0
916if eq get smmu_sp_stream k n
917if smmu_hw
918z = smmu_pt_span get smmu_str_ttbr n get smmu_sp_pa k get smmu_sp_len k 0
919end
920put smmu_sp_live k 0
921put smmu_sp_rmr k 0
922end
923end
924end
925base = mul n 8
926for w ulong 8
927put smmu_ste_words add base w 0
928put smmu_cd_words add base w 0
929end
930put smmu_str_live n 0
931put smmu_str_ttbr n 0
932put smmu_str_cca n 0
933end
934
935note Open a confined stream for PCI (seg, rid) via iort.om, auto-mapping any covering IORT RMRs.
936note Returns stream handle (1..8) on success, or negative SMMU_E* error.
937fn smmu_open_stream seg uint rid uint cd_pa ulong ttbr0_pa ulong to long
938if or eq cd_pa 0 eq ttbr0_pa 0
939ret SMMU_EARG
940end
941if or ne and cd_pa 63 0 ne and ttbr0_pa 4095 0
942ret SMMU_EARG
943end
944if or ge cd_pa SMMU_PA_40_LIMIT ge ttbr0_pa SMMU_PA_40_LIMIT
945ret SMMU_EARG
946end
947if ne iort_route_rid seg rid 0
948ret SMMU_ENOTFOUND
949end
950inst = get iort_qres 0
951sid = get iort_qres 3
952if ge inst SMMU_MAX_INST
953ret SMMU_ENOTFOUND
954end
955if eq get smmu_inst_state inst 2
956ret SMMU_EABORTONLY
957end
958if ne get smmu_inst_state inst 1
959ret SMMU_EARG
960end
961note The IORT SMMUv3 node that owns this RID must be the one this instance was brought up at.
962if ne get iort_qres 1 get smmu_inst_base inst
963ret SMMU_EBASE
964end
965if ge sid shl ulong 1 get smmu_inst_sidbits inst
966ret SMMU_EIDR
967end
968n = get smmu_str_n 0
969if ge n SMMU_MAX_STREAMS
970ret SMMU_EFULL
971end
972for i ulong n bound 8
973if ne get smmu_str_live i 0
974if eq get smmu_str_inst i inst
975if eq get smmu_str_sid i sid
976ret SMMU_EOVERLAP
977end
978end
979end
980end
981put smmu_str_ttbr n ttbr0_pa
982put smmu_str_cca n get iort_qres 8
983if smmu_hw
984smmu_zero ttbr0_pa 4096
985smmu_zero cd_pa 64
986end
987rr = smmu_map_rmrs n inst sid
988if ne rr SMMU_OK
989smmu_abandon_stream n inst sid cd_pa
990ret rr
991end
992smmu_encode_ste_cd n cd_pa ttbr0_pa
993if smmu_hw
994wr = smmu_hw_write_ste inst sid n cd_pa
995if ne wr SMMU_OK
996smmu_abandon_stream n inst sid cd_pa
997ret wr
998end
999end
1000put smmu_str_live n 1
1001put smmu_str_inst n inst
1002put smmu_str_sid n sid
1003put smmu_str_n 0 add n 1
1004cpufence
1005note CMD_CFGI_STE (§4.3.1): op 0x03, StreamID[63:32]; word1 Leaf[0] (mode 1 uses Leaf=0 so the
1006note L1STD is also re-fetched). CMD_CFGI_CD_ALL (§4.3.4): op 0x06, StreamID[63:32].
1007lf = ulong 1
1008if smmu_hw
1009lf = ulong 0
1010end
1011smmu_cmd_push inst or SMMU_CMD_CFGI_STE shl sid 32 lf
1012smmu_cmd_push inst or SMMU_CMD_CFGI_CD_ALL shl sid 32 0
1013smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
1014sr = smmu_cmd_sync inst
1015if ne sr SMMU_OK
1016put smmu_str_n 0 n
1017smmu_abandon_stream n inst sid cd_pa
1018ret sr
1019end
1020ret long add n 1
1021end
1022
1023note Map one disjoint DMA span for an open stream handle (1..8).
1024note is_its_db == 0: Normal-NC | AP[1]=1 | SH=2 Outer-Shareable | PXN | UXN;
1025note is_its_db == 1: ITS doorbell only (0x08000000..0x08100000, SID 0x100).
1026fn smmu_map_span sh long pa ulong sz ulong is_its_db ulong to long
1027if or le sh 0 gt ulong sh get smmu_str_n 0
1028ret SMMU_EARG
1029end
1030slot = sub ulong sh 1
1031if eq get smmu_str_live slot 0
1032ret SMMU_EARG
1033end
1034inst = get smmu_str_inst slot
1035pte = SMMU_PTE_NORMAL_NC
1036if ne is_its_db 0
1037if ne inst 1
1038ret SMMU_EARG
1039end
1040if ne get smmu_str_sid slot 0x100
1041ret SMMU_EARG
1042end
1043if lt add pa sz pa
1044ret SMMU_EARG
1045end
1046if or lt pa SMMU_ITS_DB_BASE gt add pa sz SMMU_ITS_DB_END
1047ret SMMU_EARG
1048end
1049pte = SMMU_PTE_DEV_ITS
1050end
1051ar = smmu_add_span_raw slot pa sz pte 0
1052if ne ar SMMU_OK
1053ret ar
1054end
1055cpufence
1056smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
1057ret smmu_cmd_sync inst
1058end
1059
1060note Map one disjoint Normal-WB Inner-Shareable span (for CPU-WB memory, e.g. frames_alloc RAM).
1061note Refused (SMMU_EATTR) unless the stream's IORT root complex reports CCA=1 (IORT spec, Memory Access
1062note Properties): a WB device mapping on a non-coherent path, or an NC mapping of CPU-WB memory, would be
1063note a mismatched-attribute alias (Arm ARM B2.16) and lose coherency. Fail closed instead.
1064fn smmu_map_span_wb sh long pa ulong sz ulong to long
1065if or le sh 0 gt ulong sh get smmu_str_n 0
1066ret SMMU_EARG
1067end
1068slot = sub ulong sh 1
1069if eq get smmu_str_live slot 0
1070ret SMMU_EARG
1071end
1072if ne get smmu_str_cca slot 1
1073ret SMMU_EATTR
1074end
1075ar = smmu_add_span_raw slot pa sz SMMU_PTE_NORMAL_WB 0
1076if ne ar SMMU_OK
1077ret ar
1078end
1079inst = get smmu_str_inst slot
1080cpufence
1081smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
1082ret smmu_cmd_sync inst
1083end
1084
1085note Unmap one non-RMR DMA span from an open stream (e.g. NVMe unmaps loaded weights before GB20B maps them).
1086fn smmu_unmap_span sh long pa ulong sz ulong to long
1087if or le sh 0 gt ulong sh get smmu_str_n 0
1088ret SMMU_EARG
1089end
1090slot = sub ulong sh 1
1091if eq get smmu_str_live slot 0
1092ret SMMU_EARG
1093end
1094inst = get smmu_str_inst slot
1095for i ulong SMMU_MAX_SPANS
1096if ne get smmu_sp_live i 0
1097if eq get smmu_sp_stream i slot
1098if and eq get smmu_sp_pa i pa eq get smmu_sp_len i sz
1099if ne get smmu_sp_rmr i 0
1100ret SMMU_EARG
1101end
1102if smmu_hw
1103ur = smmu_pt_span get smmu_str_ttbr slot pa sz 0
1104if ne ur SMMU_OK
1105ret ur
1106end
1107end
1108put smmu_sp_live i 0
1109cpufence
1110smmu_cmd_push inst SMMU_CMD_TLBI_NSNH_ALL 0
1111ret smmu_cmd_sync inst
1112end
1113end
1114end
1115end
1116ret SMMU_ENOTFOUND
1117end
1118
1119note §7.3: 32-byte record at EVENTQ_BASE + slot*32; word0 = type[7:0] | StreamID[63:32];
1120note word2 (+16) = InputAddr for F_TRANSLATION (0x10, §7.3.12).
1121fn smmu_hw_evt inst ulong slot ulong to ulong
1122epa = add get smmu_inst_evtq inst mul slot 32
1123put smmu_inst_last_addr inst smmu_mr add epa 16
1124ret smmu_mr epa
1125end
1126
1127note Drain SMMUv3 EVENTQ faults (Page 1 0x100a8/0x100ac, 32 B/record, wrap mask 0x1f, OVFLG/OVACKFLG bit 31).
1128fn smmu_drain_events inst ulong to long
1129if ge inst SMMU_MAX_INST
1130ret SMMU_EARG
1131end
1132h = get smmu_inst_h inst
1133if le h 0
1134ret SMMU_EARG
1135end
1136prod_raw = bmmio_r32 h SMMU_REG_EVTQ_PROD
1137cons_raw = bmmio_r32 h SMMU_REG_EVTQ_CONS
1138ovflg = and prod_raw SMMU_EVTQ_OVFLG
1139prod = and prod_raw SMMU_Q_IDX_MASK
1140cons = and cons_raw SMMU_Q_IDX_MASK
1141cnt = ulong 0
1142while and ne cons prod lt cnt SMMU_Q_ENTRIES bound 16
1143slot = and cons sub SMMU_Q_ENTRIES 1
1144eidx = add mul inst 64 mul slot 4
1145w0 = get smmu_evtq_buf eidx
1146if smmu_hw
1147w0 = smmu_hw_evt inst slot
1148end
1149put smmu_inst_last_evt inst w0
1150cons = and add cons 1 SMMU_Q_IDX_MASK
1151cnt = add cnt 1
1152end
1153if or gt cnt 0 ne ovflg and cons_raw SMMU_EVTQ_OVFLG
1154z = bmmio_w32 h SMMU_REG_EVTQ_CONS or cons ovflg
1155put smmu_inst_evt_cnt inst add get smmu_inst_evt_cnt inst cnt
1156end
1157ret long cnt
1158end