interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / gpu_bench / check_regs.sh

142 lines · 7532 bytes Raw Blame History
1#!/bin/bash
2# scripts/gpu_bench/check_regs.sh -- check every AMD register number in the iMac GPU driver
3# against AMD's own MIT headers in the Linux kernel, so a made-up number cannot slip in again.
4#
5# What it checks
6# 1. native/kernel/gpu/amd_gpu_regs.h: every `#define mm...` register index and every
7# `*_MASK` / `*__SHIFT` field constant must equal the value in the real header, found
8# by name. When the line carries a citation `/* header.h:LINE */` the cited line must
9# be that exact define (so the citation cannot drift either). A define with no
10# citation is a failure.
11# 2. the SMU message ids and firmware-header offset cited from smu7_ppsmc.h / smu7.h.
12# 3. scripts/gpu_bench/STEP*.bench: a `bar5_*` line whose preceding comment names a
13# register (`# mmNAME, byte 0x...`) must use that register's real index, and the byte
14# offset in the comment must be index * 4. Any other BAR5 index must be a value that
15# amd_gpu_regs.h defines (or the line is marked with a `# raw` comment above it).
16#
17# Headers: Linux v6.12, drivers/gpu/drm/amd (asic_reg/* and pm/powerplay/inc/*), downloaded
18# once into $AMD_HDR_DIR (default ~/.cache/aienos-amd-hdr). Set AMD_HDR_DIR to a directory
19# that already holds the files to run offline.
20#
21# Usage: check_regs.sh Exit: 0 all match, 1 any mismatch or uncited define, 2 no headers.
22
23set -u
24here=$(cd "$(dirname "$0")" && pwd)
25root=$(cd "$here/../.." && pwd)
26regs=$root/native/kernel/gpu/amd_gpu_regs.h
27hdr=${AMD_HDR_DIR:-$HOME/.cache/aienos-amd-hdr}
28tag=${AMD_HDR_TAG:-v6.12}
29base=https://raw.githubusercontent.com/torvalds/linux/$tag/drivers/gpu/drm/amd
30
31mkdir -p "$hdr"
32for f in include/asic_reg/dce/dce_11_2_d.h include/asic_reg/dce/dce_11_2_sh_mask.h \
33 include/asic_reg/gmc/gmc_8_1_d.h include/asic_reg/gmc/gmc_8_1_sh_mask.h \
34 include/asic_reg/gca/gfx_8_0_d.h include/asic_reg/gca/gfx_8_0_sh_mask.h \
35 include/asic_reg/oss/oss_3_0_d.h include/asic_reg/oss/oss_3_0_sh_mask.h \
36 include/asic_reg/smu/smu_7_1_3_d.h include/asic_reg/smu/smu_7_1_3_sh_mask.h \
37 include/asic_reg/bif/bif_5_1_d.h include/asic_reg/bif/bif_5_1_sh_mask.h \
38 pm/powerplay/inc/smu7_ppsmc.h pm/powerplay/inc/smu7.h; do
39 b=$(basename "$f")
40 [ -s "$hdr/$b" ] && continue
41 curl -sfL -o "$hdr/$b" "$base/$f" || { echo "cannot fetch $base/$f"; rm -f "$hdr/$b"; exit 2; }
42done
43
44fail=0
45checked=0
46bad() { echo "FAIL $*"; fail=$((fail + 1)); }
47
48# real_line NAME -> "file:line:value" of the unique `#define NAME value` in the headers
49real_line() {
50 grep -n "^#define $1[[:space:]]" "$hdr"/*.h | head -1 |
51 awk -F'[: \t]+' '{ f=$1; sub(".*/", "", f); printf "%s:%s:%s\n", f, $2, $5 }'
52}
53
54# ---- 1. amd_gpu_regs.h -------------------------------------------------------------------
55while IFS= read -r line; do
56 name=$(echo "$line" | awk '{print $2}')
57 case "$name" in
58 mm[A-Za-z0-9_]*|*_MASK|*__SHIFT) ;;
59 *) continue ;;
60 esac
61 val=$(echo "$line" | awk '{print $3}')
62 case "$val" in 0x[0-9a-fA-F]*) ;; *) continue ;; esac # macros are not literal numbers
63 val=${val%u}
64 cite=$(echo "$line" | sed -n 's|.*/\* \([a-z0-9_]*\.h\):\([0-9]*\).*|\1:\2|p')
65 checked=$((checked + 1))
66 real=$(real_line "$name")
67 if [ -z "$real" ]; then bad "$name is not in any AMD header (ours $val)"; continue; fi
68 rfile=${real%%:*}; rest=${real#*:}; rline=${rest%%:*}; rval=${rest#*:}
69 if [ $((val)) -ne $((rval)) ]; then
70 printf 'FAIL %s ours=%s real=%s (%s:%s)\n' "$name" "$val" "$rval" "$rfile" "$rline"
71 fail=$((fail + 1)); continue
72 fi
73 if [ -z "$cite" ]; then bad "$name has no header:line citation (should be $rfile:$rline)"; continue; fi
74 if [ "$cite" != "$rfile:$rline" ]; then bad "$name cites $cite but the define is at $rfile:$rline"; fi
75done < <(grep '^#define ' "$regs")
76
77# ---- 2. SMU ids and firmware header layout -----------------------------------------------
78# Run against the same cites the header gives: message id by name, struct layout by text.
79smu_msg() { # NAME EXPECTED_HEX
80 local r; r=$(grep -n "^#define $1[[:space:]]" "$hdr/smu7_ppsmc.h" | head -1)
81 local v; v=$(echo "$r" | grep -o '0x[0-9A-Fa-f]*' | head -1)
82 checked=$((checked + 1))
83 [ -n "$v" ] && [ $((v)) -eq $(($2)) ] || bad "$1 expected $2, smu7_ppsmc.h has '${v:-nothing}'"
84}
85for pair in $(grep -o '^#define PPSMC_[A-Za-z_]* *0x[0-9a-fA-F]*u\?' "$regs" | awk '{print $2 ":" $3}'); do
86 v=${pair#*:}; smu_msg "${pair%%:*}" "${v%u}"
87done
88checked=$((checked + 1))
89loc=$(grep -o 'define SMU7_FIRMWARE_HEADER_LOCATION *0x[0-9a-fA-F]*' "$hdr/smu7.h" | awk '{print $3}')
90ours=$(grep '^#define SMU7_FIRMWARE_HEADER_LOCATION' "$regs" | awk '{print $3}'); ours=${ours%u}
91[ -n "$loc" ] && [ -n "$ours" ] && [ $((loc)) -eq $((ours)) ] || bad "SMU7_FIRMWARE_HEADER_LOCATION ours=${ours:-none} real=${loc:-none}"
92# Version is the 6th dword of SMU7_Firmware_Header (after Digest[5]): byte offset 0x14.
93checked=$((checked + 1))
94if grep -q 'uint32_t Digest\[5\];' "$hdr/smu7.h" &&
95 [ "$(grep -A1 'uint32_t Digest\[5\];' "$hdr/smu7.h" | tail -1 | tr -d ' ;')" = "uint32_tVersion" ]; then
96 want=$((5 * 4))
97else
98 want=-1
99fi
100ours=$(grep '^#define SMU7_FIRMWARE_HEADER_Version_offset' "$regs" | grep -o '(\?[0-9u* ]*)\?' | head -1)
101oursn=$(grep '^#define SMU7_FIRMWARE_HEADER_Version_offset' "$regs" | sed 's/^#define [A-Za-z0-9_]* *//; s|/\*.*||; s/u//g')
102[ -n "$oursn" ] && [ $((oursn)) -eq "$want" ] || bad "SMU7_FIRMWARE_HEADER_Version_offset ours='${oursn:-none}' real=$want"
103
104# ---- 3. bench scripts --------------------------------------------------------------------
105defined=" "
106while IFS= read -r line; do
107 v=$(echo "$line" | awk '{print $3}'); v=${v%u}
108 case "$v" in 0x[0-9a-fA-F]*) defined="$defined$((v)) " ;; esac
109done < <(grep '^#define mm' "$regs")
110
111for f in "$here"/STEP*.bench; do
112 bn=$(basename "$f"); n=0; lastname=""; lastbyte=""; raw=0
113 while IFS= read -r line || [ -n "$line" ]; do
114 n=$((n + 1))
115 case "$line" in
116 '#'*)
117 nm=$(echo "$line" | grep -o 'mm[A-Z0-9_]*' | head -1)
118 [ -n "$nm" ] && { lastname=$nm; lastbyte=$(echo "$line" | sed -n 's/.*byte \(0x[0-9A-Fa-f]*\).*/\1/p'); }
119 case "$line" in '# raw'*) raw=1 ;; esac
120 continue ;;
121 esac
122 set -- $line
123 case "${1:-}" in bar5_read|bar5_write|bar5_rmw|bar5_poll) ;; *) lastname=""; lastbyte=""; raw=0; continue ;; esac
124 idx=$(($2)); checked=$((checked + 1))
125 if [ -n "$lastname" ]; then
126 real=$(real_line "$lastname")
127 if [ -z "$real" ]; then bad "$bn:$n comment names $lastname, which is not in any AMD header"
128 elif [ $((${real##*:})) -ne "$idx" ]; then
129 bad "$bn:$n $lastname is ${real##*:} in ${real%%:*}, the script uses $(printf '0x%x' "$idx")"
130 fi
131 if [ -n "$lastbyte" ] && [ $((lastbyte)) -ne $((idx * 4)) ]; then
132 bad "$bn:$n comment byte $lastbyte is not index $(printf '0x%x' "$idx") * 4"
133 fi
134 elif [ "$raw" -eq 0 ]; then
135 case "$defined" in *" $idx "*) ;; *) bad "$bn:$n index $(printf '0x%x' "$idx") is not a register in amd_gpu_regs.h (add a '# mmNAME, byte' comment or '# raw')" ;; esac
136 fi
137 lastname=""; lastbyte=""; raw=0
138 done < "$f"
139done
140
141if [ "$fail" -ne 0 ]; then echo "check_regs: $fail problem(s) in $checked check(s)"; exit 1; fi
142echo "check_regs: $checked check(s), all match the AMD headers ($tag)"