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/bar0_mmio.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 / bar0_mmio.om` 380 lines ยท 8823 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/kernel/hal/bar0_mmio.om: Pure-Omega exclusive Device-nGnRnE MMIO registry 3 note and ordered 32/64-bit register accessors for bare-metal DGX Spark (GB20B/NVMe/RTL8127/ECAM/SMMU). 4 note Dual-compatible OM-1/OM-2 (decimal literals only, no hex). Library unit: no fn main. 5 note Mode 0 = host/QEMU sparse register model (default); Mode 1 = bare-metal Device-nGnRnE MMIO seam. 6 note Hardware status: HOST_MODEL_ONLY (Spark silicon NOT_RUN). 7 8 const BMMIO_MAX_REGIONS ulong 16 9 const BMMIO_SIM_SLOTS ulong 512 10 const BMMIO_ATTR_NGNRNE ulong 0 11 12 note Verified Spark physical bases and sizes (decimal; hex in comments): 13 note GB20B BAR0: 0x24000000 (`603979776`), 64 MiB (`67108864`) 14 note NVMe BAR0: 0x62100000 (`1645215744`), 16 KiB (16384) 15 note RTL8127 B2: 0x6e900000 (`1854930944`), 256 KiB (262144) 16 note ECAM seg 4: 0xF340000000 (`1044750794752`), 2 MiB (`2097152`) 17 note ECAM seg 7: 0xF370000000 (`1045556101120`), 2 MiB (`2097152`) 18 note ECAM seg15: 0x29000000 (`687865856`), 2 MiB (`2097152`) 19 note SMMUv3 A: 0x13800000 (`327155712`), 128 KiB (131072) 20 note SMMUv3 B: 0x13000000 (`318767104`), 128 KiB (131072) 21 note SMMUv3 C: 0x14900000 (`344981504`), 128 KiB (131072) 22 const SPARK_GB20B_BAR0_PA ulong `603979776` 23 const SPARK_GB20B_BAR0_SZ ulong `67108864` 24 const SPARK_NVME_BAR0_PA ulong `1645215744` 25 const SPARK_NVME_BAR0_SZ ulong 16384 26 const SPARK_RTL8127_BAR2_PA ulong `1854930944` 27 const SPARK_RTL8127_BAR2_SZ ulong 262144 28 const SPARK_ECAM_SEG4_PA ulong `1044750794752` 29 const SPARK_ECAM_SEG7_PA ulong `1045556101120` 30 const SPARK_ECAM_SEG15_PA ulong `687865856` 31 const SPARK_ECAM_WIN_SZ ulong `2097152` 32 const SPARK_SMMU_A_PA ulong `327155712` 33 const SPARK_SMMU_B_PA ulong `318767104` 34 const SPARK_SMMU_C_PA ulong `344981504` 35 const SPARK_SMMU_SZ ulong 131072 36 37 note GB20B identity registers: BOOT_0=0x0000 (0x1bb000a1=`464519329`), BOOT_42=0x0a00 (0x1bba1000=`465178624`) 38 const GB20B_OFF_BOOT0 ulong 0 39 const GB20B_OFF_BOOT42 ulong 2560 40 const GB20B_VAL_BOOT0 ulong `464519329` 41 const GB20B_VAL_BOOT42 ulong `465178624` 42 43 const BMMIO_OK long 0 44 const BMMIO_EARG long -1 45 const BMMIO_EATTR long -2 46 const BMMIO_EOVERLAP long -3 47 const BMMIO_EFULL long -4 48 const BMMIO_EBOUNDS long -5 49 const BMMIO_EALIGN long -6 50 const BMMIO_ELOCKED long -7 51 const BMMIO_EMODE long -8 52 53 note Global state: 54 note bmmio_mode: 0 = host model, 1 = bare-metal (requires Piece 1 Device-nGnRnE mapping) 55 global bmmio_mode ulong 1 56 global bmmio_count ulong 1 57 global bmmio_err long 1 58 global bmmio_rd_ops ulong 1 59 global bmmio_wr_ops ulong 1 60 61 note Region table (up to 16 exclusive regions) 62 global bmmio_pa ulong 16 63 global bmmio_va ulong 16 64 global bmmio_sz ulong 16 65 global bmmio_attr ulong 16 66 global bmmio_live ulong 16 67 global bmmio_locked ulong 16 68 69 note Host-model sparse register store (key = (region_id << 40) | offset, value = 64-bit word) 70 global bmmio_sim_n ulong 1 71 global bmmio_sim_k ulong 512 72 global bmmio_sim_v ulong 512 73 74 note Last access trace for audit/testing (0=region, 1=off, 2=width, 3=is_write, 4=val) 75 global bmmio_last ulong 5 76 77 fn bmmio_reset to void 78 put bmmio_mode 0 0 79 put bmmio_count 0 0 80 put bmmio_err 0 0 81 put bmmio_rd_ops 0 0 82 put bmmio_wr_ops 0 0 83 put bmmio_sim_n 0 0 84 for i ulong BMMIO_MAX_REGIONS 85 put bmmio_pa i 0 86 put bmmio_va i 0 87 put bmmio_sz i 0 88 put bmmio_attr i 0 89 put bmmio_live i 0 90 put bmmio_locked i 0 91 end 92 for j ulong 5 93 put bmmio_last j 0 94 end 95 end 96 97 fn bmmio_ranges_overlap a ulong asz ulong b ulong bsz ulong to bool 98 if or eq asz 0 eq bsz 0 99 ret false 100 end 101 aend = add a asz 102 bend = add b bsz 103 if or lt aend a lt bend b 104 ret true 105 end 106 if le aend b 107 ret false 108 end 109 if le bend a 110 ret false 111 end 112 ret true 113 end 114 115 note Register an exclusive Device-nGnRnE (attr==0) XN MMIO region. 116 note Returns region handle (1..16) on success, or negative BMMIO_E* error. 117 fn bmmio_register pa ulong va ulong sz ulong attr ulong to long 118 if or eq pa 0 or eq va 0 eq sz 0 119 put bmmio_err 0 BMMIO_EARG 120 ret BMMIO_EARG 121 end 122 if or ne and pa 4095 0 or ne and va 4095 0 ne and sz 4095 0 123 put bmmio_err 0 BMMIO_EALIGN 124 ret BMMIO_EALIGN 125 end 126 if ne attr BMMIO_ATTR_NGNRNE 127 put bmmio_err 0 BMMIO_EATTR 128 ret BMMIO_EATTR 129 end 130 if or lt add pa sz pa lt add va sz va 131 put bmmio_err 0 BMMIO_EARG 132 ret BMMIO_EARG 133 end 134 n = get bmmio_count 0 135 if ge n BMMIO_MAX_REGIONS 136 put bmmio_err 0 BMMIO_EFULL 137 ret BMMIO_EFULL 138 end 139 for i ulong n bound 16 140 if ne get bmmio_live i 0 141 if bmmio_ranges_overlap pa sz get bmmio_pa i get bmmio_sz i 142 put bmmio_err 0 BMMIO_EOVERLAP 143 ret BMMIO_EOVERLAP 144 end 145 end 146 end 147 put bmmio_pa n pa 148 put bmmio_va n va 149 put bmmio_sz n sz 150 put bmmio_attr n attr 151 put bmmio_live n 1 152 put bmmio_locked n 0 153 put bmmio_count 0 add n 1 154 put bmmio_err 0 BMMIO_OK 155 ret long add n 1 156 end 157 158 fn bmmio_lock h long to long 159 if or le h 0 gt ulong h get bmmio_count 0 160 ret BMMIO_EARG 161 end 162 idx = sub ulong h 1 163 put bmmio_locked idx 1 164 ret BMMIO_OK 165 end 166 167 fn bmmio_is_live h long to bool 168 if or le h 0 gt ulong h get bmmio_count 0 169 ret false 170 end 171 idx = sub ulong h 1 172 if eq get bmmio_live idx 0 173 ret false 174 end 175 ret eq get bmmio_locked idx 0 176 end 177 178 fn bmmio_check h long off ulong width ulong to long 179 if or le h 0 gt ulong h get bmmio_count 0 180 ret BMMIO_EARG 181 end 182 idx = sub ulong h 1 183 if eq get bmmio_live idx 0 184 ret BMMIO_EARG 185 end 186 if ne get bmmio_locked idx 0 187 ret BMMIO_ELOCKED 188 end 189 if and ne width 4 ne width 8 190 ret BMMIO_EALIGN 191 end 192 if ne rem off width 0 193 ret BMMIO_EALIGN 194 end 195 endoff = add off width 196 if or lt endoff off gt endoff get bmmio_sz idx 197 ret BMMIO_EBOUNDS 198 end 199 ret BMMIO_OK 200 end 201 202 fn bmmio_sim_key h long off ulong to ulong 203 ret or shl ulong h 40 and off `1099511627775` 204 end 205 206 fn bmmio_sim_get h long off ulong to ulong 207 k = bmmio_sim_key h off 208 n = get bmmio_sim_n 0 209 for i ulong n bound 512 210 if eq get bmmio_sim_k i k 211 ret get bmmio_sim_v i 212 end 213 end 214 ret ulong 0 215 end 216 217 fn bmmio_sim_set h long off ulong v ulong to long 218 k = bmmio_sim_key h off 219 n = get bmmio_sim_n 0 220 for i ulong n bound 512 221 if eq get bmmio_sim_k i k 222 put bmmio_sim_v i v 223 ret BMMIO_OK 224 end 225 end 226 if ge n BMMIO_SIM_SLOTS 227 ret BMMIO_EFULL 228 end 229 put bmmio_sim_k n k 230 put bmmio_sim_v n v 231 put bmmio_sim_n 0 add n 1 232 ret BMMIO_OK 233 end 234 235 note Ordered 32-bit MMIO read (DSB SY before and after). Returns value; sets bmmio_err. 236 fn bmmio_r32 h long off ulong to ulong 237 rc = bmmio_check h off 4 238 if ne rc BMMIO_OK 239 put bmmio_err 0 rc 240 ret ulong 0 241 end 242 m = get bmmio_mode 0 243 if gt m 1 244 put bmmio_err 0 BMMIO_EMODE 245 ret ulong 0 246 end 247 v = ulong 0 248 if eq m 0 249 cpufence 250 v = and bmmio_sim_get h off `4294967295` 251 cpufence 252 else 253 addr = add get bmmio_va sub ulong h 1 off 254 v = and arm64_mmio_r32 addr `4294967295` 255 end 256 put bmmio_rd_ops 0 add get bmmio_rd_ops 0 1 257 put bmmio_last 0 ulong h 258 put bmmio_last 1 off 259 put bmmio_last 2 4 260 put bmmio_last 3 0 261 put bmmio_last 4 v 262 put bmmio_err 0 BMMIO_OK 263 ret v 264 end 265 266 note Ordered 32-bit MMIO write (DSB SY before and after). Returns BMMIO_OK or negative error. 267 fn bmmio_w32 h long off ulong val ulong to long 268 rc = bmmio_check h off 4 269 if ne rc BMMIO_OK 270 put bmmio_err 0 rc 271 ret rc 272 end 273 m = get bmmio_mode 0 274 if gt m 1 275 put bmmio_err 0 BMMIO_EMODE 276 ret BMMIO_EMODE 277 end 278 v = and val `4294967295` 279 if eq m 0 280 cpufence 281 sr = bmmio_sim_set h off v 282 cpufence 283 if ne sr BMMIO_OK 284 put bmmio_err 0 sr 285 ret sr 286 end 287 else 288 addr = add get bmmio_va sub ulong h 1 off 289 arm64_mmio_w32 addr v 290 end 291 put bmmio_wr_ops 0 add get bmmio_wr_ops 0 1 292 put bmmio_last 0 ulong h 293 put bmmio_last 1 off 294 put bmmio_last 2 4 295 put bmmio_last 3 1 296 put bmmio_last 4 v 297 put bmmio_err 0 BMMIO_OK 298 ret BMMIO_OK 299 end 300 301 note Ordered 64-bit MMIO read (DSB SY before and after). 302 fn bmmio_r64 h long off ulong to ulong 303 rc = bmmio_check h off 8 304 if ne rc BMMIO_OK 305 put bmmio_err 0 rc 306 ret ulong 0 307 end 308 m = get bmmio_mode 0 309 if gt m 1 310 put bmmio_err 0 BMMIO_EMODE 311 ret ulong 0 312 end 313 v = ulong 0 314 if eq m 0 315 cpufence 316 v = bmmio_sim_get h off 317 cpufence 318 else 319 addr = add get bmmio_va sub ulong h 1 off 320 v = arm64_mmio_r64 addr 321 end 322 put bmmio_rd_ops 0 add get bmmio_rd_ops 0 1 323 put bmmio_last 0 ulong h 324 put bmmio_last 1 off 325 put bmmio_last 2 8 326 put bmmio_last 3 0 327 put bmmio_last 4 v 328 put bmmio_err 0 BMMIO_OK 329 ret v 330 end 331 332 note Ordered 64-bit MMIO write (DSB SY before and after). 333 fn bmmio_w64 h long off ulong val ulong to long 334 rc = bmmio_check h off 8 335 if ne rc BMMIO_OK 336 put bmmio_err 0 rc 337 ret rc 338 end 339 m = get bmmio_mode 0 340 if gt m 1 341 put bmmio_err 0 BMMIO_EMODE 342 ret BMMIO_EMODE 343 end 344 if eq m 0 345 cpufence 346 sr = bmmio_sim_set h off val 347 cpufence 348 if ne sr BMMIO_OK 349 put bmmio_err 0 sr 350 ret sr 351 end 352 else 353 addr = add get bmmio_va sub ulong h 1 off 354 arm64_mmio_w64 addr val 355 end 356 put bmmio_wr_ops 0 add get bmmio_wr_ops 0 1 357 put bmmio_last 0 ulong h 358 put bmmio_last 1 off 359 put bmmio_last 2 8 360 put bmmio_last 3 1 361 put bmmio_last 4 val 362 put bmmio_err 0 BMMIO_OK 363 ret BMMIO_OK 364 end 365 366 note Verify GB20B identity registers: BOOT_0 == 0x1bb000a1 and BOOT_42 == 0x1bba1000. 367 fn bmmio_verify_gb20b h long to bool 368 if not bmmio_is_live h 369 ret false 370 end 371 b0 = bmmio_r32 h GB20B_OFF_BOOT0 372 if ne get bmmio_err 0 BMMIO_OK 373 ret false 374 end 375 b42 = bmmio_r32 h GB20B_OFF_BOOT42 376 if ne get bmmio_err 0 BMMIO_OK 377 ret false 378 end 379 ret and eq b0 GB20B_VAL_BOOT0 eq b42 GB20B_VAL_BOOT42 380 end