Skip to content
Merged
168 changes: 143 additions & 25 deletions src/kboot_display_handoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
* same handoff checks those against the ADT instead of adding them. A board without a panel (the
* J775D, Mac Studio, M5 Ultra), whose display subsystem scans out through one external engine's
* display DART: t6050_headless_handoff() hands its external engines over with the display
* subsystem.
* subsystem. The ESP Stage 2 also hands off the external engines beside a panel the device tree
* enables itself (t6050_adopted()) and pins the images whose DCP ABI is not the OS firmware's.
*
* Fail-soft: dt_set_display_handoff() always returns 0; a failed check is logged and changes
* nothing. Transactional: DT edits are made on a scratch copy that replaces the DT only if every
Expand Down Expand Up @@ -5091,12 +5092,26 @@ static bool dcp6_dt_describes(void *dt)
* iBoot leaves the DCP running and both of its DARTs locked on four-level roots. The kernel adopts
* that session and resolves firmware DVAs through the live tables, so this publishes no
* iommu-addresses: only what the ADT says, checked against the device tree and the registers.
* The image is pinned per OS firmware because 27.0 and 27.0.1 report the same iBoot version.
* A row is picked by the image id the nub reports (ADT uuid). A row without dcp_fw gives the DCP
* nodes the OS firmware's version, so it also pins that OS firmware (27.0 and 27.0.1 report the
* same iBoot version). A row with dcp_fw names the image's DCP ABI itself and is picked by its
* image id alone: no other handoff step reads the OS firmware.
*/
#ifdef ESP_STAGE2
/*
* The DCP image of 27.3 beta 1 takes the 27.3 method table: its nodes get 27.3.0, which the kernel
* maps to that table, rather than the beta's OS firmware numbers (27.2.98.1). Its row is built
* into the ESP Stage 2 only: a Stage 1 booting Linux directly keeps the boot framebuffer on it.
*/
static const struct fw_version_info t6050_fw_27_3 = {V27_3B, "27.3", {27, 3, 0}, 3, NULL};
#endif

static const struct {
u32 os_fw;
u32 os_fw; /* the OS firmware the image was measured on; matched only without dcp_fw */
char uuid[37];
bool ext; /* also hand off the external display engines running this image */
/* The firmware version the DCP nodes get, if not the OS firmware's (see t6050_fw_27_3) */
const struct fw_version_info *dcp_fw;
} t6050_images[] = {
{V27_0, "64863924-B56B-3E32-87FE-038677F52709", true},
/* K116s (16" M5 Pro, OLED), 26B8125 */
Expand All @@ -5105,6 +5120,10 @@ static const struct {
* machine; the image id is not shared across T6050 boards, so the rows pair with the
* image the ADT reports below. The J775D's (M5 Ultra) dcpext engines run it too */
{V27_2, "C51FB0C8-C45D-39B0-85BC-520E600FC950", true},
#ifdef ESP_STAGE2
/* J714c (14" M5 Max) on 27.3 beta 1 (26C5035, mBoot-20457.60.65.0.1) */
{V27_3B, "58CB3FFC-B2BA-352E-B20B-71A91B15FA9F", true, &t6050_fw_27_3},
#endif
};
#define T6050_NDISP 5 /* disp0 reg[0..4] are the dcp's disp-0..disp-4 */
#define T6050_BW_BASE 0x20000 /* bandwidth agent 0's scratch in the PMC metadata */
Expand Down Expand Up @@ -5148,7 +5167,8 @@ static struct {
const char *uuid; /* the pinned image of the running OS firmware */
u32 ext_aura; /* external engines Aura enabled itself (bit n for dcpextN) */
bool ext;
bool headless; /* no panel: t6050_headless_handoff() */
const struct fw_version_info *dcp_fw; /* the version the DCP nodes get */
bool headless; /* no panel: t6050_headless_handoff() */
bool prewired; /* clocks and bandwidth scratch prewired: t6050_prewired() */
} t6050;

Expand Down Expand Up @@ -5252,17 +5272,19 @@ static T6050_TEXT bool t6050_prewired(void *dt)
}

