CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / backend / amd / kfd / launch / device.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note dev: kfd,render,gpu,simds,simdpercu,arrays,arraysperengine,xcc,ctxperxcc,ctl,debug,maxwaves,ldshi,scratchhi. |
| 3 | global dev ulong 14 |
| 4 | note KFD aperture records are 56 bytes. Grow full replies; require one selected GPU. |
| 5 | global dirbuf uchar 4096 |
| 6 | global nodepath uchar 4096 |
| 7 | global noden ulong 1 |
| 8 | global nodeerr long 1 |
| 9 | global nodenum ulong 1 |
| 10 | text selected "OMEGA topology selected node=" |
| 11 | text gpulabel " gpu_id=" |
| 12 | text minorlabel " render_minor=" |
| 13 | text cpuskip "OMEGA topology skip CPU gpu_id=0\n" |
| 14 | text nodes "/sys/class/kfd/kfd/topology/nodes/" |
| 15 | text props "/properties" |
| 16 | text gpufile "/gpu_id" |
| 17 | text render "/dev/dri/renderD" |
| 18 | text gfxkey "gfx_target_version" |
| 19 | text drmkey "drm_render_minor" |
| 20 | text simdkey "simd_count" |
| 21 | text cukey "simd_per_cu" |
| 22 | text arrkey "array_count" |
| 23 | text engkey "simd_arrays_per_engine" |
| 24 | text xcckey "num_xcc" |
| 25 | text wavekey "max_waves_per_simd" |
| 26 | text absent "OMEGA device=absent errno=2\n" |
| 27 | fn addpath b view uchar * p ulong to ulong |
| 28 | span p len b len pathbuf |
| 29 | for i ulong len b bound 4096 |
| 30 | put pathbuf add p i get b i |
| 31 | end |
| 32 | ret add p len b |
| 33 | end |
| 34 | fn nodefile suffix view uchar * to long |
| 35 | n = get noden 0 |
| 36 | for i ulong n bound 4096 |
| 37 | put pathbuf i get nodepath i |
| 38 | end |
| 39 | n = addpath suffix n |
| 40 | ret fopen pathbuf 0 n 0 0 |
| 41 | end |
| 42 | fn propvalue fd int key view uchar * to ulong |
| 43 | r = fseek fd 0 0 |
| 44 | if lt r 0 |
| 45 | put nodeerr 0 r |
| 46 | ret 0 |
| 47 | end |
| 48 | idx = ulong 0 |
| 49 | match = true |
| 50 | value = ulong 0 |
| 51 | number = false |
| 52 | while true bound 9223372036854775807 |
| 53 | r = read fd one 0 1 |
| 54 | if le r 0 |
| 55 | put nodeerr 0 r |
| 56 | if eq r 0 |
| 57 | put nodeerr 0 -22 |
| 58 | end |
| 59 | ret 0 |
| 60 | end |
| 61 | c = get one 0 |
| 62 | if eq c 10 |
| 63 | if and match number |
| 64 | ret value |
| 65 | end |
| 66 | idx = ulong 0 |
| 67 | match = true |
| 68 | number = false |
| 69 | value = ulong 0 |
| 70 | continue |
| 71 | end |
| 72 | if number |
| 73 | if match |
| 74 | if or lt c 48 gt c 57 |
| 75 | put nodeerr 0 -22 |
| 76 | ret 0 |
| 77 | end |
| 78 | if gt value div sub ulong 18446744073709551615 ulong sub c 48 10 |
| 79 | put nodeerr 0 -22 |
| 80 | ret 0 |
| 81 | end |
| 82 | value = add mul value 10 ulong sub c 48 |
| 83 | end |
| 84 | else |
| 85 | if eq c 32 |
| 86 | if ne idx len key |
| 87 | match = false |
| 88 | end |
| 89 | number = true |
| 90 | else |
| 91 | if lt idx len key |
| 92 | if ne c get key idx |
| 93 | match = false |
| 94 | end |
| 95 | else |
| 96 | match = false |
| 97 | end |
| 98 | idx = add idx 1 |
| 99 | end |
| 100 | end |
| 101 | end |
| 102 | ret 0 |
| 103 | end |
| 104 | fn nodecheck to bool |
| 105 | put nodeerr 0 0 |
| 106 | stepbegin stnode get nodenum 0 |
| 107 | fd = nodefile gpufile |
| 108 | if lt fd 0 |
| 109 | r = stepresult fd |
| 110 | ret false |
| 111 | end |
| 112 | v = ulong 0 |
| 113 | while true bound 9223372036854775807 |
| 114 | r = read int fd one 0 1 |
| 115 | if lt r 0 |
| 116 | put nodeerr 0 r |
| 117 | break |
| 118 | end |
| 119 | if eq r 0 |
| 120 | break |
| 121 | end |
| 122 | c = get one 0 |
| 123 | if eq c 10 |
| 124 | break |
| 125 | end |
| 126 | if or lt c 48 gt c 57 |
| 127 | put nodeerr 0 -22 |
| 128 | break |
| 129 | end |
| 130 | if gt v 429496729 |
| 131 | put nodeerr 0 -22 |
| 132 | break |
| 133 | end |
| 134 | v = add mul v 10 ulong sub c 48 |
| 135 | end |
| 136 | r = fclose int fd |
| 137 | if lt get nodeerr 0 0 |
| 138 | r = stepresult get nodeerr 0 |
| 139 | ret false |
| 140 | end |
| 141 | if eq v 0 |
| 142 | stepend |
| 143 | emit cpuskip |
| 144 | ret false |
| 145 | end |
| 146 | if gt v 4294967295 |
| 147 | r = stepresult -22 |
| 148 | ret false |
| 149 | end |
| 150 | fd = nodefile props |
| 151 | if lt fd 0 |
| 152 | r = stepresult fd |
| 153 | ret false |
| 154 | end |
| 155 | gfx = propvalue int fd gfxkey |
| 156 | if or ne gfx 90402 lt get nodeerr 0 0 |
| 157 | r = fclose int fd |
| 158 | r = stepresult get nodeerr 0 |
| 159 | ret false |
| 160 | end |
| 161 | put dev 1 propvalue int fd drmkey |
| 162 | put dev 3 propvalue int fd simdkey |
| 163 | put dev 4 propvalue int fd cukey |
| 164 | put dev 5 propvalue int fd arrkey |
| 165 | put dev 6 propvalue int fd engkey |
| 166 | put dev 7 propvalue int fd xcckey |
| 167 | put dev 11 propvalue int fd wavekey |
| 168 | r = fclose int fd |
| 169 | if lt get nodeerr 0 0 |
| 170 | r = stepresult get nodeerr 0 |
| 171 | ret false |
| 172 | end |
| 173 | put dev 2 v |
| 174 | stepend |
| 175 | emit selected |
| 176 | printnum get nodenum 0 |
| 177 | emit gpulabel |
| 178 | printnum v |
| 179 | emit minorlabel |
| 180 | printnum get dev 1 |
| 181 | emit newline |
| 182 | ret true |
| 183 | end |
| 184 | fn nodigit c uchar to void |
| 185 | put nodenum 0 add mul get nodenum 0 10 ulong sub c 48 |
| 186 | end |
| 187 | fn topology to void |
| 188 | note O_DIRECTORY differs across ARM64 and x86. getdnts verifies the read-only fd. |
| 189 | stepbegin sttop 0 |
| 190 | dfd = int checked fopen nodes 0 len nodes 0 0 |
| 191 | found = false |
| 192 | while not found bound 9223372036854775807 |
| 193 | r = checked getdnts dfd dirbuf 0 len dirbuf |
| 194 | if eq r 0 |
| 195 | stepbegin sttop 0 |
| 196 | steprow 19 false |
| 197 | exit 66 |
| 198 | end |
| 199 | p = ulong 0 |
| 200 | while lt p ulong r bound 4096 |
| 201 | span p 20 ulong r |
| 202 | sz = add ulong get dirbuf add p 16 shl ulong get dirbuf add p 17 8 |
| 203 | if lt sz 20 |
| 204 | exit 66 |
| 205 | end |
| 206 | span p sz ulong r |
| 207 | c = get dirbuf add p 19 |
| 208 | if and ge c 48 le c 57 |
| 209 | n = len nodes |
| 210 | for i ulong n bound 4096 |
| 211 | put nodepath i get nodes i |
| 212 | end |
| 213 | j = add p 19 |
| 214 | put nodenum 0 0 |
| 215 | while true bound 4096 |
| 216 | if ge j add p sz |
| 217 | exit 66 |
| 218 | end |
| 219 | if eq get dirbuf j 0 |
| 220 | break |
| 221 | end |
| 222 | if ge n len nodepath |
| 223 | exit 66 |
| 224 | end |
| 225 | c = get dirbuf j |
| 226 | if or lt c 48 gt c 57 |
| 227 | exit 66 |
| 228 | end |
| 229 | nodigit c |
| 230 | put nodepath n get dirbuf j |
| 231 | n = add n 1 |
| 232 | j = add j 1 |
| 233 | end |
| 234 | put noden 0 n |
| 235 | if nodecheck |
| 236 | found = true |
| 237 | break |
| 238 | end |
| 239 | end |
| 240 | p = add p sz |
| 241 | end |
| 242 | end |
| 243 | r = checked fclose dfd |
| 244 | stepbegin sttop get nodenum 0 |
| 245 | stepend |
| 246 | n = addpath render 0 |
| 247 | minor = get dev 1 |
| 248 | if gt minor 4294967295 |
| 249 | exit 66 |
| 250 | end |
| 251 | b = new uchar 10 0 |
| 252 | p = ulong 10 |
| 253 | while gt minor 9 bound 10 |
| 254 | p = sub p 1 |
| 255 | put b p uchar add rem minor 10 48 |
| 256 | minor = div minor 10 |
| 257 | end |
| 258 | p = sub p 1 |
| 259 | put b p uchar add minor 48 |
| 260 | n = addpath part b p sub 10 p n |
| 261 | stepbegin strender get dev 1 |
| 262 | note No arbitrary alternate minor: ACQUIRE_VM must bind the selected GPU's render device. |
| 263 | put dev 1 ulong checked fopen pathbuf 0 n 2 0 |
| 264 | stepend |
| 265 | end |
| 266 | fn connectkfd to void |
| 267 | stepbegin stopen 0 |
| 268 | fd = fopen kfdpath 0 len kfdpath 2 0 |
| 269 | if eq fd -2 |
| 270 | steprow 2 false |
| 271 | emit absent |
| 272 | exit 77 |
| 273 | end |
| 274 | put dev 0 ulong checked fd |
| 275 | stepend |
| 276 | stepbegin stversion 0 |
| 277 | zerohdr |
| 278 | r = checked kfdctl int fd 2148027137 hdr 0 8 |
| 279 | if or ne hget 0 4 1 lt hget 4 4 14 |
| 280 | steprow 22 false |
| 281 | exit 67 |
| 282 | end |
| 283 | stepend |
| 284 | topology |
| 285 | stepbegin stvm get dev 2 |
| 286 | zerohdr |
| 287 | hput 0 4 get dev 1 |
| 288 | hput 4 4 get dev 2 |
| 289 | r = checked kfdctl int get dev 0 1074285333 hdr 0 8 |
| 290 | stepend |
| 291 | for i ulong 4 |
| 292 | put ids i uchar and shr get dev 2 mul i 8 255 |
| 293 | end |
| 294 | end |
| 295 | fn aperturedevice h long count ulong to bool |
| 296 | found = ulong 0 |
| 297 | for i ulong count bound 4294967295 |
| 298 | r = checked mapread h mul i 56 hdr 0 56 |
| 299 | if eq hget 48 4 0 |
| 300 | ret false |
| 301 | end |
| 302 | if eq hget 48 4 get dev 2 |
| 303 | found = add found 1 |
| 304 | if or lt hget 8 8 hget 0 8 lt hget 24 8 hget 16 8 |
| 305 | ret false |
| 306 | end |
| 307 | put dev 12 shr hget 0 8 32 |
| 308 | put dev 13 shr hget 16 8 32 |
| 309 | end |
| 310 | end |
| 311 | ret and eq found 1 and ne get dev 12 0 ne get dev 13 0 |
| 312 | end |
| 313 | fn readap to void |
| 314 | stepbegin stap get dev 2 |
| 315 | zerohdr |
| 316 | note A zero count asks the driver for the current inventory size. |
| 317 | r = checked kfdctl int get dev 0 3222293268 hdr 0 16 ids 0 0 |
| 318 | capacity = hget 8 4 |
| 319 | if eq capacity 0 |
| 320 | steprow 75 false |
| 321 | exit 66 |
| 322 | end |
| 323 | complete = false |
| 324 | note One spare record distinguishes a complete reply from a truncated one. |
| 325 | note Only the u32 node-count wire field bounds growth. |
| 326 | while not complete bound 4294967295 |
| 327 | if lt capacity 4294967295 |
| 328 | capacity = add capacity 1 |
| 329 | end |
| 330 | bytes = mul capacity 56 |
| 331 | h = checked mapnew -1 0 bytes 3 34 |
| 332 | zerohdr |
| 333 | hput 8 4 capacity |
| 334 | r = checked kfdctl int get dev 0 3222293268 hdr 0 16 h 0 bytes |
| 335 | count = hget 8 4 |
| 336 | valid = and ne count 0 le count capacity |
| 337 | if and valid or lt count capacity eq capacity 4294967295 |
| 338 | complete = true |
| 339 | valid = aperturedevice h count |
| 340 | end |
| 341 | r = checked mapdrop h |
| 342 | if not valid |
| 343 | steprow 22 false |
| 344 | exit 66 |
| 345 | end |
| 346 | if not complete |
| 347 | capacity = mul capacity 2 |
| 348 | if gt capacity 4294967294 |
| 349 | capacity = ulong 4294967294 |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | stepend |
| 354 | end |
| 355 | fn ctxsizes to void |
| 356 | if or eq get dev 4 0 eq get dev 7 0 |
| 357 | exit 66 |
| 358 | end |
| 359 | if eq get dev 6 0 |
| 360 | exit 66 |
| 361 | end |
| 362 | cu = div div get dev 3 get dev 4 get dev 7 |
| 363 | if eq cu 0 |
| 364 | exit 66 |
| 365 | end |
| 366 | waves = mul cu 40 |
| 367 | note ROCT topology first converts array_count / simd_arrays_per_engine into NumShaderBanks. |
| 368 | note queues.c then divides NumShaderBanks by NumArrays again; both divisions are required. |
| 369 | maxwaves = mul div div get dev 5 get dev 6 get dev 6 512 |
| 370 | if lt maxwaves waves |
| 371 | waves = maxwaves |
| 372 | end |
| 373 | if eq waves 0 |
| 374 | exit 66 |
| 375 | end |
| 376 | ctl = roundup add 40 add mul waves 8 8 4096 |
| 377 | wg = roundup mul cu add add ulong 0x80000 0x4000 add ulong 0x10000 0x1000 4096 |
| 378 | put dev 8 add ctl wg |
| 379 | put dev 9 ctl |
| 380 | put dev 10 roundup mul waves 32 64 |
| 381 | if gt get dev 8 4294967295 |
| 382 | exit 66 |
| 383 | end |
| 384 | end |
| 385 | fn mapgpu kh ulong to void |
| 386 | zerohdr |
| 387 | hput 0 8 kh |
| 388 | hput 16 4 1 |
| 389 | r = checked kfdctl int get dev 0 3222817560 hdr 0 24 ids 0 4 |
| 390 | if ne hget 20 4 1 |
| 391 | steprow 5 false |
| 392 | exit 68 |
| 393 | end |
| 394 | end |
| 395 | note Allocation record on the caller's table: CPU handle, KFD handle, byte size (24 bytes). |
| 396 | fn gpumem n ulong flags ulong table long rec ulong to long |
| 397 | sz = roundup n 4096 |
| 398 | stepbegin streserve div rec 24 |
| 399 | h = checked kfdres sz |
| 400 | stepend |
| 401 | stepbegin stalloc div rec 24 |
| 402 | zerohdr |
| 403 | hput 8 8 sz |
| 404 | hput 32 4 get dev 2 |
| 405 | hput 36 4 flags |
| 406 | r = checked kfdctl int get dev 0 3223866134 hdr 0 40 h |
| 407 | kh = hget 16 8 |
| 408 | moff = hget 24 8 |
| 409 | stepend |
| 410 | r = checked mapset table rec 8 ulong h |
| 411 | r = checked mapset table add rec 8 8 kh |
| 412 | r = checked mapset table add rec 16 8 sz |
| 413 | stepbegin stmap div rec 24 |
| 414 | mapgpu kh |
| 415 | stepend |
| 416 | stepbegin stbind div rec 24 |
| 417 | r = checked mapbind h int get dev 1 moff 3 |
| 418 | stepend |
| 419 | ret h |
| 420 | end |
| 421 | fn gpufree table long rec ulong to void |
| 422 | kh = mapget table add rec 8 8 |
| 423 | zerohdr |
| 424 | hput 0 8 kh |
| 425 | hput 16 4 1 |
| 426 | r = checked kfdctl int get dev 0 3222817561 hdr 0 24 ids 0 4 |
| 427 | if ne hget 20 4 1 |
| 428 | exit 68 |
| 429 | end |
| 430 | r = checked mapdrop long mapget table rec 8 |
| 431 | zerohdr |
| 432 | hput 0 8 kh |
| 433 | r = checked kfdctl int get dev 0 1074285335 hdr 0 8 |
| 434 | end |