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 / gsp / compute.om

758 lines · 17607 bytes Raw Blame History
1note gsp/compute.om: GB20B compute bring-up RPC builders, canonical PACKED_REGISTRY_TABLE encoder,
2note BLACKWELL_COMPUTE_B (0xcec0) / BLACKWELL_CHANNEL_GPFIFO_B (0xca6f) / usermode alloc & control
3note helpers, GB20B bit-30 work-submit token encoder, INC_METHOD pushbuffer builder, GP entry
4note encoder (LEVEL bit 41), USERD GPPut (+0x8C) update and BAR0/usermode doorbell kick helpers.
5note Source of record: NVIDIA open-gpu-kernel-modules 580.173.02 (MIT), docs/gpu/GSP_RPC_REGISTRY_REFERENCE.md,
6note and gb20b-gsp-boot.md. Pure seeded OM-1 library unit, no fn main. Prefix cb / CB.
7note Compose after: chan/gmmu.om, chan/mem.om, chan/chan.om, gsp/rpc.om, chan/params.om, gsp/boot.om.
8const CBOK long 0
9const CBEINVAL long -1
10const CBERANGE long -2
11const CBEFULL long -3
12const CBEALIGN long -4
13const CBESTATE long -5
14const CBU32MAX ulong 4294967295
15note RPC function IDs: SET_GUEST_SYSTEM_INFO 1, GET_GSP_STATIC_INFO 65, SET_REGISTRY 73,
16note GSP_RM_CONTROL 76, GSP_RM_ALLOC 103
17const CBRPCGUEST ulong 1
18const CBRPCSTAT ulong 65
19const CBRPCREG ulong 73
20const CBRPCCTRL ulong 76
21const CBRPCALLOC ulong 103
22note SET_GUEST_SYSTEM_INFO v03_00: 6*4 + 3*256 = 792 bytes
23const CBSZGUEST ulong 792
24const CBGSTRMAX ulong 256
25note PACKED_REGISTRY_TABLE bounds matching arena.om arregok and boot.om breg
26const CBREGMIN ulong 8
27const CBREGMAX ulong 65456
28const CBRMAX ulong 32
29const CBRDWORD uchar 1
30const CBRBIN uchar 2
31const CBRSTR uchar 3
32note GB20B bring-up registry key selectors
33const CBKFW ulong 0
34const CBKLOG ulong 1
35const CBKRC ulong 2
36const CBKDPM ulong 3
37const CBKPW2 ulong 4
38const CBKDISP ulong 5
39note RM class IDs: BLACKWELL_CHANNEL_GPFIFO_A 0xc96f, BLACKWELL_CHANNEL_GPFIFO_B 0xca6f,
40note BLACKWELL_COMPUTE_B 0xcec0, VOLTA_USERMODE_A 0xc361, HOPPER_USERMODE_A 0xc661
41const CBCGPFA ulong 51567
42const CBCGPFB ulong 51823
43const CBCCMP ulong 52928
44const CBCUMVA ulong 50017
45const CBCUMHA ulong 50785
46note RM control IDs: NVA06C_CTRL_CMD_GPFIFO_SCHEDULE 0xa06c0101,
47note NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN 0xc36f0108
48const CBCMSCH ulong 2691432705
49const CBCMWST ulong 3278831880
50note GB20B work-submit token bit 30 RUNLIST_DOORBELL_ENABLE (0x40000000, active-high),
51note runlist <= 127 (0x7F), chId <= 4095 (0xFFF), BAR0 doorbell 0x00BB0090, usermode +0x90, USERD GPPut +0x8C
52const CBDBEN uint 1073741824
53const CBRLMAX uint 127
54const CBCHMAX uint 4095
55const CBDBBAR0 ulong 12255376
56const CBDBUM ulong 144
57const CBGPPUT ulong 140
58note Pushbuffer INC_METHOD (2<<28), compute setup 0x0184 + 18-word bare-metal windows,
59note PCAS 0x02b4/0x02c0, WFI 0x0110, INVALIDATE_SHADER_CACHES 0x021c, inline QMD 0x0318 (96 dwords),
60note L2 flush 0x0028 (0x80000000), host semaphore 0x005C..0x006C (release 1 / WFI 0x100001).
61note Bare-metal disjoint 40-bit shader windows: shared 0x70_0000_0000 (hi 112), local 0x72_0000_0000 (hi 114).
62const CBINC uint 536870912
63const CBMSETUP uint 388
64const CBMSET1 uint 584
65const CBMSET2 uint 672
66const CBMSHWIN uint 5492
67const CBMSHLO uint 5496
68const CBMSHSZ uint 5500
69const CBMLMWIN uint 5468
70const CBMLMLO uint 5472
71const CBMLMSZ uint 5476
72const CBSHWINHI uint 112
73const CBLMWINHI uint 114
74const CBSHSZ uint 1048575
75const CBMWFI uint 272
76const CBMICACHE uint 540
77const CBICALL uint 4113
78const CBICDATA uint 16
79const CBICFLUSH uint 20
80const CBMPCASA uint 692
81const CBMPCASB uint 704
82const CBPCASACT uint 3
83const CBMIQMD uint 792
84const CBML2 uint 40
85const CBMSEM uint 92
86const CBQMDWDS ulong 96
87const CBSEMREL uint 1
88const CBSEMWFI uint 1048577
89const CBL2EXEC uint 2147483648
90const CBPBMAX ulong 16384
91note GP entry: va & ~3, LEVEL bit 41 (1<<41), VA limit 1<<40, length max 0x1FFFFF
92const CBVAMASK ulong 18446744073709551612
93const CBGPB41 ulong 2199023255552
94const CBVALIM ulong 1099511627776
95const CBLENMAX uint 2097151
96note Built-in GB20B bring-up registry key names (GSP_RPC_REGISTRY_REFERENCE.md 3.2)
97text CBKEY0 "EnableGpuFirmware"
98text CBKEY1 "RMGspLogLevel"
99text CBKEY2 "RmLogonRC"
100text CBKEY3 "RMEnableDynamicPowerManagement"
101text CBKEY4 "RMPowerFeature2"
102text CBKEY5 "RMDisableDisplay"
103note Guest string staging buffer for SET_GUEST_SYSTEM_INFO (max 256 bytes per slot)
104global cbstrin uchar 256
105note PACKED_REGISTRY_TABLE staging arrays
106global cbrcnt ulong 1
107global cbrtyp uchar 32
108global cbrval ulong 32
109global cbrvlen ulong 32
110global cbrnoff ulong 32
111global cbrnlen ulong 32
112global cbrnames uchar 2048
113global cbrnpos ulong 1
114global cbrblob uchar 4096
115global cbrbpos ulong 1
116global cbrkin uchar 128
117global cbrvin uchar 512
118note Pushbuffer (64 KiB = 16384 dwords) and inline QMD staging arrays
119global cbpb uint 16384
120global cbpblen ulong 1
121global cbqmd uint 96
122
123note Section 1: SET_GUEST_SYSTEM_INFO (RPC 1, 792 B) and GET_GSP_STATIC_INFO (RPC 65)
124note Initialize rpc_set_guest_system_info_v03_00 (792 B) in rqpay with caller-supplied version fields.
125fn cbguestinit ver0 ulong ver1 ulong guestos ulong to long
126if or gt ver0 CBU32MAX or gt ver1 CBU32MAX gt guestos CBU32MAX
127ret CBERANGE
128end
129rqclear CBSZGUEST
130rqput32 4 0 ver0
131rqput32 4 4 ver1
132rqput32 4 20 guestos
133ret long CBSZGUEST
134end
135
136note Populate one of the three 256-byte string buffers of rpc_set_guest_system_info_v03_00 from cbstrin:
137note slot 0 = driverVersionBuffer (+24, len at +8), slot 1 = driverBranchBuffer (+280, len at +12),
138note slot 2 = guestOsInfoBuffer (+536, len at +16). Never fabricates version strings.
139fn cbgueststr slot ulong n ulong to long
140if or gt slot 2 gt n CBGSTRMAX
141ret CBERANGE
142end
143rqput32 4 add 8 mul slot 4 n
144base = add 24 mul slot CBGSTRMAX
145for i ulong CBGSTRMAX
146put rqpay add base i 0
147end
148for i ulong n
149put rqpay add base i get cbstrin i
150end
151ret CBOK
152end
153
154note Prepare zeroed request payload for GET_GSP_STATIC_INFO (RPC 65) in rqpay.
155fn cbstaticprep n ulong to long
156if or eq n 0 gt n CBREGMAX
157ret CBERANGE
158end
159rqclear n
160ret long n
161end
162
163note Read a little-endian u32 at byte offset off from GET_GSP_STATIC_INFO reply payload in rqout.
164fn cbstaticrd32 off ulong to long
165n = get rqlen 0
166if or gt off n gt 4 sub n off
167ret CBERANGE
168end
169ret long rqget32 3 add 80 off
170end
171
172note Section 2: SET_REGISTRY (RPC 73) canonical PACKED_REGISTRY_TABLE builder
173fn cbrinit to void
174put cbrcnt 0 0
175put cbrnpos 0 0
176put cbrbpos 0 0
177end
178
179fn cbrchkname nlen ulong to bool
180if or eq nlen 0 gt nlen 128
181ret false
182end
183for i ulong nlen
184if eq get cbrkin i 0
185ret false
186end
187end
188ret true
189end
190
191fn cbradddw nlen ulong val ulong to long
192if not cbrchkname nlen
193ret CBEINVAL
194end
195if gt val CBU32MAX
196ret CBERANGE
197end
198cnt = get cbrcnt 0
199if ge cnt CBRMAX
200ret CBEFULL
201end
202npos = get cbrnpos 0
203if gt add npos nlen 2048
204ret CBEFULL
205end
206for i ulong nlen
207put cbrnames add npos i get cbrkin i
208end
209put cbrtyp cnt CBRDWORD
210put cbrval cnt val
211put cbrvlen cnt 4
212put cbrnoff cnt npos
213put cbrnlen cnt nlen
214put cbrnpos 0 add npos nlen
215put cbrcnt 0 add cnt 1
216ret CBOK
217end
218
219fn cbraddblob typ uchar nlen ulong vlen ulong to long
220if not cbrchkname nlen
221ret CBEINVAL
222end
223if or eq vlen 0 gt vlen 512
224ret CBERANGE
225end
226cnt = get cbrcnt 0
227if ge cnt CBRMAX
228ret CBEFULL
229end
230npos = get cbrnpos 0
231if gt add npos nlen 2048
232ret CBEFULL
233end
234bpos = get cbrbpos 0
235if gt add bpos vlen 4096
236ret CBEFULL
237end
238for i ulong nlen
239put cbrnames add npos i get cbrkin i
240end
241for i ulong vlen
242put cbrblob add bpos i get cbrvin i
243end
244put cbrtyp cnt typ
245put cbrval cnt bpos
246put cbrvlen cnt vlen
247put cbrnoff cnt npos
248put cbrnlen cnt nlen
249put cbrnpos 0 add npos nlen
250put cbrbpos 0 add bpos vlen
251put cbrcnt 0 add cnt 1
252ret CBOK
253end
254
255fn cbraddbin nlen ulong vlen ulong to long
256ret cbraddblob CBRBIN nlen vlen
257end
258
259fn cbraddstr nlen ulong vlen ulong to long
260ret cbraddblob CBRSTR nlen vlen
261end
262
263fn cbrloadkey kid ulong to long
264if eq kid CBKFW
265for i ulong len CBKEY0
266put cbrkin i get CBKEY0 i
267end
268ret long len CBKEY0
269end
270if eq kid CBKLOG
271for i ulong len CBKEY1
272put cbrkin i get CBKEY1 i
273end
274ret long len CBKEY1
275end
276if eq kid CBKRC
277for i ulong len CBKEY2
278put cbrkin i get CBKEY2 i
279end
280ret long len CBKEY2
281end
282if eq kid CBKDPM
283for i ulong len CBKEY3
284put cbrkin i get CBKEY3 i
285end
286ret long len CBKEY3
287end
288if eq kid CBKPW2
289for i ulong len CBKEY4
290put cbrkin i get CBKEY4 i
291end
292ret long len CBKEY4
293end
294if eq kid CBKDISP
295for i ulong len CBKEY5
296put cbrkin i get CBKEY5 i
297end
298ret long len CBKEY5
299end
300ret CBERANGE
301end
302
303fn cbraddkey kid ulong val ulong to long
304nlen = cbrloadkey kid
305if lt nlen 0
306ret nlen
307end
308ret cbradddw ulong nlen val
309end
310
311note Populate the 6 proposed GB20B compute bring-up registry DWORD keys.
312fn cbrgb20b loglvl ulong nodisp ulong to long
313if or gt loglvl 5 gt nodisp 1
314ret CBERANGE
315end
316r = cbraddkey CBKFW 1
317if lt r 0
318ret r
319end
320r = cbraddkey CBKLOG loglvl
321if lt r 0
322ret r
323end
324r = cbraddkey CBKRC 1
325if lt r 0
326ret r
327end
328r = cbraddkey CBKDPM 0
329if lt r 0
330ret r
331end
332r = cbraddkey CBKPW2 0
333if lt r 0
334ret r
335end
336ret cbraddkey CBKDISP nodisp
337end
338
339note Serialize one staged entry i at byte cursor pos into rqpay; returns next pos or negative error.
340fn cbrputent i ulong pos ulong to long
341e = add 8 mul i 16
342t = get cbrtyp i
343nlen = get cbrnlen i
344noff = get cbrnoff i
345if gt add pos add nlen 1 CBREGMAX
346ret CBEFULL
347end
348rqput32 4 e pos
349put rqpay add e 4 t
350put rqpay add e 5 0
351put rqpay add e 6 0
352put rqpay add e 7 0
353for j ulong nlen
354put rqpay add pos j get cbrnames add noff j
355end
356put rqpay add pos nlen 0
357pos = add pos add nlen 1
358if eq t CBRDWORD
359rqput32 4 add e 8 get cbrval i
360rqput32 4 add e 12 4
361ret long pos
362end
363vlen = get cbrvlen i
364voff = get cbrval i
365if gt add pos vlen CBREGMAX
366ret CBEFULL
367end
368rqput32 4 add e 8 pos
369rqput32 4 add e 12 vlen
370for j ulong vlen
371b = get cbrblob add voff j
372put rqpay add pos j b
373end
374ret long add pos vlen
375end
376
377note Build canonical PACKED_REGISTRY_TABLE into rqpay; returns wire byte length (0 if empty) for breg.
378fn cbrbuild to long
379cnt = get cbrcnt 0
380if eq cnt 0
381ret 0
382end
383pos = add 8 mul cnt 16
384rqclear pos
385rqput32 4 4 cnt
386for i ulong cnt
387next = cbrputent i pos
388if lt next 0
389ret next
390end
391pos = ulong next
392end
393rqput32 4 0 pos
394ret long pos
395end
396
397note Section 3: Compute bring-up RM alloc (RPC 103) and RM control (RPC 76) helpers
398note Allocate Blackwell GPFIFO channel (0xca6f for GB20B, 0xc96f for GB10B) using pain[].
399fn cballocch cli ulong parent ulong hchan ulong gb20b bool budget ulong to long
400r = pachan
401if lt r 0
402ret r
403end
404ret brmalloc cli parent hchan pacgpcls gb20b budget
405end
406
407note Allocate BLACKWELL_COMPUTE_B (0xcec0 / 52928) with 0-byte params under hchan.
408fn cballoccmp cli ulong hchan ulong hcmp ulong budget ulong to long
409r = paempty
410if lt r 0
411ret r
412end
413ret brmalloc cli hchan hcmp CBCCMP budget
414end
415
416note Allocate VOLTA_USERMODE_A (0xc361) or HOPPER_USERMODE_A (0xc661) with 0-byte params under hsub.
417fn cballocum cli ulong hsub ulong hum ulong cls ulong budget ulong to long
418if and ne cls CBCUMVA ne cls CBCUMHA
419ret CBEINVAL
420end
421r = paempty
422if lt r 0
423ret r
424end
425ret brmalloc cli hsub hum cls budget
426end
427
428note Issue NVA06C_CTRL_CMD_GPFIFO_SCHEDULE (0xa06c0101, 3 B) on TSG handle htsg.
429fn cbsched cli ulong htsg ulong en ulong skipsub ulong skipen ulong budget ulong to long
430r = pasched en skipsub skipen
431if lt r 0
432ret r
433end
434ret brmctrl cli htsg CBCMSCH budget
435end
436
437note Issue NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN (0xc36f0108, 4 B) on channel handle hchan.
438fn cbgetwst cli ulong hchan ulong budget ulong to long
439r = pawstreq
440if lt r 0
441ret r
442end
443ret brmctrl cli hchan CBCMWST budget
444end
445
446note Decode u32 workSubmitToken from the completed GET_WORK_SUBMIT_TOKEN reply in pareply.
447fn cbdecwst to long
448ret pawstdec get bplen 0
449end
450
451note Section 4: GB20B work-submit token encoder and doorbell write helpers
452note kfifoGenerateWorkSubmitTokenHal_GB202: 0x40000000 | ((runlist & 0x7F) << 16) | (chId & 0xFFF).
453note Rejects rl >= 127 (0x7F is RUNLIST_ID_INVALID_RUNLIST, gb100/dev_vm.h:625).
454fn cbtoken rl uint chid uint to long
455if or ge rl CBRLMAX gt chid CBCHMAX
456ret CBERANGE
457end
458tok = or CBDBEN or shl rl 16 chid
459ret long ulong tok
460end
461
462note Direct BAR0 VF doorbell write (0x00BB0090) ordered after prior GP entry and USERD GPPut writes.
463fn cbdbbar0 tok uint to void
464cpufence
465mmregwr CBDBBAR0 ulong tok
466end
467
468note Mapped usermode page doorbell write (umbase + 0x90) ordered after prior GP/USERD writes.
469fn cbdbum umbase ulong tok uint to long
470if ne and umbase 3 0
471ret CBEALIGN
472end
473if or ge umbase MMPALIM gt CBDBUM sub MMPALIM umbase
474ret CBERANGE
475end
476cpufence
477mmwr32 add umbase CBDBUM ulong tok
478ret CBOK
479end
480
481note Section 5: INC_METHOD pushbuffer builder, GP entry encoder, USERD GPPut and kick helpers
482fn cbmthd subc uint mthd uint count uint to uint
483ret or or or CBINC shl and count 4095 16 shl and subc 7 13 and shr mthd 2 8191
484end
485
486fn cbpbinit to void
487put cbpblen 0 0
488end
489
490fn cbpbemit w uint to long
491n = get cbpblen 0
492if ge n CBPBMAX
493ret CBEFULL
494end
495put cbpb n w
496put cbpblen 0 add n 1
497ret CBOK
498end
499
500note Compute setup (subc 1, method 0x0184, count 1, class 0xcec0).
501fn cbpbsetup cls uint to long
502n = get cbpblen 0
503if gt add n 2 CBPBMAX
504ret CBEFULL
505end
506r = cbpbemit cbmthd 1 CBMSETUP 1
507if lt r 0
508ret r
509end
510ret cbpbemit cls
511end
512
513note Emit one single-word INC_METHOD pair (subc 1) into cbpb; caller checks capacity.
514fn cbpbpair mthd uint val uint to long
515r = cbpbemit cbmthd 1 mthd 1
516if lt r 0
517ret r
518end
519ret cbpbemit val
520end
521
522note Full 18-word bare-metal compute setup with disjoint 40-bit shader windows:
523note shared=0x70_0000_0000 (size 0x000fffff), local=0x72_0000_0000 (size 0).
524fn cbpbsetup18 cls uint to long
525n = get cbpblen 0
526if gt add n 18 CBPBMAX
527ret CBEFULL
528end
529r = cbpbpair CBMSETUP cls
530if lt r 0
531ret r
532end
533r = cbpbpair CBMSET1 1
534if lt r 0
535ret r
536end
537r = cbpbpair CBMSET2 15
538if lt r 0
539ret r
540end
541r = cbpbpair CBMSHWIN CBSHWINHI
542if lt r 0
543ret r
544end
545r = cbpbpair CBMSHLO 0
546if lt r 0
547ret r
548end
549r = cbpbpair CBMSHSZ CBSHSZ
550if lt r 0
551ret r
552end
553r = cbpbpair CBMLMWIN CBLMWINHI
554if lt r 0
555ret r
556end
557r = cbpbpair CBMLMLO 0
558if lt r 0
559ret r
560end
561ret cbpbpair CBMLMSZ 0
562end
563
564note Inter-launch WAIT_FOR_IDLE (subc 1, method 0x0110, payload 0; 2 words).
565fn cbpbwfi to long
566n = get cbpblen 0
567if gt add n 2 CBPBMAX
568ret CBEFULL
569end
570ret cbpbpair CBMWFI 0
571end
572
573note Inter-launch INVALIDATE_SHADER_CACHES (subc 1, method 0x021c, flags e.g. CBICALL 0x1011; 2 words).
574fn cbpbicache flags uint to long
575n = get cbpblen 0
576if gt add n 2 CBPBMAX
577ret CBEFULL
578end
579ret cbpbpair CBMICACHE flags
580end
581
582note PCAS QMD dispatch (subc 1, SEND_PCAS_A 0x02b4 + SEND_SIGNALING_PCAS2_B 0x02c0 action 3; 4 words).
583fn cbpbpcas qmdva ulong to long
584if or eq qmdva 0 ge qmdva CBVALIM
585ret CBERANGE
586end
587if ne and qmdva 255 0
588ret CBEALIGN
589end
590n = get cbpblen 0
591if gt add n 4 CBPBMAX
592ret CBEFULL
593end
594r = cbpbpair CBMPCASA uint and shr qmdva 8 CBU32MAX
595if lt r 0
596ret r
597end
598ret cbpbpair CBMPCASB CBPCASACT
599end
600
601note Inline 384-byte (96-dword) QMD launch packet (subc 1, method 0x0318, count 98).
602fn cbpbiqmd qmdva ulong to long
603if ne and qmdva 255 0
604ret CBEALIGN
605end
606if ge qmdva CBVALIM
607ret CBERANGE
608end
609n = get cbpblen 0
610if gt add n 99 CBPBMAX
611ret CBEFULL
612end
613r = cbpbemit cbmthd 1 CBMIQMD 98
614if lt r 0
615ret r
616end
617r = cbpbemit or CBDBEN uint and shr qmdva 40 511
618if lt r 0
619ret r
620end
621r = cbpbemit uint and shr qmdva 8 CBU32MAX
622if lt r 0
623ret r
624end
625for i ulong CBQMDWDS
626r = cbpbemit get cbqmd i
627if lt r 0
628ret r
629end
630end
631ret CBOK
632end
633
634note GPU L2 cache flush before CPU readback (subc 0, method 0x0028, count 4).
635fn cbpbl2flush to long
636n = get cbpblen 0
637if gt add n 5 CBPBMAX
638ret CBEFULL
639end
640r = cbpbemit cbmthd 0 CBML2 4
641if lt r 0
642ret r
643end
644r = cbpbemit 0
645if lt r 0
646ret r
647end
648r = cbpbemit 0
649if lt r 0
650ret r
651end
652r = cbpbemit 0
653if lt r 0
654ret r
655end
656ret cbpbemit CBL2EXEC
657end
658
659note Host semaphore release (subc 0, methods 0x005C..0x006C, count 5; wfi=true uses 0x100001).
660fn cbpbsem va ulong val uint wfi bool to long
661if ne and va 3 0
662ret CBEALIGN
663end
664if or eq va 0 ge va CBVALIM
665ret CBERANGE
666end
667n = get cbpblen 0
668if gt add n 6 CBPBMAX
669ret CBEFULL
670end
671r = cbpbemit cbmthd 0 CBMSEM 5
672if lt r 0
673ret r
674end
675r = cbpbemit uint and va CBU32MAX
676if lt r 0
677ret r
678end
679r = cbpbemit uint and shr va 32 33554431
680if lt r 0
681ret r
682end
683r = cbpbemit val
684if lt r 0
685ret r
686end
687r = cbpbemit 0
688if lt r 0
689ret r
690end
691exec = CBSEMREL
692if wfi
693exec = CBSEMWFI
694end
695ret cbpbemit exec
696end
697
698note Flush staged pushbuffer words to physical/window address pbpa via mmwr32, then DSB SY.
699fn cbpbflush pbpa ulong to long
700if ne and pbpa 3 0
701ret CBEALIGN
702end
703n = get cbpblen 0
704if eq n 0
705ret CBEINVAL
706end
707for i ulong n
708mmwr32 add pbpa mul i 4 ulong get cbpb i
709end
710cpufence
711ret long n
712end
713
714note GP entry encoder with LEVEL bit 41 set: (va & ~3) | ((words & 0x1FFFFF) << 42) | (1 << 41).
715fn cbgpent va ulong words uint to ulong
716ret or or and va CBVAMASK shl and ulong words ulong CBLENMAX 42 CBGPB41
717end
718
719fn cbgpentok va ulong words uint to bool
720ret and eq and va 3 0 and lt va CBVALIM and gt words 0 le words CBLENMAX
721end
722
723note Write 64-bit GP entry to GPFIFO ring at (gpput % ent) * 8, then DSB SY; returns nextput = (gpput + 1) % ent.
724fn cbwrgp gppa ulong gpput uint ent uint gpent ulong to long
725if or or lt ent 2 gt ent 65536 ne and ent sub ent 1 0
726ret CBEINVAL
727end
728if ne and gppa 7 0
729ret CBEALIGN
730end
731idx = ulong rem gpput ent
732mmwr64 add gppa mul idx 8 gpent
733cpufence
734ret long ulong rem add gpput 1 ent
735end
736
737note Update USERD GPPut (+0x8C / 140), then DSB SY before doorbell.
738fn cbwruserd userdpa ulong nextput uint to void
739mmwr32 add userdpa CBGPPUT ulong nextput
740cpufence
741end
742
743note Full BAR0 kick sequence: write GP entry, advance USERD GPPut, fence, write BAR0 0xBB0090 doorbell.
744fn cbkickbar0 gppa ulong userdpa ulong gpent ulong gpput uint ent uint tok uint to long
745next = cbwrgp gppa gpput ent gpent
746if lt next 0
747ret next
748end
749cbwruserd userdpa uint next
750cbdbbar0 tok
751ret next
752end
753
754note Usermode doorbell kick after GP entry write: advance USERD GPPut, fence, write umbase + 0x90.
755fn cbkickum userdpa ulong umbase ulong nextput uint tok uint to long
756cbwruserd userdpa nextput
757ret cbdbum umbase tok
758end