firmware

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

可切换的压缩固件

统一控制协议和 DMA shell,每个 bitstream 包含一种算法:gzip、LZ4、Zstd、MinLZ 或 raw。 整卡切换替换压缩核,不要求将所有算法同时放进 KU5P。压缩数据保留标准 格式,切换硬件不影响已有数据的 CPU 解码。独立 HLS 适配器位于 multialgo。 板级 shell RTL、约束、派生补丁、整卡构建与 shell 仿真维护在独立私有工程; 本仓库通过外部 manifest 使用已构建的镜像,不分发这些设计文件或 bitstream。 优化版 LZ4/Zstd 的构建 ID、性能、源码归档与切换路径见 固件优化实测。

CGO_ENABLED=0 GOWORK=off go build -o bin/fpga-fw ./cmd/fpga-fw
CGO_ENABLED=0 GOWORK=off go build -o bin/fpga-compress ./cmd/fpga-compress
bin/fpga-fw inspect
bin/fpga-fw check --manifest /path/to/image/manifest.json
bin/fpga-fw switch --manifest /path/to/image/manifest.json \
  --pci 0000:39:00.0 \
  --target localhost:3121/xilinx_tcf/Digilent/210251A08870 \
  --vivado /opt/Xilinx/2025.2/Vivado/bin/vivado
bin/fpga-compress --algorithm lz4 --input data.bin --output data.lz4

PCI 地址和 JTAG 序列号是本机示例,其他主机需填写实际值。switch 要求 root, 只写易失 SRAM。它先核对 SHA256 并保存私有副本,阻止新任务进入,等待现有 SDK 句柄释放,检查所有 XDMA 描述符,再卸载驱动、移除目标 PCI 功能、装载、 扫描原父桥、恢复 XDMA,最后逐核校验算法、ABI、格式、核数和构建 ID。 任一步失败会返回错误;PCI/驱动恢复不等于旧 bitstream 恢复。回滚使用同一条 switch 命令指定之前验证过的 gzip manifest。该操作不写 Flash。

应用必须在每次事务结束后调用 Device.Close / Lease.Close。新版 SDK 在 每个句柄上持有共享锁,切换工具持有独占锁;切换期间新事务得到 ErrSwitching, 可以排队或回退 CPU。锁覆盖同一 Unix 用户,跨用户旧程序由 /proc 检查和 驱动的使用计数阻止卸载,不会强杀其他程序。不同用户或非协作程序应先停止提交。

SDK 接口

根包的 CompressReader、Writer 等原有接口继续输出 gzip。多算法可用根包的 CompressAlgorithm(ctx, "lz4", src, size, dst, hw, options, nil),或公开包 github.com/lfhy/fpga-gzip/accelerator。InspectFirmware(ctx, userDevice) 只查询身份,不打开 DMA、不重置压缩核。所有接口无 cgo、无 internal 包。

hw, err := device.Open(device.Paths{
    User: "/dev/xdma0_user", H2C: "/dev/xdma0_h2c_0", C2H: "/dev/xdma0_c2h_0",
})
if err != nil { return err }
defer hw.Close()
result, err := accelerator.CompressReader(ctx, accelerator.LZ4,
    bytes.NewReader(raw), uint64(len(raw)), dst, hw, accelerator.DefaultOptions(), nil)

算法不匹配在 RESET、START、DMA 之前返回 firmware.ErrAlgorithm。 未知 ABI、格式或未初始化 BAR 返回 firmware.ErrIncompatible。已验证的旧 gzip EOP 镜像只识别为 legacy gzip,不能被当作其他算法使用。其他没有身份信息的旧 镜像不会自动接受,需要通过适配或重新构建加入版本信息。

