mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
159 lines
6.1 KiB
Python
159 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate packet vectors independently from pinned NVIDIA GV100 RAMRL bit ranges."""
|
|
import argparse
|
|
import ast
|
|
import hashlib
|
|
import json
|
|
import operator
|
|
import re
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PINNED_COMMIT = "fd9085a7f4daaace435decb7cb95d05b083eff87"
|
|
PINNED_SHA256 = "cdba9fd05f20bdc5920b6c2bc8e4b49c238e361f0207316ad6b7951a3a718a4f"
|
|
|
|
_BIN_OPS = {
|
|
ast.Add: operator.add,
|
|
ast.Sub: operator.sub,
|
|
ast.Mult: operator.mul,
|
|
}
|
|
_UNARY_OPS = {
|
|
ast.UAdd: operator.pos,
|
|
ast.USub: operator.neg,
|
|
}
|
|
|
|
|
|
def _eval_int_expr(expr: str) -> int:
|
|
"""Safely evaluate a constant integer arithmetic expression without eval()."""
|
|
if not re.fullmatch(r"[0-9+*() -]+", expr):
|
|
raise SystemExit(f"Unsafe manual bit-range expression: {expr!r}")
|
|
|
|
def _eval(node):
|
|
if isinstance(node, ast.Expression):
|
|
return _eval(node.body)
|
|
if isinstance(node, ast.Constant) and type(node.value) is int:
|
|
return node.value
|
|
if isinstance(node, ast.UnaryOp) and type(node.op) in _UNARY_OPS:
|
|
return _UNARY_OPS[type(node.op)](_eval(node.operand))
|
|
if isinstance(node, ast.BinOp) and type(node.op) in _BIN_OPS:
|
|
return _BIN_OPS[type(node.op)](_eval(node.left), _eval(node.right))
|
|
raise SystemExit(f"Unsupported manual bit-range AST node in {expr!r}")
|
|
|
|
return _eval(ast.parse(expr.strip(), mode="eval"))
|
|
|
|
|
|
def _field(text: str, name: str) -> tuple[int, int]:
|
|
match = re.search(r"^#define " + re.escape(name) + r"\s+([^\n]+)", text, re.M)
|
|
if match is None:
|
|
raise SystemExit(f"Missing macro definition for {name}")
|
|
raw = match[1].split("/*")[0].strip()
|
|
parts = raw.split(":")
|
|
if len(parts) != 2:
|
|
raise SystemExit(f"Expected high:low range for {name}, got {raw!r}")
|
|
high_expr, low_expr = parts
|
|
return _eval_int_expr(low_expr), _eval_int_expr(high_expr)
|
|
|
|
|
|
def _put(text: str, words: list[int], name: str, value: int) -> None:
|
|
low, high = _field(text, "NV_RAMRL_ENTRY_" + name)
|
|
if low < 0 or high < low or low // 32 != high // 32 or high // 32 >= len(words):
|
|
raise SystemExit(f"Invalid word bit range {high}:{low} for {name}")
|
|
if value < 0 or value >= (1 << (high - low + 1)):
|
|
raise SystemExit(f"Value {value} out of range for {name} ({high}:{low})")
|
|
words[low // 32] |= value << (low % 32)
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Generate or verify GV100 RAMRL packet vectors."
|
|
)
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args(argv)
|
|
|
|
path = ROOT / "reports/native-fifo-gv100-dev-ram.ref.txt"
|
|
if not path.is_file():
|
|
raise SystemExit(f"Missing pinned GV100 RAMRL manual: {path}")
|
|
raw_bytes = path.read_bytes()
|
|
sha = hashlib.sha256(raw_bytes).hexdigest()
|
|
# Snapshot verified byte-identical to the last commit touching this manual.
|
|
if sha != PINNED_SHA256:
|
|
raise SystemExit(
|
|
f"Pinned GV100 RAMRL manual hash differs: expected {PINNED_SHA256}, got {sha}"
|
|
)
|
|
text = raw_bytes.decode("utf-8")
|
|
|
|
rows = []
|
|
for variant in range(64):
|
|
for inst_target in (0, 2, 3):
|
|
for userd_target in (0, 2, 3):
|
|
spec = [
|
|
variant * 31 % 4096,
|
|
variant * 127 % 4096,
|
|
variant % 2,
|
|
0x1234500000 + variant * 4096,
|
|
0x2345600000 + variant * 512,
|
|
inst_target,
|
|
userd_target,
|
|
variant % 16,
|
|
(128 + variant) % 256,
|
|
]
|
|
tsg, chid, queue, inst, userd, it, ut, scale, timeout = spec
|
|
header = [0] * 4
|
|
channel = [0] * 4
|
|
for name, value in (
|
|
("TYPE", 1),
|
|
("TSG_TIMESLICE_SCALE", scale),
|
|
("TSG_TIMESLICE_TIMEOUT", timeout),
|
|
("TSG_LENGTH", 1),
|
|
("TSG_TSGID", tsg),
|
|
):
|
|
_put(text, header, name, value)
|
|
for name, value in (
|
|
("TYPE", 0),
|
|
("CHAN_RUNQUEUE_SELECTOR", queue),
|
|
("CHAN_INST_TARGET", it),
|
|
("CHAN_USERD_TARGET", ut),
|
|
("CHAN_USERD_PTR_LO", (userd & 0xFFFFFFFF) >> 8),
|
|
("CHAN_USERD_PTR_HI", userd >> 32),
|
|
("CHAN_CHID", chid),
|
|
("CHAN_INST_PTR_LO", (inst & 0xFFFFFFFF) >> 12),
|
|
("CHAN_INST_PTR_HI", inst >> 32),
|
|
):
|
|
_put(text, channel, name, value)
|
|
rows.append(dict(spec=spec, words=header + channel))
|
|
|
|
lines = [
|
|
"// Derived independently from NVIDIA GV100 hardware manual field definitions.",
|
|
"const PACKETS: &[PacketCase] = &[",
|
|
]
|
|
for c in rows:
|
|
lines.append(
|
|
"PacketCase { spec: " + str(c["spec"]) + ", words: " + str(c["words"]) + " },"
|
|
)
|
|
lines.append("];")
|
|
generated = "\n".join(lines) + "\n"
|
|
out = ROOT / "src/nvidia/src/fifo_packet_vectors.rs"
|
|
report = dict(
|
|
passed=True,
|
|
source_url=f"https://github.com/NVIDIA/open-gpu-doc/blob/{PINNED_COMMIT}/manuals/volta/gv100/dev_ram.ref.txt",
|
|
source_sha256=sha,
|
|
cases=len(rows),
|
|
scope="Header field-derived independent vectors, not original580 instruction execution or GPU execution",
|
|
vectors=rows,
|
|
)
|
|
report_path = ROOT / "reports/native-fifo-runlist-header-oracle.json"
|
|
if args.check:
|
|
if not out.is_file() or out.read_text() != generated:
|
|
raise SystemExit("RAMRL packet vectors differ")
|
|
if not report_path.is_file() or json.loads(report_path.read_text()) != report:
|
|
raise SystemExit("RAMRL header oracle report differs")
|
|
else:
|
|
out.write_text(generated)
|
|
report_path.write_text(json.dumps(report, indent=2) + "\n")
|
|
print(len(rows), "independent NVIDIA header-derived RAMRL packets passed")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|