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
omega-mv / gpu / driver / gsp / bm_hal.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note gsp/bm_hal.om: Zero-BSS bare-metal BAR0 MMIO, Normal-NC physical DMA, CNTVCT_EL0 deadline |
| 3 | note timer, and DSB SY submission chain for GB20B on Aienux ARM64 (SPARK.EFI). |
| 4 | note Zero Linux syscalls (no sleepns/fopen/mapget/mapset); safe for target a64efi and host replay. |
| 5 | note In host replay (bmmode == 0), BAR0 and Normal-NC accesses dispatch to caller-supplied |
| 6 | note replay callbacks (bmrbarrd/bmrbarwr/bmrmemrd8/bmrmemwr8/bmrcntvct) with exact barrier |
| 7 | note trace logging (bmbtrace) so every DSB SY transition is verifiable on host. |
| 8 | note Prefix bm / BM. Library unit, no fn main. |
| 9 | |
| 10 | const BMOK long 0 |
| 11 | const BMEINVAL long -1 |
| 12 | const BMERANGE long -2 |
| 13 | const BMEALIGN long -3 |
| 14 | const BMESTATE long -4 |
| 15 | const BMETIME long -5 |
| 16 | |
| 17 | const BMPALIM ulong 1099511627776 |
| 18 | const BMBARDB ulong 12255376 |
| 19 | const BMGPPUT ulong 140 |
| 20 | const BMDBEN uint 1073741824 |
| 21 | const BMRLINV uint 127 |
| 22 | const BMCHMAX uint 4095 |
| 23 | const BMLENMX uint 2097151 |
| 24 | const BMVAMSK ulong 18446744073709551612 |
| 25 | const BMGPB41 ulong 2199023255552 |
| 26 | const BMM32 ulong 4294967295 |
| 27 | |
| 28 | note bmmode: 0 = host replay callbacks, 1 = bare-metal Aienux (wired to Piece 1/2 MMIO/NC hooks) |
| 29 | global bmmode ulong 1 |
| 30 | global bmbarbase ulong 1 |
| 31 | global bmbarsz ulong 1 |
| 32 | global bmdmabase ulong 1 |
| 33 | global bmdmasz ulong 1 |
| 34 | global bmfrq ulong 1 |
| 35 | global bmfences ulong 1 |
| 36 | global bmbarops ulong 1 |
| 37 | global bmdmaops ulong 1 |
| 38 | note Barrier trace log for host verification: 1=PB, 2=FENCE, 3=GPFIFO, 4=USERD, 5=DOORBELL |
| 39 | global bmtrc uchar 64 |
| 40 | global bmtrcn ulong 1 |
| 41 | global bmsubtok ulong 1 |
| 42 | |
| 43 | const BMFRQMAX ulong 18000000000000 |
| 44 | |
| 45 | fn bminit mode ulong barbase ulong barsz ulong dmabase ulong dmasz ulong frq ulong to long |
| 46 | if gt mode 1 |
| 47 | ret BMEINVAL |
| 48 | end |
| 49 | if or eq frq 0 gt frq BMFRQMAX |
| 50 | ret BMEINVAL |
| 51 | end |
| 52 | if or eq barsz 0 lt barsz 12255488 |
| 53 | ret BMERANGE |
| 54 | end |
| 55 | if eq mode 1 |
| 56 | if or eq barbase 0 ne and barbase 4095 0 |
| 57 | ret BMEALIGN |
| 58 | end |
| 59 | if gt barsz sub not ulong 0 barbase |
| 60 | ret BMERANGE |
| 61 | end |
| 62 | end |
| 63 | if eq dmabase 0 |
| 64 | ret BMEINVAL |
| 65 | end |
| 66 | if or eq dmasz 0 gt dmasz BMPALIM |
| 67 | ret BMERANGE |
| 68 | end |
| 69 | if or ge dmabase BMPALIM gt dmasz sub BMPALIM dmabase |
| 70 | ret BMERANGE |
| 71 | end |
| 72 | if ne and dmabase 4095 0 |
| 73 | ret BMEALIGN |
| 74 | end |
| 75 | put bmmode 0 mode |
| 76 | put bmbarbase 0 barbase |
| 77 | put bmbarsz 0 barsz |
| 78 | put bmdmabase 0 dmabase |
| 79 | put bmdmasz 0 dmasz |
| 80 | put bmfrq 0 frq |
| 81 | put bmfences 0 0 |
| 82 | put bmbarops 0 0 |
| 83 | put bmdmaops 0 0 |
| 84 | put bmtrcn 0 0 |
| 85 | ret BMOK |
| 86 | end |
| 87 | |
| 88 | fn bmtrcadd ev uchar to void |
| 89 | n = get bmtrcn 0 |
| 90 | if lt n 64 |
| 91 | put bmtrc n ev |
| 92 | put bmtrcn 0 add n 1 |
| 93 | end |
| 94 | end |
| 95 | |
| 96 | fn bmdsb to void |
| 97 | cpufence |
| 98 | put bmfences 0 add get bmfences 0 1 |
| 99 | bmtrcadd 2 |
| 100 | end |
| 101 | |
| 102 | note Overflow-safe CNTVCT_EL0 -> milliseconds: (t / frq) * 1000 + ((t % frq) * 1000) / frq |
| 103 | fn bmticks2ms ticks ulong to ulong |
| 104 | frq = get bmfrq 0 |
| 105 | if eq frq 0 |
| 106 | ret 0 |
| 107 | end |
| 108 | sec = div ticks frq |
| 109 | r = rem ticks frq |
| 110 | ret add mul sec 1000 div mul r 1000 frq |
| 111 | end |
| 112 | |
| 113 | note Overflow-safe CNTVCT_EL0 -> microseconds: (t / frq) * 1000000 + ((t % frq) * 1000000) / frq |
| 114 | fn bmticks2us ticks ulong to ulong |
| 115 | frq = get bmfrq 0 |
| 116 | if eq frq 0 |
| 117 | ret 0 |
| 118 | end |
| 119 | sec = div ticks frq |
| 120 | r = rem ticks frq |
| 121 | ret add mul sec 1000000 div mul r 1000000 frq |
| 122 | end |
| 123 | |
| 124 | fn bmnowms to ulong |
| 125 | ret bmticks2ms bmrcntvct |
| 126 | end |
| 127 | |
| 128 | fn bmnowus to ulong |
| 129 | ret bmticks2us bmrcntvct |
| 130 | end |
| 131 | |
| 132 | note Busy-wait us microseconds using CNTVCT_EL0 ticks (nested bounded loop: never traps 4 on Grace). |
| 133 | fn bmdelayus us ulong to long |
| 134 | if eq us 0 |
| 135 | ret BMOK |
| 136 | end |
| 137 | frq = get bmfrq 0 |
| 138 | if or eq frq 0 gt frq BMFRQMAX |
| 139 | ret BMESTATE |
| 140 | end |
| 141 | sec = div us 1000000 |
| 142 | ru = rem us 1000000 |
| 143 | dt = add mul sec frq div add mul ru frq 999999 1000000 |
| 144 | if eq dt 0 |
| 145 | dt = ulong 1 |
| 146 | end |
| 147 | t0 = bmrcntvct |
| 148 | for o ulong 1000 |
| 149 | for i ulong 100000 |
| 150 | if ge sub bmrcntvct t0 dt |
| 151 | ret BMOK |
| 152 | end |
| 153 | cpurelax |
| 154 | end |
| 155 | end |
| 156 | ret BMETIME |
| 157 | end |
| 158 | |
| 159 | note Checked BAR0 32-bit read with DSB SY ordering. |
| 160 | fn bmbarrd32 off ulong to ulong |
| 161 | if ne and off 3 0 |
| 162 | trap 3 |
| 163 | end |
| 164 | if ge off get bmbarsz 0 |
| 165 | trap 3 |
| 166 | end |
| 167 | if gt 4 sub get bmbarsz 0 off |
| 168 | trap 3 |
| 169 | end |
| 170 | put bmbarops 0 add get bmbarops 0 1 |
| 171 | v = bmrbarrd off |
| 172 | cpufence |
| 173 | ret and v BMM32 |
| 174 | end |
| 175 | |
| 176 | note Checked BAR0 32-bit write with DSB SY ordering before and after Device-nGnRnE store. |
| 177 | fn bmbarwr32 off ulong v ulong to void |
| 178 | if ne and off 3 0 |
| 179 | trap 3 |
| 180 | end |
| 181 | if ge off get bmbarsz 0 |
| 182 | trap 3 |
| 183 | end |
| 184 | if gt 4 sub get bmbarsz 0 off |
| 185 | trap 3 |
| 186 | end |
| 187 | put bmbarops 0 add get bmbarops 0 1 |
| 188 | cpufence |
| 189 | bmrbarwr off and v BMM32 |
| 190 | cpufence |
| 191 | end |
| 192 | |
| 193 | note Checked Normal-NC physical DMA byte read/write (<40-bit PA inside registered pool). |
| 194 | fn bmchkpa pa ulong n ulong to void |
| 195 | b = get bmdmabase 0 |
| 196 | s = get bmdmasz 0 |
| 197 | if lt pa b |
| 198 | trap 3 |
| 199 | end |
| 200 | o = sub pa b |
| 201 | if gt o s |
| 202 | trap 3 |
| 203 | end |
| 204 | if gt n sub s o |
| 205 | trap 3 |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | fn bmmemrd8 pa ulong to uchar |
| 210 | bmchkpa pa 1 |
| 211 | put bmdmaops 0 add get bmdmaops 0 1 |
| 212 | ret bmrmemrd8 pa |
| 213 | end |
| 214 | |
| 215 | fn bmmemwr8 pa ulong v uchar to void |
| 216 | bmchkpa pa 1 |
| 217 | put bmdmaops 0 add get bmdmaops 0 1 |
| 218 | bmrmemwr8 pa v |
| 219 | end |
| 220 | |
| 221 | fn bmmemrd16 pa ulong to ulong |
| 222 | bmchkpa pa 2 |
| 223 | lo = ulong bmrmemrd8 pa |
| 224 | hi = ulong bmrmemrd8 add pa 1 |
| 225 | ret or lo shl hi 8 |
| 226 | end |
| 227 | |
| 228 | fn bmmemrd32 pa ulong to ulong |
| 229 | bmchkpa pa 4 |
| 230 | v = ulong 0 |
| 231 | for i ulong 4 |
| 232 | v = or v shl ulong bmrmemrd8 add pa i mul i 8 |
| 233 | end |
| 234 | ret v |
| 235 | end |
| 236 | |
| 237 | fn bmmemwr32 pa ulong v ulong to void |
| 238 | bmchkpa pa 4 |
| 239 | for i ulong 4 |
| 240 | bmrmemwr8 add pa i uchar and shr v mul i 8 255 |
| 241 | end |
| 242 | end |
| 243 | |
| 244 | fn bmmemrd64 pa ulong to ulong |
| 245 | bmchkpa pa 8 |
| 246 | v = ulong 0 |
| 247 | for i ulong 8 |
| 248 | v = or v shl ulong bmrmemrd8 add pa i mul i 8 |
| 249 | end |
| 250 | ret v |
| 251 | end |
| 252 | |
| 253 | fn bmmemwr64 pa ulong v ulong to void |
| 254 | bmchkpa pa 8 |
| 255 | for i ulong 8 |
| 256 | bmrmemwr8 add pa i uchar and shr v mul i 8 255 |
| 257 | end |
| 258 | end |
| 259 | |
| 260 | fn bmmemzero pa ulong n ulong to void |
| 261 | if eq n 0 |
| 262 | ret |
| 263 | end |
| 264 | bmchkpa pa n |
| 265 | o = ulong 0 |
| 266 | while lt o n bound 9223372036854775807 |
| 267 | if and eq and add pa o 7 0 ge sub n o 8 |
| 268 | bmmemwr64 add pa o 0 |
| 269 | o = add o 8 |
| 270 | else |
| 271 | bmmemwr8 add pa o 0 |
| 272 | o = add o 1 |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | |
| 277 | note Checked GB20B work-submit token: strictly rejects rl >= 127 (RUNLIST_ID_INVALID_RUNLIST) |
| 278 | note and chid > 4095, sets bit 30 RUNLIST_DOORBELL_ENABLE (0x40000000). |
| 279 | fn bmtokgb20 rl uint chid uint to long |
| 280 | if or ge rl BMRLINV gt chid BMCHMAX |
| 281 | ret BMERANGE |
| 282 | end |
| 283 | tok = or BMDBEN or shl rl 16 chid |
| 284 | ret long ulong tok |
| 285 | end |
| 286 | |
| 287 | note Strict ARM64 Normal-NC submission chain: |
| 288 | note PB/QMD already written -> DSB SY -> GPFIFO -> DSB SY -> USERD.GPPut -> DSB SY -> BAR0 0x00BB0090. |
| 289 | note Returns nextput = (gpput + 1) % ent, or negative error. |
| 290 | fn bmsubmit gppa ulong userdpa ulong pbva ulong pbwds uint gpput uint ent uint to long |
| 291 | if or or lt ent 2 gt ent 65536 ne and ent sub ent 1 0 |
| 292 | ret BMEINVAL |
| 293 | end |
| 294 | if or ne and gppa 7 0 ne and userdpa 3 0 |
| 295 | ret BMEALIGN |
| 296 | end |
| 297 | if or ne and pbva 3 0 ge pbva BMPALIM |
| 298 | ret BMERANGE |
| 299 | end |
| 300 | if or eq pbwds 0 gt pbwds BMLENMX |
| 301 | ret BMERANGE |
| 302 | end |
| 303 | tok = get bmsubtok 0 |
| 304 | if eq and tok ulong BMDBEN 0 |
| 305 | ret BMESTATE |
| 306 | end |
| 307 | rl = and shr tok 16 127 |
| 308 | if ge rl ulong BMRLINV |
| 309 | ret BMERANGE |
| 310 | end |
| 311 | gpent = or or and pbva BMVAMSK shl and ulong pbwds ulong BMLENMX 42 BMGPB41 |
| 312 | idx = ulong rem gpput ent |
| 313 | nextput = rem add gpput 1 ent |
| 314 | bmtrcadd 1 |
| 315 | bmdsb |
| 316 | bmmemwr64 add gppa mul idx 8 gpent |
| 317 | bmtrcadd 3 |
| 318 | bmdsb |
| 319 | bmmemwr32 add userdpa BMGPPUT ulong nextput |
| 320 | bmtrcadd 4 |
| 321 | bmdsb |
| 322 | bmbarwr32 BMBARDB tok |
| 323 | bmtrcadd 5 |
| 324 | ret long ulong nextput |
| 325 | end |
| 326 | |
| 327 | fn bmsettok rl uint chid uint to long |
| 328 | t = bmtokgb20 rl chid |
| 329 | if lt t 0 |
| 330 | ret t |
| 331 | end |
| 332 | put bmsubtok 0 ulong t |
| 333 | ret BMOK |
| 334 | end |