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 / chan / sim.om
| 1 | note chan/sim.om: host simulator of the GB10 pieces the channel path touches, ported from aienux PR #441 |
| 2 | note native/kernel/gpu/gpu_sim.c and gpu_sim.h (branch pr441 @ c5bb3a4b). Library unit: no main. |
| 3 | note Prefix gs / GS (never gsp). It is the mode 0 device of chan/gmmu.om mmregrd / mmregwr and of the |
| 4 | note channel ops in chan/chan.om. Needs chan/gmmu.om (the window array mmwin) before it. |
| 5 | note |
| 6 | note It models: the VF MMU invalidate registers (a request completes after gsi[4] polls of INVALIDATE, |
| 7 | note never when gsi[5] "stuck" is set), a 16-entry TLB filled by its own ver3 page walker, the fault log, |
| 8 | note the doorbell (a submit token consumes that channel's ring: USERD GPPut, GP entries, push buffer |
| 9 | note words, all through the GPU MMU, so a missing mapping or a write to read-only memory faults) and the |
| 10 | note channel bind / disable / idle protocol. Every register access is logged (512 entries, then counted |
| 11 | note as dropped). The walker, offsets and bit fields are this file's own copies, not chan/gmmu.om's, so |
| 12 | note a wrong driver constant shows up as a wrong access or a fault. |
| 13 | note |
| 14 | note State (the fields of struct ck_gsim): |
| 15 | note gsmem 0 base, 1 size of the memory the GPU sees (inside mmwin, at bus address gsmem 0) |
| 16 | note gsi 0 pdb_lo, 1 pdb_hi, 2 pending, 3 polls left, 4 latency (3), 5 stuck, 6 done, 7 all |
| 17 | note gslogo / gslogv / gslogw, gsnlog, gsldrop: the register log |
| 18 | note gstv gstpdb gstva gstpa gstpg gstlo, gstn (next), gsthits: the TLB |
| 19 | note gsfva gsfpdb gsfkind gsfw, gsnf: faults (16 kept, all counted) |
| 20 | note per channel slot (4): gscb bound, gscid, gsctok, gscent, gscgpva, gscupa, gscipa, gscpdb, |
| 21 | note gscget gp_get, gscwr words_read, gscflt faulted, gscdis disabled |
| 22 | note gsdb doorbells, gsdbunk doorbell_unknown, gshang (set by a test: the engine never idles), |
| 23 | note gsidlep idle_polls |
| 24 | note fault kinds: 1 unmapped, 2 bad PDE (a PTE above PDE2), 3 aperture, 4 write to read-only, 5 outside |
| 25 | note memory |
| 26 | const GSRPDB ulong 12071072 |
| 27 | const GSRUPR ulong 12071076 |
| 28 | const GSRINV ulong 12071088 |
| 29 | const GSDBELL ulong 12255376 |
| 30 | const GSTRIG ulong 2147483648 |
| 31 | const GSADDR ulong 4503599627366400 |
| 32 | const GSBIGM ulong 4503599627370240 |
| 33 | const GSUDPUT ulong 140 |
| 34 | const GSLOGMX ulong 512 |
| 35 | const GSNONE ulong 4 |
| 36 | note gshalf's "try the other half" result; fault kinds are 1..5, so 255 never collides |
| 37 | const GSNEXT ulong 255 |
| 38 | note gspdbof's refusal: 2^64-1, never a page-aligned PDB |
| 39 | const GSBADPD ulong 18446744073709551615 |
| 40 | global gsmem ulong 2 |
| 41 | global gsi ulong 8 |
| 42 | global gslogo uint 512 |
| 43 | global gslogv uint 512 |
| 44 | global gslogw uchar 512 |
| 45 | global gsnlog ulong 1 |
| 46 | global gsldrop ulong 1 |
| 47 | global gstv uchar 16 |
| 48 | global gstpdb ulong 16 |
| 49 | global gstva ulong 16 |
| 50 | global gstpa ulong 16 |
| 51 | global gstpg ulong 16 |
| 52 | global gstlo ulong 16 |
| 53 | global gstn ulong 1 |
| 54 | global gsthits ulong 1 |
| 55 | global gsfva ulong 16 |
| 56 | global gsfpdb ulong 16 |
| 57 | global gsfkind ulong 16 |
| 58 | global gsfw ulong 16 |
| 59 | global gsnf ulong 1 |
| 60 | global gscb uchar 4 |
| 61 | global gscid uint 4 |
| 62 | global gsctok uint 4 |
| 63 | global gscent uint 4 |
| 64 | global gscgpva ulong 4 |
| 65 | global gscupa ulong 4 |
| 66 | global gscipa ulong 4 |
| 67 | global gscpdb ulong 4 |
| 68 | global gscget uint 4 |
| 69 | global gscwr uint 4 |
| 70 | global gscflt uchar 4 |
| 71 | global gscdis uchar 4 |
| 72 | global gsdb ulong 1 |
| 73 | global gsdbunk ulong 1 |
| 74 | global gshang ulong 1 |
| 75 | global gsidlep ulong 1 |
| 76 | note walk / translate results: 0 pa, 1 page size, 2 pte low 12 bits, 3 the word read |
| 77 | global gsw ulong 4 |
| 78 | fn gsinit base ulong size ulong to void |
| 79 | put gsmem 0 base |
| 80 | put gsmem 1 size |
| 81 | for i ulong 8 |
| 82 | put gsi i 0 |
| 83 | end |
| 84 | put gsi 4 3 |
| 85 | for i ulong GSLOGMX |
| 86 | put gslogo i 0 |
| 87 | put gslogv i 0 |
| 88 | put gslogw i 0 |
| 89 | end |
| 90 | put gsnlog 0 0 |
| 91 | put gsldrop 0 0 |
| 92 | for i ulong 16 |
| 93 | put gstv i 0 |
| 94 | put gstpdb i 0 |
| 95 | put gstva i 0 |
| 96 | put gstpa i 0 |
| 97 | put gstpg i 0 |
| 98 | put gstlo i 0 |
| 99 | put gsfva i 0 |
| 100 | put gsfpdb i 0 |
| 101 | put gsfkind i 0 |
| 102 | put gsfw i 0 |
| 103 | end |
| 104 | put gstn 0 0 |
| 105 | put gsthits 0 0 |
| 106 | put gsnf 0 0 |
| 107 | for i ulong 4 |
| 108 | put gscb i 0 |
| 109 | put gscid i 0 |
| 110 | put gsctok i 0 |
| 111 | put gscent i 0 |
| 112 | put gscgpva i 0 |
| 113 | put gscupa i 0 |
| 114 | put gscipa i 0 |
| 115 | put gscpdb i 0 |
| 116 | put gscget i 0 |
| 117 | put gscwr i 0 |
| 118 | put gscflt i 0 |
| 119 | put gscdis i 0 |
| 120 | end |
| 121 | put gsdb 0 0 |
| 122 | put gsdbunk 0 0 |
| 123 | put gshang 0 0 |
| 124 | put gsidlep 0 0 |
| 125 | for i ulong 4 |
| 126 | put gsw i 0 |
| 127 | end |
| 128 | end |
| 129 | fn gslog off ulong v ulong w uchar to void |
| 130 | n = get gsnlog 0 |
| 131 | if lt n GSLOGMX |
| 132 | put gslogo n uint off |
| 133 | put gslogv n uint v |
| 134 | put gslogw n w |
| 135 | put gsnlog 0 add n 1 |
| 136 | else |
| 137 | put gsldrop 0 add get gsldrop 0 1 |
| 138 | end |
| 139 | end |
| 140 | fn gsinmem pa ulong n ulong to bool |
| 141 | b = get gsmem 0 |
| 142 | s = get gsmem 1 |
| 143 | if lt pa b |
| 144 | ret false |
| 145 | end |
| 146 | o = sub pa b |
| 147 | if gt o s |
| 148 | ret false |
| 149 | end |
| 150 | ret le n sub s o |
| 151 | end |
| 152 | fn gsrd64 pa ulong to ulong |
| 153 | o = add sub pa get gsmem 0 sub get gsmem 0 get mmwb 0 |
| 154 | v = ulong 0 |
| 155 | for i ulong 8 |
| 156 | v = or shl v 8 ulong get mmwin add o sub 7 i |
| 157 | end |
| 158 | ret v |
| 159 | end |
| 160 | fn gsrd32 pa ulong to ulong |
| 161 | o = add sub pa get gsmem 0 sub get gsmem 0 get mmwb 0 |
| 162 | v = ulong 0 |
| 163 | for i ulong 4 |
| 164 | v = or shl v 8 ulong get mmwin add o sub 3 i |
| 165 | end |
| 166 | ret v |
| 167 | end |
| 168 | fn gslo lv ulong to ulong |
| 169 | if eq lv 0 |
| 170 | ret 56 |
| 171 | end |
| 172 | if eq lv 1 |
| 173 | ret 47 |
| 174 | end |
| 175 | if eq lv 2 |
| 176 | ret 38 |
| 177 | end |
| 178 | if eq lv 3 |
| 179 | ret 29 |
| 180 | end |
| 181 | ret 21 |
| 182 | end |
| 183 | fn gshi lv ulong to ulong |
| 184 | if eq lv 0 |
| 185 | ret 56 |
| 186 | end |
| 187 | if eq lv 1 |
| 188 | ret 55 |
| 189 | end |
| 190 | if eq lv 2 |
| 191 | ret 46 |
| 192 | end |
| 193 | if eq lv 3 |
| 194 | ret 37 |
| 195 | end |
| 196 | ret 28 |
| 197 | end |
| 198 | fn gschkpte pte ulong wf ulong to ulong |
| 199 | ap = and shr pte 1 3 |
| 200 | pcf = and shr pte 3 31 |
| 201 | if or eq ap 0 eq ap 1 |
| 202 | ret 3 |
| 203 | end |
| 204 | if and ne wf 0 ne and pcf 4 0 |
| 205 | ret 4 |
| 206 | end |
| 207 | ret 0 |
| 208 | end |
| 209 | note a valid PTE of page size psz: translation into gsw 0..2; 5 when the target is outside memory |
| 210 | fn gsleaf e ulong psz ulong va ulong to ulong |
| 211 | pa = add and and e GSADDR not sub psz 1 and va sub psz 1 |
| 212 | put gsw 0 pa |
| 213 | put gsw 1 psz |
| 214 | put gsw 2 and e 4095 |
| 215 | if gsinmem pa 4 |
| 216 | ret 0 |
| 217 | end |
| 218 | ret 5 |
| 219 | end |
| 220 | note the dual PDE0 at tbl + idx*16: small half (4 KiB pages) first, then big half (64 KiB) |
| 221 | fn gsdual tbl ulong idx ulong e ulong va ulong wf ulong to ulong |
| 222 | e1 = gsrd64 add add tbl mul idx 16 8 |
| 223 | f = gshalf and shr e1 1 3 and e1 GSADDR 12 va wf |
| 224 | if ne f GSNEXT |
| 225 | ret f |
| 226 | end |
| 227 | f = gshalf and shr e 1 3 and e GSBIGM 16 va wf |
| 228 | if ne f GSNEXT |
| 229 | ret f |
| 230 | end |
| 231 | ret 1 |
| 232 | end |
| 233 | note one half of the dual PDE0: aperture ap, page table pt, page shift sh; GSNEXT means try the next half |
| 234 | fn gshalf ap ulong pt ulong sh ulong va ulong wf ulong to ulong |
| 235 | if eq ap 0 |
| 236 | ret GSNEXT |
| 237 | end |
| 238 | if eq ap 1 |
| 239 | ret 3 |
| 240 | end |
| 241 | a = add pt mul and shr va sh sub shl 1 sub 21 sh 1 8 |
| 242 | if not gsinmem a 8 |
| 243 | ret 5 |
| 244 | end |
| 245 | pte = gsrd64 a |
| 246 | if eq and pte 1 0 |
| 247 | ret GSNEXT |
| 248 | end |
| 249 | f = gschkpte pte wf |
| 250 | if ne f 0 |
| 251 | ret f |
| 252 | end |
| 253 | ret gsleaf pte shl 1 sh va |
| 254 | end |
| 255 | fn gswalk pdb ulong va ulong wf ulong to ulong |
| 256 | if ne shr va 57 0 |
| 257 | ret 1 |
| 258 | end |
| 259 | tbl = pdb |
| 260 | for lv ulong 5 |
| 261 | esz = ulong 8 |
| 262 | if eq lv 4 |
| 263 | esz = 16 |
| 264 | end |
| 265 | lo = gslo lv |
| 266 | idx = and shr va lo sub shl 1 add sub gshi lv lo 1 1 |
| 267 | ea = add tbl mul idx esz |
| 268 | if not gsinmem ea esz |
| 269 | ret 5 |
| 270 | end |
| 271 | e = gsrd64 ea |
| 272 | if ne and e 1 0 |
| 273 | if lt lv 2 |
| 274 | ret 2 |
| 275 | end |
| 276 | f = gschkpte e wf |
| 277 | if ne f 0 |
| 278 | ret f |
| 279 | end |
| 280 | ret gsleaf e shl 1 lo va |
| 281 | end |
| 282 | if lt lv 4 |
| 283 | ap = and shr e 1 3 |
| 284 | if eq ap 0 |
| 285 | ret 1 |
| 286 | end |
| 287 | if eq ap 1 |
| 288 | ret 3 |
| 289 | end |
| 290 | tbl = and e GSADDR |
| 291 | continue |
| 292 | end |
| 293 | ret gsdual tbl idx e va wf |
| 294 | end |
| 295 | ret 1 |
| 296 | end |
| 297 | fn gsfault pdb ulong va ulong kind ulong wf ulong to void |
| 298 | n = get gsnf 0 |
| 299 | if lt n 16 |
| 300 | put gsfva n va |
| 301 | put gsfpdb n pdb |
| 302 | put gsfkind n kind |
| 303 | put gsfw n wf |
| 304 | end |
| 305 | put gsnf 0 add n 1 |
| 306 | end |
| 307 | fn gsxlate pdb ulong va ulong wf ulong to ulong |
| 308 | i = gstlbhit pdb va |
| 309 | if lt i 16 |
| 310 | f = gschkpte get gstlo i wf |
| 311 | if ne f 0 |
| 312 | gsfault pdb va f wf |
| 313 | ret f |
| 314 | end |
| 315 | put gsthits 0 add get gsthits 0 1 |
| 316 | put gsw 0 add get gstpa i sub va get gstva i |
| 317 | ret 0 |
| 318 | end |
| 319 | f = gswalk pdb va wf |
| 320 | if ne f 0 |
| 321 | gsfault pdb va f wf |
| 322 | ret f |
| 323 | end |
| 324 | gstfill pdb va |
| 325 | ret 0 |
| 326 | end |
| 327 | note the first valid TLB slot covering (pdb, va), 16 when none |
| 328 | fn gstlbhit pdb ulong va ulong to ulong |
| 329 | for i ulong 16 |
| 330 | if and ne get gstv i 0 eq get gstpdb i pdb |
| 331 | if lt sub va get gstva i get gstpg i |
| 332 | ret i |
| 333 | end |
| 334 | end |
| 335 | end |
| 336 | ret 16 |
| 337 | end |
| 338 | note fill TLB slot gstn % 16 from the walk result in gsw (pa, page, pte_lo) |
| 339 | fn gstfill pdb ulong va ulong to void |
| 340 | n = rem get gstn 0 16 |
| 341 | put gstn 0 add get gstn 0 1 |
| 342 | pg = get gsw 1 |
| 343 | vp = and va not sub pg 1 |
| 344 | put gstv n 1 |
| 345 | put gstpdb n pdb |
| 346 | put gstpg n pg |
| 347 | put gstva n vp |
| 348 | put gstpa n sub get gsw 0 sub va vp |
| 349 | put gstlo n get gsw 2 |
| 350 | end |
| 351 | note a GPU read of 4 bytes at va: fault kind, the word in gsw 3 |
| 352 | fn gsgrd32 pdb ulong va ulong to ulong |
| 353 | f = gsxlate pdb va 0 |
| 354 | if ne f 0 |
| 355 | ret f |
| 356 | end |
| 357 | put gsw 3 gsrd32 get gsw 0 |
| 358 | ret 0 |
| 359 | end |
| 360 | fn gschan chid uint to ulong |
| 361 | for i ulong 4 |
| 362 | if and ne get gscb i 0 eq get gscid i chid |
| 363 | ret i |
| 364 | end |
| 365 | end |
| 366 | ret GSNONE |
| 367 | end |
| 368 | fn gsconsume c ulong to void |
| 369 | if or or ne get gscflt c 0 ne get gscdis c 0 ne get gshang 0 0 |
| 370 | ret |
| 371 | end |
| 372 | upa = add get gscupa c GSUDPUT |
| 373 | if not gsinmem upa 4 |
| 374 | put gscflt c 1 |
| 375 | ret |
| 376 | end |
| 377 | pv = uint gsrd32 upa |
| 378 | ent = get gscent c |
| 379 | if ge pv ent |
| 380 | put gscflt c 1 |
| 381 | ret |
| 382 | end |
| 383 | pdb = get gscpdb c |
| 384 | while ne get gscget c pv bound 999999 |
| 385 | if not gsent c pdb add get gscgpva c mul ulong get gscget c 8 |
| 386 | put gscflt c 1 |
| 387 | ret |
| 388 | end |
| 389 | put gscget c rem add get gscget c 1 ent |
| 390 | end |
| 391 | end |
| 392 | note one GP entry at eva: read both words, then every pushbuffer word; false on a fault |
| 393 | fn gsent c ulong pdb ulong eva ulong to bool |
| 394 | if ne gsgrd32 pdb eva 0 |
| 395 | ret false |
| 396 | end |
| 397 | lo = get gsw 3 |
| 398 | if ne gsgrd32 pdb add eva 4 0 |
| 399 | ret false |
| 400 | end |
| 401 | hi = get gsw 3 |
| 402 | ret gspb c pdb or and lo 4294967292 shl and hi 255 32 and shr hi 10 2097151 |
| 403 | end |
| 404 | note read the n pushbuffer words at pbva (counted in gscwr); false on a fault |
| 405 | fn gspb c ulong pdb ulong pbva ulong n ulong to bool |
| 406 | for a ulong div add n 1023 1024 |
| 407 | for b ulong 1024 |
| 408 | w = add mul a 1024 b |
| 409 | if ge w n |
| 410 | break |
| 411 | end |
| 412 | if ne gsgrd32 pdb add pbva mul w 4 0 |
| 413 | ret false |
| 414 | end |
| 415 | put gscwr c add get gscwr c 1 |
| 416 | end |
| 417 | end |
| 418 | ret true |
| 419 | end |
| 420 | fn gsrd off ulong to ulong |
| 421 | v = ulong 0 |
| 422 | if eq off GSRINV |
| 423 | if and ne get gsi 2 0 eq get gsi 5 0 |
| 424 | if eq get gsi 3 0 |
| 425 | pdb = shl or and shr get gsi 0 4 268435455 shl and get gsi 1 1048575 28 12 |
| 426 | for i ulong 16 |
| 427 | if or ne get gsi 7 0 eq get gstpdb i pdb |
| 428 | put gstv i 0 |
| 429 | end |
| 430 | end |
| 431 | put gsi 2 0 |
| 432 | put gsi 6 add get gsi 6 1 |
| 433 | else |
| 434 | put gsi 3 sub get gsi 3 1 |
| 435 | end |
| 436 | end |
| 437 | if ne get gsi 2 0 |
| 438 | v = GSTRIG |
| 439 | end |
| 440 | end |
| 441 | gslog off v 0 |
| 442 | ret v |
| 443 | end |
| 444 | fn gswr off ulong v ulong to void |
| 445 | gslog off v 1 |
| 446 | if eq off GSRPDB |
| 447 | put gsi 0 v |
| 448 | ret |
| 449 | end |
| 450 | if eq off GSRUPR |
| 451 | put gsi 1 v |
| 452 | ret |
| 453 | end |
| 454 | if and eq off GSRINV ne and v GSTRIG 0 |
| 455 | put gsi 2 1 |
| 456 | put gsi 3 get gsi 4 |
| 457 | put gsi 7 0 |
| 458 | if ne and v 2 0 |
| 459 | put gsi 7 1 |
| 460 | end |
| 461 | ret |
| 462 | end |
| 463 | if eq off GSDBELL |
| 464 | put gsdb 0 add get gsdb 0 1 |
| 465 | for i ulong 4 |
| 466 | if and ne get gscb i 0 eq ulong get gsctok i v |
| 467 | gsconsume i |
| 468 | ret |
| 469 | end |
| 470 | end |
| 471 | put gsdbunk 0 add get gsdbunk 0 1 |
| 472 | end |
| 473 | end |
| 474 | note bind: the channel's page directory comes from its instance block (RAMIN word 128/129), as the |
| 475 | note hardware reads it; target must be sysmem (2 coherent, 3 non-coherent), bit 10 (VOL) clear |
| 476 | fn gsbind chid uint tok uint ent uint gpva ulong upa ulong ipa ulong to long |
| 477 | if ne gschan chid GSNONE |
| 478 | ret -1 |
| 479 | end |
| 480 | pdb = gspdbof ipa |
| 481 | if eq pdb GSBADPD |
| 482 | ret -1 |
| 483 | end |
| 484 | for i ulong 4 |
| 485 | if eq get gscb i 0 |
| 486 | put gscid i chid |
| 487 | put gsctok i tok |
| 488 | put gscent i ent |
| 489 | put gscgpva i gpva |
| 490 | put gscupa i upa |
| 491 | put gscipa i ipa |
| 492 | put gscpdb i pdb |
| 493 | put gscget i 0 |
| 494 | put gscwr i 0 |
| 495 | put gscflt i 0 |
| 496 | put gscdis i 0 |
| 497 | put gscb i 1 |
| 498 | ret 0 |
| 499 | end |
| 500 | end |
| 501 | ret -1 |
| 502 | end |
| 503 | note the page directory from RAMIN word 128/129 of the instance block at ipa; GSBADPD when the words |
| 504 | note are outside memory, the target is not sysmem or VOL is set (a real PDB has its low 12 bits clear) |
| 505 | fn gspdbof ipa ulong to ulong |
| 506 | a = add ipa 512 |
| 507 | if not gsinmem a 8 |
| 508 | ret GSBADPD |
| 509 | end |
| 510 | w = gsrd64 a |
| 511 | lo = and w 4294967295 |
| 512 | t = and lo 3 |
| 513 | if or and ne t 2 ne t 3 ne and lo 1024 0 |
| 514 | ret GSBADPD |
| 515 | end |
| 516 | ret or shl shr w 32 32 and lo 4294963200 |
| 517 | end |
| 518 | fn gsdisable chid uint to long |
| 519 | c = gschan chid |
| 520 | if eq c GSNONE |
| 521 | ret -1 |
| 522 | end |
| 523 | put gscdis c 1 |
| 524 | ret 0 |
| 525 | end |
| 526 | fn gsidle chid uint to long |
| 527 | put gsidlep 0 add get gsidlep 0 1 |
| 528 | c = gschan chid |
| 529 | if eq c GSNONE |
| 530 | ret 1 |
| 531 | end |
| 532 | if or ne get gshang 0 0 eq get gscdis c 0 |
| 533 | ret 0 |
| 534 | end |
| 535 | put gscb c 0 |
| 536 | ret 1 |
| 537 | end |