interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / chan / mem.om

264 lines · 6417 bytes Raw Blame History
1note chan/mem.om: GPU buffer allocator over chan/gmmu.om, ported from aienux PR #441
2note native/kernel/gpu/gpu_mem.c and gpu_mem.h (branch pr441 @ c5bb3a4b). Library unit: no main.
3note Prefix gm / GM. Needs chan/gmmu.om before it.
4note
5note A buffer is a slot index 0..63 (the C hands out struct pointers into the same 64-slot table);
6note GMNONE (64) is "no buffer". Any index past 63 is a foreign pointer and is refused like one.
7note The allocator serves the address space of chan/gmmu.om (one per program; the C takes it as an arg).
8note
9note API (state in globals; a global is never an argument):
10note gminit poll pab pages vab -> long ck_gmem_init: pages 4 KiB pages at bus address pab (inside the
11note window, clear of the page-table pool), mapped at GPU VA vab
12note gmalloc size cache flags -> long ck_gmem_alloc; the new slot in gmout (GMNONE on refusal)
13note gmfree b / gmquar b -> long ck_gmem_free / ck_gmem_quarantine
14note gmcount st -> ulong ck_gmem_count (pages in state st)
15note gmlive b -> bool slot b is a buffer in state LIVE
16note per slot: gmbpa (IOVA; CPU access goes through mmrd/mmwr handle+offset), gmbva (GPU VA), gmbsz,
17note gmbca, gmbfl, gmbst, gmbgen (uint, wraps as the C uint32)
18note codes (enum ck_gmem_status): 0 GMOK, 1 GMEINVAL, 2 GMENOMEM, 3 GMESTATE, 4 GMEMAP, 5 GMETIME
19note cache: 0 cached, 1 uncached; flags: 1 host polled (needs uncached), 2 GPU read-only
20note states: 0 free, 1 live, 2 quarantined (never handed out again)
21const GMOK long 0
22const GMEINVAL long 1
23const GMENOMEM long 2
24const GMESTATE long 3
25const GMEMAP long 4
26const GMETIME long 5
27const GMFREE uchar 0
28const GMLIVE uchar 1
29const GMQUAR uchar 2
30const GMHPOLL ulong 1
31const GMGPURO ulong 2
32const GMMAXB ulong 64
33const GMMAXP ulong 1024
34const GMNONE ulong 64
35global gmpoll ulong 1
36global gmpab ulong 1
37global gmvab ulong 1
38global gmpages ulong 1
39global gmpgst uchar 1024
40global gmbpa ulong 64
41global gmbva ulong 64
42global gmbsz ulong 64
43global gmbca ulong 64
44global gmbfl ulong 64
45global gmbst uchar 64
46global gmbgen uint 64
47global gmout ulong 1
48global gmepoch ulong 1
49fn gminit poll ulong pab ulong pages ulong vab ulong to long
50if or not mmwmutable ne get mmasep 0 get mmwgen 0
51ret GMESTATE
52end
53if and eq get mmwkind 0 1 eq get gmepoch 0 get mmwgen 0
54ret GMESTATE
55end
56if or eq get mmptn 0 0 eq get mmws 0 0
57ret GMEINVAL
58end
59if or or or eq pages 0 gt pages GMMAXP ne and pab 4095 0 ne and vab sub MMSZ2M 1 0
60ret GMEINVAL
61end
62if eq poll 0
63ret GMEINVAL
64end
65if ne gmrange pab mul pages 4096 vab GMOK
66ret GMEINVAL
67end
68put gmpoll 0 poll
69put gmpab 0 pab
70put gmvab 0 vab
71put gmpages 0 pages
72put gmepoch 0 get mmwgen 0
73for i ulong GMMAXP
74put gmpgst i GMFREE
75end
76for i ulong GMMAXB
77put gmbst i GMFREE
78put gmbgen i 0
79end
80ret GMOK
81end
82note gminit's range checks: n bytes at pab inside the window, at vab inside the 57-bit VA space, and
83note clear of every page-table page
84fn gmrange pab ulong n ulong vab ulong to long
85b = get mmwb 0
86s = get mmws 0
87if or or lt pab b gt sub pab b s gt n sub s sub pab b
88ret GMEINVAL
89end
90if or ge vab MMVALIM gt n sub MMVALIM vab
91ret GMEINVAL
92end
93ret gmptclr pab n
94end
95fn gmptclr pab ulong n ulong to long
96for i ulong get mmptn 0
97p = get mmptpa i
98if and gt add p MMPTB pab lt p add pab n
99ret GMEINVAL
100end
101end
102ret GMOK
103end
104fn gmlive b ulong to bool
105if or eq get mmwst 0 MMWNONE ne get gmepoch 0 get mmwgen 0
106ret false
107end
108if ge b GMMAXB
109ret false
110end
111ret eq get gmbst b GMLIVE
112end
113fn gmsetpg b ulong st uchar to void
114first = div sub get gmbpa b get gmpab 0 4096
115n = div get gmbsz b 4096
116for i ulong n
117put gmpgst add first i st
118end
119end
120fn gmalloc size ulong cache ulong flags ulong to long
121if or not mmwmutable ne get gmepoch 0 get mmwgen 0
122ret GMESTATE
123end
124note flags & ~3 != 0 is flags > 3 for an unsigned value
125if or or or eq size 0 gt size mul GMMAXP 4096 gt cache 1 gt flags 3
126ret GMEINVAL
127end
128if and ne and flags GMHPOLL 0 ne cache 1
129ret GMEINVAL
130end
131put gmout 0 GMNONE
132n = div add size 4095 4096
133b = gmslot
134if eq b GMNONE
135ret GMENOMEM
136end
137st = gmfit n
138if eq st GMMAXP
139ret GMENOMEM
140end
141ret gmplace b st n cache flags
142end
143note first free buffer slot, GMNONE when all 64 are taken
144fn gmslot to ulong
145for i ulong GMMAXB
146if eq get gmbst i GMFREE
147ret i
148end
149end
150ret GMNONE
151end
152note first fit: the first page of the first run of n FREE pages, GMMAXP when there is none
153fn gmfit n ulong to ulong
154run = ulong 0
155st = ulong 0
156for i ulong get gmpages 0
157if ne get gmpgst i GMFREE
158run = 0
159continue
160end
161if eq run 0
162st = i
163end
164run = add run 1
165if eq run n
166ret st
167end
168end
169ret GMMAXP
170end
171note map n pages from page st into slot b (the slot stays FREE, so its fields stay unused, until the
172note map succeeds)
173fn gmplace b ulong st ulong n ulong cache ulong flags ulong to long
174put gmbpa b add get gmpab 0 mul st 4096
175put gmbva b add get gmvab 0 mul st 4096
176put gmbsz b mul n 4096
177pcf = gmpcf cache flags
178r = mmmap get gmbva b get gmbpa b get gmbsz b MMSZ4K MMAPSYS pcf
179if ne r MMOK
180ret GMEMAP
181end
182put gmbca b cache
183put gmbfl b flags
184put gmbst b GMLIVE
185put gmbgen b add get gmbgen b 1
186gmsetpg b GMLIVE
187put gmout 0 b
188ret GMOK
189end
190note PTE PCF: uncached 1 (GPU read-only 5), cached 0 (GPU read-only 4)
191fn gmpcf cache ulong flags ulong to ulong
192ro = ne and flags GMGPURO 0
193if eq cache 1
194if ro
195ret 5
196end
197ret 1
198end
199if ro
200ret 4
201end
202ret 0
203end
204fn gmretire b ulong reuse bool to long
205if not gmlive b
206ret GMESTATE
207end
208if eq get mmwkind 0 1
209note No documented idle proof exists, even after successful TLB invalidation.
210if not mmwmutable
211gmsetpg b GMQUAR
212put gmbst b GMQUAR
213r = mmwquar get mmwgen 0
214ret GMETIME
215end
216note Unpublished owned buffers can retire without exposing their root through mminval.
217um = mmunmap get gmbva b get gmbsz b MMSZ4K
218if and reuse eq um MMOK
219gmsetpg b GMFREE
220put gmbst b GMFREE
221ret GMOK
222end
223gmsetpg b GMQUAR
224put gmbst b GMQUAR
225if not reuse
226ret GMOK
227end
228ret GMEMAP
229end
230um = mmunmap get gmbva b get gmbsz b MMSZ4K
231iv = um
232if eq um MMOK
233iv = mminval get gmpoll 0
234end
235if and reuse eq iv MMOK
236gmsetpg b GMFREE
237put gmbst b GMFREE
238ret GMOK
239end
240gmsetpg b GMQUAR
241put gmbst b GMFREE
242if not reuse
243ret GMOK
244end
245if or eq iv MMETIME eq iv MMEDEAD
246ret GMETIME
247end
248ret GMEMAP
249end
250fn gmfree b ulong to long
251ret gmretire b true
252end
253fn gmquar b ulong to long
254ret gmretire b false
255end
256fn gmcount st uchar to ulong
257c = ulong 0
258for i ulong get gmpages 0
259if eq get gmpgst i st
260c = add c 1
261end
262end
263ret c
264end