interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / gpu / nvidia / driver / card.om

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