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/w0.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 / w0.om` 554 lines ยท 11507 bytes Raw Blame History 1 note w0: bind-read-release identity probe for one vfio-pci device. Usage: w0 BDF GROUP [HEXOFF] 2 note It never writes to the device: vfiomap is read-only and every other access is pread. 3 note It never calls DEVICE_RESET (omc refuses it), but Linux vfio-pci resets the device itself: 4 note FLR on open when a reset method is enabled, and pci_reset_bus (SBR) on release. See README. 5 note Register offsets (open-gpu-kernel-modules 580.173.02, src/common/inc/swref/published/nv_ref.h): 6 note NV_PMC_BOOT_0 0x0 :108, NV_PMC_BOOT_1 0x4 :138, NV_PMC_BOOT_42 0xA00 :149. 7 note Config space NV_CONFIG_PCI_NV_0 vendor/device 0x0 :52, NV_CONFIG_PCI_NV_2 rev/class 0x8 :66, 8 note NV_CONFIG_PCI_NV_11 subsystem 0x2C :81. 9 note VFIO ABI: Linux include/uapi/linux/vfio.h; requests are _IO(';', 100+k) = 15204+k. 10 text sysdev "/sys/bus/pci/devices/" 11 text sgroup "/iommu_group" 12 text sdriver "/driver" 13 text sgroups "/sys/kernel/iommu_groups/" 14 text sdevices "/devices" 15 text vfiopci "vfio-pci" 16 text vfioc "/dev/vfio/vfio" 17 text vfiodir "/dev/vfio/" 18 text usage "w0: usage: w0 BDF GROUP [HEXOFF] (BDF like 000f:01:00.0, GROUP decimal)\n" 19 text egroup "w0: REFUSED: device iommu_group does not match GROUP\n" 20 text edriver "w0: REFUSED: device is not bound to vfio-pci\n" 21 text esingle "w0: REFUSED: iommu group must hold exactly this one device\n" 22 text eapi "w0: REFUSED: VFIO API version or TYPE1v2 extension missing\n" 23 text eviable "w0: REFUSED: group not viable\n" 24 text econt "w0: REFUSED: could not attach group to container or set TYPE1v2\n" 25 text edev "w0: REFUSED: could not get device fd\n" 26 text einfo "w0: REFUSED: device info or region info unusable\n" 27 text eones "w0: FAIL: BOOT_0 reads 0xffffffff (device not responding)\n" 28 text emis "w0: FAIL: mapped and pread values differ\n" 29 text emap "w0: FAIL: BAR0 vfiomap returned errno " 30 text lbdf "w0: device " 31 text lgroup " group " 32 text ldev "w0: device flags " 33 text lreg " regions " 34 text lirq " irqs " 35 text lbar "w0: BAR0 flags " 36 text lsize " size " 37 text loff " offset " 38 text lcfg "w0: CONFIG offset " 39 text lb0 "w0: BOOT_0 [0x000] " 40 text lb1 "w0: BOOT_1 [0x004] " 41 text lb42 "w0: BOOT_42 [0xa00] " 42 text lvd "w0: config vendor/device [0x00] " 43 text lcls "w0: config rev/class [0x08] " 44 text lsub "w0: config subsystem [0x2c] " 45 text lx "w0: extra [0x" 46 text lxe "] " 47 text lmap " mapget " 48 text lpre " pread " 49 text ldone "w0: released device, group and container\n" 50 text nl "\n" 51 global bdf uchar 32 52 global grp uchar 8 53 global xarg uchar 20 54 global path uchar 256 55 global plen ulong 1 56 global lnk uchar 256 57 global dents uchar 4096 58 global out uchar 64 59 global st uchar 8 60 global val uchar 4 61 global dinfo uchar 24 62 global rbar uchar 32 63 global rcfg uchar 32 64 global word uchar 4 65 global nums ulong 8 66 global fds long 4 67 fn fail code int msg int to void 68 note msg selects the refusal line 69 if eq msg 2 70 r = write 2 usage 0 len usage 71 end 72 if eq msg 3 73 r = write 2 egroup 0 len egroup 74 end 75 if eq msg 4 76 r = write 2 edriver 0 len edriver 77 end 78 if eq msg 5 79 r = write 2 esingle 0 len esingle 80 end 81 if eq msg 6 82 r = write 2 eapi 0 len eapi 83 end 84 if eq msg 7 85 r = write 2 eviable 0 len eviable 86 end 87 if eq msg 8 88 r = write 2 econt 0 len econt 89 end 90 if eq msg 9 91 r = write 2 edev 0 len edev 92 end 93 if eq msg 10 94 r = write 2 einfo 0 len einfo 95 end 96 exit code 97 end 98 fn hexd v ulong to uchar 99 if lt v 10 100 ret uchar add v 48 101 end 102 ret uchar add v 87 103 end 104 note print 0x + 8 hex digits (or 16 when wide), then optionally a newline 105 fn hex v ulong wide bool newline bool to void 106 n = ulong 8 107 if wide 108 n = 16 109 end 110 put out 0 uchar 48 111 put out 1 uchar 120 112 for i ulong n 113 put out add 2 i hexd and shr v mul sub sub n 1 i 4 15 114 end 115 k = add n 2 116 if newline 117 put out k uchar 10 118 k = add k 1 119 end 120 r = write 1 out 0 k 121 end 122 fn le32 a ulong which int to ulong 123 v = ulong 0 124 if eq which 0 125 v = ulong get dinfo add a 3 126 v = or shl v 8 ulong get dinfo add a 2 127 v = or shl v 8 ulong get dinfo add a 1 128 ret or shl v 8 ulong get dinfo a 129 end 130 if eq which 1 131 v = ulong get rbar add a 3 132 v = or shl v 8 ulong get rbar add a 2 133 v = or shl v 8 ulong get rbar add a 1 134 ret or shl v 8 ulong get rbar a 135 end 136 if eq which 2 137 v = ulong get rcfg add a 3 138 v = or shl v 8 ulong get rcfg add a 2 139 v = or shl v 8 ulong get rcfg add a 1 140 ret or shl v 8 ulong get rcfg a 141 end 142 if eq which 3 143 v = ulong get st add a 3 144 v = or shl v 8 ulong get st add a 2 145 v = or shl v 8 ulong get st add a 1 146 ret or shl v 8 ulong get st a 147 end 148 v = ulong get word 3 149 v = or shl v 8 ulong get word 2 150 v = or shl v 8 ulong get word 1 151 ret or shl v 8 ulong get word 0 152 end 153 fn le64 a ulong which int to ulong 154 ret or le32 a which shl le32 add a 4 which 32 155 end 156 fn setval v ulong to void 157 put val 0 uchar and v 255 158 put val 1 uchar and shr v 8 255 159 put val 2 uchar and shr v 16 255 160 put val 3 uchar and shr v 24 255 161 end 162 note path building: a fresh path, then pieces; the terminator-free length is plen 163 fn preset to void 164 put plen 0 0 165 end 166 fn padd c uchar to void 167 p = get plen 0 168 put path p c 169 put plen 0 add p 1 170 end 171 fn ptext k int to void 172 if eq k 0 173 for i ulong len sysdev 174 padd get sysdev i 175 end 176 end 177 if eq k 1 178 for i ulong len sgroup 179 padd get sgroup i 180 end 181 end 182 if eq k 2 183 for i ulong len sdriver 184 padd get sdriver i 185 end 186 end 187 if eq k 3 188 for i ulong len sgroups 189 padd get sgroups i 190 end 191 end 192 if eq k 4 193 for i ulong len sdevices 194 padd get sdevices i 195 end 196 end 197 if eq k 5 198 for i ulong len vfiodir 199 padd get vfiodir i 200 end 201 end 202 end 203 fn pbdf n ulong to void 204 for i ulong n 205 padd get bdf i 206 end 207 end 208 fn pgrp n ulong to void 209 for i ulong n 210 padd get grp i 211 end 212 end 213 note the final path component of lnk[0..n) must equal grp[0..g) (which 0) or "vfio-pci" (which 1) 214 fn lastis n ulong g ulong which int to bool 215 s = ulong 0 216 for i ulong n 217 if eq get lnk i 47 218 s = add i 1 219 end 220 end 221 m = sub n s 222 if eq which 0 223 if ne m g 224 ret false 225 end 226 for i ulong g 227 if ne get lnk add s i get grp i 228 ret false 229 end 230 end 231 ret true 232 end 233 if ne m len vfiopci 234 ret false 235 end 236 for i ulong m 237 if ne get lnk add s i get vfiopci i 238 ret false 239 end 240 end 241 ret true 242 end 243 note name at dents[o..] (zero-terminated) equals bdf[0..n) 244 fn nameis o ulong n ulong to bool 245 for i ulong n 246 if ne get dents add o i get bdf i 247 ret false 248 end 249 end 250 ret eq get dents add o n 0 251 end 252 fn isdot o ulong to bool 253 if ne get dents o 46 254 ret false 255 end 256 if eq get dents add o 1 0 257 ret true 258 end 259 ret and eq get dents add o 1 46 eq get dents add o 2 0 260 end 261 fn readcfg dev int base ulong reg ulong to ulong 262 r = pread dev word 0 4 long add base reg 263 if ne r 4 264 fail 10 10 265 end 266 ret le32 0 4 267 end 268 note stage 0: argv. nums: 0 bdf length, 1 group length, 2 extra length, 3 extra offset, 4 extra given 269 fn args to void 270 ac = argcnt 271 if or lt ac 3 gt ac 4 272 fail 2 2 273 end 274 bn = argread 1 bdf 0 32 275 gn = argread 2 grp 0 8 276 if or ne bn 12 or le gn 0 gt gn 6 277 fail 2 2 278 end 279 put nums 0 ulong bn 280 put nums 1 ulong gn 281 for i ulong 12 282 c = get bdf i 283 ok = or and ge c 48 le c 57 and ge c 97 le c 102 284 if or eq i 4 eq i 7 285 ok = eq c 58 286 end 287 if eq i 10 288 ok = eq c 46 289 end 290 if not ok 291 fail 2 2 292 end 293 end 294 for i ulong ulong gn 295 c = get grp i 296 if or lt c 48 gt c 57 297 fail 2 2 298 end 299 end 300 if eq ac 4 301 xn = argread 3 xarg 0 16 302 if or le xn 0 gt xn 16 303 fail 2 2 304 end 305 x = ulong 0 306 for i ulong ulong xn 307 c = get xarg i 308 d = ulong 99 309 if and ge c 48 le c 57 310 d = ulong sub c 48 311 end 312 if and ge c 97 le c 102 313 d = ulong sub c 87 314 end 315 if eq d 99 316 fail 2 2 317 end 318 x = or shl x 4 d 319 end 320 put nums 2 ulong xn 321 put nums 3 x 322 put nums 4 1 323 end 324 end 325 note stage 1: sysfs. iommu_group must be GROUP and the driver must be vfio-pci 326 fn sysfs to void 327 b = get nums 0 328 g = get nums 1 329 preset 330 ptext 0 331 pbdf b 332 ptext 1 333 n = readlnk path 0 get plen 0 lnk 0 256 334 if or le n 0 ge n 256 335 fail 3 3 336 end 337 if not lastis ulong n g 0 338 fail 3 3 339 end 340 preset 341 ptext 0 342 pbdf b 343 ptext 2 344 n = readlnk path 0 get plen 0 lnk 0 256 345 if or le n 0 ge n 256 346 fail 4 4 347 end 348 if not lastis ulong n 0 1 349 fail 4 4 350 end 351 end 352 note stage 2: /sys/kernel/iommu_groups/GROUP/devices must list exactly this device 353 fn single to void 354 b = get nums 0 355 preset 356 ptext 3 357 pgrp get nums 1 358 ptext 4 359 dfd = fopen path 0 get plen 0 540672 0 360 if lt dfd 0 361 fail 5 5 362 end 363 count = ulong 0 364 other = ulong 0 365 while true bound 4096 366 r = getdnts int dfd dents 0 4096 367 if lt r 0 368 fail 5 5 369 end 370 if eq r 0 371 break 372 end 373 o = ulong 0 374 while lt o ulong r bound 4096 375 rl = ulong get dents add o 17 376 rl = or shl rl 8 ulong get dents add o 16 377 if eq rl 0 378 fail 5 5 379 end 380 if not isdot add o 19 381 count = add count 1 382 if not nameis add o 19 b 383 other = add other 1 384 end 385 end 386 o = add o rl 387 end 388 end 389 r = fclose int dfd 390 if or ne count 1 ne other 0 391 fail 5 5 392 end 393 end 394 note stage 3: container API 0 + TYPE1v2, group viable, attach, set TYPE1v2, device fd by name 395 fn attach to void 396 cfd = fopen vfioc 0 len vfioc 524290 0 397 if lt cfd 0 398 fail 6 6 399 end 400 ci = int cfd 401 put fds 0 cfd 402 if ne vfioctl ci uint 15204 val 0 0 0 403 fail 6 6 404 end 405 setval 3 406 if ne vfioctl ci uint 15205 val 0 4 1 407 fail 6 6 408 end 409 preset 410 ptext 5 411 pgrp get nums 1 412 gfd = fopen path 0 get plen 0 524290 0 413 if lt gfd 0 414 fail 7 7 415 end 416 gi = int gfd 417 put fds 1 gfd 418 if ne vfioctl gi uint 15207 st 0 8 0 419 fail 7 7 420 end 421 if eq and le32 4 3 1 0 422 fail 7 7 423 end 424 setval ulong cfd 425 if ne vfioctl gi uint 15208 val 0 4 0 426 fail 8 8 427 end 428 setval 3 429 if ne vfioctl ci uint 15206 val 0 4 0 430 fail 8 8 431 end 432 dv = vfioctl gi uint 15210 bdf 0 get nums 0 433 if lt dv 0 434 fail 9 9 435 end 436 put fds 2 dv 437 r = write 1 lbdf 0 len lbdf 438 r = write 1 bdf 0 get nums 0 439 r = write 1 lgroup 0 len lgroup 440 r = write 1 grp 0 get nums 1 441 r = write 1 nl 0 len nl 442 end 443 note stage 4: device info (PCI flag 2, >= 8 regions), BAR0 (index 0) and CONFIG (index 7) region info 444 fn info to void 445 di = int get fds 2 446 if ne vfioctl di uint 15211 dinfo 0 24 0 447 fail 10 10 448 end 449 df = le32 4 0 450 nr = le32 8 0 451 r = write 1 ldev 0 len ldev 452 hex df false false 453 r = write 1 lreg 0 len lreg 454 hex nr false false 455 r = write 1 lirq 0 len lirq 456 hex le32 12 0 false true 457 if or eq and df 2 0 lt nr 8 458 fail 10 10 459 end 460 put rbar 8 0 461 if ne vfioctl di uint 15212 rbar 0 32 0 462 fail 10 10 463 end 464 put rcfg 8 7 465 if ne vfioctl di uint 15212 rcfg 0 32 0 466 fail 10 10 467 end 468 bf = le32 4 1 469 bs = le64 16 1 470 r = write 1 lbar 0 len lbar 471 hex bf false false 472 r = write 1 lsize 0 len lsize 473 hex bs true false 474 r = write 1 loff 0 len loff 475 hex le64 24 1 true true 476 r = write 1 lcfg 0 len lcfg 477 hex le64 24 2 true true 478 if or eq and bf 1 0 lt bs 4096 479 fail 10 10 480 end 481 end 482 note stage 5: config identity through pread of the CONFIG region (reads only) 483 fn config to void 484 di = int get fds 2 485 co = le64 24 2 486 r = write 1 lvd 0 len lvd 487 hex readcfg di co 0 false true 488 r = write 1 lcls 0 len lcls 489 hex readcfg di co 8 false true 490 r = write 1 lsub 0 len lsub 491 hex readcfg di co 44 false true 492 end 493 note stage 6: BOOT registers by pread of BAR0, then through the read-only map; release in reverse order 494 fn boot to int 495 di = int get fds 2 496 bo = le64 24 1 497 p0 = readcfg di bo 0 498 p1 = readcfg di bo 4 499 p42 = readcfg di bo 2560 500 h = vfiomap di rbar 0 32 501 if lt h 0 502 r = write 2 emap 0 len emap 503 hex ulong neg h false true 504 exit 12 505 end 506 m0 = mapget h 0 4 507 m1 = mapget h 4 4 508 m42 = mapget h 2560 4 509 r = write 1 lb0 0 len lb0 510 r = write 1 lmap 0 len lmap 511 hex m0 false false 512 r = write 1 lpre 0 len lpre 513 hex p0 false true 514 r = write 1 lb1 0 len lb1 515 r = write 1 lmap 0 len lmap 516 hex m1 false false 517 r = write 1 lpre 0 len lpre 518 hex p1 false true 519 r = write 1 lb42 0 len lb42 520 r = write 1 lmap 0 len lmap 521 hex m42 false false 522 r = write 1 lpre 0 len lpre 523 hex p42 false true 524 if eq get nums 4 1 525 r = write 1 lx 0 len lx 526 r = write 1 xarg 0 get nums 2 527 r = write 1 lxe 0 len lxe 528 r = write 1 lmap 0 len lmap 529 hex mapget h get nums 3 4 false true 530 end 531 r = mapdrop h 532 r = fclose di 533 r = fclose int get fds 1 534 r = fclose int get fds 0 535 r = write 1 ldone 0 len ldone 536 if eq m0 `4294967295` 537 r = write 2 eones 0 len eones 538 ret 11 539 end 540 if or or ne m0 p0 ne m1 p1 ne m42 p42 541 r = write 2 emis 0 len emis 542 ret 13 543 end 544 ret 0 545 end 546 fn main to int 547 args 548 sysfs 549 single 550 attach 551 info 552 config 553 ret boot 554 end