/*
* Picks the row of the running OS firmware and the image a nub reports: the internal DCP's, or on
* a board without a panel the engine the display subsystem scans out through. False if no row
* pairs the two.
* Picks the row of the image a nub reports: the internal DCP's, or on a board without a panel the
* engine the display subsystem scans out through. A row whose DCP nodes take the OS firmware's
* version must also be the running OS firmware's; one with its own dcp_fw is picked by the image
* alone. False if no row matches.
*/
static T6050_TEXT bool t6050_pick_image(const char *uuid, u32 len)
{
for (size_t i = 0; i < ARRAY_SIZE(t6050_images); i++)
if (os_firmware.version == t6050_images[i].os_fw && dcp6_is_uuid(uuid, len) &&
!strcmp(uuid, t6050_images[i].uuid)) {
if (dcp6_is_uuid(uuid, len) && !strcmp(uuid, t6050_images[i].uuid) &&
(t6050_images[i].dcp_fw || os_firmware.version == t6050_images[i].os_fw)) {
t6050.uuid = t6050_images[i].uuid;
t6050.ext = t6050_images[i].ext;
t6050.dcp_fw = t6050_images[i].dcp_fw ? t6050_images[i].dcp_fw : &os_firmware;
return true;
}
return false;
Expand Down Expand Up @@ -5888,9 +5910,9 @@ static T6050_TEXT int t6050_publish(void *dt)
fdt32_t v[1];

if (firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]),
"apple,firmware-version", &os_firmware) ||
"apple,firmware-version", t6050.dcp_fw) ||
firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]),
"apple,firmware-compat", &os_firmware) ||
"apple,firmware-compat", t6050.dcp_fw) ||
t6050_setprop(dt, T6050_DCP, "apple,firmware-uuid", t6050.uuid, strlen(t6050.uuid) + 1) ||
(t6050.prewired ? t6050_publish_prewired(dt) : t6050_publish_wiring(dt)))
return -1;
Expand Down Expand Up @@ -6130,9 +6152,9 @@ static T6050_TEXT int t6050_ext_one(void *dt, int n)
if (fdt_get_path(dt, targets[i], path[i], T6050_PATH))
return -1;
if (firmware_set_fdt(dt, kboot_fdt_path_exact(dt, path[0]), "apple,firmware-version",
&os_firmware) ||
t6050.dcp_fw) ||
firmware_set_fdt(dt, kboot_fdt_path_exact(dt, path[0]), "apple,firmware-compat",
&os_firmware) ||
t6050.dcp_fw) ||
fdt_setprop(dt, kboot_fdt_path_exact(dt, path[0]), "apple,firmware-uuid", t6050.uuid,
strlen(t6050.uuid) + 1) ||
fdt_setprop(dt, kboot_fdt_path_exact(dt, path[0]), "apple,bw-scratch", v, sizeof(v)))
Expand Down Expand Up @@ -6175,6 +6197,92 @@ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip)
}
}

#ifdef ESP_STAGE2
/*
* Node `n` is the PMC metadata: reg <pmc size>, 3 cells, and that window reaching the CPU at the
* ADT's address through every bus above it (t6050_identity_reg()), as the OS resolves
* apple,bw-scratch: an identity bus below a translating one does not match.
*/
static T6050_TEXT bool t6050_pmc_matches(void *dt, int n)
{
int len;
const fdt64_t *reg = fdt_getprop(dt, n, "reg", &len);
if (!reg || len != 16 || fdt64_ld(&reg[0]) != t6050.pmc ||
fdt64_ld(&reg[1]) != t6050.pmc_size || !t6050_identity_reg(dt, n))
return false;
const fdt32_t *cells = fdt_getprop(dt, n, "#apple,bw-scratch-cells", &len);
return cells && len == 4 && fdt32_ld(cells) == 3;
}

