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