#!/usr/bin/env python3 """Build an IP discovery binary (the table upstream's amdgpu_discovery.c reads) from a JSON description of the device's IP blocks. The JSON is what scripts/capture-r9700-fixtures.py records from upstream's own exports of a running card (the ip_discovery sysfs tree and AMDGPU_INFO_DEV_INFO), or a synthetic description for the test harness's self-check. Format (all numbers decimal): {"format": 1, "dies": [{"die_id": 0, "ips": [{"hw_id": 11, "instance": 0, "major": 12, "minor": 0, "revision": 1, "harvest": 0, "base_addresses": [4800, ...]}, ...]}], "gc_info": {"num_se": 4, "num_wgp0_per_sa": 4, "num_wgp1_per_sa": 0, "num_rb_per_se": 4, "num_gl2c": 16, "num_gprs": 1536, "num_max_gs_thds": 32, "gs_table_depth": 32, "gsprim_buff_depth": 1792}} The binary has the layout of include/discovery.h, version 1 header, an IP discovery table of version 4 with 32-bit base addresses, and a GC table (gc_info v1.0); checksums as amdgpu_discovery_init verifies them. What the sysfs tree does not export is not invented: an IP's variant and sub-revision are 0, and there is no harvest, VCN, MALL or NPS table. """ import json import struct import sys BINARY_SIGNATURE = 0x28211407 DISCOVERY_TABLE_SIGNATURE = 0x53445049 GC_TABLE_ID = 0x4347 TOTAL_TABLES = 6 IP_DISCOVERY, GC = 0, 1 DISCOVERY_TMR_SIZE = 10 << 10 # amdgpu_discovery.h: the buffer upstream reads it into BINARY_HEADER = struct.Struct(" DISCOVERY_TMR_SIZE or total > 0xffff: raise ValueError(f"{total} bytes: more than upstream's {DISCOVERY_TMR_SIZE}-byte discovery buffer") tables = [(0, 0, 0)] * TOTAL_TABLES tables[IP_DISCOVERY] = (ip_table_offset, checksum(ip_table), ip_size) tables[GC] = (gc_offset, checksum(gc_table), gc_size) after_checksum = bytearray() after_checksum += struct.pack("