Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/kboot_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -4467,6 +4467,106 @@ static T6050_GPU_TEXT_FN int t6050_gpu_leakage(void *dt)
return -1;
}

#ifdef ESP_STAGE2
/*
* M5 Pro (G17S) operating points, filled in place where the DT has their tables on the GPU node:
* operating-points-v2 (opp-hz, then opp-microvolt and opp-microvolt-sram, one cell per voltage
* table), apple,afr-opp and apple,cs-opp (one cell), and apple,csafr-min-sram-microvolt.
*
* sgx perf-states and perf-states-sram are perf-state-table-count tables of perf-state-count
* {Hz, mV} states. afr-perf-states, which cs-perf-states must equal, is {u64 dies, u64 count}, then
* count {u64 uV, u64 Hz}, then the SRAM floor in uV.
*
* These are the part's own voltages; the driver keeps its board's states and takes them only where
* every frequency is its own. The shapes are checked before the first write. Returns 0 when filled,
* 1 for a DT without the tables, and a negative step when the ADT or the DT does not fit; the DT is
* then left as it is.
*
* Stage 2 only: Stage 1 has no room for it, and on these Macs Stage 1 loads the ESP's Stage 2,
* which boots Linux.
*/
#define T6050_GPU_TABLES 2

static T6050_GPU_TEXT_FN int t6050_gpu_opps(void *dt)
{
int gpu = kboot_fdt_path_exact(dt, "gpu"), len;
if (gpu < 0 || !fdt_getprop(dt, gpu, "operating-points-v2", NULL))
return 1;

int sgx = adt_path_offset(adt, "/arm-io/sgx");
u32 count, tables, clen, slen, alen, cslen;
if (sgx < 0 || ADT_GETPROP(adt, sgx, "perf-state-count", &count) != sizeof(count) ||
ADT_GETPROP(adt, sgx, "perf-state-table-count", &tables) != sizeof(tables) || count < 2 ||
count > MAX_PSTATES || tables != T6050_GPU_TABLES)
return -2;
const struct perf_state *core = adt_getprop(adt, sgx, "perf-states", &clen);
const struct perf_state *sram = adt_getprop(adt, sgx, "perf-states-sram", &slen);
if (!core || !sram || clen != tables * count * sizeof(*core) || slen != clen)
return -3;
/*
* State 0 is off. The tables share one rising frequency ladder. Linux checks every state and
* voltage against its board's, so only the shape is checked here.
*/
for (u32 k = 0; k < tables * count; k++) {
u32 i = k % count;
if (core[k].freq != core[i].freq || sram[k].freq != core[i].freq ||
(i ? core[i].freq <= core[i - 1].freq : core[i].freq))
return -4;
}

/* {dies, count}, count {uV, Hz}, the SRAM floor */
const u64 *aux = adt_getprop(adt, sgx, "afr-perf-states", &alen);
const void *cs = adt_getprop(adt, sgx, "cs-perf-states", &cslen);
if (!aux || !cs || alen < 3 * sizeof(u64) || aux[0] != 1 || aux[1] < 2 ||
aux[1] > MAX_PSTATES || alen != (3 + 2 * aux[1]) * sizeof(u64) || cslen != alen ||
memcmp(aux, cs, alen))
return -5;
u32 n = aux[1];
const u64 *st = aux + 2, sram_uv = aux[2 + 2 * n];

/*
* Pass 0 checks the DT tables' shape and pass 1 fills them, so nothing is written unless
* everything fits. In-place writes move no offset.
*/
static const char *const props[3] = {"operating-points-v2", "apple,afr-opp", "apple,cs-opp"};
for (int pass = 0; pass < 2; pass++) {
for (int t = 0; t < 3; t++) {
int cells = t ? 1 : tables, opp;
int table = t6040_opp_table(dt, gpu, props[t], t ? n : count, cells * sizeof(u32));
if (table < 0)
return -7;
u32 i = 0;
fdt_for_each_subnode(opp, dt, table)
{
const void *p = fdt_getprop(dt, opp, "opp-microvolt-sram", &len);
if (t ? p != NULL : !p || len != cells * (int)sizeof(u32))
return -7;
if (!pass)
continue;
fdt32_t volts[2 * T6050_GPU_TABLES];
for (int j = 0; j < cells; j++) {
volts[j] = cpu_to_fdt32(t ? st[2 * i] : core[j * count + i].volt * 1000);
if (!t)
volts[cells + j] = cpu_to_fdt32(sram[j * count + i].volt * 1000);
}
if (fdt_setprop_inplace_u64(dt, opp, "opp-hz", t ? st[2 * i + 1] : core[i].freq) ||
fdt_setprop_inplace(dt, opp, "opp-microvolt", volts, cells * sizeof(u32)) ||
(!t && fdt_setprop_inplace(dt, opp, "opp-microvolt-sram", volts + cells,
cells * sizeof(u32))))
return -8;
i++;
}
}
const void *floor = fdt_getprop(dt, gpu, "apple,csafr-min-sram-microvolt", &len);
if (!floor || len != sizeof(u32))
return -7;
}
if (fdt_setprop_inplace_u32(dt, gpu, "apple,csafr-min-sram-microvolt", sram_uv))
return -8;
return 0;
}
#endif

