interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / gpu / driver / chan / chan.om

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