/*
* A device tree whose dcp and disp0 are enabled already: a kernel that adopts the internal DCP on
* its own. The external engines run the same image and are still handed off. They need the PMC
* metadata node their bandwidth scratch points into, which only the internal handoff publishes:
* published here beside the dcp, or a matching node already there or on another bus reused. Returns
* 0 with t6050.pmc_ph set; nonzero, with the device tree unchanged, if it is not that case.
* Built into the ESP Stage 2 only: a Stage 1 booting Linux directly leaves such a tree's external
* engines with iBoot.
*/
static T6050_TEXT int t6050_adopted(void *dt)
{
char name[40], parent[T6050_PATH], node[T6050_PATH + 40];
int dcp = kboot_fdt_path_exact(dt, "dcp");
int disp = kboot_fdt_path_exact(dt, "disp0");
u32 ph = 0;

if (dcp < 0 || disp < 0 || fdt_node_check_compatible(dt, dcp, dcp6.soc->dcp_compat) ||
fdt_node_check_compatible(dt, disp, dcp6.soc->disp_compat) || !t6050_available(dt, dcp) ||
!t6050_available(dt, disp) || !t6050.pmc || !t6050.pmc_size ||
fdt_get_path(dt, dcp, t6050.path[T6050_DCP], T6050_PATH) ||
fdt_get_path(dt, disp, t6050.path[T6050_DISP], T6050_PATH))
return -1;
/* As t6050_check_dt(): the dcp's windows reach the CPU at their own addresses */
if (dcp6_cells_2_2(dt, dcp, "dcp") || !t6050_identity_reg(dt, dcp))
bail("dcp is not on an identity-mapped bus: external engines not handed off\n");

strcpy(parent, t6050.path[T6050_DCP]);
*strrchr(parent, '/') = 0;
snprintf(name, sizeof(name), "pmc-metadata@%lx", t6050.pmc);
snprintf(node, sizeof(node), "%s/%s", parent, name);

void *scratch = dcp6_scratch(dt);
if (!scratch)
return -1;
/*
* One beside the dcp decides; otherwise a matching one elsewhere. Either, and a new one, must
* reach the CPU at the ADT's address through every bus above it (t6050_pmc_matches()).
*/
int n = kboot_fdt_path_exact(scratch, node);
for (int o = 0; n < 0 && (o = fdt_next_node(scratch, o, NULL)) >= 0;)
if (!strcmp(fdt_get_name(scratch, o, NULL), name) && t6050_pmc_matches(scratch, o))
n = o;
if (n >= 0) {
if (t6050_pmc_matches(scratch, n)) {
ph = fdt_get_phandle(scratch, n);
if (!ph &&
(fdt_generate_phandle(scratch, &ph) || fdt_setprop_u32(scratch, n, "phandle", ph)))
ph = 0;
}
} else {
fdt64_t reg[2] = {cpu_to_fdt64(t6050.pmc), cpu_to_fdt64(t6050.pmc_size)};
ph = t6050_add_node(scratch, parent[0] ? parent : "/", name);
n = kboot_fdt_path_exact(scratch, node);
if (!ph || fdt_setprop(scratch, n, "reg", reg, sizeof(reg)) ||
fdt_setprop_u32(scratch, n, "#apple,bw-scratch-cells", 3) ||
!t6050_pmc_matches(scratch, kboot_fdt_path_exact(scratch, node)))
ph = 0;
}
if (!ph) {
free(scratch);
bail("%s: could not publish the PMC metadata for the external engines\n", node);
}
if (dcp6_commit(dt, scratch))
return -1;
t6050.pmc_ph = ph;
return 0;
}
#endif /* ESP_STAGE2 */

