mirror of
https://github.com/lemonade-sdk/mac_linuxgpu.git
synced 2026-11-22 23:57:31 -10:00
Runs the unmodified upstream Linux amdgpu and amdkfd drivers on Apple Silicon Macs inside a DriverKit system extension, for AMD GPUs attached over Thunderbolt. The Linux sources come from a pinned submodule and are compiled against linuxu, a userspace implementation of the Linux kernel APIs they use. Includes the HSA runtime, the host installer app, the setup and firmware fetch scripts, and the offline test suite.
130 lines
4.9 KiB
Python
Executable File
130 lines
4.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""scripts/fw2rodata.py - optional embedded firmware fallback table.
|
|
|
|
Converts every firmware image found under an amdgpu firmware directory into
|
|
a const rodata C table that linuxu/src/fw/fw_table.c can register as a
|
|
fallback. The table is never assumed to be complete: request_firmware()
|
|
first asks the host firmware servicer, which reads the installed
|
|
linux-firmware amdgpu directory, and only then falls back to this table.
|
|
|
|
The input directory has the layout of linux-firmware's amdgpu/ directory
|
|
(a flat set of <ip>_<maj>_<min>_<rev>[_<suffix>].bin files, plus the WHENCE
|
|
and license files). Each file at relative path P is registered under the
|
|
request name "amdgpu/P", which is exactly the string upstream passes to
|
|
request_firmware(). Nothing here selects files for a particular GPU; the
|
|
table contains whatever the directory contains, and it may be empty.
|
|
|
|
Usage:
|
|
fw2rodata.py --output OUT.c [--firmware-dir DIR] [--empty]
|
|
|
|
--firmware-dir DIR amdgpu firmware directory to embed (default: firmware/)
|
|
--empty emit an empty table (no embedded fallback)
|
|
|
|
Run via `make fw-table`; the Makefile regenerates the table whenever the
|
|
directory contents or the EMBED_FIRMWARE / FW_EMBED_DIR settings change.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import os
|
|
import sys
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
ROOT = os.path.dirname(HERE)
|
|
|
|
# Files in a linux-firmware amdgpu directory that are documentation or
|
|
# licensing, not firmware images.
|
|
SKIP_NAMES = {"WHENCE", "LICENSE", "LICENSE.amdgpu", "README", "README.md"}
|
|
SKIP_SUFFIXES = (".txt", ".md")
|
|
|
|
|
|
def firmware_files(directory):
|
|
"""Yield (relative path, absolute path) for every firmware image."""
|
|
for base, dirs, files in os.walk(directory):
|
|
dirs.sort()
|
|
for name in sorted(files):
|
|
if name.startswith(".") or name in SKIP_NAMES or name.endswith(SKIP_SUFFIXES):
|
|
continue
|
|
path = os.path.join(base, name)
|
|
if not os.path.isfile(path):
|
|
continue
|
|
rel = os.path.relpath(path, directory).replace(os.sep, "/")
|
|
yield rel, path
|
|
|
|
|
|
def c_identifier(index, rel):
|
|
safe = "".join(ch if ch.isalnum() else "_" for ch in rel)
|
|
return "fwblob_%d_%s" % (index, safe)
|
|
|
|
|
|
def emit_array(var, data):
|
|
lines = ["static const uint8_t %s[%d] = {" % (var, len(data))]
|
|
for start in range(0, len(data), 16):
|
|
row = data[start:start + 16]
|
|
lines.append(" " + "".join("%d," % b for b in row))
|
|
lines.append("};")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
parser.add_argument("--output", required=True)
|
|
parser.add_argument("--firmware-dir", default=os.path.join(ROOT, "firmware"))
|
|
parser.add_argument("--empty", action="store_true")
|
|
args = parser.parse_args()
|
|
|
|
entries = []
|
|
arrays = []
|
|
if not args.empty and os.path.isdir(args.firmware_dir):
|
|
for index, (rel, path) in enumerate(firmware_files(args.firmware_dir)):
|
|
with open(path, "rb") as f:
|
|
data = f.read()
|
|
if len(data) < 16:
|
|
print("fw2rodata: skipping %s (%d bytes)" % (rel, len(data)), file=sys.stderr)
|
|
continue
|
|
var = c_identifier(index, rel)
|
|
arrays.append(emit_array(var, data))
|
|
entries.append((var, len(data), "amdgpu/" + rel,
|
|
hashlib.sha256(data).hexdigest()[:8]))
|
|
|
|
out = [
|
|
"/* GENERATED by scripts/fw2rodata.py - do not edit or commit.",
|
|
" * Optional embedded firmware fallback: %d image(s) from %s."
|
|
% (len(entries), "an empty selection" if args.empty else
|
|
os.path.relpath(args.firmware_dir, ROOT)),
|
|
" * Provenance and redistribution terms: the WHENCE and license files",
|
|
" * shipped with that directory (linux-firmware). */",
|
|
"#include <stddef.h>",
|
|
"#include <stdint.h>",
|
|
'#include "fw/fw_rodata.h"',
|
|
"",
|
|
]
|
|
out.extend(arrays)
|
|
out.append("")
|
|
if entries:
|
|
out.append("static const struct fw_rodata_entry g_fw_rodata[] = {")
|
|
for var, size, name, digest in entries:
|
|
out.append('\t{ "%s", %s, %d, 0x%sul },' % (name, var, size, digest))
|
|
out.append("};")
|
|
out.append("")
|
|
out.append("const struct fw_rodata_entry *fw_rodata_entries(size_t *count)")
|
|
out.append("{")
|
|
out.append("\tif (count) *count = %d;" % len(entries))
|
|
out.append("\treturn %s;" % ("g_fw_rodata" if entries else "NULL"))
|
|
out.append("}")
|
|
out.append("")
|
|
|
|
os.makedirs(os.path.dirname(os.path.abspath(args.output)), exist_ok=True)
|
|
tmp = args.output + ".tmp"
|
|
with open(tmp, "w") as f:
|
|
f.write("\n".join(out))
|
|
os.replace(tmp, args.output)
|
|
|
|
total = sum(size for _, size, _, _ in entries)
|
|
print("fw2rodata: wrote %s (%d images, %.1f MB)"
|
|
% (args.output, len(entries), total / 1048576.0))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|