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
omega-mv / gpu / nvidia / driver / card.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note gpu/nvidia/driver/card.om: which GPU the driver client opens, and the class list it hands to amp_runtime. |
| 3 | note Omega form of CUDApp nvrm/nvrm.c card_index and the marshalling half of select_classes (84b524ca). |
| 4 | note Needs OM-2 (unsized views): compose after an om2 part; gpu/nvidia/driver/parts.txt lists the order. |
| 5 | note Card table: the bytes NV_ESC_CARD_INFO fills, records of 72 bytes (nv_ioctl_card_info_t of NVIDIA open |
| 6 | note 580.173.02): valid u8 at 0, PCI domain u32 LE at 4, bus u8 at 8, slot u8 at 9, function u8 at 10. The C |
| 7 | note walks 64 records; here the record count is the table length divided by 72, so a caller hands 64. |
| 8 | |
| 9 | note value of one lowercase hex digit, or 16 when c is not one ("%x" output is lowercase) |
| 10 | fn drv_hexd c ulong to ulong |
| 11 | if and ge c 48 le c 57 |
| 12 | ret sub c 48 |
| 13 | end |
| 14 | if and ge c 97 le c 102 |
| 15 | ret sub c 87 |
| 16 | end |
| 17 | ret 16 |
| 18 | end |
| 19 | |
| 20 | note the hex digits b[s, s + n) as one number, or 2^64 - 1 when one of them is not a lowercase hex digit |
| 21 | fn drv_hexn b view uchar * s ulong n ulong to ulong |
| 22 | v = ulong 0 |
| 23 | for i ulong n bound 8 |
| 24 | d = drv_hexd ulong get b add s i |
| 25 | if gt d 15 |
| 26 | ret 18446744073709551615 |
| 27 | end |
| 28 | v = add shl v 4 d |
| 29 | end |
| 30 | ret v |
| 31 | end |
| 32 | |
| 33 | note A BDF the C accepts: sscanf "%x:%x:%x.%x%c" reads exactly four fields, domain <= 0xffff, bus <= 0xff, |
| 34 | note slot <= 0x1f, function <= 7, and the text equals its own "%04x:%02x:%02x.%x". That is exactly |
| 35 | note dddd:bb:ss.f in lowercase hex, 12 bytes. o gets domain, bus, slot, function. False refuses it. |
| 36 | fn drv_bdf b view uchar * o edit ulong 4 to bool |
| 37 | if ne len b 12 |
| 38 | ret false |
| 39 | end |
| 40 | if or ne get b 4 58 or ne get b 7 58 ne get b 10 46 |
| 41 | ret false |
| 42 | end |
| 43 | d = drv_hexn b 0 4 |
| 44 | u = drv_hexn b 5 2 |
| 45 | s = drv_hexn b 8 2 |
| 46 | f = drv_hexn b 11 1 |
| 47 | if or gt d 65535 or gt u 255 or gt s 31 gt f 7 |
| 48 | ret false |
| 49 | end |
| 50 | put o 0 d |
| 51 | put o 1 u |
| 52 | put o 2 s |
| 53 | put o 3 f |
| 54 | ret true |
| 55 | end |
| 56 | |
| 57 | note little-endian u32 at byte p of the card table |
| 58 | fn drv_u32 c view uchar * p ulong to ulong |
| 59 | a = ulong get c p |
| 60 | b = shl ulong get c add p 1 8 |
| 61 | x = shl ulong get c add p 2 16 |
| 62 | y = shl ulong get c add p 3 24 |
| 63 | ret or or a b or x y |
| 64 | end |
| 65 | |
| 66 | note card_index: with has false, the first valid card; with has true, the first valid card whose PCI address |
| 67 | note is the BDF in b (refused BDF: none). Returns the card index plus one, 0 when there is none. |
| 68 | fn drv_card c view uchar * has bool b view uchar * to ulong |
| 69 | o = new ulong 4 0 |
| 70 | n = div len c 72 |
| 71 | if has |
| 72 | if not drv_bdf b o |
| 73 | ret 0 |
| 74 | end |
| 75 | end |
| 76 | for i ulong n bound 9223372036854775807 |
| 77 | p = mul i 72 |
| 78 | if ne get c p 0 |
| 79 | if not has |
| 80 | ret add i 1 |
| 81 | end |
| 82 | if drv_at c p o |
| 83 | ret add i 1 |
| 84 | end |
| 85 | end |
| 86 | end |
| 87 | ret 0 |
| 88 | end |
| 89 | |
| 90 | note the card record at byte p has the PCI address o[0..3] |
| 91 | fn drv_at c view uchar * p ulong o view ulong 4 to bool |
| 92 | if ne drv_u32 c add p 4 get o 0 |
| 93 | ret false |
| 94 | end |
| 95 | if ne ulong get c add p 8 get o 1 |
| 96 | ret false |
| 97 | end |
| 98 | if ne ulong get c add p 9 get o 2 |
| 99 | ret false |
| 100 | end |
| 101 | ret eq ulong get c add p 10 get o 3 |
| 102 | end |
| 103 | |
| 104 | note select_classes marshalling: the amp_runtime class-set request m for SM sm and PCI vendor:device. Bit k of |
| 105 | note m[10] says the vendor list holds Ampere class m[4 + k] (compute B, GPFIFO A, Volta usermode A); bit k of |
| 106 | note m[11] says it holds Blackwell class m[7 + k] (compute B, channel GPFIFO A, Hopper usermode A). |
| 107 | fn drv_marshal sm ulong vendor ulong device ulong cls view uint * m edit ulong 16 to void |
| 108 | for z ulong 16 |
| 109 | put m z 0 |
| 110 | end |
| 111 | put m 0 1 |
| 112 | put m 1 sm |
| 113 | put m 2 0x806 |
| 114 | put m 3 0xc01 |
| 115 | put m 4 0xc7c0 |
| 116 | put m 5 0xc56f |
| 117 | put m 6 0xc361 |
| 118 | put m 7 0xcec0 |
| 119 | put m 8 0xc96f |
| 120 | put m 9 0xc661 |
| 121 | put m 12 0xa04 |
| 122 | put m 13 vendor |
| 123 | put m 14 device |
| 124 | for i ulong len cls bound 9223372036854775807 |
| 125 | v = ulong get cls i |
| 126 | for k ulong 3 |
| 127 | if eq v get m add 4 k |
| 128 | put m 10 or get m 10 shl ulong 1 k |
| 129 | end |
| 130 | if eq v get m add 7 k |
| 131 | put m 11 or get m 11 shl ulong 1 k |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | note select_classes: s gets target SM, discrete, compute, GPFIFO and usermode class; false is the C refusal |
| 138 | note "unsupported SM 0x%x or missing target classes" (s is then all zero). |
| 139 | fn drv_select sm ulong vendor ulong device ulong cls view uint * s edit ulong 5 to bool |
| 140 | m = new ulong 16 0 |
| 141 | q = new ulong 64 0 |
| 142 | drv_marshal sm vendor device cls m |
| 143 | for z ulong 5 |
| 144 | put s z 0 |
| 145 | end |
| 146 | if ne amp_runtime m q 1 |
| 147 | ret false |
| 148 | end |
| 149 | for z ulong 5 |
| 150 | put s z get q z |
| 151 | end |
| 152 | ret true |
| 153 | end |
| 154 | |
| 155 | note The retire window of nvrm_retire (nvrm/nvrm.c; nvrm_chan_retire applies it after its channel guard, which |
| 156 | note is not here): the channel's retired GPFIFO count after the caller reports |
| 157 | note completion up to up. It moves only when up lies in [rt, pt] counted forward from rt with u32 wrap: |
| 158 | note (up - rt) <= (pt - rt). For u32 inputs the 64-bit differences order exactly as the 32-bit ones do (a |
| 159 | note difference that wraps is above every one that does not, in both widths), so no mask is needed. |
| 160 | fn drv_retire rt ulong pt ulong up ulong to ulong |
| 161 | if le sub up rt sub pt rt |
| 162 | ret up |
| 163 | end |
| 164 | ret rt |
| 165 | end |