static T6050_GPU_INIT int dt_set_gpu_t6050(void *dt)
{
if (chip_id != T6050) {
Expand Down Expand Up @@ -4497,6 +4597,11 @@ static T6050_GPU_INIT int dt_set_gpu_t6050(void *dt)
printf("FDT: GPU: T6050: no apple,firmware-version for the driver\n");
if (t6050_gpu_leakage(dt))
printf("FDT: GPU: no leakage calibration words; the driver's generic configuration\n");
#ifdef ESP_STAGE2
int opps = t6050_gpu_opps(dt);
if (opps <= 0)
printf("FDT: GPU: T6050 operating points: %s (%d)\n", opps ? "not filled" : "filled", opps);
#endif
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/fuzz/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ libfdt="src/libfdt/fdt.c src/libfdt/fdt_ro.c src/libfdt/fdt_rw.c src/libfdt/fdt_
# The C files under test go next to nothing but themselves, so that their #include "utils.h"
# finds the host stand-in in tests/fuzz/host instead of src/utils.h
python3 -I tests/fuzz/src_copy.py "$out/src_copy.c"
cp src/kboot_audio.c src/devicetree.c "$out/"
cp src/kboot_audio.c src/adt_gpio.c src/devicetree.c "$out/"

# shellcheck disable=SC2086
$CC $cflags $incs tests/fuzz/adt_fuzz.c "$out/libadt_fuzz.a" -lpthread -ldl -lm -o "$out/adt-fuzz"
# shellcheck disable=SC2086
$CC $cflags -I"$root/tests/fuzz/host" $incs tests/fuzz/dt_fuzz.c "$out/src_copy.c" "$out/devicetree.c" \
"$out/kboot_audio.c" $libfdt "$out/libadt_fuzz.a" -lpthread -ldl -lm -o "$out/dt-fuzz"
"$out/kboot_audio.c" "$out/adt_gpio.c" $libfdt "$out/libadt_fuzz.a" -lpthread -ldl -lm -o "$out/dt-fuzz"
# pmgr.c includes ../build/build_cfg.h, which only a firmware build generates
mkdir -p "$out/src" "$out/build"
: >"$out/build/build_cfg.h"
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_fuzz_regress.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ def test_dt_fuzz_regress(adt_lib, tmp_path):
src.mkdir()
subprocess.run([sys.executable, "-I", str(FUZZ / "src_copy.py"), str(src / "src_copy.c")],
check=True)
for name in ("kboot_audio.c", "devicetree.c"):
for name in ("kboot_audio.c", "adt_gpio.c", "devicetree.c"):
shutil.copy(ROOT / "src" / name, src / name)
libfdt = [ROOT / "src/libfdt" / name for name in (
"fdt.c", "fdt_ro.c", "fdt_rw.c", "fdt_wip.c", "fdt_sw.c", "fdt_strerror.c", "fdt_check.c",
"fdt_empty_tree.c", "fdt_addresses.c")]
binary = build(adt_lib, "dt-replay",
[FUZZ / "dt_fuzz.c", src / "src_copy.c", src / "devicetree.c",
src / "kboot_audio.c", *libfdt],
src / "kboot_audio.c", src / "adt_gpio.c", *libfdt],
incs=["-I", str(FUZZ / "host")])
replay(binary, sorted(seeds.iterdir()) + sorted((FUZZ / "regress/dt").glob("*")))

Expand Down
8 changes: 5 additions & 3 deletions tests/python/test_t6041_atc_usb2_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

from pathlib import Path
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -500,9 +501,10 @@ def host_atc(tmp_path_factory):
# as long/unsigned long, colliding with src/types.h's u64/s64 typedefs. Pre-include a host-safe
# copy so TYPES_H is set before src/kboot_atc.h pulls in src/types.h.
types_h = (REPO / "src/types.h").read_text()
for line in ("typedef u64 uintptr_t;\n", "typedef s64 ptrdiff_t;\n", "typedef s64 ssize_t;\n"):
assert line in types_h
types_h = types_h.replace(line, "")
# Accept either spelling (u64/s64 or the compiler's __UINTPTR_TYPE__/__PTRDIFF_TYPE__).
for name in ("uintptr_t", "ptrdiff_t", "ssize_t"):
types_h, count = re.subn(rf"^typedef [A-Za-z0-9_]+ {name};\n", "", types_h, flags=re.M)
assert count == 1, name
types_h = types_h.replace("#include <stdint.h>\n", "#include <stdint.h>\n#include <sys/types.h>\n", 1)
(src / "types.h").write_text(types_h)
(src / "harness.c").write_text(HARNESS)
Expand Down
1 change: 1 addition & 0 deletions tests/run-t6031-handoff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ while IFS= read -r f; do
h="${f%.c}.h"
[ -f "src/$h" ] && cp "src/$h" "$tmp/src/"
done < "$root/tests/display-handoff-sources.txt"
mkdir -p "$tmp/build" && : > "$tmp/build/build_cfg.h" # a host build: no Stage 1 or Stage 2 options
for f in kboot_gpu.c kboot_gpu26.c kboot_gpu_t6031.c \
kboot_gpu_t8122_power.c kboot_t6031.c firmware.c pmgr.c; do
cp "src/$f" "$tmp/src/"
Expand Down
3 changes: 2 additions & 1 deletion tests/run-t6031-unchanged.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ run() { # run <out> <cmd...>
# ---- 1. the G15 GPU setup ----
build_g15() { # build_g15 <tree> <out>
tree=$1 out=$2
mkdir -p "$out/src"
mkdir -p "$out/src" "$out/build"
: > "$out/build/build_cfg.h" # a host build: no Stage 1 or Stage 2 options
cp "$tree/src/kboot_gpu.c" "$tree/src/firmware.c" "$tree/src/kboot_gpu26.c" "$out/src/"
cp tests/g15_host/*.h tests/kboot_gpu_g15_host.c "$out/src/"
extra="$tree/src/kboot_gpu_t8122_power.c"
Expand Down
Loading