/*
* The ADT dcpextN index whose coprocessor is at `node`'s identity-mapped reg[0], or -1.
* When `unique` is non-NULL, sets `*unique` to whether `node` is the only apple,t6050-dcpext
Expand Down Expand Up @@ -6404,11 +6512,11 @@ static T6050_TEXT void t6050_ext_reject(void *dt)
}

/*
* Write `os_firmware` to `apple,firmware-compat` and any existing `apple,firmware-version` on
* `at` in one scratch transaction. Returns 0 on success, -1 if scratch allocation, any FDT
* property write, or commit failed (leaving `dt` untouched and `scratch` freed).
* Write `fw` to `apple,firmware-compat` and any existing `apple,firmware-version` on `at` in one
* scratch transaction. Returns 0 on success, -1 if scratch allocation, any FDT property write, or
* commit failed (leaving `dt` untouched and `scratch` freed).
*/
static T6050_TEXT int t6050_ext_version_one(void *dt, int at)
static T6050_TEXT int t6050_ext_version_one(void *dt, int at, const struct fw_version_info *fw)
{
static const char *const props[] = {"apple,firmware-compat", "apple,firmware-version"};
void *scratch = dcp6_scratch(dt);
Expand All @@ -6420,7 +6528,7 @@ static T6050_TEXT int t6050_ext_version_one(void *dt, int at)
/* apple,firmware-compat (props[0]) always; apple,firmware-version only if present */
if (i && !fdt_getprop(scratch, at, props[i], NULL))
continue;
err = firmware_set_fdt(scratch, at, props[i], &os_firmware);
err = firmware_set_fdt(scratch, at, props[i], fw);
}
if (err || dcp6_commit(dt, scratch)) {
if (err)
Expand All @@ -6443,18 +6551,20 @@ static T6050_TEXT int t6050_ext_version_one(void *dt, int at)
* Each surviving engine's version edit runs in its own scratch transaction; if the live/ADT check
* fails during versioning or if allocation, writing, or commit fails, that engine is disabled (or
* deleted on a packed DT) and stripped from scanout rather than leaving stale apple,firmware-compat
* enabled.
* enabled. The version is the DCP ABI of the image row this boot picked (t6050_pick_image(); the
* engines must run that image), or the OS firmware's when no row was picked.
*/
static T6050_TEXT void t6050_ext_version(void *dt)
{
const struct fw_version_info *fw = t6050.dcp_fw ? t6050.dcp_fw : &os_firmware;
u32 done = 0;
int count = 0, err = 0, at = -1;

t6050_ext_reject(dt);

if (os_firmware.version == V_UNKNOWN || !os_firmware.num_length) {
if (fw->version == V_UNKNOWN || !fw->num_length) {
dlog("firmware %s is unknown: the external engines keep the tree's version\n",
os_firmware.string ? os_firmware.string : "");
fw->string ? fw->string : "");
return;
}
while ((at = fdt_node_offset_by_compatible(dt, at, "apple,t6050-dcpext")) >= 0) {
Expand All @@ -6467,7 +6577,7 @@ static T6050_TEXT void t6050_ext_version(void *dt)
const char *why = NULL;
if (t6050_check_ext_adt(n)) {
why = "rejected";
} else if (t6050_ext_version_one(dt, at)) {
} else if (t6050_ext_version_one(dt, at, fw)) {
why = "could not take firmware version";
} else {
done |= 1u << n;
Expand All @@ -6486,16 +6596,16 @@ static T6050_TEXT void t6050_ext_version(void *dt)
if (err || at != -FDT_ERR_NOTFOUND)
dlog("could not give the external engines the firmware version\n");
if (count)
dlog("firmware %s on %d external engine(s) the device tree enables\n", os_firmware.string,
count);
dlog("firmware %s on %d external engine(s) the device tree enables\n", fw->string, count);
}

/* Any failure leaves the display nodes disabled: the boot framebuffer keeps the panel */
static T6050_TEXT void t6050_handoff(void *dt, bool prewired)
{
memset(&t6050, 0, sizeof(t6050));
t6050.prewired = prewired;
if (!t6050_check_adt() && !t6050_check_dt(dt) && !t6050_check_darts(dt)) {
int adt_ok = !t6050_check_adt();
if (adt_ok && !t6050_check_dt(dt) && !t6050_check_darts(dt)) {
void *scratch = dcp6_scratch(dt);
if (scratch && t6050_publish(scratch)) {
dlog("DT write failed (full?)\n");
Expand All @@ -6514,6 +6624,14 @@ static T6050_TEXT void t6050_handoff(void *dt, bool prewired)
}
if (t6050_setprop(dt, -1, T6050_STATUS, "refused", sizeof("refused")))
dlog("could not set /chosen/" T6050_STATUS "\n");
#ifdef ESP_STAGE2
if (adt_ok && t6050.ext && !t6050_adopted(dt)) {
dlog("dcp and disp0 are enabled already: the kernel adopts the internal DCP; handing off "
"the external engines\n");
t6050_ext_handoff(dt, 4, -1);
return;
}
#endif
dlog("internal display handoff refused; the boot framebuffer keeps the panel\n");
}

Expand Down
64 changes: 64 additions & 0 deletions tests/python/test_t6041_display_handoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,67 @@ def test_unknown_profile_does_not_match_unknown_firmware(tmp_path):
assert "no qualified OS firmware for apple,t6041-dcp: nothing done" in result.stdout
assert "nothing done: the device tree is byte for byte unchanged" in result.stdout
assert "nothing done: no MMIO access at all" in result.stdout


@pytest.fixture(scope="module")
def t6050_host(tmp_path_factory):
return build_host(tmp_path_factory.mktemp("t6050"), host_source="t6050_display_host.c",
defines=("ESP_STAGE2",))


@pytest.fixture(scope="module")
def t6050_stage1_host(tmp_path_factory):
return build_host(tmp_path_factory.mktemp("t6050s1"), host_source="t6050_display_host.c")


T6050_SCENARIOS = re.findall(r'^ \{"(t6050-[a-z0-9.-]+)",\s+"',
(ROOT / "tests/t6050_display_host.c").read_text(), re.M)
T6050_ADOPTED = "dcp and disp0 are enabled already: the kernel adopts the internal DCP"
T6050_REFUSED = "internal display handoff refused; the boot framebuffer keeps the panel"
# As REASONS: the log line of the path each T6050 scenario is meant to take
T6050_REASONS = {
"t6050-27.3": "internal display handed off: firmware ",
"t6050-27.3-any-os": "internal display handed off: firmware ",
"t6050-adopted": T6050_ADOPTED,
"t6050-adopted-existing-pmc": T6050_ADOPTED,
"t6050-adopted-pmc-other-bus": T6050_ADOPTED,
"t6050-adopted-pmc-nested-bus": T6050_ADOPTED,
"t6050-adopted-pmc-mismatch": "could not publish the PMC metadata for the external engines",
"t6050-adopted-no-status": T6050_ADOPTED,
"t6050-adopted-dcp-only": T6050_REFUSED,
"t6050-adopted-reserved": T6050_REFUSED,
"t6050-adopted-wrong-compat": T6050_REFUSED,
"t6050-adopted-dangling-alias": T6050_REFUSED,
"t6050-adopted-mapped-bus": "dcp is not on an identity-mapped bus: external engines not handed off",
}


def test_t6050_scenario_list_matches(t6050_host):
assert scenarios(t6050_host) == T6050_SCENARIOS
assert sorted(T6050_REASONS) == sorted(T6050_SCENARIOS)


@pytest.mark.parametrize("scenario", T6050_SCENARIOS)
def test_t6050_scenario(t6050_host, scenario):
result = run(t6050_host, scenario)
assert result.returncode == 0, result.stdout + result.stderr
assert T6050_REASONS[scenario] in result.stdout, result.stdout
if T6050_REASONS[scenario] != T6050_ADOPTED:
assert T6050_ADOPTED not in result.stdout, result.stdout
handed_off = scenario in ("t6050-27.3", "t6050-27.3-any-os", "t6050-adopted", "t6050-adopted-existing-pmc",
"t6050-adopted-pmc-other-bus", "t6050-adopted-pmc-nested-bus",
"t6050-adopted-no-status")
assert ("dcpext0 handed off: bw-scratch" in result.stdout) == handed_off, result.stdout
assert result.stdout.rstrip().endswith(f"{scenario}: pass"), result.stdout
assert "FAIL" not in result.stdout, result.stdout


@pytest.mark.parametrize("scenario", T6050_SCENARIOS)
def test_t6050_stage1_scenario(t6050_stage1_host, scenario):
"""A Stage 1 can boot Linux directly: the 27.3 beta row and the adopted path are not in it."""
result = run(t6050_stage1_host, scenario)
assert result.returncode == 0, result.stdout + result.stderr
assert T6050_REFUSED in result.stdout, result.stdout
assert T6050_ADOPTED not in result.stdout, result.stdout
assert "dcpext0 handed off" not in result.stdout, result.stdout
assert result.stdout.rstrip().endswith(f"{scenario}: pass"), result.stdout
Loading
Loading