From 39b5db2b4c211d8b4c9de994f00878d301d0f9af Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 20 Nov 2026 21:59:55 -0500 Subject: [PATCH 01/10] kboot: T6050: hand off a prewired internal panel and combo PHY port 0 A T6050 device tree can describe its inherited internal display by address, with clocks and bandwidth scratch prewired: no dcp or disp0 alias, and its apple,t6050-dcp node at the ADT dcp's coprocessor address already carries clocks, apple,clock-ids and apple,bw-scratch. Neither the aliased nor the panel-less handoff takes such a tree, so the panel stayed disabled. Let t6050_handoff() take that tree shape too. Its ADT checks, image pin, register and DART checks and publication are the aliased tree's. The device tree side finds the dcp by its coprocessor address and the one display subsystem by compatible (a tree with a second one, as one per die would be, is refused: it does not say which one the dcp feeds), and, instead of refusing the prewired properties, checks them against the ADT: apple,clock-ids are disp0's clock-ids in order and each clock is a distinct, available fixed clock with no arguments, and apple,bw-scratch names disp0's bandwidth agent in a node with three scratch cells whose reg is disp0's PMC metadata. Publication then sets the clocks' rates from the ADT and enables that node instead of adding both. The dcp's register windows, and the PMC node's, must reach the CPU at their own addresses: their parent, every bus above them and the root have 2 address and 2 size cells, and at every bus ranges is empty or has no wrapping entry and its first entry holding a window's start is an identity entry that holds the whole window. For the prewired tree, the display subsystem's iommus may list after its own entry the display DARTs of the external engines the tree enables, each once: an available t8110 DART with one iommu cell whose CPU address (its reg mapping 1:1, as the engine's must) is an engine's ADT dart-dispextN, which that engine's piodma maps through as SID 16, and neither internal DART. Availability is read as Linux reads status: absent, or the whole string "okay" or "ok". An aliased tree keeps exactly one entry. As that handoff reserves no page tables, each adopted stream's ADT pt-region must be page-aligned, hold the whole 16 KiB root page and lie outside the OS's RAM. Copy the combo PHY tunables for each apple,t6050-combo-phy port the tree hands to the OS (apple,guest-port-reserved), port 0 included, and select that path by the presence of a combo PHY node rather than by the chip. Each group, the RX clock ones included, is copied whole and in the ADT's order, as before: the driver applies the records in that order. Signed-off-by: Erik Bourget --- src/kboot_atc.c | 10 +- src/kboot_display_handoff.c | 368 ++++++++++++++++--- tests/python/fdt_harness.py | 2 +- tests/python/test_t6050_tree_shape.py | 490 ++++++++++++++++++++++++++ 4 files changed, 824 insertions(+), 46 deletions(-) create mode 100644 tests/python/test_t6050_tree_shape.py diff --git a/src/kboot_atc.c b/src/kboot_atc.c index cb17ebe78..4e21c814d 100644 --- a/src/kboot_atc.c +++ b/src/kboot_atc.c @@ -1357,8 +1357,12 @@ static T6050_ATC_INIT void t6050_setup_atc(void *dt) const fdt32_t *index = fdt_getprop(dt, node, "apple,port-index", &len); u32 port = index && len == 4 ? fdt32_ld(index) : 0; - /* Port 0 carries aura's debug link: never describe it */ - if (!port || port > 5) + /* + * Only a port the tree hands to the OS (apple,guest-port-reserved): port 0 carries aura's + * debug link unless the tree takes it. + */ + if (!index || len != 4 || port > 5 || + !fdt_getprop(dt, node, "apple,guest-port-reserved", NULL)) continue; t6050_clear_atc(dt, node); bool ok = !t6050_export_atc(dt, node, port); @@ -1388,7 +1392,7 @@ int kboot_setup_atc(void *dt) char fdt_alias[32]; bool t6041_max = t6041_max_atc(dt); - if (chip_id == T6050) { + if (fdt_node_offset_by_compatible(dt, -1, "apple,t6050-combo-phy") >= 0) { t6050_setup_atc(dt); return 0; } diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 41411a043..f4a0dd5d0 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -91,9 +91,12 @@ * T6050 (M5 Pro/Max): the __OS_LOG buffers of every running display coprocessor are reserved on * every boot. When the device tree describes the internal display (aliases dcp and disp0, all its * nodes disabled) and every check in t6050_handoff() passes, that handoff is published and those - * nodes are enabled; otherwise they are left disabled. 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. + * nodes are enabled; otherwise they are left disabled. A tree may instead describe the internal + * display by address, with its firmware clocks and bandwidth scratch prewired (the J716c's): the + * 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. * * 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 @@ -2171,14 +2174,12 @@ static int dcp6_cells_2_2(void *dt, int node, const char *what) } /* - * iommus = <&dart sid> with a disabled t8110 DART at `base` whose apple,dma-range is the DVA window - * vm_base+vm_size. Returns the DART's offset. + * `iommus` (`len` bytes) = <&dart sid> with a disabled t8110 DART at `base` whose apple,dma-range + * is the DVA window vm_base+vm_size. Returns the DART's offset. */ -static int dcp6_check_iommu(void *dt, int node, const char *what, u32 sid, u64 base, u64 vm_base, - u64 vm_size) +static int dcp6_check_iommus(void *dt, const fdt32_t *iommus, int len, const char *what, u32 sid, + u64 base, u64 vm_base, u64 vm_size) { - int len; - const fdt32_t *iommus = fdt_getprop(dt, node, "iommus", &len); if (!iommus || len != 2 * sizeof(fdt32_t) || fdt32_ld(&iommus[1]) != sid) bail("%s iommus is not <&dart %u>\n", what, sid); @@ -2206,6 +2207,15 @@ static int dcp6_check_iommu(void *dt, int node, const char *what, u32 sid, u64 b return dart; } +/* As dcp6_check_iommus(), for the iommus of `node` */ +static int dcp6_check_iommu(void *dt, int node, const char *what, u32 sid, u64 base, u64 vm_base, + u64 vm_size) +{ + int len; + const fdt32_t *iommus = fdt_getprop(dt, node, "iommus", &len); + return dcp6_check_iommus(dt, iommus, len, what, sid, base, vm_base, vm_size); +} + /* Node for alias `name`, and its path. Returns the offset or a negative error. */ static int dcp6_alias_path(void *dt, const char *name, char *path, size_t len) { @@ -3449,8 +3459,20 @@ static const struct { #define T6050_INIT __attribute__((minsize, noinline, section(".init.t6050"))) #define T6050_TEXT __attribute__((minsize, noinline)) -/* Nodes, by device tree path; the first five are enabled by the handoff */ -enum { T6050_DCP, T6050_DISP, T6050_MBOXN, T6050_DART_DCP, T6050_DART_DISP, T6050_PIODMA, T6050_N }; +/* + * Nodes, by device tree path; the first five are enabled by the handoff. T6050_PMC: the PMC + * metadata node a prewired tree's apple,bw-scratch names. + */ +enum { + T6050_DCP, + T6050_DISP, + T6050_MBOXN, + T6050_DART_DCP, + T6050_DART_DISP, + T6050_PIODMA, + T6050_PMC, + T6050_N +}; static struct { char path[T6050_N][T6050_PATH]; @@ -3460,6 +3482,7 @@ static struct { const char *uuid; /* the pinned image of the running OS firmware */ bool ext; bool headless; /* no panel: t6050_headless_handoff() */ + bool prewired; /* clocks and bandwidth scratch prewired: t6050_prewired() */ } t6050; /* The dcp's reg entries and the ADT reg each equals (coproc: a prefix of it) */ @@ -3506,6 +3529,37 @@ static T6050_TEXT u32 t6050_add_node(void *dt, const char *parent, const char *n return ph; } +/* The `compat` node whose coprocessor (first reg) is at `coproc`, or -1 */ +static T6050_TEXT int t6050_node_at(void *dt, const char *compat, u64 coproc) +{ + for (int o = fdt_node_offset_by_compatible(dt, -1, compat); o >= 0; + o = fdt_node_offset_by_compatible(dt, o, compat)) { + int len; + const fdt64_t *reg = fdt_getprop(dt, o, "reg", &len); + if (reg && len >= 16 && fdt64_ld(®[0]) == coproc) + return o; + } + return -1; +} + +/* + * The tree describes its inherited internal display by address, with clocks and bandwidth + * scratch prewired: its apple,t6050-dcp node at the ADT dcp's coprocessor address already + * carries clocks, apple,clock-ids and apple,bw-scratch (the J716c's). An aliased tree adds none + * of them (t6050_check_dt() refuses them there). t6050_check_prewired() checks them against the + * ADT. + */ +static T6050_TEXT bool t6050_prewired(void *dt) +{ + u64 coproc; + int dcp = t6050_adt_reg("/arm-io/dcp", 0, &coproc, NULL) + ? -1 + : t6050_node_at(dt, "apple,t6050-dcp", coproc); + return dcp >= 0 && fdt_getprop(dt, dcp, "clocks", NULL) && + fdt_getprop(dt, dcp, "apple,clock-ids", NULL) && + fdt_getprop(dt, dcp, "apple,bw-scratch", NULL); +} + /* * 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 @@ -3608,12 +3662,128 @@ static T6050_INIT int t6050_check_adt(void) return 0; } -/* The device tree side: the nodes it describes match the ADT, and nothing is published yet */ +/* Whether `node` is available as Linux reads status: absent, "okay" or "ok" (whole strings) */ +static T6050_TEXT bool t6050_available(void *dt, int node) +{ + int len; + const char *status = fdt_getprop(dt, node, "status", &len); + return !status || (len == sizeof("okay") && !memcmp(status, "okay", len)) || + (len == sizeof("ok") && !memcmp(status, "ok", len)); +} + +/* + * Every reg window of `node` reaches the CPU at its own address. Its parent, each bus above it + * and the root have 2 address and 2 size cells. Each bus has an empty ranges, or one with no + * wrapping entry whose first entry holding the window's start (the one an OS translates through) + * maps it to the same address and holds the window whole. + */ +static T6050_INIT bool t6050_identity_reg(void *dt, int node) +{ + int len, rlen, parent = fdt_parent_offset(dt, node); + const fdt64_t *reg = fdt_getprop(dt, node, "reg", &len); + + if (!reg || !len || len % 16 || parent < 0 || fdt_address_cells(dt, parent) != 2 || + fdt_size_cells(dt, parent) != 2) + return false; + for (int bus = parent; bus > 0; bus = fdt_parent_offset(dt, bus)) { + int up = fdt_parent_offset(dt, bus); + const fdt32_t *r = fdt_getprop(dt, bus, "ranges", &rlen); + if (up < 0 || fdt_address_cells(dt, bus) != 2 || fdt_size_cells(dt, bus) != 2 || !r || + fdt_address_cells(dt, up) != 2 || fdt_size_cells(dt, up) != 2 || rlen % 24) + return false; + for (int i = 0; i < rlen / 4; i += 6) { + u64 child = (u64)fdt32_ld(&r[i]) << 32 | fdt32_ld(&r[i + 1]); + u64 from = (u64)fdt32_ld(&r[i + 2]) << 32 | fdt32_ld(&r[i + 3]); + u64 size = (u64)fdt32_ld(&r[i + 4]) << 32 | fdt32_ld(&r[i + 5]); + if (child > UINT64_MAX - size || from > UINT64_MAX - size) + return false; + } + for (int w = 0; rlen && w < len / 16; w++) { + u64 a = fdt64_ld(®[2 * w]), sz = fdt64_ld(®[2 * w + 1]); + int i = 0; + u64 child = 0, from = 0, size = 0; + for (; i < rlen / 4; i += 6) { + child = (u64)fdt32_ld(&r[i]) << 32 | fdt32_ld(&r[i + 1]); + from = (u64)fdt32_ld(&r[i + 2]) << 32 | fdt32_ld(&r[i + 3]); + size = (u64)fdt32_ld(&r[i + 4]) << 32 | fdt32_ld(&r[i + 5]); + if (a >= child && a - child < size) + break; + } + if (i >= rlen / 4 || child != from || sz > size || a - child > size - sz) + return false; + } + } + return true; +} + +/* + * A prewired tree's clocks and bandwidth scratch, against the ADT: apple,clock-ids are disp0's + * clock-ids in order, each of clocks is a distinct available fixed clock, and apple,bw-scratch is + * <&pmc 0 T6050_NDISP offset> for disp0's bandwidth agent, in a node whose reg is disp0's PMC + * metadata. t6050_publish() sets the clocks' rates and enables that node. + */ +static T6050_INIT int t6050_check_prewired(void *dt, int dcp) +{ + int len, clen; + const fdt32_t *ids = fdt_getprop(dt, dcp, "apple,clock-ids", &len); + const fdt32_t *clk = fdt_getprop(dt, dcp, "clocks", &clen); + + if (!ids || !clk || len != (int)t6050.nclk * 4 || clen != len) + bail("dcp clocks and apple,clock-ids are not disp0's %u clock(s)\n", t6050.nclk); + for (u32 i = 0; i < t6050.nclk; i++) { + int c = fdt_node_offset_by_phandle(dt, fdt32_ld(&clk[i])), ncells = 0; + const fdt32_t *cells = c < 0 ? NULL : fdt_getprop(dt, c, "#clock-cells", &ncells); + bool dup = false; + for (u32 j = 0; j < i; j++) + dup |= fdt32_ld(&clk[j]) == fdt32_ld(&clk[i]); + /* One cell per clock: each a distinct fixed clock that takes no arguments */ + if (fdt32_ld(&ids[i]) != t6050.clk_id[i] || c < 0 || + fdt_node_check_compatible(dt, c, "fixed-clock") || !cells || ncells != 4 || + fdt32_ld(cells) || dup || !t6050_available(dt, c)) + bail("dcp clock %u is not its own available fixed clock for disp0 clock %u\n", i, + t6050.clk_id[i]); + } + + const fdt32_t *s = fdt_getprop(dt, dcp, "apple,bw-scratch", &len); + int pmc = s && len == 16 ? fdt_node_offset_by_phandle(dt, fdt32_ld(&s[0])) : -1; + clen = 0; + const fdt32_t *pcells = pmc < 0 ? NULL : fdt_getprop(dt, pmc, "#apple,bw-scratch-cells", &clen); + const fdt64_t *reg = pmc < 0 ? NULL : fdt_getprop(dt, pmc, "reg", &len); + if (!pcells || clen != 4 || fdt32_ld(pcells) != 3 || !t6050_identity_reg(dt, pmc) || !reg || + len != 16 || fdt64_ld(®[0]) != t6050.pmc || fdt64_ld(®[1]) != t6050.pmc_size || + fdt32_ld(&s[1]) || fdt32_ld(&s[2]) != T6050_NDISP || fdt32_ld(&s[3]) != t6050.bw_offset || + fdt_get_path(dt, pmc, t6050.path[T6050_PMC], T6050_PATH)) + bail("dcp apple,bw-scratch is not offset 0x%x of the PMC metadata at 0x%lx\n", + t6050.bw_offset, t6050.pmc); + t6050.pmc_ph = fdt32_ld(&s[0]); + return 0; +} + +/* + * The display subsystem: disp0 in an aliased tree, and in a prewired one the only node of its + * compatible. A tree with more than one (one per die) does not say which one the dcp feeds. + */ +static T6050_INIT int t6050_disp_node(void *dt) +{ + if (!t6050.prewired) + return kboot_fdt_path_exact(dt, "disp0"); + int disp = fdt_node_offset_by_compatible(dt, -1, dcp6.soc->disp_compat); + if (disp >= 0 && fdt_node_offset_by_compatible(dt, disp, dcp6.soc->disp_compat) >= 0) + bail("more than one display subsystem: no prewired handoff\n"); + return disp; +} + +/* + * The device tree side: the nodes it describes match the ADT, and nothing is published yet. An + * aliased tree names dcp and disp0 by alias; a prewired one has its dcp at the ADT's coprocessor + * address and one display subsystem. + */ static T6050_INIT int t6050_check_dt(void *dt) { char (*path)[T6050_PATH] = t6050.path; - int dcp = kboot_fdt_path_exact(dt, "dcp"); - int disp = kboot_fdt_path_exact(dt, "disp0"); + int dcp = t6050.prewired ? t6050_node_at(dt, dcp6.soc->dcp_compat, t6050.coproc) + : kboot_fdt_path_exact(dt, "dcp"); + int disp = t6050_disp_node(dt); int parent = dcp < 0 ? dcp : fdt_parent_offset(dt, dcp); int len = -1; @@ -3621,16 +3791,21 @@ static T6050_INIT int t6050_check_dt(void *dt) fdt_node_check_compatible(dt, disp, dcp6.soc->disp_compat) || fdt_get_path(dt, dcp, path[T6050_DCP], T6050_PATH) || fdt_get_path(dt, disp, path[T6050_DISP], T6050_PATH)) - bail("aliases dcp and disp0 are not apple,t6050-dcp and apple,display-subsystem\n"); - if (parent < 0 || dcp6_cells_2_2(dt, dcp, "dcp") || !fdt_getprop(dt, parent, "ranges", &len) || - len || fdt_subnode_offset(dt, dcp, "panel") < 0) + bail("no dcp and display subsystem nodes of apple,t6050-dcp and apple,display-subsystem\n"); + if (parent < 0 || dcp6_cells_2_2(dt, dcp, "dcp") || !t6050_identity_reg(dt, dcp) || + fdt_subnode_offset(dt, dcp, "panel") < 0) bail("dcp is not on an identity-mapped bus, or has no panel child\n"); if (dcp6_status_disabled(dt, dcp, "dcp") || dcp6_status_disabled(dt, disp, "disp0")) return -1; - if (fdt_getprop(dt, dcp, "memory-region", NULL) || - fdt_getprop(dt, disp, "memory-region", NULL) || fdt_getprop(dt, dcp, "clocks", NULL) || - fdt_getprop(dt, dcp, "apple,bw-scratch", NULL)) - bail("the device tree already has a memory-region, clocks or apple,bw-scratch\n"); + if (fdt_getprop(dt, dcp, "memory-region", NULL) || fdt_getprop(dt, disp, "memory-region", NULL)) + bail("the device tree already has a memory-region\n"); + if (t6050.prewired) { + if (t6050_check_prewired(dt, dcp)) + return -1; + } else if (fdt_getprop(dt, dcp, "clocks", NULL) || + fdt_getprop(dt, dcp, "apple,bw-scratch", NULL)) { + bail("the device tree already has clocks or apple,bw-scratch\n"); + } for (size_t i = 0; i < ARRAY_SIZE(t6050_regs); i++) { u64 a, s; @@ -3656,6 +3831,63 @@ static T6050_INIT int t6050_check_dt(void *dt) return 0; } +/* + * Whether phandle `ph` is the display DART of an apple,t6050-dcpext engine the tree enables: an + * available t8110 DART with one iommu cell at the ADT's dart-dispextN of engine dcpextN, that + * the engine's piodma child maps through as SID 16. Neither internal DART is one. + */ +static T6050_TEXT bool t6050_ext_display_dart(void *dt, u32 ph) +{ + int dart = fdt_node_offset_by_phandle(dt, ph), clen = 0, rlen = 0; + const fdt32_t *cells = dart < 0 ? NULL : fdt_getprop(dt, dart, "#iommu-cells", &clen); + const fdt64_t *reg = dart < 0 ? NULL : fdt_getprop(dt, dart, "reg", &rlen); + u64 base, adt_base, coproc; + char a[32]; + + /* The addresses compared below are the CPU's: each node's reg maps 1:1 */ + if (!cells || clen != 4 || fdt32_ld(cells) != 1 || !reg || rlen < 16 || + fdt_node_check_compatible(dt, dart, "apple,t8110-dart") || !t6050_available(dt, dart) || + !t6050_identity_reg(dt, dart)) + return false; + base = fdt64_ld(®[0]); + if ((!t6050_adt_reg("/arm-io/dart-dcp", 0, &adt_base, NULL) && adt_base == base) || + (!t6050_adt_reg("/arm-io/dart-disp0", 0, &adt_base, NULL) && adt_base == base)) + return false; + for (int n = 0; n < 8; n++) { /* dcpext0..7 */ + snprintf(a, sizeof(a), "/arm-io/dart-dispext%d", n); + if (t6050_adt_reg(a, 0, &adt_base, NULL) || adt_base != base) + continue; + snprintf(a, sizeof(a), "/arm-io/dcpext%d", n); + int ext = t6050_adt_reg(a, 0, &coproc, NULL) + ? -1 + : t6050_node_at(dt, "apple,t6050-dcpext", coproc); + int pio = ext < 0 ? -1 : fdt_subnode_offset(dt, ext, "piodma"), plen = 0; + const fdt32_t *p = pio < 0 ? NULL : fdt_getprop(dt, pio, "iommus", &plen); + return t6050_available(dt, ext) && t6050_identity_reg(dt, ext) && p && plen == 8 && + fdt32_ld(&p[0]) == ph && fdt32_ld(&p[1]) == 16; + } + return false; +} + +/* + * A prewired display subsystem's iommus after its first entry: each <&dart 0> an enabled + * external engine's display DART (t6050_ext_display_dart()), each once. + */ +static T6050_TEXT int t6050_check_ext_scanout(void *dt, const fdt32_t *io, int len) +{ + for (int i = 2; i < len / 4; i += 2) { + for (int j = 2; j < i; j += 2) + if (fdt32_ld(&io[j]) == fdt32_ld(&io[i])) + bail("the display subsystem lists one external display DART twice\n"); + if (fdt32_ld(&io[i + 1]) || fdt32_ld(&io[i]) == fdt32_ld(&io[0]) || + !t6050_ext_display_dart(dt, fdt32_ld(&io[i]))) + bail("display subsystem iommus entry %d is not an enabled external engine's display " + "DART\n", + i / 2); + } + return 0; +} + /* Each stream's iommus against its ADT DART, then, with the display powered, the live state */ static T6050_TEXT int t6050_check_darts(void *dt) { @@ -3670,10 +3902,34 @@ static T6050_TEXT int t6050_check_darts(void *dt) dcp6_adt_u64_pair(node, name, &l1[i], &count) || !l1[i]) bail("%s: no reg, DVA window or %s\n", t6050_streams[i].adt, name); const char *user = t6050.path[t6050_streams[i].node]; - int dart = dcp6_check_iommu(dt, kboot_fdt_path_exact(dt, user), user, t6050_streams[i].sid, - base[i], vm_base, vm_size); + int at = kboot_fdt_path_exact(dt, user), len = 0; + const fdt32_t *io = at < 0 ? NULL : fdt_getprop(dt, at, "iommus", &len); + /* + * The display subsystem's first entry is the internal scanout stream. A prewired tree + * lists after it the display DARTs of the external engines it enables itself. + */ + if (t6050.prewired && t6050_streams[i].node == T6050_DISP && len > 8 && !(len % 8)) { + if (t6050_check_ext_scanout(dt, io, len)) + return -1; + len = 8; + } + int dart = + dcp6_check_iommus(dt, io, len, user, t6050_streams[i].sid, base[i], vm_base, vm_size); if (dart < 0 || fdt_get_path(dt, dart, t6050.path[t6050_streams[i].dart], T6050_PATH)) return -1; + + /* + * A prewired tree's handoff reserves no page tables: the stream's ADT table region must + * be page-aligned, hold its whole root page and lie outside the OS's RAM. + */ + u64 lo, hi; + snprintf(name, sizeof(name), "pt-region-%u", t6050_streams[i].sid); + if (t6050.prewired && + (dcp6_adt_u64_pair(node, name, &lo, &hi) || hi <= lo || + ((lo | hi | l1[i]) & (DART_PAGE - 1)) || l1[i] < lo || l1[i] > hi - DART_PAGE || + dcp6_overlaps(lo, hi - lo, cur_boot_args.phys_base, cur_boot_args.mem_size))) + bail("%s: %s is missing, does not hold l1-tt-%u or lies in the OS's RAM\n", + t6050_streams[i].adt, name, t6050_streams[i].sid); } if (pmgr_adt_is_powered("/arm-io/dcp") != 1 || pmgr_adt_is_powered("/arm-io/disp0") != 1) @@ -3698,20 +3954,13 @@ static T6050_TEXT int t6050_check_darts(void *dt) return 0; } -/* Write the whole handoff into `dt` (a scratch copy) */ -static T6050_TEXT int t6050_publish(void *dt) +/* The bandwidth scratch and the firmware clocks, which an aliased tree does not describe */ +static T6050_TEXT int t6050_publish_wiring(void *dt) { char name[40], node[T6050_PATH + 40], names[T6050_CLOCKS * 12]; fdt32_t cells[T6050_CLOCKS], ids[T6050_CLOCKS], v[4]; int nlen = 0; - if (firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]), - "apple,firmware-version", &os_firmware) || - firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]), - "apple,firmware-compat", &os_firmware) || - t6050_setprop(dt, T6050_DCP, "apple,firmware-uuid", t6050.uuid, strlen(t6050.uuid) + 1)) - return -1; - /* The bandwidth scratch, in the PMC metadata (disp0 reg[4]), as register window 5 */ strcpy(node, t6050.path[T6050_DCP]); *strrchr(node, '/') = 0; @@ -3747,6 +3996,45 @@ static T6050_TEXT int t6050_publish(void *dt) t6050_setprop(dt, T6050_DCP, "apple,clock-ids", ids, t6050.nclk * 4)) return -1; + return 0; +} + +/* + * A prewired tree names both (t6050_check_prewired() checked them): the clocks get their rates + * from the ADT, and the PMC metadata node is enabled. + */ +static T6050_TEXT int t6050_publish_prewired(void *dt) +{ + fdt32_t clk[T6050_CLOCKS]; + int len; + const fdt32_t *c = + fdt_getprop(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]), "clocks", &len); + + if (!c || len != (int)t6050.nclk * 4) + return -1; + memcpy(clk, c, len); + for (u32 i = 0; i < t6050.nclk; i++) { + int node = fdt_node_offset_by_phandle(dt, fdt32_ld(&clk[i])); + if (node < 0 || fdt_setprop_u32(dt, node, "clock-frequency", t6050.clk_hz[i])) + return -1; + } + return t6050_setprop(dt, T6050_PMC, "status", "okay", sizeof("okay")); +} + +/* Write the whole handoff into `dt` (a scratch copy) */ +static T6050_TEXT int t6050_publish(void *dt) +{ + char name[40], node[T6050_PATH + 40]; + fdt32_t v[1]; + + if (firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]), + "apple,firmware-version", &os_firmware) || + firmware_set_fdt(dt, kboot_fdt_path_exact(dt, t6050.path[T6050_DCP]), + "apple,firmware-compat", &os_firmware) || + 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; + /* /vram: the firmware's default framebuffer and the DRM aperture */ snprintf(name, sizeof(name), "framebuffer@%lx", t6050.vram); v[0] = cpu_to_fdt32(dcp6_add_resv(dt, name, t6050.vram, t6050.vram_size, "framebuffer")); @@ -3876,14 +4164,7 @@ static T6050_TEXT int t6050_route(void *dt, const char *path, int n) /* The apple,t6050-dcpext node whose coprocessor is at `coproc`, or -1 */ static T6050_TEXT int t6050_ext_node(void *dt, u64 coproc) { - for (int o = fdt_node_offset_by_compatible(dt, -1, "apple,t6050-dcpext"); o >= 0; - o = fdt_node_offset_by_compatible(dt, o, "apple,t6050-dcpext")) { - int len; - const fdt64_t *reg = fdt_getprop(dt, o, "reg", &len); - if (reg && len >= 16 && fdt64_ld(®[0]) == coproc) - return o; - } - return -1; + return t6050_node_at(dt, "apple,t6050-dcpext", coproc); } /* @@ -4058,9 +4339,10 @@ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip) } /* Any failure leaves the display nodes disabled: the boot framebuffer keeps the panel */ -static T6050_TEXT void t6050_handoff(void *dt) +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)) { void *scratch = dcp6_scratch(dt); if (scratch && t6050_publish(scratch)) { @@ -4220,9 +4502,11 @@ int dt_set_display_handoff(void *dt) dcp6_reserve_oslogs(dt); int engine; if (fdt_get_alias(dt, "dcp") && fdt_get_alias(dt, "disp0")) - t6050_handoff(dt); + t6050_handoff(dt, false); else if ((engine = t6050_headless_engine(dt)) >= 0) t6050_headless_handoff(dt, engine); + else if (t6050_prewired(dt)) + t6050_handoff(dt, true); else dlog("the device tree does not describe the internal display: no handoff\n"); return 0; diff --git a/tests/python/fdt_harness.py b/tests/python/fdt_harness.py index 376dd3c4c..62a7af6ec 100644 --- a/tests/python/fdt_harness.py +++ b/tests/python/fdt_harness.py @@ -8,7 +8,7 @@ REPO = Path(__file__).resolve().parents[2] LIBFDT = REPO / "src/libfdt" LIBFDT_SOURCES = ("fdt.c", "fdt_ro.c", "fdt_rw.c", "fdt_wip.c", "fdt_sw.c", "fdt_strerror.c", - "fdt_empty_tree.c") + "fdt_empty_tree.c", "fdt_addresses.c") def definition(source, signature): diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py new file mode 100644 index 000000000..b45cde2f1 --- /dev/null +++ b/tests/python/test_t6050_tree_shape.py @@ -0,0 +1,490 @@ +# SPDX-License-Identifier: MIT +"""T6050 behaviour selected by tree content: the internal display branch and the combo PHY ports. + +The functions under test are extracted from src/ and built against the in-tree libfdt; the +device trees are built here. Nothing touches hardware. +""" +from fdt_harness import PRELUDE, REPO, definition, run + + +def test_t6050_display_selected_by_tree_shape(tmp_path): + source = (REPO / "src/kboot_display_handoff.c").read_text() + selector = definition(source, "int dt_set_display_handoff(void *dt)") + # The tree shape alone selects the prewired branch: no chip or board test beside it. + assert "else if (t6050_prewired(dt))" in selector and "apple,j716c" not in source + code = PRELUDE + r''' +#define T6050_TEXT +#define ADT_DCP 0x48ae00000UL +static bool adt_has_dcp = true; +static int t6050_adt_reg(const char *path, int idx, u64 *addr, u64 *size) +{ + assert(!strcmp(path, "/arm-io/dcp") && !idx && !size); + if (!adt_has_dcp) + return -1; + *addr = ADT_DCP; + return 0; +} +''' + definition(source, "static T6050_TEXT int t6050_node_at(") + \ + definition(source, "static T6050_TEXT bool t6050_prewired(") + r''' +enum { CLOCKS = 1, IDS = 2, SCRATCH = 4, ALIASES = 8 }; +/* A T6050 tree: its dcp node at @at with the properties @props names */ +static int tree_with(u64 at, int props) +{ + fresh(); + node_at("/", "soc"); + char name[32]; + snprintf(name, sizeof(name), "dcp@%lx", (unsigned long)at); + int dcp = node_at("/soc", name); + static const char compat[] = "apple,t6050-dcp\0apple,dcp"; + assert(!fdt_setprop(tree, dcp, "compatible", compat, sizeof(compat))); + reg(dcp, at, 0x4000); + assert(!fdt_setprop_string(tree, dcp, "status", "disabled")); + if (props & CLOCKS) + assert(!fdt_setprop_u32(tree, dcp, "clocks", 1)); + if (props & IDS) + assert(!fdt_setprop_u32(tree, dcp, "apple,clock-ids", 361)); + if (props & SCRATCH) + assert(!fdt_setprop_u32(tree, dcp, "apple,bw-scratch", 2)); + if (props & ALIASES) { + int aliases = node_at("/", "aliases"); + assert(!fdt_setprop_string(tree, aliases, "dcp", "/soc/dcp@48ae00000")); + assert(!fdt_setprop_string(tree, aliases, "disp0", "/soc/display")); + } + return fdt_path_offset(tree, "/soc/dcp@48ae00000") >= 0 && at == ADT_DCP + ? fdt_path_offset(tree, "/soc/dcp@48ae00000") + : -1; +} +int main(void) +{ + int cases = 0; + /* J716c: described by address, clocks, clock ids and scratch prewired. */ + int dcp = tree_with(ADT_DCP, CLOCKS | IDS | SCRATCH); + assert(dcp >= 0 && t6050_prewired(tree)); cases++; + /* The same with the aliases too: still that shape, but the aliased path is chosen first. */ + dcp = tree_with(ADT_DCP, CLOCKS | IDS | SCRATCH | ALIASES); + assert(dcp >= 0 && t6050_prewired(tree)); cases++; + /* J714s: the aliased tree, nothing prewired. */ + tree_with(ADT_DCP, ALIASES); + assert(!t6050_prewired(tree)); cases++; + /* K116s: aliased, with clock ids but no clocks or scratch. */ + tree_with(ADT_DCP, ALIASES | IDS); + assert(!t6050_prewired(tree)); cases++; + /* J714c: no aliases, nothing prewired. */ + tree_with(ADT_DCP, 0); + assert(!t6050_prewired(tree)); cases++; + /* Any one of the three missing. */ + tree_with(ADT_DCP, IDS | SCRATCH); + assert(!t6050_prewired(tree)); cases++; + tree_with(ADT_DCP, CLOCKS | SCRATCH); + assert(!t6050_prewired(tree)); cases++; + tree_with(ADT_DCP, CLOCKS | IDS); + assert(!t6050_prewired(tree)); cases++; + /* Prewired, but not at the ADT's dcp. */ + tree_with(ADT_DCP + 0x100000, CLOCKS | IDS | SCRATCH); + assert(!t6050_prewired(tree)); cases++; + /* No dcp in the ADT (the Studio). */ + tree_with(ADT_DCP, CLOCKS | IDS | SCRATCH); + adt_has_dcp = false; + assert(!t6050_prewired(tree)); cases++; + printf("PASS display shape %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "display-shape", code, "PASS display shape 10 cases") + + +def test_t6050_combo_phy_ports_selected_by_tree(tmp_path): + source = (REPO / "src/kboot_atc.c").read_text() + setup = definition(source, "int kboot_setup_atc(void *dt)") + gate = setup[:setup.index("t6050_setup_atc(dt);")] + # The combo PHY node alone selects the T6050 path: no chip or board test before it. + assert 'fdt_node_offset_by_compatible(dt, -1, "apple,t6050-combo-phy") >= 0' in gate + assert "chip_id" not in gate and "apple,j716c" not in source + code = PRELUDE + r''' +#define T6050_ATC_INIT +static u32 exported[8]; +static int nexported; +static void t6050_clear_atc(void *t, int node) { (void)t; (void)node; } +static int t6050_export_atc(void *t, int node, u32 port) +{ + (void)t; (void)node; + exported[nexported++] = port; + return 0; +} +''' + definition(source, "static T6050_ATC_INIT void t6050_setup_atc(void *dt)") + r''' +enum { RESERVED = 1, NO_INDEX = 2 }; +static void phy(const char *name, u32 port, int flags) +{ + int n = node_at("/", name); + assert(!fdt_setprop_string(tree, n, "compatible", "apple,t6050-combo-phy")); + if (!(flags & NO_INDEX)) + assert(!fdt_setprop_u32(tree, n, "apple,port-index", port)); + if (flags & RESERVED) + assert(!fdt_setprop_empty(tree, n, "apple,guest-port-reserved")); +} +static void expect(const u32 *ports, int n) +{ + nexported = 0; + t6050_setup_atc(tree); + assert(nexported == n); + /* In any order: each expected port once */ + for (int i = 0; i < n; i++) { + int seen = 0; + for (int j = 0; j < n; j++) + seen += exported[j] == ports[i]; + assert(seen == 1); + } +} +int main(void) +{ + /* J714s and the other T6050 trees: ports 1 to 3. */ + fresh(); + phy("phy@2ab000000", 1, RESERVED); + phy("phy@383000000", 2, RESERVED); + phy("phy@38b000000", 3, RESERVED); + expect((const u32[]){1, 2, 3}, 3); + /* J716c: its tree also hands port 0 to the OS. */ + phy("phy@2a3000000", 0, RESERVED); + expect((const u32[]){1, 2, 3, 0}, 4); + /* Port 0 the tree does not hand over keeps aura's debug link. */ + fresh(); + phy("phy@2a3000000", 0, 0); + phy("phy@2ab000000", 1, RESERVED); + expect((const u32[]){1}, 1); + /* No port index, or no such port: left alone. */ + fresh(); + phy("phy@1", 0, RESERVED | NO_INDEX); + phy("phy@2", 6, RESERVED); + expect(NULL, 0); + puts("PASS combo phy ports 4 cases"); + return 0; +} +''' + run(tmp_path, "combo-ports", code, "PASS combo phy ports 4 cases") + + +def test_t6050_register_windows_map_one_to_one(tmp_path): + source = (REPO / "src/kboot_display_handoff.c").read_text() + code = PRELUDE + r''' +#define T6050_INIT +''' + definition(source, "static T6050_INIT bool t6050_identity_reg(") + r''' +static void cells(int node, u32 a, u32 s) +{ + assert(!fdt_setprop_u32(tree, node, "#address-cells", a)); + assert(!fdt_setprop_u32(tree, node, "#size-cells", s)); +} +static void ranges(int node, const u64 *e, int n) +{ + fdt32_t r[24]; + for (int i = 0; i < 3 * n; i++) { + r[2 * i] = cpu_to_fdt32(e[i] >> 32); + r[2 * i + 1] = cpu_to_fdt32((u32)e[i]); + } + assert(!fdt_setprop(tree, node, "ranges", r, 8 * 3 * n)); +} +/* /soc (with @n ranges entries) / dcp, reg 0x48ae00000+0x4000 */ +static int build(const u64 *e, int n) +{ + fresh(); + cells(0, 2, 2); + int soc = node_at("/", "soc"); + cells(soc, 2, 2); + if (n < 0) + assert(!fdt_setprop_empty(tree, soc, "ranges")); + else + ranges(soc, e, n); + int dcp = node_at("/soc", "dcp"); + reg(dcp, 0x48ae00000UL, 0x4000); + return fdt_path_offset(tree, "/soc/dcp"); +} +int main(void) +{ + int cases = 0; + /* Empty ranges, or one identity entry holding the window (the J716c's soc). */ + assert(t6050_identity_reg(tree, build(NULL, -1))); cases++; + const u64 j716c[] = {0x200000000UL, 0x200000000UL, 0x400000000UL}; + assert(t6050_identity_reg(tree, build(j716c, 1))); cases++; + /* An identity entry that ends inside the window, or does not reach it. */ + const u64 tail[] = {0x200000000UL, 0x200000000UL, 0x28ae02000UL}; + assert(!t6050_identity_reg(tree, build(tail, 1))); cases++; + const u64 short_[] = {0x200000000UL, 0x200000000UL, 0x200000000UL}; + assert(!t6050_identity_reg(tree, build(short_, 1))); cases++; + /* A translating entry first: it is the one used, even with an identity one after it. */ + const u64 first[] = {0x480000000UL, 0x500000000UL, 0x40000000UL, + 0x200000000UL, 0x200000000UL, 0x400000000UL}; + assert(!t6050_identity_reg(tree, build(first, 2))); cases++; + /* The identity entry first, an unrelated one after: accepted. */ + const u64 after[] = {0x200000000UL, 0x200000000UL, 0x400000000UL, + 0x700000000UL, 0x800000000UL, 0x100000000UL}; + assert(t6050_identity_reg(tree, build(after, 2))); cases++; + /* No ranges at all on the bus: not mapped. */ + int dcp = build(NULL, -1); + assert(!fdt_delprop(tree, fdt_parent_offset(tree, dcp), "ranges")); + assert(!t6050_identity_reg(tree, fdt_path_offset(tree, "/soc/dcp"))); cases++; + /* Directly under the root: the root's own cells are checked. */ + fresh(); + cells(0, 2, 1); + reg(node_at("/", "pmc"), 0x286700000UL, 0x114000); + assert(!t6050_identity_reg(tree, fdt_path_offset(tree, "/pmc"))); cases++; + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + assert(t6050_identity_reg(tree, fdt_path_offset(tree, "/pmc"))); cases++; + /* An empty ranges under a one-cell root: the 64-bit window would be cut. */ + dcp = build(NULL, -1); + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 1)); + assert(!t6050_identity_reg(tree, fdt_path_offset(tree, "/soc/dcp"))); cases++; + /* A wrapping entry, even one listed after the identity entry. */ + const u64 wrap[] = {0x200000000UL, 0x200000000UL, 0x400000000UL, + 0xfffffffff0000000UL, 0xfffffffff0000000UL, 0x20000000UL}; + assert(!t6050_identity_reg(tree, build(wrap, 2))); cases++; + printf("PASS identity reg %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "identity-reg", code, "PASS identity reg 11 cases") + + +def test_t6050_external_scanout_darts_resolved(tmp_path): + source = (REPO / "src/kboot_display_handoff.c").read_text() + code = PRELUDE + r''' +#define T6050_TEXT +#define T6050_INIT +#define DART_DCP 0x48a340000UL +#define DART_DISP0 0x48a300000UL +#define EXT0 0x2b2e00000UL +#define DART_DISPEXT0 0x2b2300000UL +/* The ADT: the internal DARTs, dcpext0 and its display DART; nothing else */ +static int t6050_adt_reg(const char *path, int idx, u64 *addr, u64 *size) +{ + (void)idx; (void)size; + if (!strcmp(path, "/arm-io/dart-dcp")) *addr = DART_DCP; + else if (!strcmp(path, "/arm-io/dart-disp0")) *addr = DART_DISP0; + else if (!strcmp(path, "/arm-io/dcpext0")) *addr = EXT0; + else if (!strcmp(path, "/arm-io/dart-dispext0")) *addr = DART_DISPEXT0; + else return -1; + return 0; +} +''' + definition(source, "static T6050_TEXT int t6050_node_at(") + \ + definition(source, "static T6050_TEXT bool t6050_available(") + \ + definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ + definition(source, "static T6050_TEXT bool t6050_ext_display_dart(") + r''' +enum { PH_EXT = 1, PH_DCP, PH_DISP0, PH_OTHER }; +static void dart(const char *name, u64 base, u32 ph, u32 cells, const char *status) +{ + int n = node_at("/", name); + assert(!fdt_setprop_string(tree, n, "compatible", "apple,t8110-dart")); + reg(n, base, 0x20000); + assert(!fdt_setprop_u32(tree, n, "#iommu-cells", cells)); + if (status) + assert(!fdt_setprop_string(tree, n, "status", status)); + assert(!fdt_setprop_u32(tree, n, "phandle", ph)); +} +/* dcpext0 whose piodma maps through ; `ext_status`, `cells`, `dstatus` vary */ +static void world(u32 ph, u32 sid, const char *ext_status, u32 cells, const char *dstatus) +{ + fresh(); + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart("iommu@2b2300000", DART_DISPEXT0, PH_EXT, cells, dstatus); + dart("iommu@48a340000", DART_DCP, PH_DCP, 1, NULL); + dart("iommu@48a300000", DART_DISP0, PH_DISP0, 1, NULL); + dart("iommu@500000000", 0x500000000UL, PH_OTHER, 1, NULL); + int e = node_at("/", "dcp@2b2e00000"); + assert(!fdt_setprop_string(tree, e, "compatible", "apple,t6050-dcpext")); + reg(e, EXT0, 0x4000); + if (ext_status) + assert(!fdt_setprop_string(tree, e, "status", ext_status)); + int pio = node_at("/dcp@2b2e00000", "piodma"); + fdt32_t io[2] = {cpu_to_fdt32(ph), cpu_to_fdt32(sid)}; + assert(!fdt_setprop(tree, pio, "iommus", io, sizeof(io))); +} +int main(void) +{ + int cases = 0; + /* The J716c's shape: an enabled engine's piodma on its display DART, SID 16. */ + world(PH_EXT, 16, "okay", 1, "okay"); + assert(t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, NULL, 1, NULL); + assert(t6050_ext_display_dart(tree, PH_EXT)); cases++; + /* The engine disabled, its DART disabled, a two-cell DART, or the piodma on SID 15. */ + world(PH_EXT, 16, "disabled", 1, NULL); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "disabled"); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 2, NULL); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 15, "okay", 1, NULL); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + /* A dangling phandle, even when the engine's piodma names it too. */ + world(0x77, 16, "okay", 1, NULL); + assert(!t6050_ext_display_dart(tree, 0x77)); cases++; + /* Either internal DART, or a DART that is no engine's dart-dispextN, even if piodma names it. */ + world(PH_DCP, 16, "okay", 1, NULL); + assert(!t6050_ext_display_dart(tree, PH_DCP)); cases++; + world(PH_DISP0, 16, "okay", 1, NULL); + assert(!t6050_ext_display_dart(tree, PH_DISP0)); cases++; + world(PH_OTHER, 16, "okay", 1, NULL); + assert(!t6050_ext_display_dart(tree, PH_OTHER)); cases++; + /* status as whole strings: "okay"/"ok" with their terminator, nothing longer or shorter */ + fresh(); + int n = node_at("/", "n"); + assert(t6050_available(tree, n)); cases++; + assert(!fdt_setprop_string(tree, n, "status", "ok") && t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "okay", 4) && !t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "okay\0x", 6) && !t6050_available(tree, n)); cases++; + assert(!fdt_setprop_string(tree, n, "status", "fail") && !t6050_available(tree, n)); cases++; + printf("PASS external scanout darts %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "ext-darts", code, "PASS external scanout darts 15 cases") + + +def test_t6050_rx_clock_groups_pass_whole(tmp_path): + source = (REPO / "src/kboot_atc.c").read_text() + layout = source[source.index("struct atc_tunable {"):source.index("struct atc_fuse_info {")] + table = source[source.index("static const struct t6050_atc_tunable t6050_atc_tunables[]"):] + table = table[:table.index("};")] + # Every group is copied whole, in ADT order: the driver applies the records in that order. + # The RX clock groups go to the driver's lane RX clock banks (core + 0xd000 and + 0x1d000). + assert '"apple,tunable-lane0-rx-clock", "core", 20, 0xd000}' in table + assert '"apple,tunable-lane1-rx-clock", "core", 29, 0x1d000}' in table + append = definition(source, "static int dt_append_atc_tunable(") + assert "n_tunables = tunables_len / sizeof(*tunable_adt);" in append + assert append.count("n_tunables =") == 1 + code = PRELUDE + r''' +#define PACKED __attribute__((packed)) +#define static_assert _Static_assert +static void *adt; +static const void *records; +static u32 records_len; +static const void *adt_getprop(void *a, int node, const char *name, u32 *len) +{ + (void)a; (void)node; (void)name; + *len = records_len; + return records; +} +''' + layout + definition(source, "static int dt_append_atc_tunable(") + r''' +/* + * A synthetic RX clock group of four records on four registers of the lane's RX clock bank, the + * shape a T6050 ADT gives each combo PHY lane. The offsets, masks and values are made up. + */ +#define R(off, mask, value) {0x200 + (off), 32, mask, value} +static const struct atc_tunable four[4] = { + R(0, 0x3, 0x1), R(8, 0xf0, 0x50), R(0x20, 0x100, 0x0), R(0x2c, 0x7000, 0x3000), +}; +/* That group as the OS gets it: per record, in this order */ +static const u32 four_cells[12] = { + 0x200, 0x3, 0x1, 0x208, 0xf0, 0x50, 0x220, 0x100, 0x0, 0x22c, 0x7000, 0x3000, +}; +/* Copies @n records through the T6050 RX clock path; checks each one; returns how many */ +static int copied(const struct atc_tunable *t, u32 n) +{ + fresh(); + int node = node_at("/", "phy"); + /* As t6050_export_atc() builds it: offsets within the bank, whose size bounds them */ + struct adt_tunable_info info = {"tunable_LN0_RX_CFG_TX_OF_RXCLK", + "apple,tunable-lane0-rx-clock", 0, 0x4000, true}; + records = t; + records_len = n * sizeof(*t); + assert(!dt_append_atc_tunable(tree, 0, node, &info)); + int len; + const fdt32_t *v = fdt_getprop(tree, node, "apple,tunable-lane0-rx-clock", &len); + assert(v && len % 12 == 0); + for (int i = 0; i < len / 12; i++) + assert(fdt32_ld(&v[3 * i]) == t[i].offset && fdt32_ld(&v[3 * i + 1]) == t[i].mask && + fdt32_ld(&v[3 * i + 2]) == t[i].value); + return len / 12; +} +int main(void) +{ + int cases = 0, len; + struct atc_tunable g[8]; + /* The four records reach the OS unchanged and in order. */ + assert(copied(four, 4) == 4); + const fdt32_t *v = + fdt_getprop(tree, fdt_path_offset(tree, "/phy"), "apple,tunable-lane0-rx-clock", &len); + assert(len == sizeof(four_cells)); + for (int i = 0; i < 12; i++) + assert(fdt32_ld(&v[i]) == four_cells[i]); + cases++; + /* A group that revisits the same registers and fields later passes whole, in order: the + * later records can be the final values, so none is dropped. */ + memcpy(g, four, sizeof(four)); + for (int i = 0; i < 4; i++) + g[4 + i] = (struct atc_tunable)R(four[i].offset - 0x200, four[i].mask, 0); + assert(copied(g, 8) == 8); cases++; + for (int i = 0; i < 4; i++) + g[7 - i] = (struct atc_tunable)R(four[i].offset - 0x200, four[i].mask, 0); + assert(copied(g, 8) == 8); cases++; + memcpy(g, four, sizeof(four)); + memcpy(g + 4, four, sizeof(four)); + assert(copied(g, 8) == 8); cases++; + /* An odd group passes whole. */ + assert(copied(four, 3) == 3); cases++; + /* A record outside the bank fails the group. */ + g[0] = (struct atc_tunable)R(0x4000, 0x1, 0x1); + fresh(); + int node = node_at("/", "phy"); + struct adt_tunable_info info = {"tunable_LN0_RX_CFG_TX_OF_RXCLK", + "apple,tunable-lane0-rx-clock", 0, 0x4000, true}; + records = g; + records_len = sizeof(*g); + assert(dt_append_atc_tunable(tree, 0, node, &info)); cases++; + printf("PASS rx clock %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "rx-clock", code, "PASS rx clock 6 cases") + + +def test_t6050_prewired_display_subsystem_is_the_only_one(tmp_path): + source = (REPO / "src/kboot_display_handoff.c").read_text() + check = definition(source, "static T6050_INIT int t6050_check_dt(void *dt)") + assert "int disp = t6050_disp_node(dt);" in check + code = PRELUDE + r''' +#define T6050_INIT +static struct { bool prewired; } t6050; +static struct { const char *name, *disp_compat; } soc = {"t6050", "apple,display-subsystem"}; +static struct { const typeof(soc) *soc; } dcp6 = {&soc}; +#define dlog(fmt, ...) printf("DCP: %s: " fmt, dcp6.soc->name, ##__VA_ARGS__) +#undef bail +#define bail(...) do { dlog(__VA_ARGS__); return -1; } while (0) +static int kboot_fdt_path_exact(void *t, const char *alias) +{ + const char *path = fdt_get_alias(t, alias); + return path ? fdt_path_offset(t, path) : -FDT_ERR_NOTFOUND; +} +''' + definition(source, "static T6050_INIT int t6050_disp_node(") + r''' +static int subsystem(const char *name) +{ + int n = node_at("/", name); + assert(!fdt_setprop_string(tree, n, "compatible", "apple,display-subsystem")); + return n; +} +int main(void) +{ + int cases = 0; + /* Prewired (the J716c): its one display subsystem. */ + fresh(); + subsystem("display"); + t6050.prewired = true; + assert(t6050_disp_node(tree) == fdt_path_offset(tree, "/display")); cases++; + /* A second one, enabled or not: refused. */ + int second = subsystem("display-die1"); + assert(!fdt_setprop_string(tree, second, "status", "disabled")); + assert(t6050_disp_node(tree) < 0); cases++; + /* None. */ + fresh(); + assert(t6050_disp_node(tree) < 0); cases++; + /* An aliased tree names its own by disp0, however many there are. */ + fresh(); + subsystem("display"); + subsystem("display-die1"); + assert(!fdt_setprop_string(tree, node_at("/", "aliases"), "disp0", "/display-die1")); + t6050.prewired = false; + assert(t6050_disp_node(tree) == fdt_path_offset(tree, "/display-die1")); cases++; + printf("PASS display subsystem %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "disp-node", code, "PASS display subsystem 4 cases") From 0113c4eea3c14f5ce3f787d028fda769957b8e63 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 20 Nov 2026 22:01:00 -0500 Subject: [PATCH 02/10] kboot: T6050: give the external engines the stub's firmware version A device tree can enable its external display engines itself (the J716c's does) and name the firmware build its driver was written for in their apple,firmware-compat (27.0). The engines run the stub's firmware, and the driver picks their method IDs from that property, so on a 27.2 stub they were sent 27.0 IDs. On every T6050 boot, after the display handoff picks any pinned image, set apple,firmware-compat on each available apple,t6050-dcpext node (status absent, or first status string "okay" or "ok" matching Linux's of_device_is_available() predicate) to this boot's version when the stub's version is a known one, adding it where the node has none, and apple,firmware-version where the node has one, whatever the version's number of cells (a four-cell version grows them), as the internal dcp's handoff does. t6050_ext_index() requires an identity-mapped reg (t6050_identity_reg()), matching the scanout path, so a node behind a translated bus cannot alias an ADT dcpextN. Only an engine matching an ADT dcpextN whose nub reports running == 1 and a valid UUID (matching the pinned image when one was selected) and whose firmware segments (other than __OS_LOG) and DART page-table regions (dart-dcpextN SID 23, dart-dispextN SID 16) lie outside the OS's RAM is updated or kept in the handoff; an available engine that fails matching or validation is disabled (or deleted if a packed tree cannot fit status = "disabled") and removed from any apple,display-subsystem scanout iommus (without failing the internal panel handoff), so the OS never probes a rejected engine with a stale method table. Scanout iommus compaction mutates the property in place via fdt_getprop_w() and fdt_setprop_placeholder() without allocating or imposing a fixed length cap. Rejection runs on its own scratch copy (falling back to editing the DT in place if scratch allocation fails) before firmware-version writes, so rejected engines are still disabled and stripped from scanout on an unknown stub version and are not rolled back if a packed DT lacks room for the firmware-version write or if scanout cleanup fails. The version update runs in a separate scratch transaction: a failed write (no room) or walk leaves versions unchanged while keeping any rejections, and an unknown stub version leaves the tree's versions as they were. A node that is not available stays t6050_ext_one()'s, which publishes the version itself when it hands the engine off; that handoff now passes over an engine the tree enables instead of logging it as left with iBoot. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 407 +++++++++++++- tests/python/test_t6050_handoff_bounds.py | 451 ++++++++++++++- tests/python/test_t6050_tree_shape.py | 642 +++++++++++++++++++++- 3 files changed, 1471 insertions(+), 29 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index f4a0dd5d0..b301e3766 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -3452,6 +3452,7 @@ static const struct { #define T6050_CLOCKS 4 #define T6050_MBOX 0x8000 /* the ASC mailbox, from the coprocessor base */ #define T6050_PATH 64 +#define T6050_ENGINES 8 /* dcpext0..7 */ #define T6050_STATUS "asahi,t6050-dcp-handoff" #define T6050_TCR_WANT (DART_T8110_TCR_TRANSLATE | DART_T8110_TCR_FOUR_LEVEL) #define T6050_TCR_MASK (T6050_TCR_WANT | DART_T8110_TCR_BYPASS_DART) @@ -3662,13 +3663,17 @@ static T6050_INIT int t6050_check_adt(void) return 0; } -/* Whether `node` is available as Linux reads status: absent, "okay" or "ok" (whole strings) */ +/* Whether `node` is available as Linux reads status: absent, or first string "okay" or "ok" */ static T6050_TEXT bool t6050_available(void *dt, int node) { int len; - const char *status = fdt_getprop(dt, node, "status", &len); - return !status || (len == sizeof("okay") && !memcmp(status, "okay", len)) || - (len == sizeof("ok") && !memcmp(status, "ok", len)); + const char *status = node < 0 ? NULL : fdt_getprop(dt, node, "status", &len); + if (!status) + return node >= 0 && len == -FDT_ERR_NOTFOUND; + size_t slen = strnlen(status, len); + if (slen == (size_t)len) + return false; + return (slen == 4 && !memcmp(status, "okay", 4)) || (slen == 2 && !memcmp(status, "ok", 2)); } /* @@ -3832,11 +3837,78 @@ static T6050_INIT int t6050_check_dt(void *dt) } /* - * Whether phandle `ph` is the display DART of an apple,t6050-dcpext engine the tree enables: an - * available t8110 DART with one iommu cell at the ADT's dart-dispextN of engine dcpextN, that + * An engine's memory stays out of the OS's RAM [phys_base, +mem_size): every segment of the nub at + * `nub_path` but __OS_LOG (dcp6_reserve_oslogs() reserves those) and, for each of the `nstreams` + * streams (ADT DART darts[i], stream sids[i]), its ADT page-table region pt-region-, which + * must be page-aligned and hold the stream's l1-tt root page. + */ +static T6050_TEXT int t6050_check_placement(const char *nub_path, const char *const *darts, + const u8 *sids, int nstreams) +{ + u64 ram = cur_boot_args.phys_base, ram_size = cur_boot_args.mem_size; + u32 len = 0; + int nub = adt_path_offset(adt, nub_path); + const struct adt_segment_ranges *seg = + nub < 0 ? NULL : adt_getprop(adt, nub, "segment-ranges", &len); + + if (!seg || !len || len % sizeof(*seg) || len > 16 * sizeof(*seg)) + bail("%s: no or bad segment-ranges\n", nub_path); + for (u32 i = 0; i < len / sizeof(*seg); i++) { + struct adt_segment_ranges r; + memcpy(&r, &seg[i], sizeof(r)); + u64 size = ALIGN_UP((u64)r.size, SZ_16K); + if (r.unk != SEG_FLAGS_OSLOG && dcp6_overlaps(r.phys, size, ram, ram_size)) + bail("%s: segment %u (0x%lx) lies in the OS's RAM\n", nub_path, i, r.phys); + } + + for (int i = 0; i < nstreams; i++) { + char name[16]; + u64 lo, hi, l1, count; + int node = adt_path_offset(adt, darts[i]); + snprintf(name, sizeof(name), "l1-tt-%u", sids[i]); + if (node < 0 || dcp6_adt_u64_pair(node, name, &l1, &count) || !l1) + bail("%s: no %s\n", darts[i], name); + snprintf(name, sizeof(name), "pt-region-%u", sids[i]); + if (dcp6_adt_u64_pair(node, name, &lo, &hi) || hi <= lo || + ((lo | hi | l1) & (DART_PAGE - 1)) || l1 < lo || l1 > hi - DART_PAGE || + dcp6_overlaps(lo, hi - lo, ram, ram_size)) + bail("%s: %s is missing, does not hold l1-tt-%u or lies in the OS's RAM\n", darts[i], + name, sids[i]); + } + return 0; +} + +/* + * External engine `n` in the ADT: its nub must report running == 1 and a valid UUID (matching the + * pinned image when one has been selected), and its firmware segments and both DART page-table + * regions (dart-dcpextN SID 23, dart-dispextN SID 16) must lie outside the OS's RAM. + */ +static T6050_TEXT int t6050_check_ext_adt(int n) +{ + char nub_path[64], dd[2][32]; + const char *darts[2] = {dd[0], dd[1]}; + static const u8 sids[2] = {23, 16}; + u32 len = 0, val = 0; + + if (n < 0 || n >= T6050_ENGINES) + return -1; + snprintf(nub_path, sizeof(nub_path), "/arm-io/dcpext%d/iop-dcpext%d-nub", n, n); + int nub = adt_path_offset(adt, nub_path); + const char *uuid = nub < 0 ? NULL : adt_getprop(adt, nub, "uuid", &len); + if (!dcp6_is_uuid(uuid, len) || (t6050.uuid && strcmp(uuid, t6050.uuid)) || + dcp6_adt_u32(nub, "running", &val) || val != 1) + bail("dcpext%d: not running the pinned image\n", n); + snprintf(dd[0], sizeof(dd[0]), "/arm-io/dart-dcpext%d", n); + snprintf(dd[1], sizeof(dd[1]), "/arm-io/dart-dispext%d", n); + return t6050_check_placement(nub_path, darts, sids, 2); +} + +/* + * Whether phandle `ph` is the display DART of an available apple,t6050-dcpext engine matching ADT + * dcpextN (`*eng`): an available t8110 DART with one iommu cell at the ADT's dart-dispextN, that * the engine's piodma child maps through as SID 16. Neither internal DART is one. */ -static T6050_TEXT bool t6050_ext_display_dart(void *dt, u32 ph) +static T6050_TEXT bool t6050_ext_dart_engine(void *dt, u32 ph, int *eng) { int dart = fdt_node_offset_by_phandle(dt, ph), clen = 0, rlen = 0; const fdt32_t *cells = dart < 0 ? NULL : fdt_getprop(dt, dart, "#iommu-cells", &clen); @@ -3853,7 +3925,7 @@ static T6050_TEXT bool t6050_ext_display_dart(void *dt, u32 ph) if ((!t6050_adt_reg("/arm-io/dart-dcp", 0, &adt_base, NULL) && adt_base == base) || (!t6050_adt_reg("/arm-io/dart-disp0", 0, &adt_base, NULL) && adt_base == base)) return false; - for (int n = 0; n < 8; n++) { /* dcpext0..7 */ + for (int n = 0; n < T6050_ENGINES; n++) { /* dcpext0..7 */ snprintf(a, sizeof(a), "/arm-io/dart-dispext%d", n); if (t6050_adt_reg(a, 0, &adt_base, NULL) || adt_base != base) continue; @@ -3863,15 +3935,30 @@ static T6050_TEXT bool t6050_ext_display_dart(void *dt, u32 ph) : t6050_node_at(dt, "apple,t6050-dcpext", coproc); int pio = ext < 0 ? -1 : fdt_subnode_offset(dt, ext, "piodma"), plen = 0; const fdt32_t *p = pio < 0 ? NULL : fdt_getprop(dt, pio, "iommus", &plen); - return t6050_available(dt, ext) && t6050_identity_reg(dt, ext) && p && plen == 8 && - fdt32_ld(&p[0]) == ph && fdt32_ld(&p[1]) == 16; + if (!t6050_available(dt, ext) || !t6050_identity_reg(dt, ext) || !p || plen != 8 || + fdt32_ld(&p[0]) != ph || fdt32_ld(&p[1]) != 16) + return false; + if (eng) + *eng = n; + return true; } return false; } +/* + * Whether phandle `ph` is the display DART of an apple,t6050-dcpext engine the tree enables whose + * ADT image and placement checks pass. + */ +static T6050_TEXT bool t6050_ext_display_dart(void *dt, u32 ph) +{ + int n; + return t6050_ext_dart_engine(dt, ph, &n) && !t6050_check_ext_adt(n); +} + /* * A prewired display subsystem's iommus after its first entry: each <&dart 0> an enabled - * external engine's display DART (t6050_ext_display_dart()), each once. + * external engine's display DART (t6050_ext_dart_engine()), each once. Engines whose ADT image or + * placement check fails are dropped from the published scanout list and disabled. */ static T6050_TEXT int t6050_check_ext_scanout(void *dt, const fdt32_t *io, int len) { @@ -3880,7 +3967,7 @@ static T6050_TEXT int t6050_check_ext_scanout(void *dt, const fdt32_t *io, int l if (fdt32_ld(&io[j]) == fdt32_ld(&io[i])) bail("the display subsystem lists one external display DART twice\n"); if (fdt32_ld(&io[i + 1]) || fdt32_ld(&io[i]) == fdt32_ld(&io[0]) || - !t6050_ext_display_dart(dt, fdt32_ld(&io[i]))) + !t6050_ext_dart_engine(dt, fdt32_ld(&io[i]), NULL)) bail("display subsystem iommus entry %d is not an enabled external engine's display " "DART\n", i / 2); @@ -4001,7 +4088,8 @@ static T6050_TEXT int t6050_publish_wiring(void *dt) /* * A prewired tree names both (t6050_check_prewired() checked them): the clocks get their rates - * from the ADT, and the PMC metadata node is enabled. + * from the ADT, the PMC metadata node is enabled, and any external engine failing ADT image or + * placement checks is left out of the display subsystem's scanout iommus. */ static T6050_TEXT int t6050_publish_prewired(void *dt) { @@ -4018,6 +4106,22 @@ static T6050_TEXT int t6050_publish_prewired(void *dt) if (node < 0 || fdt_setprop_u32(dt, node, "clock-frequency", t6050.clk_hz[i])) return -1; } + int disp = kboot_fdt_path_exact(dt, t6050.path[T6050_DISP]); + fdt32_t *io = disp < 0 ? NULL : fdt_getprop_w(dt, disp, "iommus", &len); + if (io && len > 8 && !(len % 8)) { + int nkept = 2; + for (int i = 2; i < len / 4; i += 2) { + if (!t6050_ext_display_dart(dt, fdt32_ld(&io[i]))) + continue; + if (nkept != i) + memmove(&io[nkept], &io[i], 2 * sizeof(io[0])); + nkept += 2; + } + void *prop_data; + if (nkept < len / 4 && + fdt_setprop_placeholder(dt, disp, "iommus", nkept * (int)sizeof(io[0]), &prop_data)) + return -1; + } return t6050_setprop(dt, T6050_PMC, "status", "okay", sizeof("okay")); } @@ -4192,6 +4296,10 @@ static T6050_TEXT int t6050_ext_one(void *dt, int n) if (t6050_adt_reg(a, 0, &coproc, NULL)) bail("dcpext%d: no ADT reg\n", n); node = t6050_ext_node(dt, coproc); + if (node >= 0 && t6050_available(dt, node)) { + dlog("dcpext%d: the device tree enables it itself\n", n); + return 1; + } if (node < 0 || dcp6_status_disabled(dt, node, a)) bail("dcpext%d: no disabled node at 0x%lx\n", n, coproc); @@ -4323,19 +4431,280 @@ static T6050_TEXT int t6050_ext_one(void *dt, int n) return 0; } -/* Each engine but `skip` in its own transaction, after the internal display's (or the first's) */ +/* + * Each engine but `skip` in its own transaction, after the internal display's (or the first's). + * One the device tree enables itself is left to it (t6050_ext_version()). + */ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip) { for (int n = 0; n < engines; n++) { if (n == skip) continue; void *scratch = dcp6_scratch(dt); - if (!scratch || t6050_ext_one(scratch, n) || dcp6_commit(dt, scratch)) { - if (scratch) - free(scratch); + int ret = scratch ? t6050_ext_one(scratch, n) : -1; + if (ret && scratch) + free(scratch); + if (ret > 0) + continue; + if (ret || dcp6_commit(dt, scratch)) dlog("dcpext%d stays with iBoot: no handoff\n", n); + } +} + +/* + * The ADT dcpextN index whose coprocessor is at `node`'s identity-mapped reg[0], or -1 + */ +static T6050_TEXT int t6050_ext_index(void *dt, int node) +{ + int len; + const fdt64_t *reg = fdt_getprop(dt, node, "reg", &len); + if (!reg || len < 16 || !t6050_identity_reg(dt, node)) + return -1; + u64 coproc = fdt64_ld(®[0]); + for (int n = 0; n < T6050_ENGINES; n++) { + char a[32]; + u64 adt_coproc; + snprintf(a, sizeof(a), "/arm-io/dcpext%d", n); + if (!t6050_adt_reg(a, 0, &adt_coproc, NULL) && adt_coproc == coproc) + return n; + } + return -1; +} + +/* Whether `ph` is the display DART node matching `dart_addr` (an identity-mapped t8110 DART) */ +static T6050_TEXT bool t6050_dart_at(void *dt, u32 ph, u64 dart_addr) +{ + int dart = fdt_node_offset_by_phandle(dt, ph), rlen = 0; + const fdt64_t *reg = dart < 0 ? NULL : fdt_getprop(dt, dart, "reg", &rlen); + return reg && rlen >= 16 && !fdt_node_check_compatible(dt, dart, "apple,t8110-dart") && + t6050_identity_reg(dt, dart) && fdt64_ld(®[0]) == dart_addr; +} + +/* + * Remove any scanout reference to a rejected external engine `ext` (index `n`, or -1) from every + * apple,display-subsystem node's `iommus`. Matches by `ext`'s piodma `iommus` phandle and, when + * `n >= 0`, by `/arm-io/dart-dispextN` address, never dropping `/arm-io/dart-disp0`. + */ +static T6050_TEXT int t6050_drop_ext_scanout(void *dt, int ext, int n, bool *changed) +{ + int pio = fdt_subnode_offset(dt, ext, "piodma"), plen = 0; + const fdt32_t *p = pio < 0 ? NULL : fdt_getprop(dt, pio, "iommus", &plen); + u32 pio_ph = p && plen >= 8 ? fdt32_ld(&p[0]) : 0; + u64 dispext_addr = 0, disp0_addr = 0; + bool has_dispext = false, has_disp0 = false; + + if (n >= 0) { + char a[32]; + snprintf(a, sizeof(a), "/arm-io/dart-dispext%d", n); + has_dispext = !t6050_adt_reg(a, 0, &dispext_addr, NULL); + } + has_disp0 = !t6050_adt_reg("/arm-io/dart-disp0", 0, &disp0_addr, NULL); + + int ret = 0; + + for (int disp = fdt_node_offset_by_compatible(dt, -1, "apple,display-subsystem"); disp >= 0; + disp = fdt_node_offset_by_compatible(dt, disp, "apple,display-subsystem")) { + int len = 0; + fdt32_t *io = fdt_getprop_w(dt, disp, "iommus", &len); + if (!io || len <= 0 || len % 8) + continue; + int nkept = 0; + bool dropped = false; + for (int i = 0; i < len / 4; i += 2) { + u32 ph = fdt32_ld(&io[i]); + bool is_disp0 = has_disp0 && t6050_dart_at(dt, ph, disp0_addr); + bool match = !is_disp0 && ((pio_ph && ph == pio_ph) || + (has_dispext && t6050_dart_at(dt, ph, dispext_addr))); + if (match) { + dropped = true; + continue; + } + if (nkept != i) + memmove(&io[nkept], &io[i], 2 * sizeof(io[0])); + nkept += 2; + } + if (!dropped) + continue; + if (nkept) { + void *prop_data; + if (fdt_setprop_placeholder(dt, disp, "iommus", nkept * (int)sizeof(io[0]), + &prop_data)) { + ret = -1; + continue; + } + *changed = true; + } else { + if (fdt_delprop(dt, disp, "iommus")) { + ret = -1; + continue; + } + *changed = true; + if (t6050_available(dt, disp) && fdt_setprop_string(dt, disp, "status", "disabled")) { + if (fdt_del_node(dt, disp)) { + ret = -1; + continue; + } + disp = -1; + } } } + return ret; +} + +/* + * Walk apple,t6050-dcpext nodes in `dt`: return the count, or a negative FDT error. + * Stores `target`'s 0-based ordinal in `*ord` when `ord` is set, and the offset of the + * `want_ord`-th node in `*found` when `found` is set. + */ +static T6050_TEXT int t6050_ext_walk(void *dt, int target, int *ord, int want_ord, int *found) +{ + int count = 0, o = -1; + + if (ord) + *ord = -1; + if (found) + *found = -FDT_ERR_NOTFOUND; + while ((o = fdt_node_offset_by_compatible(dt, o, "apple,t6050-dcpext")) >= 0) { + if (ord && o == target) + *ord = count; + if (found && count == want_ord) + *found = o; + count++; + } + return o == -FDT_ERR_NOTFOUND ? count : o; +} + +/* + * Disable every available apple,t6050-dcpext node that has no identity-mapped ADT match or fails + * t6050_check_ext_adt(), and strip it from any display-subsystem scanout iommus. Runs on a + * scratch copy when available (falling back to editing `dt` in place if scratch allocation fails) + * before firmware-version writes, independent of os_firmware and without rolling back if a packed + * DT lacks room for a firmware-version update or if scanout cleanup fails. Shrinking iommus + * first frees space in packed trees; if setting status = "disabled" still hits NOSPACE (e.g. + * absent status), delete the rejected node instead so Linux never probes it. + */ +static T6050_TEXT void t6050_ext_reject(void *dt) +{ + void *scratch = NULL; + bool in_place = false, changed = false; + int err = 0, at = fdt_node_offset_by_compatible(dt, -1, "apple,t6050-dcpext"); + + while (at >= 0) { + void *cur = scratch ? scratch : dt; + if (!t6050_available(cur, at)) { + at = fdt_node_offset_by_compatible(cur, at, "apple,t6050-dcpext"); + continue; + } + int n = t6050_ext_index(cur, at); + if (n >= 0 && !t6050_check_ext_adt(n)) { + at = fdt_node_offset_by_compatible(cur, at, "apple,t6050-dcpext"); + continue; + } + if (!scratch && !in_place) { + scratch = dcp6_scratch(dt); + if (scratch) + cur = scratch; + else + in_place = true; + } + int ord, total = t6050_ext_walk(cur, at, &ord, -1, NULL); + const char *name = fdt_get_name(cur, at, NULL); + if (total < 0 || ord < 0) { + err = -1; + break; + } + if (n < 0) + dlog("external engine %s has no matching ADT dcpextN: disabling\n", + name ? name : "(unknown)"); + else + dlog("dcpext%d rejected: disabling and keeping out of scanout\n", n); + if (t6050_drop_ext_scanout(cur, at, n, &changed)) + dlog("could not drop %s from display scanout\n", + name ? name : "rejected external engine"); + int after = t6050_ext_walk(cur, -1, NULL, ord, &at); + if (after < 0) { + err = -1; + break; + } + if (after != total) { + /* A deleted display-subsystem node enclosed an engine: re-scan from the top. */ + at = fdt_node_offset_by_compatible(cur, -1, "apple,t6050-dcpext"); + continue; + } + if (at < 0 || + (fdt_setprop_string(cur, at, "status", "disabled") && fdt_del_node(cur, at))) { + err = -1; + break; + } + changed = true; + at = fdt_node_offset_by_compatible(cur, -1, "apple,t6050-dcpext"); + } + if (err || (at < 0 && at != -FDT_ERR_NOTFOUND)) + dlog("could not disable rejected external engines%s\n", + changed ? " (partial)" : ": nothing changed"); + if (scratch && changed) + dcp6_commit(dt, scratch); + else + free(scratch); +} + +/* + * External engines the device tree enables itself (the J716c's) run this boot's firmware, but + * the tree names the build it was written for in their apple,firmware-compat, from which the + * driver picks their method IDs. Set it to this boot's version on every such engine that passes + * the ADT running/uuid and placement checks, adding it where the tree left it out (the driver + * has no method table without it), and rewrite apple,firmware-version where the node has one, + * whatever their number of cells, as t6050_publish() does for the internal dcp. An available + * engine with no valid ADT match or failing the ADT running/uuid or placement checks is disabled + * and stripped from any display-subsystem scanout iommus first by t6050_ext_reject(), even on an + * unknown stub or when the version write runs out of space. A node not available (status absent, + * "okay" or "ok") is t6050_ext_one()'s. The version edit is made on its own scratch copy: a + * failed write or walk leaves the versions unchanged while keeping any rejections. + */ +static T6050_TEXT void t6050_ext_version(void *dt) +{ + static const char *const props[] = {"apple,firmware-compat", "apple,firmware-version"}; + int count = 0, err = 0, at = -1; + bool changed = false; + + t6050_ext_reject(dt); + + if (os_firmware.version == V_UNKNOWN || !os_firmware.num_length) { + dlog("firmware %s is unknown: the external engines keep the tree's version\n", + os_firmware.string ? os_firmware.string : ""); + return; + } + void *scratch = dcp6_scratch(dt); + if (!scratch) + return; + while (!err && (at = fdt_node_offset_by_compatible(scratch, at, "apple,t6050-dcpext")) >= 0) { + bool set = false; + if (!t6050_available(scratch, at)) + continue; + int n = t6050_ext_index(scratch, at); + if (n < 0 || t6050_check_ext_adt(n)) + continue; + for (size_t i = 0; !err && i < ARRAY_SIZE(props); i++) { + /* 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); + set = set || !err; + } + count += set; + changed |= set; + } + /* Only -FDT_ERR_NOTFOUND is a walk's normal end */ + if (err || at != -FDT_ERR_NOTFOUND) { + free(scratch); + dlog("could not give the external engines the firmware version: version unchanged\n"); + } else if (!changed) { + free(scratch); + } else if (!dcp6_commit(dt, scratch)) { + if (count) + dlog("firmware %s on %d external engine(s) the device tree enables\n", + os_firmware.string, count); + } } /* Any failure leaves the display nodes disabled: the boot framebuffer keeps the panel */ @@ -4365,8 +4734,6 @@ static T6050_TEXT void t6050_handoff(void *dt, bool prewired) dlog("internal display handoff refused; the boot framebuffer keeps the panel\n"); } -#define T6050_ENGINES 8 /* dcpext0..7 */ - /* * A board without a panel (the J775D, Mac Studio, M5 Ultra): its device tree has no dcp or disp0 * alias, and its display subsystem scans out through one external engine's display DART (the @@ -4498,6 +4865,7 @@ int dt_set_display_handoff(void *dt) if (dcp6.soc->t6050) { if (chip_id != T6050) return 0; + memset(&t6050, 0, sizeof(t6050)); dcp6_find_nubs(); dcp6_reserve_oslogs(dt); int engine; @@ -4509,6 +4877,7 @@ int dt_set_display_handoff(void *dt) t6050_handoff(dt, true); else dlog("the device tree does not describe the internal display: no handoff\n"); + t6050_ext_version(dt); return 0; } diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index adeb342e2..94c36e4a7 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -37,10 +37,17 @@ META_SIZE, BW = 0x40000, 0x20000 HOST = r''' -#include "kboot_display_handoff.c" - #include +static int fail_malloc; +static void *test_malloc(size_t size) +{ + return fail_malloc ? NULL : malloc(size); +} +#define malloc test_malloc +#include "kboot_display_handoff.c" +#undef malloc + void *adt = (void *)1; u32 chip_id = T6050; volatile enum exc_guard_t exc_guard; @@ -70,10 +77,10 @@ char name[32]; u8 data[96]; u32 len; - } props[8]; + } props[10]; int nprops, nreg; u64 reg[5][2]; -} nodes[16]; +} nodes[32]; static int nnodes; static struct node *node(const char *path) @@ -200,7 +207,7 @@ static struct { u64 addr; u32 val; -} words[16]; +} words[32]; static int nwords; static void word(u64 addr, u32 val) @@ -264,6 +271,8 @@ int pmgr_adt_is_powered(const char *path) { + if (!strcmp(path, "/arm-io/dcp") || !strcmp(path, "/arm-io/disp0")) + return 1; DIE("pmgr_adt_is_powered(%s)\n", path); } @@ -387,7 +396,7 @@ */ static int headless(int argc, char **argv) { - static char dt[0x10000]; + static _Alignas(8) char dt[0x10000]; u64 vram = arg(argc, argv, "vram", 0), vram_size = arg(argc, argv, "vsize", 0); u64 meta_size = arg(argc, argv, "meta", 0), bw = arg(argc, argv, "bw", 0); const char *nub = "/arm-io/dcpext0/iop-dcpext0-nub"; @@ -429,6 +438,7 @@ OK(soc); OK(fdt_setprop_u32(dt, soc, "#address-cells", 2)); OK(fdt_setprop_u32(dt, soc, "#size-cells", 2)); + OK(fdt_setprop_empty(dt, soc, "ranges")); for (int i = 0; i < 2; i++) { int d = fdt_dev(dt, soc, i ? "iommu@389308000" : "iommu@389304000", i ? DART_DISP : DART_DCP, i ? PH_DART_DISP : PH_DART_DCP); @@ -522,6 +532,303 @@ return 0; } +/* + * A prewired panel tree with two enabled external engines (dcpext0 healthy, dcpext1 optionally + * failing running/uuid/segment/pt-region checks via fail=0..4). Runs the full + * dt_set_display_handoff() and reports the published DT state. + */ +static int prewired(int argc, char **argv) +{ + static _Alignas(8) char dt[0x10000]; + u64 fail = arg(argc, argv, "fail", 0); + u64 unknown = arg(argc, argv, "unknown", 0); + u64 nocompat = arg(argc, argv, "nocompat", 0); + u64 absent_status = arg(argc, argv, "absent_status", 0); + u64 extra_status = arg(argc, argv, "extra_status", 0); + u64 disp_enabled = arg(argc, argv, "disp_enabled", 0); + u64 oversized_iommus = arg(argc, argv, "oversized_iommus", 0); + u64 fail_scratch = arg(argc, argv, "fail_scratch", 0); + u64 pack = arg(argc, argv, "pack", 0); + u64 nested = arg(argc, argv, "nested", 0); + u64 vram = arg(argc, argv, "vram", 0), vram_size = arg(argc, argv, "vsize", 0); + u64 meta_size = arg(argc, argv, "meta", 0), ram = cur_boot_args.phys_base; + const char *uuid = image(true); + if (unknown) { + os_firmware = (struct fw_version_info){V_UNKNOWN, "unknown", {0}, 0, NULL}; + } else { + os_firmware.version = V27_2; + os_firmware.string = "27.2"; + os_firmware.num[0] = 27; + os_firmware.num[1] = 2; + os_firmware.num[2] = 0; + os_firmware.num_length = 3; + } + /* Use the 27.2 ext=true UUID row */ + uuid = NULL; + for (size_t i = 0; i < ARRAY_SIZE(t6050_images); i++) + if (t6050_images[i].os_fw == V27_2 && t6050_images[i].ext) + uuid = t6050_images[i].uuid; + if (!uuid) + DIE("no 27.2 ext row\n"); + + u64 dcp_coproc = 0x48ae00000UL, dart_dcp = 0x48a340000UL, dart_disp = 0x48a300000UL; + u64 ext_coproc[2] = {0x2b2e00000UL, 0x2b3e00000UL}; + u64 ext_ddcp[2] = {0x2b2400000UL, 0x2b3400000UL}; + u64 ext_ddisp[2] = {0x2b2300000UL, 0x2b3300000UL}; + u64 l1_dcp = 0x20100004000UL, l1_disp = 0x20100104000UL; + u8 agent[24] = {1, 0, 3}; + u32 fn[3] = {0x77, 0, 0x2a5}, clock_id = 256 + 7, freq[8] = {0}; + + prop(DCP6_NUB, "uuid", uuid, 37); + prop_u32(DCP6_NUB, "running", 1); + segments(DCP6_NUB, vram, vram_size); + reg("/arm-io/dcp", 0, dcp_coproc, 0x4000); + reg("/arm-io/dcp", 1, 0x48a000000UL, 0x4000); + for (int i = 0; i < 4; i++) + reg("/arm-io/disp0", i, 0x489200000UL + i * 0x10000UL, 0x4000); + reg("/arm-io/disp0", 4, PMC, meta_size); + prop("/arm-io/disp0", "function-bw_req_interrupt0", fn, sizeof(fn)); + prop_u32("/arm-io/disp0", "clock-ids", clock_id); + memcpy(agent + 4, &fn[2], 4); + prop("/arm-io/pmgr", "bwr-agent", agent, sizeof(agent)); + prop_u32("/arm-io/pmgr", "AAPL,phandle", fn[0]); + freq[7] = 594000000; + prop("/arm-io", "clock-frequencies", freq, sizeof(freq)); + prop_u32("/backlight", "user-accessible-max-nits", 600 << 16); + reg("/vram", 0, vram, vram_size); + word(dcp_coproc + ASC_CPU_CONTROL, ASC_CPU_CONTROL_RUN); + + const char *idarts[2] = {"/arm-io/dart-dcp", "/arm-io/dart-disp0"}; + for (int i = 0; i < 2; i++) { + u64 base = i ? dart_disp : dart_dcp, l1 = i ? l1_disp : l1_dcp; + reg(idarts[i], 0, base, 0x4000); + prop_u64s(idarts[i], "vm-base", VM_BASE, 0, 1); + prop_u64s(idarts[i], "vm-size", VM_SIZE, 0, 1); + u32 sid = i ? 0 : 23; + prop_u64s(idarts[i], i ? "l1-tt-0" : "l1-tt-23", l1, 1, 2); + prop_u64s(idarts[i], i ? "pt-region-0" : "pt-region-23", l1 - 0x4000, l1 + 0x40000, 2); + word(base + DART_T8110_PROTECT, DART_T8110_PROTECT_TTBR_TCR); + word(base + DART_T8110_TCR(sid), T6050_TCR_WANT); + word(base + DART_T8110_TTBR(sid), + DART_T8110_TTBR_VALID | + FIELD_PREP(DART_T8110_TTBR_ADDR, l1 >> DART_T8110_TTBR_SHIFT)); + if (i) { + prop_u64s(idarts[i], "l1-tt-16", l1 + 0x4000, 1, 2); + prop_u64s(idarts[i], "pt-region-16", l1 - 0x4000, l1 + 0x40000, 2); + word(base + DART_T8110_TCR(16), T6050_TCR_WANT); + word(base + DART_T8110_TTBR(16), + DART_T8110_TTBR_VALID | + FIELD_PREP(DART_T8110_TTBR_ADDR, (l1 + 0x4000) >> DART_T8110_TTBR_SHIFT)); + } + } + + for (int e = 0; e < 2; e++) { + char nubp[48], dcpp[32], ddcpp[32], ddispp[32]; + snprintf(nubp, sizeof(nubp), "/arm-io/dcpext%d/iop-dcpext%d-nub", e, e); + snprintf(dcpp, sizeof(dcpp), "/arm-io/dcpext%d", e); + snprintf(ddcpp, sizeof(ddcpp), "/arm-io/dart-dcpext%d", e); + snprintf(ddispp, sizeof(ddispp), "/arm-io/dart-dispext%d", e); + const char *euuid = (e == 1 && fail == 2) ? "64863924-B56B-3E32-87FE-038677F52709" : uuid; + prop(nubp, "uuid", euuid, 37); + prop_u32(nubp, "running", (e == 1 && fail == 1) ? 0 : 1); + struct adt_segment_ranges eseg = { + .phys = (e == 1 && fail == 3) ? ram + 0x100000 : 0x20000000000UL + (u64)e * 0x1000000UL, + .iova = 0x10000000, + .remap = 0x10000000, + .size = 0x4000, + }; + static const char enames[] = "__TEXT"; + prop(nubp, "segment-ranges", &eseg, sizeof(eseg)); + prop(nubp, "segment-names", enames, sizeof(enames)); + reg(dcpp, 0, ext_coproc[e], 0x4000); + reg(ddcpp, 0, ext_ddcp[e], 0x4000); + reg(ddispp, 0, ext_ddisp[e], 0x4000); + u64 l1 = 0x20200000000UL + (u64)e * 0x1000000UL; + prop_u64s(ddcpp, "l1-tt-23", l1, 1, 2); + prop_u64s(ddcpp, "pt-region-23", l1 - 0x4000, l1 + 0x40000, 2); + u64 pt16_lo = (e == 1 && fail == 4) ? ram + 0x200000 : l1 + 0x100000; + prop_u64s(ddispp, "l1-tt-16", pt16_lo, 1, 2); + prop_u64s(ddispp, "pt-region-16", pt16_lo, pt16_lo + 0x40000, 2); + } + + enum { PH_CLK = 10, PH_EXT0_DISP, PH_EXT1_DISP }; + OK(fdt_create_empty_tree(dt, sizeof(dt))); + OK(fdt_setprop_string(dt, 0, "compatible", "apple,t6050")); + OK(fdt_setprop_u32(dt, 0, "#address-cells", 2)); + OK(fdt_setprop_u32(dt, 0, "#size-cells", 2)); + OK(fdt_add_subnode(dt, 0, "chosen")); + OK(fdt_add_subnode(dt, 0, "reserved-memory")); + int clk = fdt_add_subnode(dt, 0, "clock-dcp-fw"); + OK(clk); + OK(fdt_setprop_string(dt, clk, "compatible", "fixed-clock")); + OK(fdt_setprop_u32(dt, clk, "#clock-cells", 0)); + OK(fdt_setprop_u32(dt, clk, "phandle", PH_CLK)); + + int soc = fdt_add_subnode(dt, 0, "soc"); + OK(soc); + OK(fdt_setprop_u32(dt, soc, "#address-cells", 2)); + OK(fdt_setprop_u32(dt, soc, "#size-cells", 2)); + OK(fdt_setprop_empty(dt, soc, "ranges")); + + int ddcp = fdt_dev(dt, soc, "iommu@48a340000", dart_dcp, PH_DART_DCP); + OK(fdt_setprop_string(dt, ddcp, "compatible", "apple,t8110-dart")); + OK(fdt_setprop_u32(dt, ddcp, "#iommu-cells", 1)); + fdt_u32s(dt, ddcp, "apple,dma-range", 4, VM_BASE >> 32, (u32)VM_BASE, VM_SIZE >> 32, + (u32)VM_SIZE); + int ddisp = fdt_dev(dt, soc, "iommu@48a300000", dart_disp, PH_DART_DISP); + OK(fdt_setprop_string(dt, ddisp, "compatible", "apple,t8110-dart")); + OK(fdt_setprop_u32(dt, ddisp, "#iommu-cells", 1)); + fdt_u32s(dt, ddisp, "apple,dma-range", 4, VM_BASE >> 32, (u32)VM_BASE, VM_SIZE >> 32, + (u32)VM_SIZE); + + int mbox = fdt_dev(dt, soc, "mbox@48ae08000", dcp_coproc + T6050_MBOX, PH_MBOX); + OK(fdt_setprop_string(dt, mbox, "compatible", "apple,asc-mailbox-v4")); + int pmc = fdt_dev(dt, soc, "pmc-metadata@510f00000", PMC, PH_PMC); + fdt_reg(dt, pmc, PMC, meta_size); + OK(fdt_setprop_u32(dt, pmc, "#apple,bw-scratch-cells", 3)); + + int dcp = fdt_dev(dt, soc, "dcp@48ae00000", dcp_coproc, 0); + OK(fdt_setprop_string(dt, dcp, "compatible", "apple,t6050-dcp")); + { + fdt64_t r[14] = { + cpu_to_fdt64(dcp_coproc), cpu_to_fdt64(0x4000), + cpu_to_fdt64(0x48a000000UL), cpu_to_fdt64(0x4000), + cpu_to_fdt64(0x489200000UL), cpu_to_fdt64(0x4000), + cpu_to_fdt64(0x489210000UL), cpu_to_fdt64(0x4000), + cpu_to_fdt64(0x489220000UL), cpu_to_fdt64(0x4000), + cpu_to_fdt64(0x489230000UL), cpu_to_fdt64(0x4000), + cpu_to_fdt64(PMC), cpu_to_fdt64(meta_size), + }; + OK(fdt_setprop(dt, dcp, "reg", r, sizeof(r))); + static const char rnames[] = "coproc\0iop-vbar\0disp-0\0disp-1\0disp-2\0disp-3\0disp-4"; + OK(fdt_setprop(dt, dcp, "reg-names", rnames, sizeof(rnames))); + } + fdt_u32s(dt, dcp, "clocks", 1, PH_CLK, 0, 0, 0); + fdt_u32s(dt, dcp, "apple,clock-ids", 1, clock_id, 0, 0, 0); + fdt_u32s(dt, dcp, "apple,bw-scratch", 4, PH_PMC, 0, T6050_NDISP, 0x20000 + 3 * 0x4000); + fdt_u32s(dt, dcp, "mboxes", 1, PH_MBOX, 0, 0, 0); + fdt_u32s(dt, dcp, "iommus", 2, PH_DART_DCP, 23, 0, 0); + OK(fdt_add_subnode(dt, dcp, "panel")); + int piodma = fdt_add_subnode(dt, dcp, "piodma"); + OK(piodma); + fdt_u32s(dt, piodma, "iommus", 2, PH_DART_DISP, 16, 0, 0); + + const char *ext1_path = "/soc/dcpext1"; + for (int e = 0; e < 2; e++) { + char dn[32], en[32]; + u32 dph = e ? PH_EXT1_DISP : PH_EXT0_DISP; + snprintf(dn, sizeof(dn), "iommu@%lx", (unsigned long)ext_ddisp[e]); + int ed = fdt_add_subnode(dt, soc, dn); + OK(ed); + fdt_reg(dt, ed, ext_ddisp[e], 0x4000); + OK(fdt_setprop_string(dt, ed, "compatible", "apple,t8110-dart")); + OK(fdt_setprop_u32(dt, ed, "#iommu-cells", 1)); + OK(fdt_setprop_u32(dt, ed, "phandle", dph)); + + int parent = soc; + if (e == 1 && nested) { + int b1 = fdt_add_subnode(dt, soc, "bus-level-one-display-fabric"); + OK(b1); + OK(fdt_setprop_u32(dt, b1, "#address-cells", 2)); + OK(fdt_setprop_u32(dt, b1, "#size-cells", 2)); + fdt32_t idr[6] = {cpu_to_fdt32(2), 0, cpu_to_fdt32(2), 0, cpu_to_fdt32(4), 0}; + OK(fdt_setprop(dt, b1, "ranges", idr, sizeof(idr))); + int b2 = fdt_add_subnode(dt, b1, "bus-level-two-external-engines"); + OK(b2); + OK(fdt_setprop_u32(dt, b2, "#address-cells", 2)); + OK(fdt_setprop_u32(dt, b2, "#size-cells", 2)); + OK(fdt_setprop_empty(dt, b2, "ranges")); + parent = b2; + snprintf(en, sizeof(en), "dcpext@%lx", (unsigned long)ext_coproc[e]); + ext1_path = "/soc/bus-level-one-display-fabric/bus-level-two-external-engines/" + "dcpext@2b3e00000"; + } else { + snprintf(en, sizeof(en), "dcpext%d", e); + } + int ext = fdt_add_subnode(dt, parent, en); + OK(ext); + fdt_reg(dt, ext, ext_coproc[e], 0x4000); + OK(fdt_setprop_string(dt, ext, "compatible", "apple,t6050-dcpext")); + if (e == 1 && extra_status) { + static const char extra[] = "okay\0disabled"; + OK(fdt_setprop(dt, ext, "status", extra, sizeof(extra))); + } else if (!(e == 1 && absent_status)) { + OK(fdt_setprop_string(dt, ext, "status", "okay")); + } + if (!(e == 0 && nocompat)) { + fdt_u32s(dt, ext, "apple,firmware-compat", 3, 27, 0, 0, 0); + fdt_u32s(dt, ext, "apple,firmware-version", 3, 27, 0, 0, 0); + } + int epio = fdt_add_subnode(dt, ext, "piodma"); + OK(epio); + fdt_u32s(dt, epio, "iommus", 2, dph, 16, 0, 0); + } + + int disp = fdt_add_subnode(dt, soc, "display-subsystem"); + OK(disp); + OK(fdt_setprop_string(dt, disp, "compatible", "apple,display-subsystem")); + OK(fdt_setprop_string(dt, disp, "status", disp_enabled ? "okay" : "disabled")); + if (oversized_iommus) { + fdt32_t io[36]; + io[0] = cpu_to_fdt32(PH_DART_DISP); + io[1] = 0; + io[2] = cpu_to_fdt32(PH_EXT0_DISP); + io[3] = 0; + io[4] = cpu_to_fdt32(PH_EXT1_DISP); + io[5] = 0; + for (int i = 3; i < 18; i++) { + io[2 * i] = cpu_to_fdt32(100 + i); + io[2 * i + 1] = 0; + } + OK(fdt_setprop(dt, disp, "iommus", io, sizeof(io))); + } else { + fdt32_t io[6] = { + cpu_to_fdt32(PH_DART_DISP), 0, + cpu_to_fdt32(PH_EXT0_DISP), 0, + cpu_to_fdt32(PH_EXT1_DISP), 0, + }; + OK(fdt_setprop(dt, disp, "iommus", io, sizeof(io))); + } + + if (pack) + OK(fdt_pack(dt)); + + fail_malloc = fail_scratch ? 1 : 0; + int rc = dt_set_display_handoff(dt); + fail_malloc = 0; + if (rc) + DIE("dt_set_display_handoff() failed\n"); + if (fdt_check_header(dt)) + DIE("the device tree is not valid\n"); + + int chosen = fdt_path_offset(dt, "/chosen"); + const char *said = fdt_getprop(dt, chosen, T6050_STATUS, NULL); + int e0 = fdt_path_offset(dt, "/soc/dcpext0"), e1 = fdt_path_offset(dt, ext1_path); + int len = 0; + const fdt32_t *c0 = e0 < 0 ? NULL : fdt_getprop(dt, e0, "apple,firmware-compat", &len); + const fdt32_t *c1 = e1 < 0 ? NULL : fdt_getprop(dt, e1, "apple,firmware-compat", &len); + const fdt32_t *v1 = e1 < 0 ? NULL : fdt_getprop(dt, e1, "apple,firmware-version", &len); + char c0s[32] = "none", c1s[32] = "none", v1s[32] = "none"; + if (c0) + snprintf(c0s, sizeof(c0s), "%u.%u.%u", fdt32_ld(&c0[0]), fdt32_ld(&c0[1]), fdt32_ld(&c0[2])); + if (c1) + snprintf(c1s, sizeof(c1s), "%u.%u.%u", fdt32_ld(&c1[0]), fdt32_ld(&c1[1]), fdt32_ld(&c1[2])); + if (v1) + snprintf(v1s, sizeof(v1s), "%u.%u.%u", fdt32_ld(&v1[0]), fdt32_ld(&v1[1]), fdt32_ld(&v1[2])); + printf("result chosen=%s dcp=%s subsystem=%s ext0=%s ext1=%s ext0_fw=%s ext1_fw=%s " + "ext1_ver=%s\n", + said ? said : "none", status(dt, "/soc/dcp@48ae00000"), + status(dt, "/soc/display-subsystem"), status(dt, "/soc/dcpext0"), + e1 < 0 ? "deleted" : status(dt, ext1_path), c0s, c1s, v1s); + disp = fdt_path_offset(dt, "/soc/display-subsystem"); + const fdt32_t *io = fdt_getprop(dt, disp, "iommus", &len); + printf("iommus"); + for (int i = 0; io && i < len / 4; i++) + printf(" %u", fdt32_ld(&io[i])); + printf("\n"); + return 0; +} + int main(int argc, char **argv) { if (argc < 2) @@ -536,6 +843,8 @@ return headless(argc, argv); if (!strcmp(argv[1], "panel")) return panel(argc, argv); + if (!strcmp(argv[1], "prewired")) + return prewired(argc, argv); return 2; } ''' @@ -589,6 +898,8 @@ def __init__(self, text): self.reserved.append((name, int(base, 16), int(size, 16))) elif kind == "reservations": assert int(rest) == len(self.reserved) + elif kind == "iommus": + self.iommus = [int(x) for x in rest.split()] else: raise AssertionError(line) @@ -708,6 +1019,134 @@ def test_panel_framebuffer_must_lie_in_vram(host, fb, size, taken): assert r.state["rc"] == "-1" and len(r.log) == 1 +@pytest.mark.parametrize("fail", [0, 1, 2, 3, 4]) +def test_prewired_panel_keeps_rejected_external_engines_out_of_handoff(host, fail): + r = run(host, "prewired", fail=fail, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "published", r.log + assert r.state["dcp"] == "okay" and r.state["subsystem"] == "okay" + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + if fail == 0: + assert r.state["ext1"] == "okay" and r.state["ext1_fw"] == "27.2.0" + assert r.state["ext1_ver"] == "27.2.0" + assert r.iommus == [2, 0, 11, 0, 12, 0] + else: + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + +def test_prewired_panel_rejects_external_engines_on_unknown_stub_and_packed_dt(host): + # Unknown stub: dt_set_display_handoff() refuses the internal panel, keeps healthy ext0 at + # the tree's version, and still disables stopped ext1 and removes its display DART. + r = run(host, "prewired", unknown=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "refused", r.log + assert r.state["dcp"] == "disabled" and r.state["subsystem"] == "disabled" + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.0.0" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + # Packed DT where ext0's firmware-compat write fails with NOSPACE: rejection is not rolled + # back, so stopped ext1 stays disabled and out of display-subsystem iommus. + r = run(host, "prewired", pack=1, nocompat=1, fail=1, meta=META_SIZE, vram=VRAM, + vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "none" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + # Packed DT where ext1 has no status property so status = "disabled" cannot fit: ext1 is + # deleted instead and its display DART is removed from display-subsystem iommus. + r = run(host, "prewired", pack=1, nocompat=1, absent_status=1, fail=1, meta=META_SIZE, + vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "okay" + assert r.state["ext1"] == "deleted" + assert r.iommus == [2, 0, 11, 0] + + +def test_prewired_panel_rejects_nested_bus_external_engine(host): + # Stopped ext1 behind two identity-mapped buses with path > 64 bytes: still disabled and + # stripped from display-subsystem iommus while healthy ext0 and the panel stay up. + r = run(host, "prewired", nested=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "published", r.log + assert r.state["dcp"] == "okay" and r.state["subsystem"] == "okay" + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + # Healthy ext1 behind the same two identity-mapped buses stays okay and gets 27.2.0. + r = run(host, "prewired", nested=1, fail=0, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "published", r.log + assert r.state["ext1"] == "okay" and r.state["ext1_fw"] == "27.2.0" + assert r.state["ext1_ver"] == "27.2.0" + assert r.iommus == [2, 0, 11, 0, 12, 0] + + # Packed DT with absent status on the nested-bus stopped engine deletes it and strips scanout. + r = run(host, "prewired", pack=1, nocompat=1, absent_status=1, nested=1, fail=1, meta=META_SIZE, + vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "okay" + assert r.state["ext1"] == "deleted" + assert r.iommus == [2, 0, 11, 0] + + +def test_prewired_panel_rejects_external_engines_across_failure_and_status_edges(host): + # Scratch allocation failure during rejection: falls back to in-place edits so stopped ext1 + # is still disabled and stripped from scanout (and deleted on a packed DT with absent status). + r = run(host, "prewired", fail_scratch=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + r = run(host, "prewired", pack=1, absent_status=1, fail_scratch=1, fail=1, meta=META_SIZE, + vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext1"] == "deleted" + assert r.iommus == [2, 0, 11, 0] + + # Oversized display-subsystem iommus (> 128 bytes): compacts iommus in place without + # allocating, strips ext1's DART while keeping other phandles intact, disables rejected ext1, + # and updates healthy ext0 to 27.2.0. + expected_oversized = [2, 0, 11, 0] + [x for i in range(3, 18) for x in (100 + i, 0)] + r = run(host, "prewired", oversized_iommus=1, fail=1, meta=META_SIZE, vram=VRAM, + vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == expected_oversized + + # Extra-bytes status string ("okay\0disabled"): Linux probes the first string, so rejection + # still disables ext1 and strips its display DART while keeping the panel and ext0 active. + r = run(host, "prewired", extra_status=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "published", r.log + assert r.state["dcp"] == "okay" and r.state["subsystem"] == "okay" + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + +@pytest.mark.parametrize("fail_scratch", [0, 1]) +def test_prewired_panel_drops_rejected_scanout_from_enabled_oversized_iommus(host, fail_scratch): + # Display subsystem already enabled ("okay") with >128-byte iommus: rejected ext1 is disabled + # AND its DART phandle (12) is removed from iommus in place while all other phandles stay + # intact, both with scratch allocation and with in-place fallback. + expected_iommus = [2, 0, 11, 0] + [x for i in range(3, 18) for x in (100 + i, 0)] + r = run(host, "prewired", disp_enabled=1, oversized_iommus=1, fail_scratch=fail_scratch, + fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["subsystem"] == "okay" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == expected_iommus + if not fail_scratch: + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + + @pytest.mark.parametrize("mode", ["headless", "panel"]) def test_vram_at_the_top_of_the_address_space(host, mode): # /vram ends where the address space does: `vram + vram_size` is 0 there, and still right diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index b45cde2f1..a47d5869f 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -245,13 +245,94 @@ def test_t6050_register_windows_map_one_to_one(tmp_path): def test_t6050_external_scanout_darts_resolved(tmp_path): source = (REPO / "src/kboot_display_handoff.c").read_text() + header = (REPO / "src/adt.h").read_text() + layout_start = header.index("struct adt_segment_ranges {") + layout = header[layout_start:header.index("} PACKED;", layout_start) + len("} PACKED;")] code = PRELUDE + r''' #define T6050_TEXT #define T6050_INIT +#define T6050_ENGINES 8 +#define PACKED __attribute__((packed)) +#define SEG_FLAGS_OSLOG 0xa +#define SZ_16K 0x4000UL +#define DART_PAGE 0x4000UL +#define ALIGN_DOWN(x, a) ((x) & ~((u64)(a) - 1)) +#define ALIGN_UP(x, a) ALIGN_DOWN((x) + (a) - 1, a) +#define dlog(fmt, ...) printf(fmt, ##__VA_ARGS__) +''' + layout + r''' +static struct { u64 phys_base, mem_size; } cur_boot_args = {0x10000000000UL, 0x200000000UL}; +static struct { const char *uuid; } t6050; +static void *adt; #define DART_DCP 0x48a340000UL #define DART_DISP0 0x48a300000UL #define EXT0 0x2b2e00000UL #define DART_DISPEXT0 0x2b2300000UL +#define UUID_A "C51FB0C8-C45D-39B0-85BC-520E600FC950" +#define UUID_B "64863924-B56B-3E32-87FE-038677F52709" +static struct adt_segment_ranges ext0_segs[2]; +static u32 ext0_nsegs; +static u32 ext0_running; +static const char *ext0_uuid; +static u64 pt23[2], l1_23[2], pt16[2], l1_16[2]; +static void reset_adt(void) +{ + t6050.uuid = UUID_A; + ext0_uuid = UUID_A; + ext0_running = 1; + ext0_nsegs = 2; + ext0_segs[0] = (struct adt_segment_ranges){0x10200000000UL, 0, 0x10200000000UL, 0x4000, 0}; + ext0_segs[1] = (struct adt_segment_ranges){0x10004500000UL, 0, 0x10004500000UL, 0x4000, SEG_FLAGS_OSLOG}; + pt23[0] = 0x10201000000UL; pt23[1] = 0x10201040000UL; + l1_23[0] = 0x10201000000UL; l1_23[1] = 1; + pt16[0] = 0x10201100000UL; pt16[1] = 0x10201140000UL; + l1_16[0] = 0x10201100000UL; l1_16[1] = 1; +} +enum { ADT_NUB = 1, ADT_DDCP, ADT_DDISP }; +static int adt_path_offset(const void *a, const char *path) +{ + (void)a; + if (!strcmp(path, "/arm-io/dcpext0/iop-dcpext0-nub")) return ADT_NUB; + if (!strcmp(path, "/arm-io/dart-dcpext0")) return ADT_DDCP; + if (!strcmp(path, "/arm-io/dart-dispext0")) return ADT_DDISP; + return -1; +} +static const void *adt_getprop(const void *a, int node, const char *name, u32 *len) +{ + (void)a; + if (node == ADT_NUB) { + if (!strcmp(name, "uuid") && ext0_uuid) { + if (len) *len = strlen(ext0_uuid) + 1; + return ext0_uuid; + } + if (!strcmp(name, "running")) { + if (len) *len = sizeof(ext0_running); + return &ext0_running; + } + if (!strcmp(name, "segment-ranges") && ext0_nsegs) { + if (len) *len = ext0_nsegs * sizeof(ext0_segs[0]); + return ext0_segs; + } + } else if (node == ADT_DDCP) { + if (!strcmp(name, "l1-tt-23") && l1_23[0]) { + if (len) *len = sizeof(l1_23); + return l1_23; + } + if (!strcmp(name, "pt-region-23")) { + if (len) *len = sizeof(pt23); + return pt23; + } + } else if (node == ADT_DDISP) { + if (!strcmp(name, "l1-tt-16") && l1_16[0]) { + if (len) *len = sizeof(l1_16); + return l1_16; + } + if (!strcmp(name, "pt-region-16")) { + if (len) *len = sizeof(pt16); + return pt16; + } + } + return NULL; +} /* The ADT: the internal DARTs, dcpext0 and its display DART; nothing else */ static int t6050_adt_reg(const char *path, int idx, u64 *addr, u64 *size) { @@ -263,9 +344,16 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): else return -1; return 0; } -''' + definition(source, "static T6050_TEXT int t6050_node_at(") + \ +''' + definition(source, "static bool dcp6_overlaps(") + \ + definition(source, "static int dcp6_adt_u32(") + \ + definition(source, "static int dcp6_adt_u64_pair(") + \ + definition(source, "static bool dcp6_is_uuid(") + \ + definition(source, "static T6050_TEXT int t6050_node_at(") + \ definition(source, "static T6050_TEXT bool t6050_available(") + \ definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ + definition(source, "static T6050_TEXT int t6050_check_placement(") + \ + definition(source, "static T6050_TEXT int t6050_check_ext_adt(") + \ + definition(source, "static T6050_TEXT bool t6050_ext_dart_engine(") + \ definition(source, "static T6050_TEXT bool t6050_ext_display_dart(") + r''' enum { PH_EXT = 1, PH_DCP, PH_DISP0, PH_OTHER }; static void dart(const char *name, u64 base, u32 ph, u32 cells, const char *status) @@ -282,6 +370,7 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): static void world(u32 ph, u32 sid, const char *ext_status, u32 cells, const char *dstatus) { fresh(); + reset_adt(); assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); dart("iommu@2b2300000", DART_DISPEXT0, PH_EXT, cells, dstatus); @@ -324,19 +413,46 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): assert(!t6050_ext_display_dart(tree, PH_DISP0)); cases++; world(PH_OTHER, 16, "okay", 1, NULL); assert(!t6050_ext_display_dart(tree, PH_OTHER)); cases++; - /* status as whole strings: "okay"/"ok" with their terminator, nothing longer or shorter */ + /* Placement and ADT image checks: segment in OS RAM, pt-region in RAM, running/uuid. */ + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_segs[0].phys = cur_boot_args.phys_base + 0x100000; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + pt23[0] = cur_boot_args.phys_base + 0x200000; + pt23[1] = cur_boot_args.phys_base + 0x240000; + l1_23[0] = pt23[0]; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + pt16[0] = cur_boot_args.phys_base + 0x300000; + pt16[1] = cur_boot_args.phys_base + 0x340000; + l1_16[0] = pt16[0]; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + l1_16[0] = 0; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_running = 0; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_uuid = UUID_B; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + /* status as Linux reads it: absent, or first string "okay"/"ok", regardless of trailing bytes */ fresh(); int n = node_at("/", "n"); assert(t6050_available(tree, n)); cases++; assert(!fdt_setprop_string(tree, n, "status", "ok") && t6050_available(tree, n)); cases++; assert(!fdt_setprop(tree, n, "status", "okay", 4) && !t6050_available(tree, n)); cases++; - assert(!fdt_setprop(tree, n, "status", "okay\0x", 6) && !t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "", 0) && !t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "okay\0x", 6) && t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "okay\0disabled", 14) && t6050_available(tree, n)); cases++; + assert(!fdt_setprop(tree, n, "status", "ok\0extra", 9) && t6050_available(tree, n)); cases++; assert(!fdt_setprop_string(tree, n, "status", "fail") && !t6050_available(tree, n)); cases++; + assert(!t6050_available(tree, -1)); cases++; printf("PASS external scanout darts %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-darts", code, "PASS external scanout darts 15 cases") + run(tmp_path, "ext-darts", code, "PASS external scanout darts 25 cases") def test_t6050_rx_clock_groups_pass_whole(tmp_path): @@ -488,3 +604,521 @@ def test_t6050_prewired_display_subsystem_is_the_only_one(tmp_path): } ''' run(tmp_path, "disp-node", code, "PASS display subsystem 4 cases") + + +def test_t6050_external_engines_get_the_stub_version(tmp_path): + source = (REPO / "src/kboot_display_handoff.c").read_text() + firmware = (REPO / "src/firmware.c").read_text() + header = (REPO / "src/adt.h").read_text() + layout_start = header.index("struct adt_segment_ranges {") + layout = header[layout_start:header.index("} PACKED;", layout_start) + len("} PACKED;")] + code = PRELUDE + r''' +#include +#define T6050_TEXT +#define T6050_INIT +#define T6050_PATH 64 +#define T6050_ENGINES 8 +#define PACKED __attribute__((packed)) +#define SEG_FLAGS_OSLOG 0xa +#define SZ_16K 0x4000UL +#define DART_PAGE 0x4000UL +#define ALIGN_DOWN(x, a) ((x) & ~((u64)(a) - 1)) +#define ALIGN_UP(x, a) ALIGN_DOWN((x) + (a) - 1, a) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define OS_VER_COMP 4 +enum fw_version { V_UNKNOWN = 0, V27_0, V27_2, V27_3 }; +struct fw_version_info { + enum fw_version version; + const char *string; + u32 num[OS_VER_COMP]; + size_t num_length; + const char *iboot; +}; +''' + layout + r''' +static struct fw_version_info os_firmware; +static struct { u64 phys_base, mem_size; } cur_boot_args = {0x10000000000UL, 0x200000000UL}; +static struct { const char *uuid; } t6050; +static struct { const char *name; } soc = {"t6050"}; +static struct { const typeof(soc) *soc; } dcp6 = {&soc}; +static void *adt; +#define dlog(fmt, ...) printf("DCP: %s: " fmt, dcp6.soc->name, ##__VA_ARGS__) +#define UUID_A "C51FB0C8-C45D-39B0-85BC-520E600FC950" +#define UUID_B "64863924-B56B-3E32-87FE-038677F52709" +#define DART_DISP0 0x48a300000UL +struct fake_ext { + u64 coproc; + u64 dart_disp; + u32 running; + const char *uuid; + struct adt_segment_ranges seg[2]; + u32 nseg; + u64 pt23[2], l1_23[2], pt16[2], l1_16[2]; +}; +static struct fake_ext exts[3]; +static void reset_adt(void) +{ + t6050.uuid = UUID_A; + for (int i = 0; i < 3; i++) { + exts[i] = (struct fake_ext){ + .coproc = 0x2b2e00000UL + (u64)i * 0x1000000UL, + .dart_disp = 0x2b2300000UL + (u64)i * 0x1000000UL, + .running = 1, + .uuid = UUID_A, + .seg = { + {0x10200000000UL + (u64)i * 0x1000000UL, 0, + 0x10200000000UL + (u64)i * 0x1000000UL, 0x4000, 0}, + {0x10004500000UL + (u64)i * 0x4000UL, 0, + 0x10004500000UL + (u64)i * 0x4000UL, 0x4000, SEG_FLAGS_OSLOG}, + }, + .nseg = 2, + .pt23 = {0x10201000000UL, 0x10201040000UL}, + .l1_23 = {0x10201000000UL, 1}, + .pt16 = {0x10201100000UL, 0x10201140000UL}, + .l1_16 = {0x10201100000UL, 1}, + }; + } +} +static int kboot_fdt_path_exact(void *t, const char *path) +{ + return fdt_path_offset(t, path); +} +static int t6050_adt_reg(const char *path, int idx, u64 *addr, u64 *size) +{ + (void)idx; (void)size; + if (!strcmp(path, "/arm-io/dart-disp0")) { + *addr = DART_DISP0; + return 0; + } + for (int i = 0; i < 3; i++) { + char a[32]; + snprintf(a, sizeof(a), "/arm-io/dcpext%d", i); + if (!strcmp(path, a)) { + *addr = exts[i].coproc; + return 0; + } + snprintf(a, sizeof(a), "/arm-io/dart-dispext%d", i); + if (!strcmp(path, a)) { + *addr = exts[i].dart_disp; + return 0; + } + } + return -1; +} +static int adt_path_offset(const void *a, const char *path) +{ + (void)a; + for (int i = 0; i < 3; i++) { + char b[64]; + snprintf(b, sizeof(b), "/arm-io/dcpext%d/iop-dcpext%d-nub", i, i); + if (!strcmp(path, b)) return i * 3 + 1; + snprintf(b, sizeof(b), "/arm-io/dart-dcpext%d", i); + if (!strcmp(path, b)) return i * 3 + 2; + snprintf(b, sizeof(b), "/arm-io/dart-dispext%d", i); + if (!strcmp(path, b)) return i * 3 + 3; + } + return -1; +} +static const void *adt_getprop(const void *a, int node, const char *name, u32 *len) +{ + (void)a; + if (node <= 0) + return NULL; + int i = (node - 1) / 3, kind = (node - 1) % 3; + if (kind == 0) { + if (!strcmp(name, "uuid") && exts[i].uuid) { + if (len) *len = strlen(exts[i].uuid) + 1; + return exts[i].uuid; + } + if (!strcmp(name, "running")) { + if (len) *len = sizeof(exts[i].running); + return &exts[i].running; + } + if (!strcmp(name, "segment-ranges") && exts[i].nseg) { + if (len) *len = exts[i].nseg * sizeof(exts[i].seg[0]); + return exts[i].seg; + } + } else if (kind == 1) { + if (!strcmp(name, "l1-tt-23") && exts[i].l1_23[0]) { + if (len) *len = sizeof(exts[i].l1_23); + return exts[i].l1_23; + } + if (!strcmp(name, "pt-region-23")) { + if (len) *len = sizeof(exts[i].pt23); + return exts[i].pt23; + } + } else { + if (!strcmp(name, "l1-tt-16") && exts[i].l1_16[0]) { + if (len) *len = sizeof(exts[i].l1_16); + return exts[i].l1_16; + } + if (!strcmp(name, "pt-region-16")) { + if (len) *len = sizeof(exts[i].pt16); + return exts[i].pt16; + } + } + return NULL; +} +''' + definition(firmware, "int firmware_set_fdt(") + \ + definition(source, "static bool dcp6_overlaps(") + \ + definition(source, "static int dcp6_adt_u32(") + \ + definition(source, "static int dcp6_adt_u64_pair(") + \ + definition(source, "static bool dcp6_is_uuid(") + r''' +static bool fail_scratch; +static void *test_malloc(size_t n) +{ + return fail_scratch ? NULL : malloc(n); +} +#define malloc test_malloc +''' + definition(source, "static void *dcp6_scratch(") + r''' +#undef malloc +''' + definition(source, "static int dcp6_commit(") + \ + definition(source, "static T6050_TEXT bool t6050_available(") + \ + definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ + definition(source, "static T6050_TEXT int t6050_check_placement(") + \ + definition(source, "static T6050_TEXT int t6050_check_ext_adt(") + \ + definition(source, "static T6050_TEXT int t6050_ext_index(") + \ + definition(source, "static T6050_TEXT bool t6050_dart_at(") + \ + definition(source, "static T6050_TEXT int t6050_drop_ext_scanout(") + \ + definition(source, "static T6050_TEXT int t6050_ext_walk(") + \ + definition(source, "static T6050_TEXT void t6050_ext_reject(") + \ + definition(source, "static T6050_TEXT void t6050_ext_version(") + r''' +static const u32 tree_version[3] = {27, 0, 0}; +enum { PH_DISP0 = 10, PH_DART0 = 11, PH_DART1 = 12, PH_DART2 = 13 }; +static void dart_node(const char *name, u64 base, u32 ph) +{ + int n = node_at("/", name); + assert(!fdt_setprop_string(tree, n, "compatible", "apple,t8110-dart")); + reg(n, base, 0x20000); + assert(!fdt_setprop_u32(tree, n, "#iommu-cells", 1)); + assert(!fdt_setprop_u32(tree, n, "phandle", ph)); +} +/* A dcpext node with reg, piodma, apple,firmware-compat, and apple,firmware-version when @version */ +static int engine_under(const char *parent, const char *name, u64 coproc, const char *status, + bool version, u32 dart_ph) +{ + int n = node_at(parent, name); + fdt32_t v[3]; + for (int i = 0; i < 3; i++) + v[i] = cpu_to_fdt32(tree_version[i]); + assert(!fdt_setprop_string(tree, n, "compatible", "apple,t6050-dcpext")); + reg(n, coproc, 0x4000); + assert(!fdt_setprop(tree, n, "apple,firmware-compat", v, sizeof(v))); + if (version) + assert(!fdt_setprop(tree, n, "apple,firmware-version", v, sizeof(v))); + if (status) + assert(!fdt_setprop_string(tree, n, "status", status)); + char ep[160]; + int elen = snprintf(ep, sizeof(ep), "%s%s%s", strcmp(parent, "/") ? parent : "", "/", name); + assert(elen > 0 && (size_t)elen < sizeof(ep)); + int pio = node_at(ep, "piodma"); + fdt32_t io[2] = {cpu_to_fdt32(dart_ph), cpu_to_fdt32(16)}; + assert(!fdt_setprop(tree, pio, "iommus", io, sizeof(io))); + return n; +} +/* Whether @prop of node @name is @want's cells */ +static bool is(const char *name, const char *prop, const struct fw_version_info *want) +{ + int len; + const fdt32_t *v = fdt_getprop(tree, fdt_path_offset(tree, name), prop, &len); + if (!v || len != (int)(want->num_length * 4)) + return false; + for (size_t i = 0; i < want->num_length; i++) + if (fdt32_ld(&v[i]) != want->num[i]) + return false; + return true; +} +static bool is_disabled(const char *path) +{ + int n = fdt_path_offset(tree, path); + const char *s = n < 0 ? NULL : fdt_getprop(tree, n, "status", NULL); + return s && !strcmp(s, "disabled"); +} +static bool has_scanout_dart(u32 ph) +{ + int disp = fdt_path_offset(tree, "/display"); + int len = 0; + const fdt32_t *io = disp < 0 ? NULL : fdt_getprop(tree, disp, "iommus", &len); + for (int i = 0; io && i + 2 <= len / 4; i += 2) + if (fdt32_ld(&io[i]) == ph) + return true; + return false; +} +static void world(void) +{ + fresh(); + reset_adt(); + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b2300000", exts[0].dart_disp, PH_DART0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + dart_node("iommu@2b4300000", exts[2].dart_disp, PH_DART2); + int disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + fdt32_t io[8] = {cpu_to_fdt32(PH_DISP0), 0, cpu_to_fdt32(PH_DART0), 0, + cpu_to_fdt32(PH_DART1), 0, cpu_to_fdt32(PH_DART2), 0}; + assert(!fdt_setprop(tree, disp, "iommus", io, sizeof(io))); + engine_under("/", "dcpext0", exts[0].coproc, NULL, true, PH_DART0); + engine_under("/", "dcpext1", exts[1].coproc, "okay", false, PH_DART1); + engine_under("/", "dcpext2", exts[2].coproc, "disabled", true, PH_DART2); +} +static void assert_rejected(const char *path, u32 dart_ph, const struct fw_version_info *old) +{ + assert(is_disabled(path)); + assert(is(path, "apple,firmware-compat", old)); + assert(has_scanout_dart(PH_DISP0)); + assert(has_scanout_dart(PH_DART0)); + assert(!has_scanout_dart(dart_ph)); +} +int main(void) +{ + static const struct fw_version_info old = {V27_0, "27.0", {27, 0, 0}, 3, NULL}; + static const struct fw_version_info v272 = {V27_2, "27.2", {27, 2, 0}, 3, NULL}; + static const struct fw_version_info v273 = {V27_3, "27.3.1", {27, 3, 1, 4}, 4, NULL}; + static unsigned char before[sizeof(tree)]; + int cases = 0; + /* A three-cell stub: the engines the tree enables take it, a disabled one keeps the tree's. */ + world(); + os_firmware = v272; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert(is("/dcpext0", "apple,firmware-version", &v272)); + assert(is("/dcpext1", "apple,firmware-compat", &v272)); + assert(!fdt_getprop(tree, fdt_path_offset(tree, "/dcpext1"), "apple,firmware-version", NULL)); + assert(is("/dcpext2", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DART0) && has_scanout_dart(PH_DART1)); cases++; + /* An enabled engine the tree gave no apple,firmware-compat gets one: the driver has no + * method table without it. apple,firmware-version is not added where it was absent. */ + world(); + assert(!fdt_delprop(tree, fdt_path_offset(tree, "/dcpext1"), "apple,firmware-compat")); + os_firmware = v272; + t6050_ext_version(tree); + assert(is("/dcpext1", "apple,firmware-compat", &v272)); + assert(!fdt_getprop(tree, fdt_path_offset(tree, "/dcpext1"), "apple,firmware-version", NULL)); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); cases++; + /* A four-cell stub fits too: the properties grow. */ + world(); + os_firmware = v273; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v273)); + assert(is("/dcpext0", "apple,firmware-version", &v273)); + assert(is("/dcpext1", "apple,firmware-compat", &v273)); + assert(is("/dcpext2", "apple,firmware-version", &old)); cases++; + /* No room in the tree for the four cells: nothing changes. */ + world(); + assert(!fdt_pack(tree)); + memcpy(before, tree, fdt_totalsize(tree)); + t6050_ext_version(tree); + assert(!memcmp(before, tree, fdt_totalsize(tree))); cases++; + /* An unknown firmware: nothing changes. */ + world(); + os_firmware = (struct fw_version_info){V_UNKNOWN, NULL, {0}, 0, NULL}; + memcpy(before, tree, sizeof(tree)); + t6050_ext_version(tree); + assert(!memcmp(before, tree, sizeof(tree))); cases++; + /* One engine fails placement (segment in OS RAM): disabled and dropped from scanout. */ + world(); + os_firmware = v272; + exts[1].seg[0].phys = cur_boot_args.phys_base + 0x100000; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* One engine fails pt-region placement: disabled and dropped from scanout. */ + world(); + exts[1].pt16[0] = cur_boot_args.phys_base + 0x200000; + exts[1].pt16[1] = cur_boot_args.phys_base + 0x240000; + exts[1].l1_16[0] = exts[1].pt16[0]; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* One engine fails running or uuid check: disabled and dropped from scanout. */ + world(); + exts[1].running = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + exts[1].uuid = UUID_B; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* An engine with no matching ADT dcpextN: disabled and dropped from scanout. */ + world(); + reg(fdt_path_offset(tree, "/dcpext1"), 0x2b9e00000UL, 0x4000); + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* An engine behind a translated bus (non-1:1 ranges) whose raw reg equals dcpext1 is + * rejected rather than matched to ADT dcpext1. */ + fresh(); + reset_adt(); + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b2300000", exts[0].dart_disp, PH_DART0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + int disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + fdt32_t io2[6] = {cpu_to_fdt32(PH_DISP0), 0, cpu_to_fdt32(PH_DART0), 0, + cpu_to_fdt32(PH_DART1), 0}; + assert(!fdt_setprop(tree, disp, "iommus", io2, sizeof(io2))); + engine_under("/", "dcpext0", exts[0].coproc, "okay", true, PH_DART0); + int bus = node_at("/", "bus"); + assert(!fdt_setprop_u32(tree, bus, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, bus, "#size-cells", 2)); + fdt32_t tr[6] = {cpu_to_fdt32((u32)(exts[1].coproc >> 32)), cpu_to_fdt32((u32)exts[1].coproc), + cpu_to_fdt32(5), 0, 0, cpu_to_fdt32(0x10000)}; + assert(!fdt_setprop(tree, bus, "ranges", tr, sizeof(tr))); + engine_under("/bus", "dcpext1", exts[1].coproc, "okay", false, PH_DART1); + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/bus/dcpext1", PH_DART1, &old); cases++; + /* Even when every available engine is rejected (zero version updates), the rejection still + * commits. */ + fresh(); + reset_adt(); + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + fdt32_t io3[4] = {cpu_to_fdt32(PH_DISP0), 0, cpu_to_fdt32(PH_DART1), 0}; + assert(!fdt_setprop(tree, disp, "iommus", io3, sizeof(io3))); + engine_under("/", "dcpext1", exts[1].coproc, "okay", false, PH_DART1); + exts[1].running = 0; + t6050_ext_version(tree); + assert(is_disabled("/dcpext1") && has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART1)); + cases++; + /* Unknown stub: healthy engines keep the tree's version, rejected ones are still disabled and + * stripped from scanout. */ + world(); + os_firmware = (struct fw_version_info){V_UNKNOWN, NULL, {0}, 0, NULL}; + exts[1].running = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &old) && !is_disabled("/dcpext0")); + assert(has_scanout_dart(PH_DISP0) && has_scanout_dart(PH_DART0)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* Packed DT where the four-cell version write fails: rejection is NOT rolled back. */ + world(); + os_firmware = v273; + exts[1].running = 0; + assert(!fdt_pack(tree)); + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &old) && !is_disabled("/dcpext0")); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* Packed DT where status = "disabled" cannot fit (absent status on dcpext0): the rejected + * node is deleted instead and its scanout DART is stripped. */ + world(); + os_firmware = v273; + exts[0].running = 0; + assert(!fdt_pack(tree)); + t6050_ext_version(tree); + assert(fdt_path_offset(tree, "/dcpext0") < 0 && !has_scanout_dart(PH_DART0)); + assert(has_scanout_dart(PH_DISP0) && has_scanout_dart(PH_DART1)); cases++; + /* Stopped engine behind two identity-mapped buses with path > 64 bytes: still disabled and + * stripped from scanout rather than aborting on T6050_PATH. */ + fresh(); + reset_adt(); + os_firmware = v272; + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b2300000", exts[0].dart_disp, PH_DART0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + assert(!fdt_setprop(tree, disp, "iommus", io2, sizeof(io2))); + engine_under("/", "dcpext0", exts[0].coproc, "okay", true, PH_DART0); + int b1 = node_at("/", "bus-level-one-display-fabric"); + assert(!fdt_setprop_u32(tree, b1, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, b1, "#size-cells", 2)); + fdt32_t idr[6] = {cpu_to_fdt32(2), 0, cpu_to_fdt32(2), 0, cpu_to_fdt32(4), 0}; + assert(!fdt_setprop(tree, b1, "ranges", idr, sizeof(idr))); + int b2 = node_at("/bus-level-one-display-fabric", "bus-level-two-external-engines"); + assert(!fdt_setprop_u32(tree, b2, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, b2, "#size-cells", 2)); + assert(!fdt_setprop_empty(tree, b2, "ranges")); + static const char nested_ep[] = + "/bus-level-one-display-fabric/bus-level-two-external-engines/dcpext@2b3e00000"; + _Static_assert(sizeof(nested_ep) > T6050_PATH, "test path must exceed T6050_PATH"); + engine_under("/bus-level-one-display-fabric/bus-level-two-external-engines", + "dcpext@2b3e00000", exts[1].coproc, "okay", false, PH_DART1); + exts[1].running = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected(nested_ep, PH_DART1, &old); cases++; + /* Scratch allocation failure during rejection: falls back to in-place editing so the + * unsafe engine is still disabled (or deleted on a packed DT) and stripped from scanout. */ + world(); + os_firmware = v272; + exts[1].running = 0; + fail_scratch = true; + t6050_ext_version(tree); + fail_scratch = false; + assert(is("/dcpext0", "apple,firmware-compat", &old)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + os_firmware = v272; + exts[0].running = 0; + assert(!fdt_pack(tree)); + fail_scratch = true; + t6050_ext_version(tree); + fail_scratch = false; + assert(fdt_path_offset(tree, "/dcpext0") < 0 && !has_scanout_dart(PH_DART0)); + assert(has_scanout_dart(PH_DISP0) && has_scanout_dart(PH_DART1)); cases++; + /* Scanout iommus > 128 bytes on an enabled display-subsystem: compacts iommus in place + * without allocating, strips PH_DART1 while keeping other phandles intact, disables the + * rejected engine, and updates healthy engines. */ + for (int inplace = 0; inplace < 2; inplace++) { + world(); + os_firmware = v272; + exts[1].running = 0; + int d = fdt_path_offset(tree, "/display"); + assert(!fdt_setprop_string(tree, d, "status", "okay")); + fdt32_t big_io[36]; + big_io[0] = cpu_to_fdt32(PH_DISP0); big_io[1] = 0; + big_io[2] = cpu_to_fdt32(PH_DART0); big_io[3] = 0; + big_io[4] = cpu_to_fdt32(PH_DART1); big_io[5] = 0; + for (int i = 3; i < 18; i++) { + big_io[2 * i] = cpu_to_fdt32(100 + i); + big_io[2 * i + 1] = 0; + } + assert(!fdt_setprop(tree, d, "iommus", big_io, sizeof(big_io))); + if (inplace) { + assert(!fdt_pack(tree)); + fail_scratch = true; + } + t6050_ext_version(tree); + fail_scratch = false; + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + if (!inplace) + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + else + assert(is("/dcpext0", "apple,firmware-compat", &old)); + int len = 0; + const fdt32_t *got = fdt_getprop(tree, fdt_path_offset(tree, "/display"), "iommus", &len); + assert(got && len == 34 * (int)sizeof(fdt32_t)); + assert(fdt32_ld(&got[0]) == PH_DISP0 && !fdt32_ld(&got[1])); + assert(fdt32_ld(&got[2]) == PH_DART0 && !fdt32_ld(&got[3])); + for (int i = 3; i < 18; i++) { + int j = (i - 1) * 2; + assert(fdt32_ld(&got[j]) == (u32)(100 + i) && !fdt32_ld(&got[j + 1])); + } + assert(!has_scanout_dart(PH_DART1)); + cases++; + } + /* status = "okay\0..." with trailing bytes: Linux probes the first string, so rejection + * still disables the engine and strips its scanout DART. */ + world(); + os_firmware = v272; + exts[1].running = 0; + assert(!fdt_setprop(tree, fdt_path_offset(tree, "/dcpext1"), "status", "okay\0disabled", 14)); + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + printf("PASS dcpext version %d cases\n", cases); + return 0; +} +''' + run(tmp_path, "ext-version", code, "PASS dcpext version 21 cases") From 4101923fe3822b3df8685a1a92d827b5ff4726bd Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 15:45:38 -0500 Subject: [PATCH 03/10] kboot: T6050: scope external-engine pass to input-DT-enabled engines t6050_ext_version() and t6050_ext_reject() run after t6050_handoff() and t6050_headless_handoff() so they can see any pinned t6050.uuid, which previously caused them to inspect and reject engines Aura had just enabled via t6050_ext_one(). When pt-region-* was omitted from a headless ADT/fixture, t6050_headless_handoff() recorded "published" and enabled the engine, display subsystem, and providers, and t6050_ext_reject() then disabled the engine and display subsystem while leaving the providers enabled. Record engines committed by t6050_ext_one() in t6050.ext_aura and skip them in t6050_ext_reject() and t6050_ext_version(), keeping the pass scoped to engines already enabled in the input DT. Add a headless missing-pt-region regression test and an ext_aura scoping test. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 11 ++++++++--- tests/python/test_t6050_handoff_bounds.py | 14 +++++++++++++- tests/python/test_t6050_tree_shape.py | 17 ++++++++++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index b66e49a0d..241a5ac7f 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -4830,6 +4830,7 @@ static struct { u32 bw_offset, nits, nclk, clk_id[T6050_CLOCKS], clk_hz[T6050_CLOCKS], pmc_ph; const char *gamut; 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() */ bool prewired; /* clocks and bandwidth scratch prewired: t6050_prewired() */ @@ -5810,6 +5811,8 @@ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip) continue; if (ret || dcp6_commit(dt, scratch)) dlog("dcpext%d stays with iBoot: no handoff\n", n); + else + t6050.ext_aura |= 1u << n; } } @@ -5937,7 +5940,8 @@ static T6050_TEXT int t6050_ext_walk(void *dt, int target, int *ord, int want_or } /* - * Disable every available apple,t6050-dcpext node that has no identity-mapped ADT match or fails + * Disable every available apple,t6050-dcpext node already enabled in the input DT (not one Aura + * just enabled via t6050_ext_one()) that has no identity-mapped ADT match or fails * t6050_check_ext_adt(), and strip it from any display-subsystem scanout iommus. Runs on a * scratch copy when available (falling back to editing `dt` in place if scratch allocation fails) * before firmware-version writes, independent of os_firmware and without rolling back if a packed @@ -5958,7 +5962,7 @@ static T6050_TEXT void t6050_ext_reject(void *dt) continue; } int n = t6050_ext_index(cur, at); - if (n >= 0 && !t6050_check_ext_adt(n)) { + if (n >= 0 && ((t6050.ext_aura & (1u << n)) || !t6050_check_ext_adt(n))) { at = fdt_node_offset_by_compatible(cur, at, "apple,t6050-dcpext"); continue; } @@ -6044,7 +6048,7 @@ static T6050_TEXT void t6050_ext_version(void *dt) if (!t6050_available(scratch, at)) continue; int n = t6050_ext_index(scratch, at); - if (n < 0 || t6050_check_ext_adt(n)) + if (n < 0 || (t6050.ext_aura & (1u << n)) || t6050_check_ext_adt(n)) continue; for (size_t i = 0; !err && i < ARRAY_SIZE(props); i++) { /* apple,firmware-compat (props[0]) always; apple,firmware-version only if present */ @@ -6195,6 +6199,7 @@ static T6050_TEXT void t6050_headless_handoff(void *dt, int engine) t6050_headless_publish(scratch, engine, vram, vram_size))) { free(scratch); } else if (scratch && !dcp6_commit(dt, scratch)) { + t6050.ext_aura |= 1u << engine; dlog("dcpext%d handed off with the display subsystem: firmware %s, framebuffer " "0x%lx+0x%lx\n", engine, os_firmware.string, vram, vram_size); diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index 26b3884b1..7022b59dd 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -455,6 +455,7 @@ static _Alignas(8) char dt[0x10000]; u64 vram = arg(argc, argv, "vram", 0), vram_size = arg(argc, argv, "vsize", 0); u64 meta_size = arg(argc, argv, "meta", 0), bw = arg(argc, argv, "bw", 0); + u64 no_pt_region = arg(argc, argv, "no_pt_region", 0); const char *nub = "/arm-io/dcpext0/iop-dcpext0-nub"; prop(nub, "uuid", image(true), 37); @@ -469,7 +470,8 @@ prop_u64s(d, "vm-base", VM_BASE, 0, 1); prop_u64s(d, "vm-size", VM_SIZE, 0, 1); prop_u64s(d, i ? "l1-tt-16" : "l1-tt-23", l1, 1, 2); - prop_u64s(d, i ? "pt-region-16" : "pt-region-23", l1 - 0x4000, l1 + 0x40000, 2); + if (!no_pt_region) + prop_u64s(d, i ? "pt-region-16" : "pt-region-23", l1 - 0x4000, l1 + 0x40000, 2); word(base + DART_T8110_PROTECT, DART_T8110_PROTECT_TTBR_TCR); word(base + DART_T8110_TCR(sid), T6050_TCR_WANT); word(base + DART_T8110_TTBR(sid), @@ -1117,6 +1119,16 @@ def test_panel_framebuffer_must_lie_in_vram(host, fb, size, taken): assert r.state["rc"] == "-1" and len(r.log) == 1 +def test_headless_without_pt_region_stays_handed_off(host): + # t6050_ext_version() / t6050_ext_reject() apply only to engines already enabled in the input + # DT, not engines Aura just enabled via t6050_ext_one(): omitting pt-region-* from the + # headless ADT still leaves dcpext0, display-subsystem, and their providers enabled. + r = headless(host, no_pt_region=1) + assert handed_off(r) + assert not r.said("rejected") + assert not r.said("the device tree enables") + + @pytest.mark.parametrize("fail", [0, 1, 2, 3, 4]) def test_prewired_panel_keeps_rejected_external_engines_out_of_handoff(host, fail): r = run(host, "prewired", fail=fail, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index a47d5869f..5466ba256 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -261,7 +261,7 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): #define dlog(fmt, ...) printf(fmt, ##__VA_ARGS__) ''' + layout + r''' static struct { u64 phys_base, mem_size; } cur_boot_args = {0x10000000000UL, 0x200000000UL}; -static struct { const char *uuid; } t6050; +static struct { const char *uuid; u32 ext_aura; } t6050; static void *adt; #define DART_DCP 0x48a340000UL #define DART_DISP0 0x48a300000UL @@ -637,7 +637,7 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): ''' + layout + r''' static struct fw_version_info os_firmware; static struct { u64 phys_base, mem_size; } cur_boot_args = {0x10000000000UL, 0x200000000UL}; -static struct { const char *uuid; } t6050; +static struct { const char *uuid; u32 ext_aura; } t6050; static struct { const char *name; } soc = {"t6050"}; static struct { const typeof(soc) *soc; } dcp6 = {&soc}; static void *adt; @@ -1117,8 +1117,19 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): t6050_ext_version(tree); assert(is("/dcpext0", "apple,firmware-compat", &v272)); assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* An engine Aura just enabled itself (ext_aura bit) is left untouched by t6050_ext_version(), + * even if its ADT lacks pt-region. */ + world(); + os_firmware = v272; + exts[1].pt16[0] = exts[1].pt16[1] = 0; + t6050.ext_aura = 1u << 1; + t6050_ext_version(tree); + t6050.ext_aura = 0; + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert(!is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DART1)); cases++; printf("PASS dcpext version %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 21 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 22 cases") From be98aca94ec8c5f41517fdfb462b7752f98a0972 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 15:59:05 -0500 Subject: [PATCH 04/10] kboot: T6050: reject duplicate external-engine reg mappings A device tree with multiple apple,t6050-dcpext nodes at the same coprocessor register address caused t6050_node_at() to pick the first match, allowing t6050_ext_one() to enable it and set its t6050.ext_aura bit so t6050_ext_reject() skipped a second, input-enabled node at the same address even when its ADT checks failed. Require t6050_node_at() and t6050_ext_index() to return -1 unless a matching coprocessor register address is unique across the tree, so ambiguous duplicate-reg mappings are refused before committing and rejected by t6050_ext_reject(). Add headless and unit regression cases. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 24 ++++++++++++++------ tests/python/test_t6050_handoff_bounds.py | 27 ++++++++++++++++++++++- tests/python/test_t6050_tree_shape.py | 16 +++++++++++++- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 241a5ac7f..5ab166dc1 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -4897,17 +4897,24 @@ static T6050_TEXT u32 t6050_add_node(void *dt, const char *parent, const char *n return ph; } -/* The `compat` node whose coprocessor (first reg) is at `coproc`, or -1 */ +/* + * The unique `compat` node whose coprocessor (first reg) is at `coproc`, or -1 when missing or + * ambiguous (more than one matching node). + */ static T6050_TEXT int t6050_node_at(void *dt, const char *compat, u64 coproc) { - for (int o = fdt_node_offset_by_compatible(dt, -1, compat); o >= 0; - o = fdt_node_offset_by_compatible(dt, o, compat)) { + int match = -1, o = -1; + + while ((o = fdt_node_offset_by_compatible(dt, o, compat)) >= 0) { int len; const fdt64_t *reg = fdt_getprop(dt, o, "reg", &len); - if (reg && len >= 16 && fdt64_ld(®[0]) == coproc) - return o; + if (reg && len >= 16 && fdt64_ld(®[0]) == coproc) { + if (match >= 0) + return -1; + match = o; + } } - return -1; + return o == -FDT_ERR_NOTFOUND ? match : -1; } /* @@ -5817,7 +5824,8 @@ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip) } /* - * The ADT dcpextN index whose coprocessor is at `node`'s identity-mapped reg[0], or -1 + * The ADT dcpextN index whose coprocessor is uniquely mapped at `node`'s identity-mapped reg[0], + * or -1 */ static T6050_TEXT int t6050_ext_index(void *dt, int node) { @@ -5826,6 +5834,8 @@ static T6050_TEXT int t6050_ext_index(void *dt, int node) if (!reg || len < 16 || !t6050_identity_reg(dt, node)) return -1; u64 coproc = fdt64_ld(®[0]); + if (t6050_node_at(dt, "apple,t6050-dcpext", coproc) != node) + return -1; for (int n = 0; n < T6050_ENGINES; n++) { char a[32]; u64 adt_coproc; diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index 7022b59dd..56c9edd84 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -456,6 +456,7 @@ u64 vram = arg(argc, argv, "vram", 0), vram_size = arg(argc, argv, "vsize", 0); u64 meta_size = arg(argc, argv, "meta", 0), bw = arg(argc, argv, "bw", 0); u64 no_pt_region = arg(argc, argv, "no_pt_region", 0); + u64 dup_ext = arg(argc, argv, "dup_ext", 0); const char *nub = "/arm-io/dcpext0/iop-dcpext0-nub"; prop(nub, "uuid", image(true), 37); @@ -516,6 +517,15 @@ int piodma = fdt_add_subnode(dt, ext, "piodma"); OK(piodma); fdt_u32s(dt, piodma, "iommus", 2, PH_DART_DISP, 16, 0, 0); + if (dup_ext) { + int dup = fdt_dev(dt, soc, "dcpext-dup@389c00000", COPROC, 0); + OK(fdt_setprop_string(dt, dup, "compatible", "apple,t6050-dcpext")); + OK(fdt_setprop_string(dt, dup, "status", "okay")); + fdt_u32s(dt, dup, "mboxes", 1, PH_MBOX, 0, 0, 0); + int dpio = fdt_add_subnode(dt, dup, "piodma"); + OK(dpio); + fdt_u32s(dt, dpio, "iommus", 2, PH_DART_DISP, 16, 0, 0); + } int disp = fdt_add_subnode(dt, soc, "display-subsystem"); OK(disp); OK(fdt_setprop_string(dt, disp, "compatible", "apple,display-subsystem")); @@ -529,11 +539,14 @@ int chosen = fdt_path_offset(dt, "/chosen"); const char *said = fdt_getprop(dt, chosen, T6050_STATUS, NULL); - printf("result chosen=%s engine=%s subsystem=%s dart-dcp=%s dart-disp=%s mbox=%s pmc=%s\n", + printf("result chosen=%s engine=%s subsystem=%s dart-dcp=%s dart-disp=%s mbox=%s pmc=%s", said ? said : "none", status(dt, "/soc/dcpext@389c00000"), status(dt, "/soc/display-subsystem"), status(dt, "/soc/iommu@389304000"), status(dt, "/soc/iommu@389308000"), status(dt, "/soc/mbox@389c08000"), status(dt, "/soc/pmc-metadata@510f00000")); + if (dup_ext) + printf(" dup=%s", status(dt, "/soc/dcpext-dup@389c00000")); + printf("\n"); int len = 0, n, count = 0; const fdt32_t *s = fdt_getprop(dt, fdt_path_offset(dt, "/soc/dcpext@389c00000"), "apple,bw-scratch", &len); @@ -1129,6 +1142,18 @@ def test_headless_without_pt_region_stays_handed_off(host): assert not r.said("the device tree enables") +@pytest.mark.parametrize("no_pt_region", [1, 0]) +def test_headless_duplicate_ext_node_refused(host, no_pt_region): + # A second, input-enabled apple,t6050-dcpext node at the same coprocessor address makes the + # mapping ambiguous: the headless handoff refuses before committing and t6050_ext_reject() + # disables the duplicate node too, even when pt-region-* is absent. + r = headless(host, no_pt_region=no_pt_region, dup_ext=1) + assert r.state == {"chosen": "refused", **DISABLED, "dup": "disabled"}, r.log + assert r.reserved == [OSLOG] + assert r.said(REFUSED) + assert not r.said("handed off") + + @pytest.mark.parametrize("fail", [0, 1, 2, 3, 4]) def test_prewired_panel_keeps_rejected_external_engines_out_of_handoff(host, fail): r = run(host, "prewired", fail=fail, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index 5466ba256..2183ecca4 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -776,6 +776,7 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ definition(source, "static T6050_TEXT int t6050_check_placement(") + \ definition(source, "static T6050_TEXT int t6050_check_ext_adt(") + \ + definition(source, "static T6050_TEXT int t6050_node_at(") + \ definition(source, "static T6050_TEXT int t6050_ext_index(") + \ definition(source, "static T6050_TEXT bool t6050_dart_at(") + \ definition(source, "static T6050_TEXT int t6050_drop_ext_scanout(") + \ @@ -1128,8 +1129,21 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert(is("/dcpext0", "apple,firmware-compat", &v272)); assert(!is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); assert(has_scanout_dart(PH_DART1)); cases++; + /* Duplicate apple,t6050-dcpext nodes at the same coprocessor address are ambiguous and + * rejected rather than exempted by ext_aura or version-stamped. */ + world(); + os_firmware = v272; + exts[1].pt16[0] = exts[1].pt16[1] = 0; + engine_under("/", "dcpext1-dup", exts[1].coproc, "okay", true, PH_DART1); + t6050.ext_aura = 1u << 1; + t6050_ext_version(tree); + t6050.ext_aura = 0; + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); + assert(is_disabled("/dcpext1-dup") && is("/dcpext1-dup", "apple,firmware-compat", &old)); + cases++; printf("PASS dcpext version %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 22 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 23 cases") From f6e5eaceec09f98e9d41b6f64dc9879d094eb388 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 16:14:11 -0500 Subject: [PATCH 05/10] kboot: T6050: reject all duplicate-reg nodes and preserve ADT scanout index In t6050_ext_reject(), deleting or mutating the first duplicate apple,t6050-dcpext node on a packed tree could make a second duplicate at the same coprocessor address look unique and survive into version-stamping after its scanout DART had already been removed. Record ambiguous ADT indices across the input tree before mutating anything and reject every available node at those indices. Also preserve the ADT index returned by t6050_ext_index() via a separate uniqueness output so t6050_drop_ext_scanout() still strips /arm-io/dart-dispextN from display-subsystem iommus when duplicate nodes lack piodma subnodes. Add packed-tree and no-piodma duplicate-node regression cases. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 44 ++++++++++++---- tests/python/test_t6050_handoff_bounds.py | 55 ++++++++++++++++++-- tests/python/test_t6050_tree_shape.py | 61 ++++++++++++++++++++--- 3 files changed, 138 insertions(+), 22 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 5ab166dc1..d1ccfe17e 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -5824,18 +5824,21 @@ static T6050_TEXT void t6050_ext_handoff(void *dt, int engines, int skip) } /* - * The ADT dcpextN index whose coprocessor is uniquely mapped at `node`'s identity-mapped reg[0], - * or -1 + * 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 + * node at that coprocessor address. */ -static T6050_TEXT int t6050_ext_index(void *dt, int node) +static T6050_TEXT int t6050_ext_index(void *dt, int node, bool *unique) { int len; const fdt64_t *reg = fdt_getprop(dt, node, "reg", &len); + if (unique) + *unique = false; if (!reg || len < 16 || !t6050_identity_reg(dt, node)) return -1; u64 coproc = fdt64_ld(®[0]); - if (t6050_node_at(dt, "apple,t6050-dcpext", coproc) != node) - return -1; + if (unique) + *unique = t6050_node_at(dt, "apple,t6050-dcpext", coproc) == node; for (int n = 0; n < T6050_ENGINES; n++) { char a[32]; u64 adt_coproc; @@ -5963,16 +5966,35 @@ static T6050_TEXT void t6050_ext_reject(void *dt) { void *scratch = NULL; bool in_place = false, changed = false; - int err = 0, at = fdt_node_offset_by_compatible(dt, -1, "apple,t6050-dcpext"); + u32 ambig = 0; + int err = 0, at = -1; + + /* + * Record ambiguous ADT mappings before mutating or deleting nodes can make a survivor look + * unique. + */ + while ((at = fdt_node_offset_by_compatible(dt, at, "apple,t6050-dcpext")) >= 0) { + bool unique; + int n = t6050_ext_index(dt, at, &unique); + if (n >= 0 && !unique) + ambig |= 1u << n; + } + if (at != -FDT_ERR_NOTFOUND) { + dlog("could not disable rejected external engines: nothing changed\n"); + return; + } + at = fdt_node_offset_by_compatible(dt, -1, "apple,t6050-dcpext"); while (at >= 0) { void *cur = scratch ? scratch : dt; if (!t6050_available(cur, at)) { at = fdt_node_offset_by_compatible(cur, at, "apple,t6050-dcpext"); continue; } - int n = t6050_ext_index(cur, at); - if (n >= 0 && ((t6050.ext_aura & (1u << n)) || !t6050_check_ext_adt(n))) { + bool unique; + int n = t6050_ext_index(cur, at, &unique); + if (n >= 0 && unique && !(ambig & (1u << n)) && + ((t6050.ext_aura & (1u << n)) || !t6050_check_ext_adt(n))) { at = fdt_node_offset_by_compatible(cur, at, "apple,t6050-dcpext"); continue; } @@ -6054,11 +6076,11 @@ static T6050_TEXT void t6050_ext_version(void *dt) if (!scratch) return; while (!err && (at = fdt_node_offset_by_compatible(scratch, at, "apple,t6050-dcpext")) >= 0) { - bool set = false; + bool set = false, unique; if (!t6050_available(scratch, at)) continue; - int n = t6050_ext_index(scratch, at); - if (n < 0 || (t6050.ext_aura & (1u << n)) || t6050_check_ext_adt(n)) + int n = t6050_ext_index(scratch, at, &unique); + if (n < 0 || !unique || (t6050.ext_aura & (1u << n)) || t6050_check_ext_adt(n)) continue; for (size_t i = 0; !err && i < ARRAY_SIZE(props); i++) { /* apple,firmware-compat (props[0]) always; apple,firmware-version only if present */ diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index 56c9edd84..9a629d069 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -621,6 +621,8 @@ u64 fail_scratch = arg(argc, argv, "fail_scratch", 0); u64 pack = arg(argc, argv, "pack", 0); u64 nested = arg(argc, argv, "nested", 0); + u64 dup_ext = arg(argc, argv, "dup_ext", 0); + u64 no_piodma = arg(argc, argv, "no_piodma", 0); u64 vram = arg(argc, argv, "vram", 0), vram_size = arg(argc, argv, "vsize", 0); u64 meta_size = arg(argc, argv, "meta", 0), ram = cur_boot_args.phys_base; const char *uuid = image(true); @@ -830,9 +832,23 @@ fdt_u32s(dt, ext, "apple,firmware-compat", 3, 27, 0, 0, 0); fdt_u32s(dt, ext, "apple,firmware-version", 3, 27, 0, 0, 0); } - int epio = fdt_add_subnode(dt, ext, "piodma"); - OK(epio); - fdt_u32s(dt, epio, "iommus", 2, dph, 16, 0, 0); + if (!(e == 1 && no_piodma)) { + int epio = fdt_add_subnode(dt, ext, "piodma"); + OK(epio); + fdt_u32s(dt, epio, "iommus", 2, dph, 16, 0, 0); + } + if (e == 1 && dup_ext) { + int dup = fdt_dev(dt, soc, "dcpext1-dup", ext_coproc[e], 0); + OK(fdt_setprop_string(dt, dup, "compatible", "apple,t6050-dcpext")); + OK(fdt_setprop_string(dt, dup, "status", "okay")); + fdt_u32s(dt, dup, "apple,firmware-compat", 3, 27, 0, 0, 0); + fdt_u32s(dt, dup, "apple,firmware-version", 3, 27, 0, 0, 0); + if (!no_piodma) { + int dpio = fdt_add_subnode(dt, dup, "piodma"); + OK(dpio); + fdt_u32s(dt, dpio, "iommus", 2, dph, 16, 0, 0); + } + } } int disp = fdt_add_subnode(dt, soc, "display-subsystem"); @@ -887,10 +903,20 @@ if (v1) snprintf(v1s, sizeof(v1s), "%u.%u.%u", fdt32_ld(&v1[0]), fdt32_ld(&v1[1]), fdt32_ld(&v1[2])); printf("result chosen=%s dcp=%s subsystem=%s ext0=%s ext1=%s ext0_fw=%s ext1_fw=%s " - "ext1_ver=%s\n", + "ext1_ver=%s", said ? said : "none", status(dt, "/soc/dcp@48ae00000"), status(dt, "/soc/display-subsystem"), status(dt, "/soc/dcpext0"), e1 < 0 ? "deleted" : status(dt, ext1_path), c0s, c1s, v1s); + if (dup_ext) { + int dup = fdt_path_offset(dt, "/soc/dcpext1-dup"); + const fdt32_t *dc = dup < 0 ? NULL : fdt_getprop(dt, dup, "apple,firmware-compat", &len); + char dcs[32] = "none"; + if (dc) + snprintf(dcs, sizeof(dcs), "%u.%u.%u", fdt32_ld(&dc[0]), fdt32_ld(&dc[1]), + fdt32_ld(&dc[2])); + printf(" dup=%s dup_fw=%s", dup < 0 ? "deleted" : status(dt, "/soc/dcpext1-dup"), dcs); + } + printf("\n"); disp = fdt_path_offset(dt, "/soc/display-subsystem"); const fdt32_t *io = fdt_getprop(dt, disp, "iommus", &len); printf("iommus"); @@ -1282,6 +1308,27 @@ def test_prewired_panel_drops_rejected_scanout_from_enabled_oversized_iommus(hos assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" +def test_prewired_panel_rejects_packed_and_no_piodma_duplicate_ext_nodes(host): + # Packed DT with two healthy duplicate ext1 nodes (both having piodma): deleting the first + # duplicate must not let the second survive as unique, stay okay, or get version-stamped. + r = run(host, "prewired", pack=1, absent_status=1, dup_ext=1, fail=0, meta=META_SIZE, + vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "okay" + assert r.state["ext1"] == "deleted" + assert r.state["dup"] == "disabled" and r.state["dup_fw"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + # Duplicate healthy ext1 nodes that both lack piodma: both are disabled and PH_EXT1_DISP (12) + # is still removed from display-subsystem iommus via the preserved ADT index. + r = run(host, "prewired", dup_ext=1, no_piodma=1, fail=0, meta=META_SIZE, vram=VRAM, + vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "refused", r.log + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + assert r.state["ext1"] == "disabled" and r.state["ext1_fw"] == "27.0.0" + assert r.state["dup"] == "disabled" and r.state["dup_fw"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + + @pytest.mark.parametrize("mode", ["headless", "panel"]) def test_vram_at_the_top_of_the_address_space(host, mode): # /vram ends where the address space does: `vram + vram_size` is 0 there, and still right diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index 2183ecca4..92f60ceba 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -808,12 +808,14 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert(!fdt_setprop(tree, n, "apple,firmware-version", v, sizeof(v))); if (status) assert(!fdt_setprop_string(tree, n, "status", status)); - char ep[160]; - int elen = snprintf(ep, sizeof(ep), "%s%s%s", strcmp(parent, "/") ? parent : "", "/", name); - assert(elen > 0 && (size_t)elen < sizeof(ep)); - int pio = node_at(ep, "piodma"); - fdt32_t io[2] = {cpu_to_fdt32(dart_ph), cpu_to_fdt32(16)}; - assert(!fdt_setprop(tree, pio, "iommus", io, sizeof(io))); + if (dart_ph) { + char ep[160]; + int elen = snprintf(ep, sizeof(ep), "%s%s%s", strcmp(parent, "/") ? parent : "", "/", name); + assert(elen > 0 && (size_t)elen < sizeof(ep)); + int pio = node_at(ep, "piodma"); + fdt32_t io[2] = {cpu_to_fdt32(dart_ph), cpu_to_fdt32(16)}; + assert(!fdt_setprop(tree, pio, "iommus", io, sizeof(io))); + } return n; } /* Whether @prop of node @name is @want's cells */ @@ -1142,8 +1144,53 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert_rejected("/dcpext1", PH_DART1, &old); assert(is_disabled("/dcpext1-dup") && is("/dcpext1-dup", "apple,firmware-compat", &old)); cases++; + /* Packed DT with two healthy duplicates (both having piodma): deleting the first duplicate + * must not let the second appear unique, stay enabled, or get version-stamped. */ + fresh(); + reset_adt(); + os_firmware = v273; + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b2300000", exts[0].dart_disp, PH_DART0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + fdt32_t io_dup[6] = {cpu_to_fdt32(PH_DISP0), 0, cpu_to_fdt32(PH_DART0), 0, + cpu_to_fdt32(PH_DART1), 0}; + assert(!fdt_setprop(tree, disp, "iommus", io_dup, sizeof(io_dup))); + engine_under("/", "dcpext0", exts[0].coproc, "okay", true, PH_DART0); + engine_under("/", "dcpext1", exts[1].coproc, NULL, true, PH_DART1); + engine_under("/", "dcpext1-dup", exts[1].coproc, "okay", true, PH_DART1); + assert(!fdt_pack(tree)); + t6050_ext_version(tree); + assert(fdt_path_offset(tree, "/dcpext1") < 0); + assert_rejected("/dcpext1-dup", PH_DART1, &old); + assert(is("/dcpext0", "apple,firmware-compat", &v273) && !is_disabled("/dcpext0")); + cases++; + /* Two duplicates that both lack piodma (with healthy ADT state): both are still disabled and + * their scanout DART is still removed via the preserved ADT index. */ + fresh(); + reset_adt(); + os_firmware = v272; + assert(!fdt_setprop_u32(tree, 0, "#address-cells", 2)); + assert(!fdt_setprop_u32(tree, 0, "#size-cells", 2)); + dart_node("iommu@48a300000", DART_DISP0, PH_DISP0); + dart_node("iommu@2b2300000", exts[0].dart_disp, PH_DART0); + dart_node("iommu@2b3300000", exts[1].dart_disp, PH_DART1); + disp = node_at("/", "display"); + assert(!fdt_setprop_string(tree, disp, "compatible", "apple,display-subsystem")); + assert(!fdt_setprop(tree, disp, "iommus", io_dup, sizeof(io_dup))); + engine_under("/", "dcpext0", exts[0].coproc, "okay", true, PH_DART0); + engine_under("/", "dcpext1", exts[1].coproc, "okay", true, 0); + engine_under("/", "dcpext1-dup", exts[1].coproc, "okay", true, 0); + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); + assert(is_disabled("/dcpext1-dup") && is("/dcpext1-dup", "apple,firmware-compat", &old)); + cases++; printf("PASS dcpext version %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 23 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 25 cases") From 9b661123e3d064b0da3ffbbbbd80aee9aad4b337 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 02:34:03 -0500 Subject: [PATCH 06/10] kboot: T6050: fail closed when external engine version update fails In t6050_ext_version(), a failed scratch allocation, firmware-compat/version property write, or commit previously rolled back and left an input-DT-enabled apple,t6050-dcpext engine active with stale or missing apple,firmware-compat, causing Linux to pick the wrong method table. Run each surviving engine's version update in its own scratch transaction via t6050_ext_version_one(), and extract t6050_ext_disable() from t6050_ext_reject() so any engine whose compatibility could not be updated is disabled (or deleted on a packed DT where status = "disabled" does not fit) and stripped from every apple,display-subsystem scanout iommus. Update the packed-tree and scratch-failure tests to assert the fail-closed behavior. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 142 +++++++++++++--------- tests/python/test_t6050_handoff_bounds.py | 30 +++-- tests/python/test_t6050_tree_shape.py | 84 +++++++++---- 3 files changed, 164 insertions(+), 92 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 89b4f40d7..76760f167 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -4603,6 +4603,36 @@ static T6050_TEXT int t6050_ext_walk(void *dt, int target, int *ord, int want_or return o == -FDT_ERR_NOTFOUND ? count : o; } +/* + * Disable (or delete via fdt_del_node() on a packed DT where status = "disabled" cannot fit) + * `at` (ADT index `n`, or -1) and drop it from every display-subsystem scanout `iommus`. + * Returns -1 on FDT error, 1 if an enclosing display-subsystem node was deleted (caller must + * rescan from the top), or 0 otherwise. + */ +static T6050_TEXT int t6050_ext_disable(void *dt, int at, int n, bool *changed, const char *why) +{ + int ord, total = t6050_ext_walk(dt, at, &ord, -1, NULL); + const char *name = fdt_get_name(dt, at, NULL); + if (total < 0 || ord < 0) + return -1; + if (n < 0) + dlog("external engine %s %s: disabling\n", name ? name : "(unknown)", why); + else + dlog("dcpext%d %s: disabling and keeping out of scanout\n", n, why); + if (t6050_drop_ext_scanout(dt, at, n, changed)) + dlog("could not drop %s from display scanout\n", name ? name : "external engine"); + int after = t6050_ext_walk(dt, -1, NULL, ord, &at); + if (after < 0) + return -1; + if (after != total) + /* A deleted display-subsystem node enclosed an engine: re-scan from the top. */ + return 1; + if (at < 0 || (fdt_setprop_string(dt, at, "status", "disabled") && fdt_del_node(dt, at))) + return -1; + *changed = true; + return 0; +} + /* * Disable every available apple,t6050-dcpext node already enabled in the input DT (not one Aura * just enabled via t6050_ext_one()) that has no identity-mapped ADT match or fails @@ -4656,36 +4686,11 @@ static T6050_TEXT void t6050_ext_reject(void *dt) else in_place = true; } - int ord, total = t6050_ext_walk(cur, at, &ord, -1, NULL); - const char *name = fdt_get_name(cur, at, NULL); - if (total < 0 || ord < 0) { + if (t6050_ext_disable(cur, at, n, &changed, + n < 0 ? "has no matching ADT dcpextN" : "rejected") < 0) { err = -1; break; } - if (n < 0) - dlog("external engine %s has no matching ADT dcpextN: disabling\n", - name ? name : "(unknown)"); - else - dlog("dcpext%d rejected: disabling and keeping out of scanout\n", n); - if (t6050_drop_ext_scanout(cur, at, n, &changed)) - dlog("could not drop %s from display scanout\n", - name ? name : "rejected external engine"); - int after = t6050_ext_walk(cur, -1, NULL, ord, &at); - if (after < 0) { - err = -1; - break; - } - if (after != total) { - /* A deleted display-subsystem node enclosed an engine: re-scan from the top. */ - at = fdt_node_offset_by_compatible(cur, -1, "apple,t6050-dcpext"); - continue; - } - if (at < 0 || - (fdt_setprop_string(cur, at, "status", "disabled") && fdt_del_node(cur, at))) { - err = -1; - break; - } - changed = true; at = fdt_node_offset_by_compatible(cur, -1, "apple,t6050-dcpext"); } if (err || (at < 0 && at != -FDT_ERR_NOTFOUND)) @@ -4697,6 +4702,33 @@ static T6050_TEXT void t6050_ext_reject(void *dt) free(scratch); } +/* + * 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). + */ +static T6050_TEXT int t6050_ext_version_one(void *dt, int at) +{ + static const char *const props[] = {"apple,firmware-compat", "apple,firmware-version"}; + void *scratch = dcp6_scratch(dt); + int err = 0; + + if (!scratch) + return -1; + for (size_t i = 0; !err && i < ARRAY_SIZE(props); i++) { + /* 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); + } + if (err || dcp6_commit(dt, scratch)) { + if (err) + free(scratch); + return -1; + } + return 0; +} + /* * External engines the device tree enables itself (the J716c's) run this boot's firmware, but * the tree names the build it was written for in their apple,firmware-compat, from which the @@ -4706,15 +4738,15 @@ static T6050_TEXT void t6050_ext_reject(void *dt) * whatever their number of cells, as t6050_publish() does for the internal dcp. An available * engine with no valid ADT match or failing the ADT running/uuid or placement checks is disabled * and stripped from any display-subsystem scanout iommus first by t6050_ext_reject(), even on an - * unknown stub or when the version write runs out of space. A node not available (status absent, - * "okay" or "ok") is t6050_ext_one()'s. The version edit is made on its own scratch copy: a - * failed write or walk leaves the versions unchanged while keeping any rejections. + * unknown stub. A node not available (status absent, "okay" or "ok") is t6050_ext_one()'s. + * Each surviving engine's version edit runs in its own scratch transaction; 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. */ static T6050_TEXT void t6050_ext_version(void *dt) { - static const char *const props[] = {"apple,firmware-compat", "apple,firmware-version"}; + u32 done = 0; int count = 0, err = 0, at = -1; - bool changed = false; t6050_ext_reject(dt); @@ -4723,37 +4755,31 @@ static T6050_TEXT void t6050_ext_version(void *dt) os_firmware.string ? os_firmware.string : ""); return; } - void *scratch = dcp6_scratch(dt); - if (!scratch) - return; - while (!err && (at = fdt_node_offset_by_compatible(scratch, at, "apple,t6050-dcpext")) >= 0) { - bool set = false, unique; - if (!t6050_available(scratch, at)) + while ((at = fdt_node_offset_by_compatible(dt, at, "apple,t6050-dcpext")) >= 0) { + bool unique; + if (!t6050_available(dt, at)) continue; - int n = t6050_ext_index(scratch, at, &unique); - if (n < 0 || !unique || (t6050.ext_aura & (1u << n)) || t6050_check_ext_adt(n)) + int n = t6050_ext_index(dt, at, &unique); + if (n < 0 || !unique || ((t6050.ext_aura | done) & (1u << n)) || t6050_check_ext_adt(n)) continue; - for (size_t i = 0; !err && i < ARRAY_SIZE(props); i++) { - /* 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); - set = set || !err; + done |= 1u << n; + if (t6050_ext_version_one(dt, at)) { + bool dummy = false; + if (t6050_ext_disable(dt, at, n, &dummy, "could not take firmware version") < 0) { + err = -1; + break; + } + } else { + count++; } - count += set; - changed |= set; + at = -1; } /* Only -FDT_ERR_NOTFOUND is a walk's normal end */ - if (err || at != -FDT_ERR_NOTFOUND) { - free(scratch); - dlog("could not give the external engines the firmware version: version unchanged\n"); - } else if (!changed) { - free(scratch); - } else if (!dcp6_commit(dt, scratch)) { - if (count) - dlog("firmware %s on %d external engine(s) the device tree enables\n", - os_firmware.string, count); - } + 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); } /* Any failure leaves the display nodes disabled: the boot framebuffer keeps the panel */ diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index 49b651592..43cbd7822 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -1194,14 +1194,15 @@ def test_prewired_panel_rejects_external_engines_on_unknown_stub_and_packed_dt(h assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" assert r.iommus == [2, 0, 11, 0] - # Packed DT where ext0's firmware-compat write fails with NOSPACE: rejection is not rolled - # back, so stopped ext1 stays disabled and out of display-subsystem iommus. + # Packed DT where ext0's firmware-compat write fails with NOSPACE: ext0 is disabled and + # stripped from display-subsystem iommus rather than advertising stale/missing compat, and + # stopped ext1 stays disabled and out of display-subsystem iommus. r = run(host, "prewired", pack=1, nocompat=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) - assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "none" + assert r.state["ext0"] == "disabled" and r.state["ext0_fw"] == "none" assert r.state["ext1"] == "disabled" assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" - assert r.iommus == [2, 0, 11, 0] + assert r.iommus == [2, 0] # Packed DT where ext1 has no status property so status = "disabled" cannot fit: ext1 is # deleted instead and its display DART is removed from display-subsystem iommus. @@ -1241,18 +1242,21 @@ def test_prewired_panel_rejects_nested_bus_external_engine(host): def test_prewired_panel_rejects_external_engines_across_failure_and_status_edges(host): - # Scratch allocation failure during rejection: falls back to in-place edits so stopped ext1 - # is still disabled and stripped from scanout (and deleted on a packed DT with absent status). + # Scratch allocation failure during rejection/version update: falls back to in-place edits so + # stopped ext1 is disabled and stripped from scanout, and ext0 (whose compat cannot be updated) + # is also disabled and stripped from scanout rather than keeping stale apple,firmware-compat. r = run(host, "prewired", fail_scratch=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "disabled" assert r.state["ext1"] == "disabled" assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" - assert r.iommus == [2, 0, 11, 0] + assert r.iommus == [2, 0] r = run(host, "prewired", pack=1, absent_status=1, fail_scratch=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["ext0"] == "disabled" assert r.state["ext1"] == "deleted" - assert r.iommus == [2, 0, 11, 0] + assert r.iommus == [2, 0] # Oversized display-subsystem iommus (> 128 bytes): compacts iommus in place without # allocating, strips ext1's DART while keeping other phandles intact, disables rejected ext1, @@ -1281,16 +1285,20 @@ def test_prewired_panel_rejects_external_engines_across_failure_and_status_edges def test_prewired_panel_drops_rejected_scanout_from_enabled_oversized_iommus(host, fail_scratch): # Display subsystem already enabled ("okay") with >128-byte iommus: rejected ext1 is disabled # AND its DART phandle (12) is removed from iommus in place while all other phandles stay - # intact, both with scratch allocation and with in-place fallback. - expected_iommus = [2, 0, 11, 0] + [x for i in range(3, 18) for x in (100 + i, 0)] + # intact. With fail_scratch=0 ext0 is updated to 27.2.0; with fail_scratch=1 ext0 cannot + # update compat and is also disabled with its DART phandle (11) stripped in place. + tail = [x for i in range(3, 18) for x in (100 + i, 0)] r = run(host, "prewired", disp_enabled=1, oversized_iommus=1, fail_scratch=fail_scratch, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) assert r.state["subsystem"] == "okay" assert r.state["ext1"] == "disabled" assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" - assert r.iommus == expected_iommus if not fail_scratch: assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.2.0" + assert r.iommus == [2, 0, 11, 0] + tail + else: + assert r.state["ext0"] == "disabled" + assert r.iommus == [2, 0] + tail def test_prewired_panel_rejects_packed_and_no_piodma_duplicate_ext_nodes(host): diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index 92f60ceba..a14156fee 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -781,7 +781,9 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): definition(source, "static T6050_TEXT bool t6050_dart_at(") + \ definition(source, "static T6050_TEXT int t6050_drop_ext_scanout(") + \ definition(source, "static T6050_TEXT int t6050_ext_walk(") + \ + definition(source, "static T6050_TEXT int t6050_ext_disable(") + \ definition(source, "static T6050_TEXT void t6050_ext_reject(") + \ + definition(source, "static T6050_TEXT int t6050_ext_version_one(") + \ definition(source, "static T6050_TEXT void t6050_ext_version(") + r''' static const u32 tree_version[3] = {27, 0, 0}; enum { PH_DISP0 = 10, PH_DART0 = 11, PH_DART1 = 12, PH_DART2 = 13 }; @@ -907,12 +909,33 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert(is("/dcpext0", "apple,firmware-version", &v273)); assert(is("/dcpext1", "apple,firmware-compat", &v273)); assert(is("/dcpext2", "apple,firmware-version", &old)); cases++; - /* No room in the tree for the four cells: nothing changes. */ + /* Packed DT with no free room for four cells: dcpext1 fails first (disabled, PH_DART1 + * stripped, +4B net free), leaving too little room for dcpext0's compat+version so dcpext0 + * fails too and is deleted (absent status) with PH_DART0 stripped. */ world(); assert(!fdt_pack(tree)); - memcpy(before, tree, fdt_totalsize(tree)); t6050_ext_version(tree); - assert(!memcmp(before, tree, fdt_totalsize(tree))); cases++; + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(fdt_path_offset(tree, "/dcpext0") < 0); + assert(has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART0) && + !has_scanout_dart(PH_DART1)); cases++; + /* Packed DT where every enabled engine fails the four-cell write and status = "disabled" fits: + * both engines are disabled and stripped from scanout rather than keeping stale compat. */ + world(); + assert(!fdt_setprop_string(tree, fdt_path_offset(tree, "/dcpext0"), "status", "okay")); + { + fdt32_t v[3]; + for (int i = 0; i < 3; i++) + v[i] = cpu_to_fdt32(tree_version[i]); + assert(!fdt_setprop(tree, fdt_path_offset(tree, "/dcpext1"), + "apple,firmware-version", v, sizeof(v))); + } + assert(!fdt_pack(tree)); + t6050_ext_version(tree); + assert(is_disabled("/dcpext0") && is("/dcpext0", "apple,firmware-compat", &old)); + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART0) && + !has_scanout_dart(PH_DART1)); cases++; /* An unknown firmware: nothing changes. */ world(); os_firmware = (struct fw_version_info){V_UNKNOWN, NULL, {0}, 0, NULL}; @@ -1002,14 +1025,17 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert(is("/dcpext0", "apple,firmware-compat", &old) && !is_disabled("/dcpext0")); assert(has_scanout_dart(PH_DISP0) && has_scanout_dart(PH_DART0)); assert_rejected("/dcpext1", PH_DART1, &old); cases++; - /* Packed DT where the four-cell version write fails: rejection is NOT rolled back. */ + /* Packed DT where the four-cell version write fails: rejection is NOT rolled back, and the + * engine whose compatibility could not be updated is deleted (absent status) and stripped from + * scanout instead of advertising stale apple,firmware-compat. */ world(); os_firmware = v273; exts[1].running = 0; assert(!fdt_pack(tree)); t6050_ext_version(tree); - assert(is("/dcpext0", "apple,firmware-compat", &old) && !is_disabled("/dcpext0")); - assert_rejected("/dcpext1", PH_DART1, &old); cases++; + assert(fdt_path_offset(tree, "/dcpext0") < 0 && !has_scanout_dart(PH_DART0)); + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART1)); cases++; /* Packed DT where status = "disabled" cannot fit (absent status on dcpext0): the rejected * node is deleted instead and its scanout DART is stripped. */ world(); @@ -1051,16 +1077,19 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): t6050_ext_version(tree); assert(is("/dcpext0", "apple,firmware-compat", &v272)); assert_rejected(nested_ep, PH_DART1, &old); cases++; - /* Scratch allocation failure during rejection: falls back to in-place editing so the - * unsafe engine is still disabled (or deleted on a packed DT) and stripped from scanout. */ + /* Scratch allocation failure during rejection/version update: falls back to in-place editing so + * both the unsafe engine and any engine whose compatibility could not be updated are disabled + * (or deleted on a packed DT) and stripped from scanout. */ world(); os_firmware = v272; exts[1].running = 0; fail_scratch = true; t6050_ext_version(tree); fail_scratch = false; - assert(is("/dcpext0", "apple,firmware-compat", &old)); - assert_rejected("/dcpext1", PH_DART1, &old); cases++; + assert(is_disabled("/dcpext0") && is("/dcpext0", "apple,firmware-compat", &old)); + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART0) && + !has_scanout_dart(PH_DART1)); cases++; world(); os_firmware = v272; exts[0].running = 0; @@ -1069,10 +1098,12 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): t6050_ext_version(tree); fail_scratch = false; assert(fdt_path_offset(tree, "/dcpext0") < 0 && !has_scanout_dart(PH_DART0)); - assert(has_scanout_dart(PH_DISP0) && has_scanout_dart(PH_DART1)); cases++; + assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); + assert(has_scanout_dart(PH_DISP0) && !has_scanout_dart(PH_DART1)); cases++; /* Scanout iommus > 128 bytes on an enabled display-subsystem: compacts iommus in place * without allocating, strips PH_DART1 while keeping other phandles intact, disables the - * rejected engine, and updates healthy engines. */ + * rejected engine, and either updates healthy engines (inplace == 0) or deletes dcpext0 and + * strips PH_DART0 when scratch allocation fails (inplace == 1). */ for (int inplace = 0; inplace < 2; inplace++) { world(); os_firmware = v272; @@ -1095,18 +1126,25 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): t6050_ext_version(tree); fail_scratch = false; assert(is_disabled("/dcpext1") && is("/dcpext1", "apple,firmware-compat", &old)); - if (!inplace) - assert(is("/dcpext0", "apple,firmware-compat", &v272)); - else - assert(is("/dcpext0", "apple,firmware-compat", &old)); int len = 0; const fdt32_t *got = fdt_getprop(tree, fdt_path_offset(tree, "/display"), "iommus", &len); - assert(got && len == 34 * (int)sizeof(fdt32_t)); - assert(fdt32_ld(&got[0]) == PH_DISP0 && !fdt32_ld(&got[1])); - assert(fdt32_ld(&got[2]) == PH_DART0 && !fdt32_ld(&got[3])); - for (int i = 3; i < 18; i++) { - int j = (i - 1) * 2; - assert(fdt32_ld(&got[j]) == (u32)(100 + i) && !fdt32_ld(&got[j + 1])); + if (!inplace) { + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert(got && len == 34 * (int)sizeof(fdt32_t)); + assert(fdt32_ld(&got[0]) == PH_DISP0 && !fdt32_ld(&got[1])); + assert(fdt32_ld(&got[2]) == PH_DART0 && !fdt32_ld(&got[3])); + for (int i = 3; i < 18; i++) { + int j = (i - 1) * 2; + assert(fdt32_ld(&got[j]) == (u32)(100 + i) && !fdt32_ld(&got[j + 1])); + } + } else { + assert(fdt_path_offset(tree, "/dcpext0") < 0 && !has_scanout_dart(PH_DART0)); + assert(got && len == 32 * (int)sizeof(fdt32_t)); + assert(fdt32_ld(&got[0]) == PH_DISP0 && !fdt32_ld(&got[1])); + for (int i = 3; i < 18; i++) { + int j = (i - 2) * 2; + assert(fdt32_ld(&got[j]) == (u32)(100 + i) && !fdt32_ld(&got[j + 1])); + } } assert(!has_scanout_dart(PH_DART1)); cases++; @@ -1193,4 +1231,4 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 25 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 26 cases") From 1458e4eb7cba98d091a5324621c6b8932a3d87f5 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 02:58:19 -0500 Subject: [PATCH 07/10] kboot: T6050: place display and ATC handoff helpers in .rodata.xtext Place the T6050 display handoff (T6050_INIT / T6050_TEXT in src/kboot_display_handoff.c) and combo PHY handoff (T6050_ATC / T6050_ATC_INIT in src/kboot_atc.c) in .rodata.xtext via AURA_XTEXT instead of .init.t6050/.text, guarding Clang's minsize attribute with __clang__. With .init and .text sharing one 64 KiB step in aura-raw.ld, keeping these run-once helpers in .text left default clang (without -Oz) at 112 B of Stage 1 headroom; moving them to .rodata.xtext brings Stage 1 headroom to 8,128 B (clang), 6,904 B (GCC 16.2), and 6,560 B (Ubuntu 24.04 GCC 13.3). Signed-off-by: Erik Bourget --- src/kboot_atc.c | 9 +++++++-- src/kboot_display_handoff.c | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/kboot_atc.c b/src/kboot_atc.c index 6966986ed..28623a5e0 100644 --- a/src/kboot_atc.c +++ b/src/kboot_atc.c @@ -1423,8 +1423,13 @@ struct t6050_atc_tunable { u32 offset; }; -#define T6050_ATC __attribute__((minsize, noinline)) -#define T6050_ATC_INIT __attribute__((minsize, noinline, section(".init.t6050"))) +#ifdef __clang__ +#define T6050_ATC_MINSIZE __attribute__((minsize)) +#else +#define T6050_ATC_MINSIZE +#endif +#define T6050_ATC T6050_ATC_MINSIZE AURA_XTEXT +#define T6050_ATC_INIT T6050_ATC_MINSIZE AURA_XTEXT static const struct t6050_atc_tunable t6050_atc_tunables[] = { {"tunable_ATC0AXI2AF", "apple,tunable-axi2af", "axi2af", 41, 0}, diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 76760f167..94fd36f64 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -3451,9 +3451,14 @@ static const struct { #define T6050_STATUS "asahi,t6050-dcp-handoff" #define T6050_TCR_WANT (DART_T8110_TCR_TRANSLATE | DART_T8110_TCR_FOUR_LEVEL) #define T6050_TCR_MASK (T6050_TCR_WANT | DART_T8110_TCR_BYPASS_DART) -/* Runs once per boot: size over speed, and out of .text where it fits (aura.ld) */ -#define T6050_INIT __attribute__((minsize, noinline, section(".init.t6050"))) -#define T6050_TEXT __attribute__((minsize, noinline)) +/* Runs once per boot: size over speed, and out of .text into .rodata.xtext (AURA_XTEXT) */ +#ifdef __clang__ +#define T6050_MINSIZE __attribute__((minsize)) +#else +#define T6050_MINSIZE +#endif +#define T6050_INIT T6050_MINSIZE AURA_XTEXT +#define T6050_TEXT T6050_MINSIZE AURA_XTEXT /* * Nodes, by device tree path; the first five are enabled by the handoff. T6050_PMC: the PMC From d3184b1778fc3534d0ccaaea94c199c82b2db779 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 12:35:07 -0500 Subject: [PATCH 08/10] tests: fdt_harness: keep fdt_addresses.c out of LIBFDT_SOURCES test_wdt_arm.py (merged from main) appends fdt_addresses.c to LIBFDT_SOURCES, so including fdt_addresses.c in LIBFDT_SOURCES linked it twice and failed on macOS ld (and Linux ld) with duplicate symbols (_fdt_address_cells, _fdt_size_cells, _fdt_appendprop_addrrange). Keep LIBFDT_SOURCES unchanged for external callers and link fdt_addresses.c inside fdt_harness.run() instead. Signed-off-by: Erik Bourget --- tests/python/fdt_harness.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/fdt_harness.py b/tests/python/fdt_harness.py index 62a7af6ec..21459e163 100644 --- a/tests/python/fdt_harness.py +++ b/tests/python/fdt_harness.py @@ -8,7 +8,7 @@ REPO = Path(__file__).resolve().parents[2] LIBFDT = REPO / "src/libfdt" LIBFDT_SOURCES = ("fdt.c", "fdt_ro.c", "fdt_rw.c", "fdt_wip.c", "fdt_sw.c", "fdt_strerror.c", - "fdt_empty_tree.c", "fdt_addresses.c") + "fdt_empty_tree.c") def definition(source, signature): @@ -23,7 +23,7 @@ def run(tmp_path, name, code, expect): command = host_cc() + ["-std=gnu11", "-O1", "-g", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-fsanitize=undefined", "-fno-sanitize-recover=all", "-I", str(LIBFDT), str(path)] - command += [str(LIBFDT / f) for f in LIBFDT_SOURCES] + ["-o", str(binary)] + command += [str(LIBFDT / f) for f in (*LIBFDT_SOURCES, "fdt_addresses.c")] + ["-o", str(binary)] built = subprocess.run(command, capture_output=True, text=True) assert built.returncode == 0, built.stdout + built.stderr result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=30) From 1ddba986798f8a3a93695e92fbd97404b86e440f Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 12:48:45 -0500 Subject: [PATCH 09/10] kboot: T6050: verify live DART roots on pre-enabled external engines t6050_check_ext_adt() only checked the ADT's l1-tt values and pt-region placement, so a pre-enabled apple,t6050-dcpext engine whose live DART root differed from the ADT (for instance pointing into OS RAM) could still retain its scanout DART in t6050_publish_prewired() and receive firmware version properties in t6050_ext_version(). Extract the guarded live power, CPU run, and DART lock/TTBR checks from t6050_ext_one() into t6050_check_ext_live() and call it from both t6050_ext_one() and t6050_check_ext_adt() so scanout retention, rejection, and version updates verify the live registers match the validated ADT roots. Supply external power and DART register state in the host fixtures and cover live roots that differ from the ADT. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 97 ++++++--- tests/python/test_t6050_handoff_bounds.py | 46 ++++- tests/python/test_t6050_tree_shape.py | 237 +++++++++++++++++++++- 3 files changed, 334 insertions(+), 46 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 94fd36f64..f66593c2f 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -3914,15 +3914,65 @@ static T6050_TEXT int t6050_check_placement(const char *nub_path, const char *co } /* - * External engine `n` in the ADT: its nub must report running == 1 and a valid UUID (matching the - * pinned image when one has been selected), and its firmware segments and both DART page-table - * regions (dart-dcpextN SID 23, dart-dispextN SID 16) must lie outside the OS's RAM. + * External engine `n` is live in hardware: its power words (and its DARTs' domains) are active, + * its ASC CPU is running, and both DART streams (SID 23 on dart-dcpextN, SID 16 on dart-dispextN) + * are locked in four-level translate mode on the ADT's l1-tt roots. + */ +static T6050_TEXT int t6050_check_ext_live(int n, u64 coproc, const u64 base[2]) +{ + char a[24]; + u64 ps[12]; + u32 pval[12]; + + snprintf(a, sizeof(a), "/arm-io/dcpext%d", n); + int nps = pmgr_adt_collect_psregs(a, ps, NULL, ARRAY_SIZE(ps)); + if (nps <= 0) + bail("dcpext%d: no power words\n", n); + int before = dcp6_guard_begin(); + for (int i = 0; i < nps; i++) + pval[i] = read32(ps[i]); + if (dcp6_guard_end(before)) + bail("dcpext%d: reading power words faulted\n", n); + for (int i = 0; i < nps; i++) + if (!dcp6_ps_on(pval[i])) + bail("dcpext%d: not powered: 0x%lx = 0x%x\n", n, ps[i], pval[i]); + + before = dcp6_guard_begin(); + u32 bad = read32(coproc + ASC_CPU_CONTROL) & ASC_CPU_CONTROL_RUN ? 0 : BIT(2); + for (int i = 0; i < 2; i++) { + u32 sid = i ? 16 : 23; + char d[32], t[16]; + u64 l1, cnt; + snprintf(d, sizeof(d), i ? "/arm-io/dart-dispext%d" : "/arm-io/dart-dcpext%d", n); + snprintf(t, sizeof(t), "l1-tt-%u", sid); + if (dcp6_adt_u64_pair(adt_path_offset(adt, d), t, &l1, &cnt) || !l1) { + bad |= BIT(i); + continue; + } + u32 tcr = read32(base[i] + DART_T8110_TCR(sid)), + ttbr = read32(base[i] + DART_T8110_TTBR(sid)); + if (!(read32(base[i] + DART_T8110_PROTECT) & DART_T8110_PROTECT_TTBR_TCR) || + (tcr & T6050_TCR_MASK) != T6050_TCR_WANT || !(ttbr & DART_T8110_TTBR_VALID) || + ((u64)FIELD_GET(DART_T8110_TTBR_ADDR, ttbr) << DART_T8110_TTBR_SHIFT) != l1) + bad |= BIT(i); + } + if (dcp6_guard_end(before) || bad) + bail("dcpext%d: not live (mask 0x%x: bit 2 stopped, 0-1 a stream not locked)\n", n, bad); + return 0; +} + +/* + * External engine `n` in the ADT and hardware: its nub must report running == 1 and a valid UUID + * (matching the pinned image when one has been selected), its firmware segments and both DART + * page-table regions (dart-dcpextN SID 23, dart-dispextN SID 16) must lie outside the OS's RAM, + * and it must be powered and locked on those ADT roots. */ static T6050_TEXT int t6050_check_ext_adt(int n) { - char nub_path[64], dd[2][32]; + char nub_path[64], eng_path[24], dd[2][32]; const char *darts[2] = {dd[0], dd[1]}; static const u8 sids[2] = {23, 16}; + u64 coproc, base[2]; u32 len = 0, val = 0; if (n < 0 || n >= T6050_ENGINES) @@ -3933,9 +3983,15 @@ static T6050_TEXT int t6050_check_ext_adt(int n) if (!dcp6_is_uuid(uuid, len) || (t6050.uuid && strcmp(uuid, t6050.uuid)) || dcp6_adt_u32(nub, "running", &val) || val != 1) bail("dcpext%d: not running the pinned image\n", n); + snprintf(eng_path, sizeof(eng_path), "/arm-io/dcpext%d", n); snprintf(dd[0], sizeof(dd[0]), "/arm-io/dart-dcpext%d", n); snprintf(dd[1], sizeof(dd[1]), "/arm-io/dart-dispext%d", n); - return t6050_check_placement(nub_path, darts, sids, 2); + if (t6050_check_placement(nub_path, darts, sids, 2)) + return -1; + if (t6050_adt_reg(eng_path, 0, &coproc, NULL) || t6050_adt_reg(dd[0], 0, &base[0], NULL) || + t6050_adt_reg(dd[1], 0, &base[1], NULL)) + bail("dcpext%d: no ADT reg\n", n); + return t6050_check_ext_live(n, coproc, base); } /* @@ -4301,7 +4357,7 @@ static T6050_TEXT int t6050_ext_node(void *dt, u64 coproc) static T6050_TEXT int t6050_ext_one(void *dt, int n) { char a[40], b[40]; - u64 coproc, base[2], l1, cnt, vm_base, vm_size; + u64 coproc, base[2], vm_base, vm_size; u32 len = 0, val = 0; int node = -1, dart[2]; @@ -4340,33 +4396,8 @@ static T6050_TEXT int t6050_ext_one(void *dt, int n) * The engine's power words and their parents' (its DARTs' domains). dispextN has no ADT * gates, and iBoot leaves the CPU idling under AUTO_ENABLE (DISPEXTn_CPU 0x1404024f). */ - u64 ps[12]; - int nps = pmgr_adt_collect_psregs(a, ps, NULL, ARRAY_SIZE(ps)); - if (nps <= 0) - bail("dcpext%d: no power words\n", n); - for (int i = 0; i < nps; i++) - if (!dcp6_ps_on(read32(ps[i]))) - bail("dcpext%d: not powered: 0x%lx = 0x%x\n", n, ps[i], read32(ps[i])); - int before = dcp6_guard_begin(); - u32 bad = read32(coproc + ASC_CPU_CONTROL) & ASC_CPU_CONTROL_RUN ? 0 : BIT(2); - for (int i = 0; i < 2; i++) { - u32 sid = i ? 16 : 23; - char d[32], t[16]; - snprintf(d, sizeof(d), i ? "/arm-io/dart-dispext%d" : "/arm-io/dart-dcpext%d", n); - snprintf(t, sizeof(t), "l1-tt-%u", sid); - if (dcp6_adt_u64_pair(adt_path_offset(adt, d), t, &l1, &cnt) || !l1) { - bad |= BIT(i); - continue; - } - u32 tcr = read32(base[i] + DART_T8110_TCR(sid)), - ttbr = read32(base[i] + DART_T8110_TTBR(sid)); - if (!(read32(base[i] + DART_T8110_PROTECT) & DART_T8110_PROTECT_TTBR_TCR) || - (tcr & T6050_TCR_MASK) != T6050_TCR_WANT || !(ttbr & DART_T8110_TTBR_VALID) || - ((u64)FIELD_GET(DART_T8110_TTBR_ADDR, ttbr) << DART_T8110_TTBR_SHIFT) != l1) - bad |= BIT(i); - } - if (dcp6_guard_end(before) || bad) - bail("dcpext%d: not live (mask 0x%x: bit 2 stopped, 0-1 a stream not locked)\n", n, bad); + if (t6050_check_ext_live(n, coproc, base)) + return -1; /* Its bandwidth agent: the one dispextN's bw_req interrupt names */ snprintf(b, sizeof(b), "/arm-io/dispext%d", n); diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index 43cbd7822..aed6ea3ff 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -236,11 +236,18 @@ static struct { u64 addr; u32 val; -} words[32]; +} words[64]; static int nwords; static void word(u64 addr, u32 val) { + for (int i = 0; i < nwords; i++) + if (words[i].addr == addr) { + words[i].val = val; + return; + } + if (nwords == (int)ARRAY_SIZE(words)) + DIE("too many MMIO words\n"); words[nwords].addr = addr; words[nwords++].val = val; } @@ -282,9 +289,10 @@ int pmgr_adt_collect_psregs(const char *path, u64 *out, u8 *flags, int max) { UNUSED(flags); - if (strcmp(path, "/arm-io/dcpext0") || max < 1) + if (strncmp(path, "/arm-io/dcpext", 14) || path[14] < '0' || path[14] > '7' || path[15] || + max < 1) return -1; - out[0] = PS_WORD; + out[0] = PS_WORD + 4 * (u64)(path[14] - '0'); return 1; } @@ -700,6 +708,24 @@ u64 pt16_lo = (e == 1 && fail == 4) ? ram + 0x200000 : l1 + 0x100000; prop_u64s(ddispp, "l1-tt-16", pt16_lo, 1, 2); prop_u64s(ddispp, "pt-region-16", pt16_lo, pt16_lo + 0x40000, 2); + u64 live_23 = (e == 1 && fail == 6) ? ram + 0x300000 : l1; + u64 live_16 = (e == 1 && fail == 5) ? ram + 0x200000 : pt16_lo; + word(PS_WORD + 4 * (u64)e, + (e == 1 && fail == 7) + ? 0 + : (FIELD_PREP(PMGR_PS_ACTUAL, PMGR_PS_ACTIVE) | + FIELD_PREP(PMGR_PS_TARGET, PMGR_PS_ACTIVE))); + word(ext_coproc[e] + ASC_CPU_CONTROL, (e == 1 && fail == 8) ? 0 : ASC_CPU_CONTROL_RUN); + word(ext_ddcp[e] + DART_T8110_PROTECT, DART_T8110_PROTECT_TTBR_TCR); + word(ext_ddcp[e] + DART_T8110_TCR(23), T6050_TCR_WANT); + word(ext_ddcp[e] + DART_T8110_TTBR(23), + DART_T8110_TTBR_VALID | + FIELD_PREP(DART_T8110_TTBR_ADDR, live_23 >> DART_T8110_TTBR_SHIFT)); + word(ext_ddisp[e] + DART_T8110_PROTECT, DART_T8110_PROTECT_TTBR_TCR); + word(ext_ddisp[e] + DART_T8110_TCR(16), T6050_TCR_WANT); + word(ext_ddisp[e] + DART_T8110_TTBR(16), + DART_T8110_TTBR_VALID | + FIELD_PREP(DART_T8110_TTBR_ADDR, live_16 >> DART_T8110_TTBR_SHIFT)); } enum { PH_CLK = 10, PH_EXT0_DISP, PH_EXT1_DISP }; @@ -1165,7 +1191,7 @@ def test_headless_duplicate_ext_node_refused(host, no_pt_region): assert not r.said("handed off") -@pytest.mark.parametrize("fail", [0, 1, 2, 3, 4]) +@pytest.mark.parametrize("fail", [0, 1, 2, 3, 4, 5, 6, 7, 8]) def test_prewired_panel_keeps_rejected_external_engines_out_of_handoff(host, fail): r = run(host, "prewired", fail=fail, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) @@ -1184,7 +1210,17 @@ def test_prewired_panel_keeps_rejected_external_engines_out_of_handoff(host, fai def test_prewired_panel_rejects_external_engines_on_unknown_stub_and_packed_dt(host): # Unknown stub: dt_set_display_handoff() refuses the internal panel, keeps healthy ext0 at - # the tree's version, and still disables stopped ext1 and removes its display DART. + # the tree's version, and still disables ext1 whose live DART root differs from the ADT (and + # removes its display DART). + r = run(host, "prewired", unknown=1, fail=5, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, + **framebuffer(VRAM, VRAM_SIZE)) + assert r.state["chosen"] == "refused", r.log + assert r.state["dcp"] == "disabled" and r.state["subsystem"] == "disabled" + assert r.state["ext0"] == "okay" and r.state["ext0_fw"] == "27.0.0" + assert r.state["ext1"] == "disabled" + assert r.state["ext1_fw"] == "27.0.0" and r.state["ext1_ver"] == "27.0.0" + assert r.iommus == [2, 0, 11, 0] + r = run(host, "prewired", unknown=1, fail=1, meta=META_SIZE, vram=VRAM, vsize=VRAM_SIZE, **framebuffer(VRAM, VRAM_SIZE)) assert r.state["chosen"] == "refused", r.log diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index a14156fee..e3f0a0583 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -258,6 +258,33 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): #define DART_PAGE 0x4000UL #define ALIGN_DOWN(x, a) ((x) & ~((u64)(a) - 1)) #define ALIGN_UP(x, a) ALIGN_DOWN((x) + (a) - 1, a) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define BIT(x) (1UL << (x)) +#define GENMASK(msb, lsb) ((~0UL << (lsb)) & (~0UL >> (63 - (msb)))) +#define FIELD_GET(mask, val) (((val) & (mask)) >> __builtin_ctzll(mask)) +#define FIELD_PREP(mask, val) (((u64)(val) << __builtin_ctzll(mask)) & (mask)) +#define sysop(x) do { } while (0) +enum exc_guard_t { GUARD_OFF = 0, GUARD_MARK = 2, GUARD_SILENT = 0x100 }; +static volatile enum exc_guard_t exc_guard; +static volatile int exc_count; +#define ASC_CPU_CONTROL 0x44 +#define ASC_CPU_CONTROL_RUN BIT(4) +#define PMGR_AUTO_ENABLE BIT(28) +#define PMGR_PS_ACTUAL GENMASK(7, 4) +#define PMGR_PS_TARGET GENMASK(3, 0) +#define PMGR_PS_ACTIVE 0xf +#define DART_T8110_PROTECT 0x200 +#define DART_T8110_PROTECT_TTBR_TCR BIT(0) +#define DART_T8110_TCR(sid) (0x1000 + 4 * (sid)) +#define DART_T8110_TCR_TRANSLATE BIT(0) +#define DART_T8110_TCR_BYPASS_DART BIT(1) +#define DART_T8110_TCR_FOUR_LEVEL BIT(3) +#define DART_T8110_TTBR(sid) (0x1400 + 4 * (sid)) +#define DART_T8110_TTBR_VALID BIT(0) +#define DART_T8110_TTBR_ADDR GENMASK(29, 2) +#define DART_T8110_TTBR_SHIFT 14 +#define T6050_TCR_WANT (DART_T8110_TCR_TRANSLATE | DART_T8110_TCR_FOUR_LEVEL) +#define T6050_TCR_MASK (T6050_TCR_WANT | DART_T8110_TCR_BYPASS_DART) #define dlog(fmt, ...) printf(fmt, ##__VA_ARGS__) ''' + layout + r''' static struct { u64 phys_base, mem_size; } cur_boot_args = {0x10000000000UL, 0x200000000UL}; @@ -266,7 +293,9 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): #define DART_DCP 0x48a340000UL #define DART_DISP0 0x48a300000UL #define EXT0 0x2b2e00000UL +#define DART_DCPEXT0 0x2b2400000UL #define DART_DISPEXT0 0x2b2300000UL +#define PS_WORD 0x290000100UL #define UUID_A "C51FB0C8-C45D-39B0-85BC-520E600FC950" #define UUID_B "64863924-B56B-3E32-87FE-038677F52709" static struct adt_segment_ranges ext0_segs[2]; @@ -274,6 +303,13 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): static u32 ext0_running; static const char *ext0_uuid; static u64 pt23[2], l1_23[2], pt16[2], l1_16[2]; +static u32 ext0_ps, ext0_ctl, ext0_prot[2], ext0_tcr[2], ext0_ttbr[2]; +static bool ext0_fault; +static u32 make_ttbr(u64 l1, bool valid) +{ + return (valid ? DART_T8110_TTBR_VALID : 0) | + FIELD_PREP(DART_T8110_TTBR_ADDR, l1 >> DART_T8110_TTBR_SHIFT); +} static void reset_adt(void) { t6050.uuid = UUID_A; @@ -286,6 +322,38 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): l1_23[0] = 0x10201000000UL; l1_23[1] = 1; pt16[0] = 0x10201100000UL; pt16[1] = 0x10201140000UL; l1_16[0] = 0x10201100000UL; l1_16[1] = 1; + ext0_ps = FIELD_PREP(PMGR_PS_ACTUAL, PMGR_PS_ACTIVE) | FIELD_PREP(PMGR_PS_TARGET, PMGR_PS_ACTIVE); + ext0_ctl = ASC_CPU_CONTROL_RUN; + ext0_prot[0] = ext0_prot[1] = DART_T8110_PROTECT_TTBR_TCR; + ext0_tcr[0] = ext0_tcr[1] = T6050_TCR_WANT; + ext0_ttbr[0] = make_ttbr(l1_23[0], true); + ext0_ttbr[1] = make_ttbr(l1_16[0], true); + ext0_fault = false; +} +static int pmgr_adt_collect_psregs(const char *path, u64 *out, u8 *flags, int max) +{ + (void)flags; + if (strcmp(path, "/arm-io/dcpext0") || max < 1) + return -1; + out[0] = PS_WORD; + return 1; +} +static u32 read32(u64 addr) +{ + if (ext0_fault) { + exc_count++; + return 0; + } + if (addr == PS_WORD) return ext0_ps; + if (addr == EXT0 + ASC_CPU_CONTROL) return ext0_ctl; + if (addr == DART_DCPEXT0 + DART_T8110_PROTECT) return ext0_prot[0]; + if (addr == DART_DCPEXT0 + DART_T8110_TCR(23)) return ext0_tcr[0]; + if (addr == DART_DCPEXT0 + DART_T8110_TTBR(23)) return ext0_ttbr[0]; + if (addr == DART_DISPEXT0 + DART_T8110_PROTECT) return ext0_prot[1]; + if (addr == DART_DISPEXT0 + DART_T8110_TCR(16)) return ext0_tcr[1]; + if (addr == DART_DISPEXT0 + DART_T8110_TTBR(16)) return ext0_ttbr[1]; + assert(0 && "unexpected read32"); + return 0; } enum { ADT_NUB = 1, ADT_DDCP, ADT_DDISP }; static int adt_path_offset(const void *a, const char *path) @@ -333,18 +401,22 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): } return NULL; } -/* The ADT: the internal DARTs, dcpext0 and its display DART; nothing else */ +/* The ADT: the internal DARTs, dcpext0 and its DARTs; nothing else */ static int t6050_adt_reg(const char *path, int idx, u64 *addr, u64 *size) { (void)idx; (void)size; if (!strcmp(path, "/arm-io/dart-dcp")) *addr = DART_DCP; else if (!strcmp(path, "/arm-io/dart-disp0")) *addr = DART_DISP0; else if (!strcmp(path, "/arm-io/dcpext0")) *addr = EXT0; + else if (!strcmp(path, "/arm-io/dart-dcpext0")) *addr = DART_DCPEXT0; else if (!strcmp(path, "/arm-io/dart-dispext0")) *addr = DART_DISPEXT0; else return -1; return 0; } -''' + definition(source, "static bool dcp6_overlaps(") + \ +''' + definition(source, "static int dcp6_guard_begin(") + \ + definition(source, "static int dcp6_guard_end(") + \ + definition(source, "static bool dcp6_ps_on(") + \ + definition(source, "static bool dcp6_overlaps(") + \ definition(source, "static int dcp6_adt_u32(") + \ definition(source, "static int dcp6_adt_u64_pair(") + \ definition(source, "static bool dcp6_is_uuid(") + \ @@ -352,6 +424,7 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): definition(source, "static T6050_TEXT bool t6050_available(") + \ definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ definition(source, "static T6050_TEXT int t6050_check_placement(") + \ + definition(source, "static T6050_TEXT int t6050_check_ext_live(") + \ definition(source, "static T6050_TEXT int t6050_check_ext_adt(") + \ definition(source, "static T6050_TEXT bool t6050_ext_dart_engine(") + \ definition(source, "static T6050_TEXT bool t6050_ext_display_dart(") + r''' @@ -436,6 +509,32 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): world(PH_EXT, 16, "okay", 1, "okay"); ext0_uuid = UUID_B; assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + /* Stale ADT passes placement checks while live DART root differs (e.g. points into OS RAM). */ + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_ttbr[1] = make_ttbr(cur_boot_args.phys_base + 0x300000, true); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_ttbr[0] = make_ttbr(cur_boot_args.phys_base + 0x200000, true); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + /* Live DART unlocked, bypass mode, invalid TTBR, CPU stopped, unpowered, or faulted MMIO. */ + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_prot[1] = 0; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_tcr[1] |= DART_T8110_TCR_BYPASS_DART; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_ttbr[1] = make_ttbr(l1_16[0], false); + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_ctl = 0; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_ps = 0; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; + world(PH_EXT, 16, "okay", 1, "okay"); + ext0_fault = true; + assert(!t6050_ext_display_dart(tree, PH_EXT)); cases++; /* status as Linux reads it: absent, or first string "okay"/"ok", regardless of trailing bytes */ fresh(); int n = node_at("/", "n"); @@ -452,7 +551,7 @@ def test_t6050_external_scanout_darts_resolved(tmp_path): return 0; } ''' - run(tmp_path, "ext-darts", code, "PASS external scanout darts 25 cases") + run(tmp_path, "ext-darts", code, "PASS external scanout darts 33 cases") def test_t6050_rx_clock_groups_pass_whole(tmp_path): @@ -625,6 +724,33 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): #define ALIGN_DOWN(x, a) ((x) & ~((u64)(a) - 1)) #define ALIGN_UP(x, a) ALIGN_DOWN((x) + (a) - 1, a) #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define BIT(x) (1UL << (x)) +#define GENMASK(msb, lsb) ((~0UL << (lsb)) & (~0UL >> (63 - (msb)))) +#define FIELD_GET(mask, val) (((val) & (mask)) >> __builtin_ctzll(mask)) +#define FIELD_PREP(mask, val) (((u64)(val) << __builtin_ctzll(mask)) & (mask)) +#define sysop(x) do { } while (0) +enum exc_guard_t { GUARD_OFF = 0, GUARD_MARK = 2, GUARD_SILENT = 0x100 }; +static volatile enum exc_guard_t exc_guard; +static volatile int exc_count; +#define ASC_CPU_CONTROL 0x44 +#define ASC_CPU_CONTROL_RUN BIT(4) +#define PMGR_AUTO_ENABLE BIT(28) +#define PMGR_PS_ACTUAL GENMASK(7, 4) +#define PMGR_PS_TARGET GENMASK(3, 0) +#define PMGR_PS_ACTIVE 0xf +#define DART_T8110_PROTECT 0x200 +#define DART_T8110_PROTECT_TTBR_TCR BIT(0) +#define DART_T8110_TCR(sid) (0x1000 + 4 * (sid)) +#define DART_T8110_TCR_TRANSLATE BIT(0) +#define DART_T8110_TCR_BYPASS_DART BIT(1) +#define DART_T8110_TCR_FOUR_LEVEL BIT(3) +#define DART_T8110_TTBR(sid) (0x1400 + 4 * (sid)) +#define DART_T8110_TTBR_VALID BIT(0) +#define DART_T8110_TTBR_ADDR GENMASK(29, 2) +#define DART_T8110_TTBR_SHIFT 14 +#define T6050_TCR_WANT (DART_T8110_TCR_TRANSLATE | DART_T8110_TCR_FOUR_LEVEL) +#define T6050_TCR_MASK (T6050_TCR_WANT | DART_T8110_TCR_BYPASS_DART) +#define PS_BASE 0x290000100UL #define OS_VER_COMP 4 enum fw_version { V_UNKNOWN = 0, V27_0, V27_2, V27_3 }; struct fw_version_info { @@ -647,20 +773,27 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): #define DART_DISP0 0x48a300000UL struct fake_ext { u64 coproc; + u64 dart_dcp; u64 dart_disp; u32 running; const char *uuid; struct adt_segment_ranges seg[2]; u32 nseg; u64 pt23[2], l1_23[2], pt16[2], l1_16[2]; + u32 ps, ctl, prot[2], tcr[2]; + u64 ttbr_l1[2]; + bool ttbr_valid[2], fault; }; static struct fake_ext exts[3]; static void reset_adt(void) { t6050.uuid = UUID_A; for (int i = 0; i < 3; i++) { + u64 l1_dcp = 0x10201000000UL + (u64)i * 0x1000000UL; + u64 l1_disp = 0x10201100000UL + (u64)i * 0x1000000UL; exts[i] = (struct fake_ext){ .coproc = 0x2b2e00000UL + (u64)i * 0x1000000UL, + .dart_dcp = 0x2b2400000UL + (u64)i * 0x1000000UL, .dart_disp = 0x2b2300000UL + (u64)i * 0x1000000UL, .running = 1, .uuid = UUID_A, @@ -671,13 +804,58 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): 0x10004500000UL + (u64)i * 0x4000UL, 0x4000, SEG_FLAGS_OSLOG}, }, .nseg = 2, - .pt23 = {0x10201000000UL, 0x10201040000UL}, - .l1_23 = {0x10201000000UL, 1}, - .pt16 = {0x10201100000UL, 0x10201140000UL}, - .l1_16 = {0x10201100000UL, 1}, + .pt23 = {l1_dcp, l1_dcp + 0x40000}, + .l1_23 = {l1_dcp, 1}, + .pt16 = {l1_disp, l1_disp + 0x40000}, + .l1_16 = {l1_disp, 1}, + .ps = FIELD_PREP(PMGR_PS_ACTUAL, PMGR_PS_ACTIVE) | + FIELD_PREP(PMGR_PS_TARGET, PMGR_PS_ACTIVE), + .ctl = ASC_CPU_CONTROL_RUN, + .prot = {DART_T8110_PROTECT_TTBR_TCR, DART_T8110_PROTECT_TTBR_TCR}, + .tcr = {T6050_TCR_WANT, T6050_TCR_WANT}, + .ttbr_l1 = {l1_dcp, l1_disp}, + .ttbr_valid = {true, true}, }; } } +static int pmgr_adt_collect_psregs(const char *path, u64 *out, u8 *flags, int max) +{ + (void)flags; + for (int i = 0; i < 3; i++) { + char a[32]; + snprintf(a, sizeof(a), "/arm-io/dcpext%d", i); + if (!strcmp(path, a) && max >= 1) { + out[0] = PS_BASE + (u64)i * 4; + return 1; + } + } + return -1; +} +static u32 read32(u64 addr) +{ + for (int i = 0; i < 3; i++) { + if (addr == PS_BASE + (u64)i * 4) { + if (exts[i].fault) { exc_count++; return 0; } + return exts[i].ps; + } + if (addr == exts[i].coproc + ASC_CPU_CONTROL) { + if (exts[i].fault) { exc_count++; return 0; } + return exts[i].ctl; + } + for (int d = 0; d < 2; d++) { + u64 base = d ? exts[i].dart_disp : exts[i].dart_dcp; + u32 sid = d ? 16 : 23; + if (addr == base + DART_T8110_PROTECT) return exts[i].prot[d]; + if (addr == base + DART_T8110_TCR(sid)) return exts[i].tcr[d]; + if (addr == base + DART_T8110_TTBR(sid)) + return (exts[i].ttbr_valid[d] ? DART_T8110_TTBR_VALID : 0) | + FIELD_PREP(DART_T8110_TTBR_ADDR, + exts[i].ttbr_l1[d] >> DART_T8110_TTBR_SHIFT); + } + } + assert(0 && "unexpected read32"); + return 0; +} static int kboot_fdt_path_exact(void *t, const char *path) { return fdt_path_offset(t, path); @@ -696,6 +874,11 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): *addr = exts[i].coproc; return 0; } + snprintf(a, sizeof(a), "/arm-io/dart-dcpext%d", i); + if (!strcmp(path, a)) { + *addr = exts[i].dart_dcp; + return 0; + } snprintf(a, sizeof(a), "/arm-io/dart-dispext%d", i); if (!strcmp(path, a)) { *addr = exts[i].dart_disp; @@ -759,6 +942,9 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): return NULL; } ''' + definition(firmware, "int firmware_set_fdt(") + \ + definition(source, "static int dcp6_guard_begin(") + \ + definition(source, "static int dcp6_guard_end(") + \ + definition(source, "static bool dcp6_ps_on(") + \ definition(source, "static bool dcp6_overlaps(") + \ definition(source, "static int dcp6_adt_u32(") + \ definition(source, "static int dcp6_adt_u64_pair(") + \ @@ -775,6 +961,7 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): definition(source, "static T6050_TEXT bool t6050_available(") + \ definition(source, "static T6050_INIT bool t6050_identity_reg(") + \ definition(source, "static T6050_TEXT int t6050_check_placement(") + \ + definition(source, "static T6050_TEXT int t6050_check_ext_live(") + \ definition(source, "static T6050_TEXT int t6050_check_ext_adt(") + \ definition(source, "static T6050_TEXT int t6050_node_at(") + \ definition(source, "static T6050_TEXT int t6050_ext_index(") + \ @@ -1227,8 +1414,42 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): assert_rejected("/dcpext1", PH_DART1, &old); assert(is_disabled("/dcpext1-dup") && is("/dcpext1-dup", "apple,firmware-compat", &old)); cases++; + /* Stale ADT passes placement checks while live DART root differs (points into OS RAM): + * disabled and dropped from scanout without version-stamping. */ + world(); + os_firmware = v272; + exts[1].ttbr_l1[1] = cur_boot_args.phys_base + 0x300000; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + exts[1].ttbr_l1[0] = cur_boot_args.phys_base + 0x200000; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* Live DART unlocked, CPU stopped, unpowered, or faulted MMIO: disabled and dropped. */ + world(); + exts[1].prot[1] = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + exts[1].ctl = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + exts[1].ps = 0; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + exts[1].fault = true; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; printf("PASS dcpext version %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 26 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 32 cases") From e6a3bcb13fca056fd90a72685e88f24800ab92b4 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 13:16:03 -0500 Subject: [PATCH 10/10] kboot: T6050: disable external engine if live check fails during versioning If an input-enabled apple,t6050-dcpext engine passes t6050_ext_reject() but fails t6050_check_ext_adt() (for example its live power, CPU run, or DART state changes before t6050_ext_version() stamps it), the versioning loop previously skipped it and left it enabled with its original firmware properties and display-subsystem scanout entry. Disable and strip scanout via t6050_ext_disable() when t6050_check_ext_adt() fails in t6050_ext_version(), and only mark the engine done once its firmware properties update cleanly so a rescan after enclosing display-subsystem deletion cannot skip it. Add host tests covering an engine that passes rejection and stops running before versioning on both normal and packed device trees. Signed-off-by: Erik Bourget --- src/kboot_display_handoff.c | 25 ++++++++++++++++--------- tests/python/test_t6050_tree_shape.py | 23 ++++++++++++++++++++++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index f66593c2f..1878a0080 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -4775,9 +4775,10 @@ static T6050_TEXT int t6050_ext_version_one(void *dt, int at) * engine with no valid ADT match or failing the ADT running/uuid or placement checks is disabled * and stripped from any display-subsystem scanout iommus first by t6050_ext_reject(), even on an * unknown stub. A node not available (status absent, "okay" or "ok") is t6050_ext_one()'s. - * Each surviving engine's version edit runs in its own scratch transaction; 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. + * 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. */ static T6050_TEXT void t6050_ext_version(void *dt) { @@ -4796,17 +4797,23 @@ static T6050_TEXT void t6050_ext_version(void *dt) if (!t6050_available(dt, at)) continue; int n = t6050_ext_index(dt, at, &unique); - if (n < 0 || !unique || ((t6050.ext_aura | done) & (1u << n)) || t6050_check_ext_adt(n)) + if (n < 0 || !unique || ((t6050.ext_aura | done) & (1u << n))) continue; - done |= 1u << n; - if (t6050_ext_version_one(dt, at)) { + const char *why = NULL; + if (t6050_check_ext_adt(n)) { + why = "rejected"; + } else if (t6050_ext_version_one(dt, at)) { + why = "could not take firmware version"; + } else { + done |= 1u << n; + count++; + } + if (why) { bool dummy = false; - if (t6050_ext_disable(dt, at, n, &dummy, "could not take firmware version") < 0) { + if (t6050_ext_disable(dt, at, n, &dummy, why) < 0) { err = -1; break; } - } else { - count++; } at = -1; } diff --git a/tests/python/test_t6050_tree_shape.py b/tests/python/test_t6050_tree_shape.py index e3f0a0583..720683924 100644 --- a/tests/python/test_t6050_tree_shape.py +++ b/tests/python/test_t6050_tree_shape.py @@ -783,6 +783,7 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): u32 ps, ctl, prot[2], tcr[2]; u64 ttbr_l1[2]; bool ttbr_valid[2], fault; + int ctl_fail_after; }; static struct fake_ext exts[3]; static void reset_adt(void) @@ -840,6 +841,8 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): } if (addr == exts[i].coproc + ASC_CPU_CONTROL) { if (exts[i].fault) { exc_count++; return 0; } + if (exts[i].ctl_fail_after > 0 && --exts[i].ctl_fail_after == 0) + exts[i].ctl = 0; return exts[i].ctl; } for (int d = 0; d < 2; d++) { @@ -1448,8 +1451,26 @@ def test_t6050_external_engines_get_the_stub_version(tmp_path): t6050_ext_version(tree); assert(is("/dcpext0", "apple,firmware-compat", &v272)); assert_rejected("/dcpext1", PH_DART1, &old); cases++; + /* Passes during t6050_ext_reject(), then stops running before t6050_ext_version() stamps it: + * still disabled (or deleted on a packed DT with absent status) and dropped from scanout. */ + world(); + os_firmware = v272; + exts[1].ctl_fail_after = 2; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert_rejected("/dcpext1", PH_DART1, &old); cases++; + world(); + os_firmware = v272; + assert(!fdt_delprop(tree, fdt_path_offset(tree, "/dcpext1"), "status")); + assert(!fdt_pack(tree)); + exts[1].ctl_fail_after = 2; + t6050_ext_version(tree); + assert(is("/dcpext0", "apple,firmware-compat", &v272)); + assert(fdt_path_offset(tree, "/dcpext1") < 0 && has_scanout_dart(PH_DISP0) && + has_scanout_dart(PH_DART0) && !has_scanout_dart(PH_DART1)); + cases++; printf("PASS dcpext version %d cases\n", cases); return 0; } ''' - run(tmp_path, "ext-version", code, "PASS dcpext version 32 cases") + run(tmp_path, "ext-version", code, "PASS dcpext version 34 cases")