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