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 / chan.om
| 1 | note chan/chan.om: GPFIFO compute channel (ring, USERD, instance block, doorbell), ported from aienux |
| 2 | note PR #441 native/kernel/gpu/gpu_chan.c and gpu_chan.h (branch pr441 @ c5bb3a4b). Library unit: no main. |
| 3 | note Prefix ch / CH. Needs chan/gmmu.om and chan/mem.om before it, and chan/sim.om (mode 0 ops). |
| 4 | note |
| 5 | note A channel is a slot 0..3 (CHSLOTS); its buffers are chan/mem.om slots. The CPU writes the ring, |
| 6 | note USERD GPPut and the instance block through mmrd/mmwr (array or owned mapping); doorbell mmregwr. |
| 7 | note |
| 8 | note Channel ops (C: a function-pointer table): chbindop / chdisop / chidlop. mmmode 0 calls the |
| 9 | note simulator (chan/sim.om gsbind / gsdisable / gsidle). On GB10 the channel is allocated, scheduled |
| 10 | note and torn down by GSP-RM (RM_ALLOC BLACKWELL_CHANNEL_GPFIFO_A, encoded by chan/params.om), not by |
| 11 | note register writes, so any other mode traps 9 here: the hardware ops are the GSP RPC path. |
| 12 | note |
| 13 | note API (state in globals; a global is never an argument): |
| 14 | note chinit -> void all slots closed and empty |
| 15 | note chcreate c chid rl ent -> long ck_gpu_chan_create (chid, rl, ent uint) |
| 16 | note chsubmit c pb off n -> long ck_gpu_chan_submit (pb a mem slot, off and n uint) |
| 17 | note chretire c upto -> void ck_gpu_chan_retire (upto uint) |
| 18 | note chteardown c budget nx -> long ck_gpu_chan_teardown; extras: the first nx slots of chx |
| 19 | note (GMNONE entries are skipped, as C null pointers) |
| 20 | note chtoken rl chid -> uint ck_gpu_submit_token |
| 21 | note chrluserd pa -> long ck_gpu_rl_userd, the two words in chrlo |
| 22 | note chgpent va n -> ulong ck_gpu_gp_entry; chmthd: ck_gpu_mthd |
| 23 | note per slot: chchid chrl chtok chent chpdb chgpva chupa chipa chrl0 chrl1 (descriptor), chgp chud |
| 24 | note chin (ring, USERD, instance mem slots), chput chret (uint, wrap as uint32), chopen |
| 25 | note codes (enum ck_gpu_chan_status): 0 CHOK, 1 CHEINVAL, 2 CHENOMEM, 3 CHESTATE, 4 CHEFULL, 5 CHEBIND, |
| 26 | note 6 CHETIME |
| 27 | const CHOK long 0 |
| 28 | const CHEINVAL long 1 |
| 29 | const CHENOMEM long 2 |
| 30 | const CHESTATE long 3 |
| 31 | const CHEFULL long 4 |
| 32 | const CHEBIND long 5 |
| 33 | const CHETIME long 6 |
| 34 | const CHSLOTS ulong 4 |
| 35 | note CHID_MAX 0xFFF, RUNLIST_INVALID 0x7F, GP length max 0x1FFFFF, GP VA limit 1<<40 |
| 36 | const CHIDMAX uint 4095 |
| 37 | const CHRLINV uint 127 |
| 38 | const CHLENMX uint 2097151 |
| 39 | const CHVALIM ulong 1099511627776 |
| 40 | note USERD 512 bytes, GPPut at 0x8C; instance block 4096 bytes, page directory at word 128 (byte 512) |
| 41 | const CHUDSZ ulong 512 |
| 42 | const CHGPPUT ulong 140 |
| 43 | const CHINSZ ulong 4096 |
| 44 | const CHPDBW ulong 512 |
| 45 | note RAMIN PDB: target sysmem coherent 2, BIG_PAGE_SIZE_64K bit 11 (2048); low address mask 0xFFFFF000 |
| 46 | const CHTSYS uint 2 |
| 47 | const CHBIG64 uint 2048 |
| 48 | const CHLOM uint 4294963200 |
| 49 | note doorbell VF base 0xB80000 + 0x30090 = 0xBB0090; usermode BAR page offset +0x90 (144) |
| 50 | const CHDBELL ulong 12255376 |
| 51 | const CHUMDB ulong 144 |
| 52 | note GB20B work-submit token bit 30 RUNLIST_DOORBELL_ENABLE (0x40000000, active-high) |
| 53 | const CHDBEN uint 1073741824 |
| 54 | note Blackwell GPFIFO channel classes: GB10B 0xc96f (51567), GB20B 0xca6f (51823) |
| 55 | const CHCLSGB10 ulong 51567 |
| 56 | const CHCLSGB20 ulong 51823 |
| 57 | note GP entry: va & ~3 (mask 2^64-4), LENGTH at bit 42, LEVEL bit 41 set as physics nvrm_gp_entry does |
| 58 | const CHVAM ulong 18446744073709551612 |
| 59 | const CHGPB41 ulong 2199023255552 |
| 60 | note method header (nvrm_mthd, INC_METHOD): 2 << 28 |
| 61 | const CHNINC uint 536870912 |
| 62 | global chchid uint 4 |
| 63 | global chrl uint 4 |
| 64 | global chtok uint 4 |
| 65 | global chent uint 4 |
| 66 | global chpdb ulong 4 |
| 67 | global chgpva ulong 4 |
| 68 | global chupa ulong 4 |
| 69 | global chipa ulong 4 |
| 70 | global chrl0 uint 4 |
| 71 | global chrl1 uint 4 |
| 72 | global chgp ulong 4 |
| 73 | global chud ulong 4 |
| 74 | global chin ulong 4 |
| 75 | global chput uint 4 |
| 76 | global chret uint 4 |
| 77 | global chopen uchar 4 |
| 78 | global chepoch ulong 4 |
| 79 | global chrlo uint 2 |
| 80 | global chx ulong 8 |
| 81 | fn chtoken rl uint chid uint to uint |
| 82 | ret or shl and rl CHRLINV 16 and chid CHIDMAX |
| 83 | end |
| 84 | note GB20B work-submit token (kfifoGenerateWorkSubmitTokenHal_GB202): bit 30 active-high. |
| 85 | fn chtokgb20 rl uint chid uint to uint |
| 86 | ret or CHDBEN chtoken rl chid |
| 87 | end |
| 88 | note Checked GB20B work-submit token: rejects rl >= 127 (RUNLIST_ID_INVALID_RUNLIST) or chid > 4095. |
| 89 | fn chtokgb20chk rl uint chid uint to long |
| 90 | if or ge rl CHRLINV gt chid CHIDMAX |
| 91 | ret -1 |
| 92 | end |
| 93 | ret long ulong chtokgb20 rl chid |
| 94 | end |
| 95 | note Select Blackwell GPFIFO channel class: 0xca6f for GB20B, 0xc96f for GB10B. |
| 96 | fn chcls gb20b bool to ulong |
| 97 | if gb20b |
| 98 | ret CHCLSGB20 |
| 99 | end |
| 100 | ret CHCLSGB10 |
| 101 | end |
| 102 | note Override slot work-submit token (e.g. GB20B bit-30 token or GSP GET_WORK_SUBMIT_TOKEN reply). |
| 103 | fn chsettok c ulong tok uint to long |
| 104 | if ge c CHSLOTS |
| 105 | ret CHEINVAL |
| 106 | end |
| 107 | if eq get chopen c 0 |
| 108 | ret CHESTATE |
| 109 | end |
| 110 | put chtok c tok |
| 111 | ret CHOK |
| 112 | end |
| 113 | fn chrluserd pa ulong to long |
| 114 | if or ne and pa 255 0 ge pa MMPALIM |
| 115 | ret CHEINVAL |
| 116 | end |
| 117 | put chrlo 0 and uint and pa 4294967295 4294967040 |
| 118 | put chrlo 1 and uint and shr pa 32 4294967295 1048575 |
| 119 | ret CHOK |
| 120 | end |
| 121 | fn chgpent va ulong n uint to ulong |
| 122 | ret or or and va CHVAM shl and ulong n 2097151 42 CHGPB41 |
| 123 | end |
| 124 | fn chmthd subc uint mthd uint count uint to uint |
| 125 | ret or or or CHNINC shl count 16 shl subc 13 shr mthd 2 |
| 126 | end |
| 127 | fn chbindop c ulong to long |
| 128 | if eq get mmwkind 0 1 |
| 129 | note Hardware channel RPC is absent here. Refuse before publication; never walk unrelated mmwin. |
| 130 | ret -95 |
| 131 | end |
| 132 | if ne get mmmode 0 0 |
| 133 | trap 9 |
| 134 | end |
| 135 | ret gsbind get chchid c get chtok c get chent c get chgpva c get chupa c get chipa c |
| 136 | end |
| 137 | fn chdisop chid uint to long |
| 138 | if ne get mmmode 0 0 |
| 139 | trap 9 |
| 140 | end |
| 141 | ret gsdisable chid |
| 142 | end |
| 143 | fn chidlop chid uint to long |
| 144 | if ne get mmmode 0 0 |
| 145 | trap 9 |
| 146 | end |
| 147 | ret gsidle chid |
| 148 | end |
| 149 | fn chinit to void |
| 150 | if and eq get mmwkind 0 1 not mmwmutable |
| 151 | trap 9 |
| 152 | end |
| 153 | for c ulong CHSLOTS |
| 154 | put chopen c 0 |
| 155 | put chgp c GMNONE |
| 156 | put chud c GMNONE |
| 157 | put chin c GMNONE |
| 158 | put chput c 0 |
| 159 | put chret c 0 |
| 160 | end |
| 161 | end |
| 162 | note create's error path: free whichever of the three buffers exist, ENOMEM |
| 163 | fn chnomem c ulong to long |
| 164 | if ne get chin c GMNONE |
| 165 | r = gmfree get chin c |
| 166 | end |
| 167 | if ne get chud c GMNONE |
| 168 | r = gmfree get chud c |
| 169 | end |
| 170 | if ne get chgp c GMNONE |
| 171 | r = gmfree get chgp c |
| 172 | end |
| 173 | put chgp c GMNONE |
| 174 | put chud c GMNONE |
| 175 | put chin c GMNONE |
| 176 | ret CHENOMEM |
| 177 | end |
| 178 | note Separate backing preparation from RPC allocation/scheduling; state2 cannot submit. |
| 179 | fn chcreate c ulong chid uint rl uint ent uint to long |
| 180 | r = chprep c chid rl ent |
| 181 | if ne r CHOK |
| 182 | ret r |
| 183 | end |
| 184 | if ne chbindop c 0 |
| 185 | r = gmfree get chin c |
| 186 | r = gmfree get chud c |
| 187 | r = gmfree get chgp c |
| 188 | put chopen c 0 |
| 189 | ret CHEBIND |
| 190 | end |
| 191 | put chopen c 1 |
| 192 | ret CHOK |
| 193 | end |
| 194 | fn chprep c ulong chid uint rl uint ent uint to long |
| 195 | if or not mmwmutable ne get gmepoch 0 get mmwgen 0 |
| 196 | ret CHESTATE |
| 197 | end |
| 198 | if ge c CHSLOTS |
| 199 | ret CHEINVAL |
| 200 | end |
| 201 | if or gt chid CHIDMAX ge rl CHRLINV |
| 202 | ret CHEINVAL |
| 203 | end |
| 204 | if or or lt ent 2 gt ent 65536 ne and ent sub ent 1 0 |
| 205 | ret CHEINVAL |
| 206 | end |
| 207 | put chopen c 0 |
| 208 | put chepoch c get mmwgen 0 |
| 209 | put chput c 0 |
| 210 | put chret c 0 |
| 211 | put chgp c GMNONE |
| 212 | put chud c GMNONE |
| 213 | put chin c GMNONE |
| 214 | if ne gmalloc mul ulong ent 8 1 GMGPURO GMOK |
| 215 | ret chnomem c |
| 216 | end |
| 217 | put chgp c get gmout 0 |
| 218 | if ne gmalloc CHUDSZ 1 GMHPOLL GMOK |
| 219 | ret chnomem c |
| 220 | end |
| 221 | put chud c get gmout 0 |
| 222 | if ne gmalloc CHINSZ 1 0 GMOK |
| 223 | ret chnomem c |
| 224 | end |
| 225 | put chin c get gmout 0 |
| 226 | ret chfill c chid rl ent |
| 227 | end |
| 228 | note zero gpfifo, USERD and instance block; write the RAMIN page-directory words |
| 229 | fn chzero c ulong to ulong |
| 230 | g = get chgp c |
| 231 | u = get chud c |
| 232 | n = get chin c |
| 233 | mmzero get gmbpa g get gmbsz g |
| 234 | mmzero get gmbpa u get gmbsz u |
| 235 | mmzero get gmbpa n get gmbsz n |
| 236 | pdb = get mmroot 0 |
| 237 | ip = get gmbpa n |
| 238 | mmwr32 add ip CHPDBW ulong or or and uint and pdb 4294967295 CHLOM CHBIG64 CHTSYS |
| 239 | mmwr32 add ip add CHPDBW 4 shr pdb 32 |
| 240 | ret pdb |
| 241 | end |
| 242 | note fill the descriptor, the runlist entry, then bind through the channel ops |
| 243 | fn chfill c ulong chid uint rl uint ent uint to long |
| 244 | pdb = chzero c |
| 245 | put chchid c chid |
| 246 | put chrl c rl |
| 247 | put chtok c chtoken rl chid |
| 248 | put chent c ent |
| 249 | put chpdb c pdb |
| 250 | put chgpva c get gmbva get chgp c |
| 251 | put chupa c get gmbpa get chud c |
| 252 | put chipa c get gmbpa get chin c |
| 253 | if ne chrluserd get gmbpa get chud c CHOK |
| 254 | ret chnomem c |
| 255 | end |
| 256 | put chrl0 c get chrlo 0 |
| 257 | put chrl1 c get chrlo 1 |
| 258 | put chopen c 2 |
| 259 | ret CHOK |
| 260 | end |
| 261 | fn chsubmit c ulong pb ulong off uint n uint to long |
| 262 | if or ge c CHSLOTS ne get chopen c 1 |
| 263 | ret CHESTATE |
| 264 | end |
| 265 | if ne get chepoch c get mmwgen 0 |
| 266 | ret CHESTATE |
| 267 | end |
| 268 | if not gmlive pb |
| 269 | ret CHEINVAL |
| 270 | end |
| 271 | if or or ne and off 3 0 eq n 0 gt n CHLENMX |
| 272 | ret CHEINVAL |
| 273 | end |
| 274 | span = mul ulong n 4 |
| 275 | sz = get gmbsz pb |
| 276 | if or gt ulong off sz gt span sub sz ulong off |
| 277 | ret CHEINVAL |
| 278 | end |
| 279 | va = add get gmbva pb ulong off |
| 280 | if or ge va CHVALIM gt span sub CHVALIM va |
| 281 | ret CHEINVAL |
| 282 | end |
| 283 | ret chpush c va n |
| 284 | end |
| 285 | note ring full check, GP entry at put % entries, GPPut in USERD, doorbell |
| 286 | note Strict ARM64 Normal-NC ordering: PB/QMD -> DSB SY -> GPFIFO -> DSB SY -> USERD.GPPut -> DSB SY -> BAR0 0x00BB0090. |
| 287 | fn chpush c ulong va ulong n uint to long |
| 288 | p = get chput c |
| 289 | e = get chent c |
| 290 | if ge sub p get chret c sub e 1 |
| 291 | ret CHEFULL |
| 292 | end |
| 293 | cpufence |
| 294 | mmwr64 add get gmbpa get chgp c mul ulong rem p e 8 chgpent va n |
| 295 | cpufence |
| 296 | p = add p 1 |
| 297 | put chput c p |
| 298 | mmwr32 add get gmbpa get chud c CHGPPUT ulong rem p e |
| 299 | cpufence |
| 300 | mmregwr CHDBELL ulong get chtok c |
| 301 | ret CHOK |
| 302 | end |
| 303 | fn chretire c ulong upto uint to void |
| 304 | if ge c CHSLOTS |
| 305 | ret |
| 306 | end |
| 307 | if ne get chepoch c get mmwgen 0 |
| 308 | ret |
| 309 | end |
| 310 | r = get chret c |
| 311 | if le sub upto r sub get chput c r |
| 312 | put chret c upto |
| 313 | end |
| 314 | end |
| 315 | fn chteardown c ulong budget uint nx ulong to long |
| 316 | if or ge c CHSLOTS eq get chopen c 0 |
| 317 | ret CHESTATE |
| 318 | end |
| 319 | if ne get chepoch c get mmwgen 0 |
| 320 | ret CHESTATE |
| 321 | end |
| 322 | put chopen c 0 |
| 323 | chid = get chchid c |
| 324 | d = chdisop chid |
| 325 | idle = false |
| 326 | for i uint budget |
| 327 | if and eq d 0 eq chidlop chid 1 |
| 328 | idle = true |
| 329 | break |
| 330 | end |
| 331 | end |
| 332 | if and not idle eq get mmwkind 0 1 |
| 333 | r = mmwquar get mmwgen 0 |
| 334 | end |
| 335 | rc = CHOK |
| 336 | for k ulong 3 |
| 337 | b = get chin c |
| 338 | if eq k 1 |
| 339 | b = get chud c |
| 340 | end |
| 341 | if eq k 2 |
| 342 | b = get chgp c |
| 343 | end |
| 344 | if not idle |
| 345 | r = gmquar b |
| 346 | else |
| 347 | if ne gmfree b GMOK |
| 348 | rc = CHETIME |
| 349 | end |
| 350 | end |
| 351 | end |
| 352 | if not idle |
| 353 | for i ulong nx |
| 354 | b = get chx i |
| 355 | if gmlive b |
| 356 | r = gmquar b |
| 357 | end |
| 358 | end |
| 359 | rc = CHETIME |
| 360 | end |
| 361 | put chgp c GMNONE |
| 362 | put chud c GMNONE |
| 363 | put chin c GMNONE |
| 364 | ret rc |
| 365 | end |