Repository navigation
Firmware
Part III of the system development guide.
Modern GPUs run several microcontrollers (for power management, display, video decoding, security) whose programs the driver must load at attach: firmware files, shipped separately from the driver and usually under the vendor's own licence. This chapter covers how drivers get them on NetBSD and on Linux, and how Linux drivers' calls are served on aiBSD.
firmware_handle_t fh;
error = firmware_open("amdgpu", "navi10_sos.bin", &fh);
size = firmware_get_size(fh);
buf = firmware_malloc(size);
error = firmware_read(fh, 0, buf, size);
firmware_close(fh);
...
firmware_free(buf, size);- Files are looked up as
<path>/<driver>/<name>for each directory in thehw.firmware.pathsysctl, by default/libdata/firmwareand/usr/libdata/firmware. The driver name is the first argument, so each driver has its own directory. - Reading files needs the root file system, so a driver that needs
firmware at attach defers that part with
config_mountroot()(see devices). Firmware for devices needed before root is mounted must be built into the kernel or the driver must cope without it. -
firmware_malloc()/firmware_free()allocate the buffer; reading is in one or morefirmware_read()calls at given offsets.
In each directory of the path, firmware_open() tries name and,
if that is missing, name.xz, before moving to the next directory, as
Linux's loader does. An uncompressed file therefore overrides a
compressed one next to it, which is how a developer tries a new image
without touching the installed set.
A plain file is read in place: the handle keeps the vnode and each
firmware_read() reads from it. An .xz file is decompressed whole
at open, and the handle serves reads from memory; drivers see the same
size and bytes either way. Decompression uses XZ Embedded
(sys/external/bsd/xz-embedded, 0BSD, the decoder Linux carries as
lib/xz), built as the attribute xz_dec, which firmload depends on:
- The uncompressed size is known before decoding: an xz stream ends
with a 12-byte footer (magic
YZ) giving the size of the index just before it, and the index lists every block's compressed and uncompressed size. firmload sums them (firmware_xz_size()), caps the total (256 MiB), and allocates exactly that. - XZ Embedded runs in single-call mode (
XZ_SINGLE): input and output are whole buffers, and LZMA2's dictionary (the window of recent output that back-references copy from) is the output buffer itself. Decoding needs no memory beyond the image, whatever dictionary size the file was compressed with; that is why the build can use the strongest settings (xz -9e). - The decoder checks the index against the data and the integrity
check (CRC32 or CRC64) of each block, so a truncated or corrupt file
fails at open with
EFTYPE, not later inside the driver. - XZ Embedded is built in its portable configuration
(
include/xz_config.h): NetBSD's kernel does not define__KERNEL__, and the header supplies what the decoder expects (__always_inlinewith theinlinekeyword, among others). Rump kernels build the same files intolibrumpvfs, so firmload behaves identically there.
const struct firmware *fw;
err = request_firmware(&fw, "amdgpu/navi10_sos.bin", adev->dev);
/* fw->data, fw->size */
release_firmware(fw);- Names are paths relative to
/lib/firmware, and include the driver's directory themselves (amdgpu/...,i915/...). - The device argument is used for messages and for the device the request is made for (user-space fallback loading, which is mostly disabled today).
- Variants:
request_firmware_direct()(no fallback, no warning),firmware_request_nowarn()(no warning if missing — drivers probe for optional files this way),request_firmware_nowait()(load in the background and call a callback),request_firmware_into_buf(). - Firmware can also be built into the kernel image.
linux_firmware.c implements the Linux calls on firmload(9):
- The driver directory is the NetBSD driver name of the device's
NetBSD counterpart:
device_cfdriver(linux_device_bsddev(dev))-> cd_name. (linux_device_bsddev()walks up the Linux device's parents to the nearest one that stands for a NetBSD device — see devices.) - If the Linux name starts with that directory (
amdgpu/foo.binfor driveramdgpu), the prefix is stripped, so the file is found at/libdata/firmware/amdgpu/foo.binas NetBSD expects. Names under another directory are looked for under the driver's (a possible source of collisions, noted in the code). -
request_firmware()reads the whole file into afirmware_malloc()buffer;release_firmware()frees it. NetBSD errnos are negated. -
request_firmware_direct()andfirmware_request_nowarn()are the same call (NetBSD prints nothing when a file is missing). -
request_firmware_nowait()queues the load on a workqueue and calls the callback with the firmware or NULL, holding a reference to the module meanwhile.
Driver names that differ between the systems matter here: a NetBSD
attachment named differently from the Linux driver directory (as
i915drmkms is from i915) needs its firmware installed under the
NetBSD name, or a mapping. No i915 firmware is installed (the gpufw
set has amdgpu, radeon and nouveau); Intel's display (DMC)
firmware, and GuC/HuC for newer GPUs, come with the i915 port.
GPU vendors publish their firmware in Linux's linux-firmware
repository, with a WHENCE file that lists every file, its licence,
and links: alternative names under which a file is also loaded
(one image serving several chips, or a directory renamed). The tree
uses it directly:
-
The source is a git submodule at
external/linux-firmware/dist, pinned at a commit and fetched shallow (git submodule update --init external/linux-firmware/dist).FIRMWARESRCDIR(bsd.own.mk) names it and can point at any other checkout. No firmware binaries live in the source tree's own history; updating firmware is moving the submodule. -
What a driver can load is declared by the driver. Linux drivers name every file they may request with
MODULE_FIRMWARE("amdgpu/..."). Linux records these in the module's.modinfo; here the macro (drm2/include/linux/module.h) puts each name, NUL-terminated, into an ELF sectionlinux_firmwareof the object, and the linker concatenates the sections of a module's objects. Reading the list back from the built module (objcopy -O binary -j linux_firmware) gives the names after every macro and#ifdef CONFIG_*has been evaluated, which no search of the sources can do (nouveau composes GSP image names from version macros; features configured off drop their files). -
external/linux-firmware/firmware.shturns that into what the build installs, per driver directory (amdgpu,radeon,nouveau):Command Reads Writes lists MODOBJDIR...each built <driver>.kmod'slinux_firmwaresection, for every architecture given<driver>/DECLARED, sorted and uniqueresolve [LINUXFW]DECLARED, linux-firmware and itsWHENCElinks (which may name directories), minus the patterns in<driver>/EXCLUDE<driver>/FIRMWARE: a file per line, orname target relative-targetfor a link; names linux-firmware lacks go to<driver>/MISSINGsetsFIRMWAREthe gpufwset list and the mtree specification (etc/mtree/NetBSD.dist.base); entries no longer installed are marked obsolete, so upgrades remove themEXCLUDEdrops files a driver declares but this build never loads (nouveau declares two GSP firmware generations and uses one).MISSINGlists names a driver asks for that the pinned linux-firmware does not have: chips the driver supports before their firmware is published, or names it only tries. Runlistsafter a driver update,resolveandsetsafter that or after moving the submodule; the three outputs are committed, so a normal build reads onlyFIRMWARE. -
The build (
external/linux-firmware/firmware.mk, one directory per driver) installs each listed file under/libdata/firmware/<driver>/compressed (xz -9e -T1 --check=crc32: one thread, so every build host produces the same bytes), each link as a symbolic link to the compressed target, and the vendor's licence file. The driver prefix of the Linux name is dropped (amdgpu/navi10_sos.binbecomes/libdata/firmware/amdgpu/navi10_sos.bin.xz), which is wherelinux_firmware.cand firmload look.
Sets. The binary set is still gpufw, so the installer handles
it as before; its entries carry the build flags amdgpufirmware,
radeonfirmware, nouveaufirmware. The source sets leave the
submodule (gigabytes) out of src.tgz and put the files the build
installs, with WHENCE and the licences, into firmwaresrc.tgz
(distrib/sets/makesrctars).
Building without the submodule. A plain checkout without
submodules must still build. When FIRMWARESRCDIR has no WHENCE,
MKAMDGPUFIRMWARE, MKRADEONFIRMWARE and MKNOUVEAUFIRMWARE default
to no (unless set explicitly), the set lists follow, and
external/linux-firmware prints how to fetch it. Setting an option
to yes without the source is an error. A release must not ship
without GPU firmware by accident, though: an installed system whose
gpufw lacks amdgpu's files has a driver that fails on users'
machines. So when the options are off only because the source is
missing on a port that ships firmware by default, bsd.own.mk sets
FIRMWARE_MISSING, and maketars stops (check_FIRMWARE in
distrib/sets/Makefile) with the two ways out: fetch the submodule,
or set the options to no explicitly.
Compatibility with stock NetBSD. An aiBSD kernel reads NetBSD's
gpufw set, whose files are uncompressed: firmload finds name before
trying name.xz. A stock NetBSD kernel does not read aiBSD's set,
whose files exist only as name.xz: its firmload has no xz support.
Installing an aiBSD system from NetBSD's sets keeps working; mixing a
NetBSD kernel with aiBSD's firmware does not.
-
sysctl hw.firmware.path; list/libdata/firmware/amdgpu. -
objcopy -O binary -j linux_firmware amdgpu.kmod /dev/stdout | tr '\0' '\n'lists the firmware a module declares. -
xz -l /libdata/firmware/amdgpu/<file>.xzshows the sizes the index records, which firmload reads before decoding. - With a driver attached, watch for missing-firmware errors with
dmesg | grep -i firmware.
firmload(9), sys/dev/firmload.c;
sys/external/bsd/xz-embedded/ (dist/linux/lib/xz, include/xz_config.h,
conf/files.xz_dec);
sys/external/bsd/drm2/linux/linux_firmware.c;
sys/external/bsd/drm2/include/linux/module.h (MODULE_FIRMWARE);
external/linux-firmware/ (firmware.sh, firmware.mk,
<driver>/{DECLARED,FIRMWARE,MISSING,EXCLUDE});
share/mk/bsd.own.mk (FIRMWARESRCDIR, MK*FIRMWARE,
FIRMWARE_MISSING); distrib/sets/Makefile (check_FIRMWARE),
distrib/sets/makesrctars (firmwaresrc.tgz);
distrib/sets/lists/gpufw/mi.
aiBSD system development guide
Tutorial
II — Building
III — The kernel
- Kernel basics
- Booting and kernel modules
- The boot loader
- Threads, processes, tasks
- Devices and drivers
- Files, descriptors, device nodes
- PCI
- Virtual memory
- Memory and DMA
- Page tables, TLBs, memory types
- Firmware
- State and events
- The console
- Power management
IV — Linux compat layer
V — DRM
VI — Testing
VII — Upstreams
Appendices