gzip 使用已有的头部、CRC32、ISIZE 校验。LZ4 和 Zstd HLS 输出没有内容校验和, 因此通用 SDK 同时用有界纯 Go 解码器验证整个输出,再比对输入 CRC32 和长度; 这部分消耗 CPU,计入 Result.Total。MinLZ 同样完整解码,额外检查流内 CRC32C 和 EOF;其核实现及范围见 MinLZ 指导。raw 直接校验长度和 CRC32。 输入在调用结束前必须保持不变。空输入暂不支持。库不改变应用的 GOMAXPROCS。 文件 CLI 在验证并同步临时文件后才发布目标文件。

HTTP / Unix 服务

CGO_ENABLED=0 GOWORK=off go build -o bin/fpga-gzipd ./cmd/fpga-gzipd
bin/fpga-gzipd --unix /run/fpga-compress.sock --cores 4 \
  --algorithms gzip,lz4,zstd,minlz,raw

使用 client.CompressAlgorithm(ctx, firmware.LZ4, src, size, dst) 或 POST /compress?algorithm=lz4,请求仍需完整 Content-Length。只接受与当前镜像 匹配的算法,未装载时 HTTP 409。认证沿用 WithAuth,默认关闭。 多算法响应包含 X-FPGA-Algorithm,并在成功后给出 X-FPGA-Complete: 1 和 输出长度 trailer。SDK 客户端检查该完成标志,避免将传输中途失败误判为成功。

同一卡的 DMA 核由一个调度进程管理:使用此 daemon,或使用 compactfs 的算法 worker。应用通过该进程提交请求;各进程的 device.Pool 不共享核心租借状态。

ABI v1

控制 bank 为 core * 0x1000。原有 0x00..0x40 的 DMA 控制和 EOP 保持不变。 下列字段只读,每个有效 bank 内容相同。

偏移 含义
0x44 Magic 0x434d5058,CMPX
0x48 ABI,major:minor 各 16 bit,当前 0x00010000
0x4c 算法 ID:1 raw、2 gzip、3 LZ4、4 Zstd、5 MinLZ
0x50 编码格式版本,当前 1
0x54 可用独立 DMA 核数,1–4
0x58 特性位:bit 0 EOP、bit 1 标准帧
0x5c/0x60 构建 ID 低/高 32 bit

新算法首先增加稳定 ID、格式验证器和固件 profile,再接入压缩核。 manifest.json 与 .bit 同目录,包含 SHA256、算法、构建 ID、核数和验证阶段; 只有通过时序检查的镜像才生成可装载 manifest,首次上板仍需完整解码验证。

Documentation

Overview

Package firmware describes the versioned compression shell shared by FPGA images. Algorithm IDs identify the bytes on C2H, independently of the SDK's historical gzip API and of the number of compression engines in an image.

Index

Constants

View Source
const (
	RegMagic             uint32 = 0x44
	RegABI               uint32 = 0x48
	RegAlgorithm         uint32 = 0x4c
	RegFormat            uint32 = 0x50
	RegCores             uint32 = 0x54
	RegFeatures          uint32 = 0x58
	RegBuildLo           uint32 = 0x5c
	RegBuildHi           uint32 = 0x60
	Magic                uint32 = 0x434d5058 // CMPX
	ABI                  uint32 = 0x00010000 // major 1, minor 0
	FormatV1             uint32 = 1
	FeatureEOP           uint32 = 1 << 0
	FeatureStandardFrame uint32 = 1 << 1
	LegacyEOP            uint32 = 0x475a0001
)

Variables

View Source
var (
	ErrIncompatible = errors.New("incompatible FPGA firmware")
	ErrAlgorithm    = errors.New("FPGA firmware algorithm mismatch")
)
View Source
var ErrSwitching = errors.New("FPGA firmware switch is in progress")

Functions

func DeviceOwners

func DeviceOwners() ([]string, error)

DeviceOwners reports all XDMA descriptors because unloading a shared xdma module affects every card. It reads only descriptor links and process names.

func StageBitstream

func StageBitstream(bitPath, checksum string) (string, func(), error)

StageBitstream copies and hashes an artifact before any device is quiesced. Programming the private verified copy prevents replacement during the switch.

Types

type Algorithm

