diff --git a/src/kboot_atc.c b/src/kboot_atc.c index 5a00e2d5d..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}, @@ -1502,8 +1507,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); @@ -1579,7 +1588,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 d565321c2..5d2b413e5 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 @@ -2176,14 +2179,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); @@ -2211,6 +2212,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) { @@ -3492,15 +3502,33 @@ 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) -/* 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 */ -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]; @@ -3508,8 +3536,10 @@ 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() */ } t6050; /* The dcp's reg entries and the ADT reg each equals (coproc: a prefix of it) */ @@ -3573,6 +3603,44 @@ static T6050_TEXT u32 t6050_add_node(void *dt, const char *parent, const char *n return ph; } +/* + * 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) +{ + 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) { + if (match >= 0) + return -1; + match = o; + } + } + return o == -FDT_ERR_NOTFOUND ? match : -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 @@ -3685,12 +3753,132 @@ 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, or first string "okay" or "ok" */ +static T6050_TEXT bool t6050_available(void *dt, int node) +{ + int 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)); +} + +/* + * 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; @@ -3698,16 +3886,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; @@ -3733,6 +3926,201 @@ static T6050_INIT int t6050_check_dt(void *dt) return 0; } +/* + * 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` 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], 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) + 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(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); + 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); +} + +/* + * 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_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); + 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 < 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; + 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); + 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_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) +{ + 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_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); + } + 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) { @@ -3747,10 +4135,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) @@ -3775,20 +4187,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; @@ -3824,6 +4229,62 @@ 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, 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) +{ + 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; + } + 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")); +} + +/* 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")); @@ -3937,14 +4398,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); } /* @@ -3958,7 +4412,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]; @@ -3972,6 +4426,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); @@ -3993,33 +4451,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); @@ -4110,25 +4543,348 @@ 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); + else + t6050.ext_aura |= 1u << n; + } +} + +/* + * 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, 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 (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; + 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 (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 + * 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; + 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; + } + 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; + } + if (!scratch && !in_place) { + scratch = dcp6_scratch(dt); + if (scratch) + cur = scratch; + else + in_place = true; + } + if (t6050_ext_disable(cur, at, n, &changed, + n < 0 ? "has no matching ADT dcpextN" : "rejected") < 0) { + err = -1; + break; } + 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); +} + +/* + * 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 + * 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. 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 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) +{ + u32 done = 0; + int count = 0, err = 0, at = -1; + + 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; + } + 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(dt, at, &unique); + if (n < 0 || !unique || ((t6050.ext_aura | done) & (1u << n))) + continue; + 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, why) < 0) { + err = -1; + break; + } + } + at = -1; + } + /* Only -FDT_ERR_NOTFOUND is a walk's normal end */ + 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 */ -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)) { @@ -4151,8 +4907,6 @@ static T6050_TEXT void t6050_handoff(void *dt) 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 @@ -4252,6 +5006,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); @@ -4280,15 +5035,19 @@ 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; 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"); + t6050_ext_version(dt); return 0; } diff --git a/tests/python/fdt_harness.py b/tests/python/fdt_harness.py index 376dd3c4c..21459e163 100644 --- a/tests/python/fdt_harness.py +++ b/tests/python/fdt_harness.py @@ -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) diff --git a/tests/python/test_t6050_handoff_bounds.py b/tests/python/test_t6050_handoff_bounds.py index cedfcf2c6..40d4c4e2d 100644 --- a/tests/python/test_t6050_handoff_bounds.py +++ b/tests/python/test_t6050_handoff_bounds.py @@ -38,10 +38,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; @@ -71,10 +78,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) @@ -229,11 +236,18 @@ static struct { u64 addr; u32 val; -} words[16]; +} 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; } @@ -275,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; } @@ -293,6 +308,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); } @@ -421,9 +438,11 @@ */ 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); + 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); @@ -438,7 +457,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), @@ -463,6 +483,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); @@ -482,6 +503,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")); @@ -495,11 +525,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); @@ -556,6 +589,347 @@ 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 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); + 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); + 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 }; + 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); + } + 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"); + 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", + 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"); + for (int i = 0; io && i < len / 4; i++) + printf(" %u", fdt32_ld(&io[i])); + printf("\n"); + return 0; +} + static int t6041_headless_fb(void) { static char dt[0x10000]; @@ -610,6 +984,8 @@ return headless(argc, argv); if (!strcmp(argv[1], "panel")) return panel(argc, argv); + if (!strcmp(argv[1], "prewired")) + return prewired(argc, argv); if (!strcmp(argv[1], "t6041-headless")) return t6041_headless_fb(); return 2; @@ -672,6 +1048,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) @@ -791,6 +1169,195 @@ 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("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, 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)) + 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 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 + 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: 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"] == "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] + + # 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/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] + + 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] + + # 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. 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" + 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): + # 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 new file mode 100644 index 000000000..720683924 --- /dev/null +++ b/tests/python/test_t6050_tree_shape.py @@ -0,0 +1,1476 @@ +# 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() + 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 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}; +static struct { const char *uuid; u32 ext_aura; } t6050; +static void *adt; +#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]; +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 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; + 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; + 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) +{ + (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 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 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(") + \ + 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_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''' +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(); + 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); + 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++; + /* 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++; + /* 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"); + 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", "", 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 33 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") + + +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 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 { + 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; u32 ext_aura; } 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_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; + int ctl_fail_after; +}; +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, + .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 = {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; } + 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++) { + 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); +} +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-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; + 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 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(") + 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_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(") + \ + 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 }; +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)); + 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 */ +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++; + /* 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)); + t6050_ext_version(tree); + 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}; + 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, 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(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(); + 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/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_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; + 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(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 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; + 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)); + int len = 0; + const fdt32_t *got = fdt_getprop(tree, fdt_path_offset(tree, "/display"), "iommus", &len); + 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++; + } + /* 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++; + /* 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++; + /* 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++; + /* 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++; + /* 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++; + /* 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 34 cases")