type Algorithm uint32
const (
	Raw Algorithm = 1 + iota
	Gzip
	LZ4
	Zstd
	MinLZ
)

func ParseAlgorithm

func ParseAlgorithm(name string) (Algorithm, error)

func (Algorithm) String

func (a Algorithm) String() string

type CardLock

type CardLock struct {
	// contains filtered or unexported fields
}

CardLock serializes programming against all cooperating DMA handles of the current Unix user. An exclusive gate first stops new admissions, then the activity lock waits for existing transactions to close. SIGKILL releases both.

func LockProgramming

func LockProgramming(ctx context.Context, device string) (*CardLock, error)

LockProgramming blocks new DMA opens, then drains existing handles. It does not stop processes or unload drivers; the caller owns the programming steps.

func LockTransaction

func LockTransaction(device string) (*CardLock, error)

LockTransaction is nonblocking so callers can apply backpressure or use CPU fallback while an image is being loaded. Keep the returned lock until all stream syscalls have finished and the BAR has closed.

func (*CardLock) Close

func (l *CardLock) Close() error

type Info

type Info struct {
	Magic     uint32    `json:"magic"`
	ABI       uint32    `json:"abi"`
	Algorithm Algorithm `json:"algorithm_id"`
	Format    uint32    `json:"format"`
	Cores     uint32    `json:"cores"`
	Features  uint32    `json:"features"`
	Build     uint64    `json:"build"`
	Legacy    bool      `json:"legacy"`
}

Info is read-only and repeated in each independent 0x1000-byte control bank. Legacy is true only for the previously validated gzip EOP shell. An unrecognized/uninitialized BAR is never inferred to be a compression image.

func Inspect

func Inspect(ctx context.Context, r RegisterReader) (Info, error)

func InspectBAR

func InspectBAR(ctx context.Context, user string, bank uint32) (Info, error)

InspectBAR opens only the user BAR and performs read-only identity queries. The caller must hold a transaction or programming lock for the complete call.

func Require

func Require(ctx context.Context, hardware any, want Algorithm) (Info, error)

Require runs before RESET, START and DMA. Non-device gzip simulators without register access retain the original Hardware interface. Every other algorithm requires an explicit, versioned identity; legacy firmware is gzip-only.

func Switch

func Switch(ctx context.Context, manifest Manifest, bitPath string, options SwitchOptions) (Info, error)

Switch programs volatile SRAM through JTAG. It never writes configuration Flash. The complete artifact is verified before blocking admissions, and the programming lock is retained through post-load identity verification. Older clients are checked through /proc and must finish before driver unload.

func (Info) Validate

func (i Info) Validate() error

type Manifest

type Manifest struct {
	Schema     uint32 `json:"schema"`
	Name       string `json:"name"`
	Part       string `json:"part"`
	Algorithm  string `json:"algorithm"`
	ABI        uint32 `json:"abi"`
	Format     uint32 `json:"format"`
	Cores      uint32 `json:"cores"`
	Build      uint64 `json:"build"`
	Legacy     bool   `json:"legacy,omitempty"`
	Bitstream  string `json:"bitstream"`
	SHA256     string `json:"sha256"`
	Validation string `json:"validation"`
	Source     string `json:"source,omitempty"`
}

Manifest travels alongside an immutable bitstream. JSON here is an artifact description, not filesystem metadata. All images share ABI/format IDs; each image declares its actual algorithm, available DMA banks and build ID.

func LoadManifest

func LoadManifest(path string) (Manifest, string, error)

func (Manifest) Check

func (m Manifest) Check(info Info) error

func (Manifest) Validate

func (m Manifest) Validate() error

type Provider

type Provider interface {
	FirmwareInfo(context.Context) (Info, error)
}

type RegisterReader

type RegisterReader interface {
	ReadReg32(context.Context, uint32) (uint32, error)
}

type SwitchOptions

type SwitchOptions struct {
	UserDevice string
	PCI        string
	Target     string
	Vivado     string
	HWServer   string
	Log        io.Writer
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL