diff --git a/src/kboot.c b/src/kboot.c index 0692feae9..3dcfc48b7 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -85,6 +85,7 @@ extern u64 relocated_from; int dt_set_gpu(void *dt); int dt_set_display_handoff(void *dt); +bool dt_t8132_external_display(void); u64 kboot_ram_base(void) { @@ -8504,6 +8505,8 @@ static int dt_set_display(void) disp0_piodma_alias = compat_piodma_alias; if (chip_id == T8132) { + if (dt_t8132_external_display()) + return dt_set_display_handoff(dt); if (dt_set_m4_dcp_handoff()) printf("FDT: M4 DCP handoff refused; keeping the boot framebuffer\n"); else diff --git a/src/kboot_display_handoff.c b/src/kboot_display_handoff.c index 7ed78d444..f16642cc1 100644 --- a/src/kboot_display_handoff.c +++ b/src/kboot_display_handoff.c @@ -88,6 +88,11 @@ * firmware row pins the DCP image on dcp and dcpext, publishes the /arm-io/disp0 firmware clocks * on dcp, and publishes empty apple,clock-ids on dcpext. * + * Headless T8132 boards (such as J773g) on macOS 27.2 (26B123) only; 26.6.2 and internal-panel + * boards keep kboot.c's M4 handoff. Decided from the ADT (/arm-io/disp0 has no internal panel): + * the internal DCP is not handed over, dcpext0 and dcpext1 are, each with its own pipe's firmware + * clocks (/arm-io/dispextN) instead of empty apple,clock-ids. + * * 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 @@ -231,6 +236,45 @@ static const struct dcp6_seg_def dcp6_seg_defs_t6030[DCP6_SEGS] = { /* Owners of the nubs whose __OS_LOG is reserved: /arm-io//iop--nub */ static const char *const dcp6_nubs_t6030[] = {"dcp", "dcpext0", "dcpext1"}; +/* + * J616s external DCPs use the same four-segment ASC layout as the T6030 + * external DCPs. The kernel DT supplies disabled text/data/heap placeholders; + * fill those only from this boot's ADT. In particular, do not enable the DCP, + * its DART or any display route here: the kernel's PMP-aware gate owns that. + */ +struct dcp6_ext { + const char *name, *dt_path, *adt_path, *nub_path, *dart_path; + u64 coproc, dart; + const char *disp_path; /* its ADT pipe, whose clocks dcp_fw.ext_clocks publishes */ +}; + +static const struct dcp6_ext t6041_exts[] = { + {"dcpext0", "/dcp@3a2e00000", "/arm-io/dcpext0", "/arm-io/dcpext0/iop-dcpext0-nub", + "/arm-io/dart-dcpext0", 0x3a2e00000UL, 0x3a2340000UL}, + {"dcpext1", "/dcp@602e00000", "/arm-io/dcpext1", "/arm-io/dcpext1/iop-dcpext1-nub", + "/arm-io/dart-dcpext1", 0x602e00000UL, 0x602340000UL}, + /* + * J614s: no dcpext0 in the ADT; its HDMI processor is dcpext2. Only a + * device tree that describes /dcp@606e00000 gets it (t6040-j614s.dts). + */ + {"dcpext2", "/dcp@606e00000", "/arm-io/dcpext2", "/arm-io/dcpext2/iop-dcpext2-nub", + "/arm-io/dart-dcpext2", 0x606e00000UL, 0x606340000UL}, + /* J614c/J616c: dcpext3, a third USB-C processor (linux t6041-j616c-usbc-dcpext3.dtsi). */ + {"dcpext3", "/dcp@306e00000", "/arm-io/dcpext3", "/arm-io/dcpext3/iop-dcpext3-nub", + "/arm-io/dart-dcpext3", 0x306e00000UL, 0x306340000UL}, +}; + +/* + * T8132 (J773g): t8132-dcpext.dtsi's nodes, under /soc. iBoot leaves both running the internal + * DCP's image. dcpext0 drives the HDMI port on 27.2; dcpext1 serves the USB-C ports. + */ +static const struct dcp6_ext t8132_exts[] = { + {"dcpext0", "/soc/dcp@286e00000", "/arm-io/dcpext0", "/arm-io/dcpext0/iop-dcpext0-nub", + "/arm-io/dart-dcpext0", 0x286e00000UL, 0x286340000UL, "/arm-io/dispext0"}, + {"dcpext1", "/soc/dcp@48ae00000", "/arm-io/dcpext1", "/arm-io/dcpext1/iop-dcpext1-nub", + "/arm-io/dart-dcpext1", 0x48ae00000UL, 0x48a340000UL, "/arm-io/dispext1"}, +}; + /* One OS firmware release's DCP contract when an SoC supports more than one */ struct dcp_fw { u32 os_fw; @@ -238,6 +282,7 @@ struct dcp_fw { const char *uuid; /* pinned DCP firmware image, or NULL with any_uuid */ bool any_uuid; /* no image pinned: the running image's ADT uuid is published */ bool disp0_clocks; /* publish /arm-io/disp0 clocks on dcp and uuid + clock-ids on dcpext */ + bool ext_clocks; /* with disp0_clocks: dcpext's clock-ids are its pipe's, not empty */ }; /* One SoC's internal display handoff */ @@ -278,6 +323,12 @@ struct dcp_soc { int nsegs; const char *const *nubs; /* owners whose __OS_LOG is reserved, or NULL: every dcp/dcpextN */ int nnubs; + /* External processors handed over on their own (dcp6_ext_handoffs()), or NULL */ + const struct dcp6_ext *exts; + int nexts; + const char *ext_compat; /* their device tree compatible */ + /* This SoC entry serves headless boards only (internal-panel boards keep another path) */ + bool headless_only; }; static const char *const dcp_boards_t6030[] = {"apple,j516s", "apple,j514s", NULL}; @@ -453,6 +504,9 @@ static const struct dcp_soc dcp_soc_t6041 = { .dart_disp = 0x402300000UL, .vm_base = DCP6_DART_VM_BASE, .vm_size = DCP6_DART_VM_SIZE, + .exts = t6041_exts, + .nexts = ARRAY_SIZE(t6041_exts), + .ext_compat = "apple,t6041-dcpext", }; /* @@ -498,6 +552,55 @@ static const struct dcp_soc dcp_soc_t6041_max = { .dart_disp = 0x402300000UL, .vm_base = DCP6_DART_VM_BASE, .vm_size = DCP6_DART_VM_SIZE, + .exts = t6041_exts, + .nexts = ARRAY_SIZE(t6041_exts), + .ext_compat = "apple,t6041-dcpext", +}; + +/* + * Headless T8132 (such as J773g) on macOS 27.2 (26B123). iBoot leaves dcp, dcpext0 and dcpext1 + * running the same DCP image, and /arm-io/disp0 is marked external (no internal panel). The + * internal DCP is not handed over; the external processors are. Their firmware sends a + * clock-frequency request for its pipe, which macOS answers from /arm-io/dispextN's clock-ids, so + * those are published on each (ext_clocks). The addresses and streams are this ADT's: dcp at + * 0x30ae00000 through dart-dcp SID 23, disp0 and its piodma through dart-disp0 SIDs 0 and 16, of + * 32 streams each; the DVA window is the device tree's apple,dma-range (the ADT's vm-size spans + * 3 TiB). Decided from the ADT rather than board name: any headless T8132 board on this firmware + * takes this path, while internal-panel boards and 26.6.2 keep the M4 handoff of kboot.c. + */ +static const struct dcp_fw dcp_fws_t8132[] = { + {.os_fw = V27_2, + .os_fw_name = "27.2", + .uuid = "B3BA603C-3CA7-3E21-B393-44EB20BD2AEF", + .disp0_clocks = true, + .ext_clocks = true}, +}; + +static const struct dcp_soc dcp_soc_t8132 = { + .name = "T8132", + .chip = T8132, + .compat = "apple,t8132", + .dcp_compat = "apple,t8132-dcp", + .disp_compat = "apple,t8132-display-subsystem", + .marker = "apple,t8132-handoff", + .adt_psreg = true, + .log_streams = true, + .sids = true, + .sid_dcp = 23, + .sid_disp = 0, + .sid_piodma = 16, + .streams = 32, + .fws = dcp_fws_t8132, + .nfws = ARRAY_SIZE(dcp_fws_t8132), + .coproc = 0x30ae00000UL, + .dart_dcp = 0x30a340000UL, + .dart_disp = 0x30a300000UL, + .vm_base = DCP6_DART_VM_BASE, + .vm_size = DCP6_DART_VM_SIZE, + .exts = t8132_exts, + .nexts = ARRAY_SIZE(t8132_exts), + .ext_compat = "apple,t8132-dcpext", + .headless_only = true, }; /* @@ -516,9 +619,9 @@ static const struct dcp_soc dcp_soc_t6050 = { .t6050 = true, }; -static const struct dcp_soc *const dcp_socs[] = {&dcp_soc_t6030, &dcp_soc_t8122, - &dcp_soc_t6031, &dcp_soc_t6041, - &dcp_soc_t6041_max, &dcp_soc_t6050}; +static const struct dcp_soc *const dcp_socs[] = {&dcp_soc_t6030, &dcp_soc_t8122, &dcp_soc_t6031, + &dcp_soc_t6041, &dcp_soc_t6041_max, &dcp_soc_t8132, + &dcp_soc_t6050}; struct dcp6_seg { u64 phys; @@ -544,12 +647,17 @@ struct dcp6_sid { enum { VM_DCP, VM_DISP }; /* dart-dcp, dart-disp0 */ +/* A display pipe's firmware clocks: its ADT clock-ids and their /arm-io clock-frequencies */ +struct dcp6_clocks { + u32 n, id[DCP6_CLOCKS_MAX], hz[DCP6_CLOCKS_MAX]; +}; + /* Per-kboot state, used by the armed part. Reset on every call. */ static struct { const struct dcp_soc *soc; const struct dcp_fw *fw; /* matched OS firmware row, or NULL */ struct dcp_fw fw_single; - u32 nclk, clk_id[DCP6_CLOCKS_MAX], clk_hz[DCP6_CLOCKS_MAX]; + struct dcp6_clocks clk; /* /arm-io/disp0's, for dcp */ u64 coproc, dart_dcp, dart_disp; /* as checked against the ADT */ u64 vm_base[2], vm_size[2]; /* DVA windows of dart-dcp and dart-disp0 */ bool vm_ok; @@ -2411,32 +2519,33 @@ static int dcp25_publish_clock(void *dt) sizeof(cells)); } -/* /arm-io/disp0 clock-ids index /arm-io clock-frequencies from DCP_CLOCK_ID_BASE. */ -static int dcp6_read_disp0_clocks(void) +/* A pipe's clock-ids (/arm-io/disp0, dispextN) index /arm-io clock-frequencies from 256 */ +static int dcp6_read_clocks(const char *path, struct dcp6_clocks *c) { - int disp = adt_path_offset(adt, "/arm-io/disp0"); + int disp = adt_path_offset(adt, path); int arm = adt_path_offset(adt, "/arm-io"); u32 len = 0, flen = 0; const u8 *raw_ids = disp < 0 ? NULL : adt_getprop(adt, disp, "clock-ids", &len); const u8 *freq = arm < 0 ? NULL : adt_getprop(adt, arm, "clock-frequencies", &flen); u32 nclk = raw_ids ? len / sizeof(u32) : 0; + memset(c, 0, sizeof(*c)); if (!nclk || len % sizeof(u32) || nclk > DCP6_CLOCKS_MAX || !freq || flen % sizeof(u32)) - bail("%s display clock metadata is missing or invalid\n", dcp6.fw->os_fw_name); + bail("%s %s clock metadata is missing or invalid\n", dcp6.fw->os_fw_name, path); for (u32 i = 0; i < nclk; i++) { u32 id, hz; memcpy(&id, raw_ids + i * sizeof(u32), sizeof(id)); if (id < DCP_CLOCK_ID_BASE || id - DCP_CLOCK_ID_BASE >= flen / sizeof(u32)) - bail("%s display clock %u is out of range\n", dcp6.fw->os_fw_name, id); + bail("%s %s clock %u is out of range\n", dcp6.fw->os_fw_name, path, id); for (u32 j = 0; j < i; j++) - if (dcp6.clk_id[j] == id) - bail("%s display clock %u is duplicated\n", dcp6.fw->os_fw_name, id); + if (c->id[j] == id) + bail("%s %s clock %u is duplicated\n", dcp6.fw->os_fw_name, path, id); memcpy(&hz, freq + (id - DCP_CLOCK_ID_BASE) * sizeof(u32), sizeof(hz)); - dcp6.clk_id[i] = id; - dcp6.clk_hz[i] = hz; + c->id[i] = id; + c->hz[i] = hz; } - if (!dcp6.clk_hz[0]) - bail("%s primary display clock frequency is zero\n", dcp6.fw->os_fw_name); - dcp6.nclk = nclk; + if (!c->hz[0]) + bail("%s %s primary clock frequency is zero\n", dcp6.fw->os_fw_name, path); + c->n = nclk; return 0; } @@ -2450,38 +2559,55 @@ static bool dcp6_disp0_profile_ok(void) dlog("%s DCP image mismatch: nothing done\n", dcp6.fw->os_fw_name); return false; } - return dcp6_read_disp0_clocks() == 0; + return dcp6_read_clocks("/arm-io/disp0", &dcp6.clk) == 0; } -static int dcp6_publish_disp0_clocks(void *dt, const char *dcp_path) +/* + * One fixed-clock provider /clock-dcp-firmware- per clock, and clocks, clock-names + * (firmware-0, firmware-1, ...) and apple,clock-ids on the processor at `dcp_path`. A provider + * the device tree already has is used if it is a fixed-clock of the same rate, and refuses the + * processor otherwise. + */ +static int dcp6_publish_clocks(void *dt, const char *dcp_path, const struct dcp6_clocks *c) { fdt32_t clocks[DCP6_CLOCKS_MAX], fw_ids[DCP6_CLOCKS_MAX]; char names[DCP6_CLOCKS_MAX * 12]; int names_len = 0; - if (!dcp6.nclk) + if (!c->n) return -1; - for (u32 i = 0; i < dcp6.nclk; i++) { + for (u32 i = 0; i < c->n; i++) { char name[32], path[36]; - snprintf(name, sizeof(name), "clock-dcp-firmware-%u", dcp6.clk_id[i]); + int len; + snprintf(name, sizeof(name), "clock-dcp-firmware-%u", c->id[i]); snprintf(path, sizeof(path), "/%s", name); - int node = fdt_add_subnode(dt, 0, name); - if (node < 0 || fdt_setprop_string(dt, node, "compatible", "fixed-clock") || - fdt_setprop_u32(dt, fdt_path_offset(dt, path), "#clock-cells", 0) || - fdt_setprop_u32(dt, fdt_path_offset(dt, path), "clock-frequency", dcp6.clk_hz[i])) - return -1; + int node = fdt_subnode_offset(dt, 0, name); + if (node >= 0) { + const fdt32_t *hz = fdt_getprop(dt, node, "clock-frequency", &len); + if (fdt_node_check_compatible(dt, node, "fixed-clock") || !hz || len != 4 || + fdt32_ld(hz) != c->hz[i]) + bail("%s exists with another rate\n", path); + const fdt32_t *cells = fdt_getprop(dt, node, "#clock-cells", &len); + if (!cells || len != 4 || fdt32_ld(cells)) + bail("%s is not a single clock\n", path); + } else { + node = fdt_add_subnode(dt, 0, name); + if (node < 0 || fdt_setprop_string(dt, node, "compatible", "fixed-clock") || + fdt_setprop_u32(dt, fdt_path_offset(dt, path), "#clock-cells", 0) || + fdt_setprop_u32(dt, fdt_path_offset(dt, path), "clock-frequency", c->hz[i])) + return -1; + } u32 ph = dcp6_phandle(dt, path); if (!ph) return -1; clocks[i] = cpu_to_fdt32(ph); - fw_ids[i] = cpu_to_fdt32(dcp6.clk_id[i]); + fw_ids[i] = cpu_to_fdt32(c->id[i]); names_len += snprintf(names + names_len, sizeof(names) - names_len, "firmware-%u", i) + 1; } - if (fdt_setprop(dt, fdt_path_offset(dt, dcp_path), "clocks", clocks, - dcp6.nclk * sizeof(*clocks)) || + if (fdt_setprop(dt, fdt_path_offset(dt, dcp_path), "clocks", clocks, c->n * sizeof(*clocks)) || fdt_setprop(dt, fdt_path_offset(dt, dcp_path), "clock-names", names, names_len) || fdt_setprop(dt, fdt_path_offset(dt, dcp_path), "apple,clock-ids", fw_ids, - dcp6.nclk * sizeof(*fw_ids))) + c->n * sizeof(*fw_ids))) return -1; return 0; } @@ -2561,7 +2687,7 @@ static int dcp6_publish(void *dt, const struct dcp6_dt *n) dcp = fdt_path_offset(dt, n->dcp); if (dcp < 0 || firmware_set_fdt(dt, dcp, "apple,firmware-compat", dcp6_fw_release())) return -1; - if (dcp6.fw && dcp6.fw->disp0_clocks && dcp6_publish_disp0_clocks(dt, n->dcp)) + if (dcp6.fw && dcp6.fw->disp0_clocks && dcp6_publish_clocks(dt, n->dcp, &dcp6.clk)) return -1; return 0; } @@ -2598,8 +2724,6 @@ static int dcp6_check_resv(void *dt) return 0; } -static bool t6041_headless(void *dt); - static __attribute__((noinline)) void dcp6_handoff(void *dt) { struct dcp6_dt n; @@ -3073,47 +3197,27 @@ static void dcpext6_handoffs(void *dt) } /* - * J616s external DCPs use the same four-segment ASC layout as the T6030 - * external DCPs. The kernel DT supplies disabled text/data/heap placeholders; - * fill those only from this boot's ADT. In particular, do not enable the DCP, - * its DART or any display route here: the kernel's PMP-aware gate owns that. + * Headless machines (such as J575c Mac Studio, or J773g Mac mini on 27.2): no internal panel + * (/arm-io/disp0 has `external` or is absent). The internal DCP is never handed over: an older + * Stage 1 starts it, finds nothing connected and puts it to sleep, and a kernel has no panel to + * drive with it anyway. The external processors are left as iBoot started them, so they are handed + * over on their own: the first one's ADT __TEXT stands in for the panel's, and its reservation is + * made here instead of by the internal handoff. */ -struct t6041_ext { - const char *name, *dt_path, *adt_path, *nub_path, *dart_path; - u64 coproc, dart; -}; - -static const struct t6041_ext t6041_exts[] = { - {"dcpext0", "/dcp@3a2e00000", "/arm-io/dcpext0", "/arm-io/dcpext0/iop-dcpext0-nub", - "/arm-io/dart-dcpext0", 0x3a2e00000UL, 0x3a2340000UL}, - {"dcpext1", "/dcp@602e00000", "/arm-io/dcpext1", "/arm-io/dcpext1/iop-dcpext1-nub", - "/arm-io/dart-dcpext1", 0x602e00000UL, 0x602340000UL}, - /* - * J614s: no dcpext0 in the ADT; its HDMI processor is dcpext2. Only a - * device tree that describes /dcp@606e00000 gets it (t6040-j614s.dts). - */ - {"dcpext2", "/dcp@606e00000", "/arm-io/dcpext2", "/arm-io/dcpext2/iop-dcpext2-nub", - "/arm-io/dart-dcpext2", 0x606e00000UL, 0x606340000UL}, - /* J614c/J616c: dcpext3, a third USB-C processor (linux t6041-j616c-usbc-dcpext3.dtsi). */ - {"dcpext3", "/dcp@306e00000", "/arm-io/dcpext3", "/arm-io/dcpext3/iop-dcpext3-nub", - "/arm-io/dart-dcpext3", 0x306e00000UL, 0x306340000UL}, -}; +static bool dcp6_adt_headless(void) +{ + int disp0 = adt_path_offset(adt, "/arm-io/disp0"); + return disp0 < 0 || adt_getprop(adt, disp0, "external", NULL) != NULL; +} -/* - * J575c (Mac Studio): no panel. The internal DCP is never handed over: an older Stage 1 starts it, - * finds nothing connected and puts it to sleep, and a kernel has no panel to drive with it anyway. - * The external processors are left as iBoot started them, so they are handed over on their own: - * the first one's ADT __TEXT stands in for the panel's, and its reservation is made here instead - * of by the internal handoff. - */ -static bool t6041_headless(void *dt) +static bool dcp6_headless(void) { - return dcp6.soc == &dcp_soc_t6041_max && !fdt_node_check_compatible(dt, 0, "apple,j575c"); + return dcp6.soc->exts && dcp6_adt_headless(); } -static struct dcp6_seg t6041_headless_text; +static struct dcp6_seg dcp6_headless_text; -static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) +static int dcp6_ext_fill(void *dt, const struct dcp6_ext *ext) { static const u32 flags[DCPEXT6_SEGS] = {SEG_FLAGS_TEXT, SEG_FLAGS_DATA, SEG_FLAGS_OSLOG, SEG_FLAGS_HEAP}; @@ -3129,34 +3233,51 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) /* A DT without #451's external nodes is an ordinary internal-only boot. */ if (node < 0) return 1; - if (fdt_node_check_compatible(dt, node, "apple,t6041-dcpext") || - dcp6_status_disabled(dt, node, ext->name) || dcp6_adt_reg(ext->adt_path, 0, &coproc) || - coproc != ext->coproc || dcp6_adt_reg(ext->dart_path, 0, &dart) || dart != ext->dart) + if (fdt_node_check_compatible(dt, node, dcp6.soc->ext_compat)) + bail("%s is not %s\n", ext->name, dcp6.soc->ext_compat); + if (dcp6_status_disabled(dt, node, ext->name)) return -1; + if (dcp6_adt_reg(ext->adt_path, 0, &coproc) || coproc != ext->coproc || + dcp6_adt_reg(ext->dart_path, 0, &dart) || dart != ext->dart) + bail("%s: ADT %s or %s is not at 0x%lx, 0x%lx\n", ext->name, ext->adt_path, ext->dart_path, + ext->coproc, ext->dart); reg = fdt_getprop(dt, node, "reg", &len); if (!reg || len < 2 * (int)sizeof(fdt64_t) || fdt64_ld(reg) != coproc) - return -1; + bail("%s: reg is not 0x%lx\n", ext->name, coproc); iommu = fdt_getprop(dt, node, "iommus", &len); if (!iommu || len != 2 * (int)sizeof(fdt32_t) || fdt32_ld(&iommu[1]) != 23) - return -1; + bail("%s: iommus is not <&dart 23>\n", ext->name); + char dart_what[32]; + snprintf(dart_what, sizeof(dart_what), "%s DART", ext->name); int dart_node = fdt_node_offset_by_phandle(dt, fdt32_ld(iommu)); - if (dart_node < 0 || fdt_node_check_compatible(dt, dart_node, "apple,t8110-dart") || - dcp6_status_disabled(dt, dart_node, ext->name)) + if (dart_node < 0 || fdt_node_check_compatible(dt, dart_node, "apple,t8110-dart")) + bail("%s is not an apple,t8110-dart\n", dart_what); + if (dcp6_status_disabled(dt, dart_node, dart_what)) return -1; reg = fdt_getprop(dt, dart_node, "reg", &len); if (!reg || len < 2 * (int)sizeof(fdt64_t) || fdt64_ld(reg) != dart) - return -1; + bail("%s: reg is not 0x%lx\n", dart_what, dart); mr = fdt_getprop(dt, node, "memory-region", &len); if (!mr || len != 3 * (int)sizeof(fdt32_t)) - return -1; + bail("%s: no text, data and heap memory-region placeholders\n", ext->name); for (int i = 0; i < 3; i++) ph[i] = fdt32_ld(&mr[i]); if (!ph[0] || !ph[1] || !ph[2] || ph[0] == ph[1] || ph[0] == ph[2] || ph[1] == ph[2]) - return -1; + bail("%s: memory-region placeholders are not three distinct nodes\n", ext->name); const char *names = fdt_getprop(dt, node, "memory-region-names", &len); if (!names || len != sizeof(mr_names) || memcmp(names, mr_names, sizeof(mr_names))) - return -1; + bail("%s: memory-region-names is not text, data, heap\n", ext->name); + /* Its pipe's clocks replace a single static `clocks`, never firmware clocks already named. */ + struct dcp6_clocks clk = {0}; + if (dcp6.fw && dcp6.fw->ext_clocks) { + const void *clks = fdt_getprop(dt, node, "clocks", &len); + if ((clks && len != sizeof(fdt32_t)) || fdt_getprop(dt, node, "clock-names", NULL) || + fdt_getprop(dt, node, "apple,clock-ids", NULL) || !ext->disp_path) + bail("%s already has firmware clocks\n", ext->name); + if (dcp6_read_clocks(ext->disp_path, &clk)) + return -1; + } for (int i = 0; i < 3; i++) { int region = fdt_node_offset_by_phandle(dt, ph[i]); const char *status = region < 0 ? NULL : fdt_getprop(dt, region, "status", NULL); @@ -3166,7 +3287,8 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) !fdt_getprop(dt, region, "no-map", NULL) || fdt_getprop(dt, region, "iommu-addresses", NULL) || !r || len != 2 * (int)sizeof(fdt64_t) || fdt64_ld(r) || fdt64_ld(&r[1]) != SZ_16K) - return -1; + bail("%s: the %s placeholder is not an empty, disabled apple,asc-mem\n", ext->name, + parts[i]); } int nub = adt_path_offset(adt, ext->nub_path); @@ -3178,14 +3300,14 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) if (dcp6.fw && dcp6.fw->uuid && (!ext_uuid || uuid_len != strlen(dcp6.fw->uuid) + 1 || memcmp(ext_uuid, dcp6.fw->uuid, uuid_len))) - return -1; + bail("%s: ADT image uuid is not %s\n", ext->name, dcp6.fw->uuid); if (!ranges || seg_len != DCPEXT6_SEGS * sizeof(*ranges) || !seg_names || !dcp6_seg_name_is(seg_names, names_len, SEG_TEXT, "__TEXT") || !dcp6_seg_name_is(seg_names, names_len, SEG_DATA, "__DATA") || !dcp6_seg_name_is(seg_names, names_len, SEG_OSLOG, "__OS_LOG") || dcp6_adt_u32(nub, "pre-loaded", &preloaded) || preloaded != 1 || dcp6_adt_u32(nub, "running", &running) || running != 1 || dcp6_dram(&dram, &dram_size)) - return -1; + bail("%s: iBoot did not leave its four-segment firmware loaded and running\n", ext->name); ram_end = kboot_ram_base() + kboot_ram_size(); if (ram_end < kboot_ram_base()) return -1; @@ -3198,32 +3320,36 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) if (s->flags != flags[i] || !r.size || s->size > DCP6_SEG_MAX_SIZE || (s->phys & (SZ_16K - 1)) || s->phys < dram || s->phys + s->size < s->phys || s->phys + s->size > dram + dram_size) - return -1; + bail("%s: segment %d 0x%lx+0x%lx (flags 0x%x) is not a DRAM segment of this layout\n", + ext->name, i, s->phys, s->size, s->flags); if (i == SEG_OSLOG) { if (s->remap != s->phys || s->size > DCP6_OSLOG_MAX) - return -1; + bail("%s: __OS_LOG is not a physical buffer\n", ext->name); } else if (s->remap < DCP6_DART_VM_BASE || s->remap - DCP6_DART_VM_BASE > DCP6_DART_VM_SIZE - s->size || (s->remap & (SZ_16K - 1)) || !(s->phys + s->size <= kboot_ram_base() || s->phys >= ram_end) || dcp6_carveout_conflict(s->phys, s->size, ext->name)) { - return -1; + bail("%s: segment %d 0x%lx+0x%lx at 0x%lx is kernel RAM, a carveout's, or outside " + "the DVA window\n", + ext->name, i, s->phys, s->size, s->remap); } for (int j = 0; j < i; j++) if (dcp6_overlaps(s->phys, s->size, segs[j].phys, segs[j].size) || (i != SEG_OSLOG && j != SEG_OSLOG && dcp6_overlaps(s->remap, s->size, segs[j].remap, segs[j].size))) - return -1; + bail("%s: segments %d and %d overlap\n", ext->name, j, i); } /* The two external processors and the panel share immutable TEXT. */ const struct dcp6_seg *shared_text = &dcp6.segs[SEG_TEXT]; - if (t6041_headless(dt)) { - if (!t6041_headless_text.phys) - t6041_headless_text = segs[SEG_TEXT]; - shared_text = &t6041_headless_text; + if (dcp6_headless()) { + if (!dcp6_headless_text.phys) + dcp6_headless_text = segs[SEG_TEXT]; + shared_text = &dcp6_headless_text; } if (segs[SEG_TEXT].phys != shared_text->phys || segs[SEG_TEXT].size != shared_text->size) - return -1; + bail("%s: __TEXT 0x%lx is not the shared one at 0x%lx\n", ext->name, segs[SEG_TEXT].phys, + shared_text->phys); u32 owner = dcp6_phandle(dt, ext->dt_path); if (!owner) @@ -3237,7 +3363,7 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) const struct dcp6_seg *text = &segs[SEG_TEXT]; snprintf(text_name, sizeof(text_name), "dcp-text@%lx", text->phys); int shared = dcp6_find_resv(dt, text_name, text->phys, text->size); - if (shared < 0 && t6041_headless(dt)) { + if (shared < 0 && dcp6_headless()) { if (dcp6_resv_conflict(dt, text->phys, text->size, text_name) || !dcp6_add_resv(dt, text_name, text->phys, text->size, "apple,asc-mem")) return -1; @@ -3282,8 +3408,11 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) return -1; if (dcp6.fw && dcp6.fw->disp0_clocks) { if (fdt_setprop_string(dt, fdt_path_offset(dt, ext->dt_path), "apple,firmware-uuid", - dcp6.fw->uuid) || - fdt_setprop_empty(dt, fdt_path_offset(dt, ext->dt_path), "apple,clock-ids")) + dcp6.fw->uuid)) + return -1; + if (dcp6.fw->ext_clocks + ? dcp6_publish_clocks(dt, ext->dt_path, &clk) + : fdt_setprop_empty(dt, fdt_path_offset(dt, ext->dt_path), "apple,clock-ids")) return -1; } return 0; @@ -3294,7 +3423,7 @@ static int t6041_ext_fill(void *dt, const struct t6041_ext *ext) * chains join, nor its own DART (disp0's, which iBoot leaves locked). Enable both once an external * processor has its firmware memory; the kernel's gate then enables that processor's chain. */ -static int t6041_headless_display(void *dt) +static int dcp6_headless_display(void *dt) { int display = fdt_path_offset(dt, "disp0"); int len; @@ -3327,39 +3456,39 @@ static int t6041_headless_display(void *dt) return display < 0 ? -1 : fdt_setprop_string(dt, display, "status", "okay"); } -static void t6041_ext_handoffs(void *dt) +static void dcp6_ext_handoffs(void *dt) { int panel = fdt_path_offset(dt, "dcp"); bool panel_handoff = panel >= 0 && fdt_getprop(dt, panel, dcp6.soc->marker, NULL); bool published = false; if (!panel_handoff) { - if (!t6041_headless(dt)) + if (!dcp6_headless()) return; dlog("no internal display handoff (no panel): external processors only\n"); } - for (size_t i = 0; i < ARRAY_SIZE(t6041_exts); i++) { + for (int i = 0; i < dcp6.soc->nexts; i++) { + const struct dcp6_ext *ext = &dcp6.soc->exts[i]; void *scratch = dcp6_scratch(dt); if (!scratch) return; - int ret = t6041_ext_fill(scratch, &t6041_exts[i]); + int ret = dcp6_ext_fill(scratch, ext); if (ret) { if (ret < 0) - dlog("%s firmware memory refused; placeholder remains disabled\n", - t6041_exts[i].name); + dlog("%s firmware memory refused; placeholder remains disabled\n", ext->name); free(scratch); continue; } if (!dcp6_commit(dt, scratch)) { - dlog("%s firmware memory published; processor remains disabled\n", t6041_exts[i].name); + dlog("%s firmware memory published; processor remains disabled\n", ext->name); published = true; } } - if (!published || !t6041_headless(dt) || panel_handoff) + if (!published || !dcp6_headless() || panel_handoff) return; void *scratch = dcp6_scratch(dt); if (!scratch) return; - if (t6041_headless_display(scratch)) { + if (dcp6_headless_display(scratch)) { dlog("display subsystem not enabled; the external processors stay disabled\n"); free(scratch); } else if (!dcp6_commit(dt, scratch)) { @@ -4169,6 +4298,33 @@ static T6050_TEXT void t6050_headless_handoff(void *dt, int engine) dlog("display handoff refused; the boot framebuffer keeps the display\n"); } +/* + * Headless T8132 boots running a qualified external-only DCP image: macOS drives the external + * ports from dcpext and parks the internal DCP, so kboot routes them to dt_set_display_handoff(). + * Uses the same dcp6_adt_headless(), dcp6_select_fw() and t8132_exts[] table as the handoff so + * routing and handoff cannot disagree on the OS firmware row or pinned image UUID. + */ +bool dt_t8132_external_display(void) +{ + if (chip_id != dcp_soc_t8132.chip || !dcp6_adt_headless()) + return false; + + const struct dcp_fw *fw = dcp6_select_fw(&dcp_soc_t8132); + if (!fw || !fw->uuid) + return false; + + size_t uuid_len = strlen(fw->uuid) + 1; + for (int i = 0; i < dcp_soc_t8132.nexts; i++) { + int nub = adt_path_offset(adt, dcp_soc_t8132.exts[i].nub_path); + u32 len = 0, running = 0; + const char *got = nub < 0 ? NULL : adt_getprop(adt, nub, "uuid", &len); + if (got && len == uuid_len && !memcmp(got, fw->uuid, uuid_len) && + !dcp6_adt_u32(nub, "running", &running) && running == 1) + return true; + } + return false; +} + /* Always returns 0: nothing here may stop the boot */ int dt_set_display_handoff(void *dt) { @@ -4203,6 +4359,17 @@ int dt_set_display_handoff(void *dt) return 0; } + if (dcp6.soc->headless_only) { + if (chip_id != dcp6.soc->chip) { + dlog("not a %s (chip 0x%x): nothing done\n", dcp6.soc->name, chip_id); + return 0; + } + if (!dcp6_adt_headless()) { + dlog("internal panel present: nothing done\n"); + return 0; + } + } + dcp6.fw = dcp6_select_fw(dcp6.soc); /* Reject unsupported firmware before reserving __OS_LOG or touching registers. */ @@ -4238,7 +4405,7 @@ int dt_set_display_handoff(void *dt) dcp6_check_hw(); dcp6_dry_run(); - if (armed && t6041_headless(dt)) + if (armed && dcp6_headless()) dlog("no panel: the internal DCP is not handed over\n"); else if (armed) dcp6_handoff(dt); @@ -4246,7 +4413,7 @@ int dt_set_display_handoff(void *dt) dlog("not armed (no chosen.%s=1): no display handoff\n", dcp6.soc->optin); if (dcp6.soc->ext) dcpext6_handoffs(dt); - if (dcp6.soc == &dcp_soc_t6041 || dcp6.soc == &dcp_soc_t6041_max) - t6041_ext_handoffs(dt); + if (dcp6.soc->exts) + dcp6_ext_handoffs(dt); return 0; } diff --git a/tests/python/test_t6041_display_handoff.py b/tests/python/test_t6041_display_handoff.py index e72938266..42ad8655b 100644 --- a/tests/python/test_t6041_display_handoff.py +++ b/tests/python/test_t6041_display_handoff.py @@ -73,7 +73,7 @@ def run(binary, scenario): return subprocess.run([str(binary), scenario], capture_output=True, text=True, env=env) -SCENARIOS = re.findall(r'^ \{"([a-z0-9-]+)", (?:PUBLISH|NOTHING|REFUSE|REFUSE_NO_OSLOG),', +SCENARIOS = re.findall(r'^ \{"([a-z0-9-]+)", (?:PUBLISH|HEADLESS|NOTHING|REFUSE|REFUSE_NO_OSLOG),', (ROOT / "tests/t6041_display_host.c").read_text(), re.M) @@ -107,6 +107,7 @@ def test_scenario_list_matches(host): "j614c": PUBLISHED, "j614c-external": PUBLISHED, "j614c-on-t6040": "not a J616C/J614C/J575C (chip 0x6040): nothing done", + "j575c-headless": "display subsystem and its DART enabled for the external processors", "no-display-dt": NOT_DESCRIBED, "no-dcp-alias": NOT_DESCRIBED, "wrong-dcp-compat": NOT_DESCRIBED, diff --git a/tests/python/test_t8132_display_route.py b/tests/python/test_t8132_display_route.py new file mode 100644 index 000000000..603d22235 --- /dev/null +++ b/tests/python/test_t8132_display_route.py @@ -0,0 +1,187 @@ +# SPDX-License-Identifier: MIT +"""Which display handoff a T8132 boot gets, via the shared kboot_display_handoff.c helper. + +Failure model: +- Any headless T8132 (/arm-io/disp0 marked external or absent) whose OS firmware matches a + qualified row of dcp_soc_t8132 (dcp6_select_fw()) and whose ADT has a running dcpext0 or + dcpext1 nub carrying that row's pinned DCP image goes to dt_set_display_handoff(). +- Every other T8132 boot keeps the M4 handoff (dt_set_m4_dcp_handoff()): + - any board with an internal panel (/arm-io/disp0 present without `external`); + - a headless board on an unqualified OS firmware (26.6.2, 27.0, unknown), even if the UUID + matches, so kboot routing and dt_set_display_handoff() cannot disagree; + - a headless board running any other DCP image (e.g. 27.2.1 or missing UUID); + - a headless board whose external DCP nubs are not running; + - any non-T8132 chip. +- The pinned UUID and dcp6_select_fw() live in one place (src/kboot_display_handoff.c), not + duplicated in src/kboot.c. +- dt_set_display() asks before it takes the M4 path, and returns what the external handoff + returns (always 0). +""" + +import re +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from host_compiler import host_cc +from test_t6041_display_handoff import LIBFDT, ROOT + +HARNESS = r''' +#define main desktop26_main +#include "t6041_display_host.c" +#undef main + +bool dt_t8132_external_display(void); + +struct route_case { + u32 chip; + enum fw_version fw; + bool has_disp0; + bool disp0_external; + const char *uuid[2]; + u32 running[2]; + bool has_running[2]; +}; + +static void reset_adt(void) +{ + for (int i = 0; i < nnodes; i++) { + for (int j = 0; j < nodes[i].nprops; j++) + free(nodes[i].props[j]); + nodes[i].nprops = 0; + nodes[i].nreg = 0; + nodes[i].path[0] = 0; + } + nnodes = 0; +} + +static int check_case(const char *desc, struct route_case cfg, int want) +{ + static const char *const nubs[2] = { + "/arm-io/dcpext0/iop-dcpext0-nub", + "/arm-io/dcpext1/iop-dcpext1-nub", + }; + + reset_adt(); + chip_id = cfg.chip; + os_firmware = fw_versions[cfg.fw]; + if (cfg.has_disp0) { + prop_str("/arm-io/disp0", "compatible", "disp0,t8132"); + if (cfg.disp0_external) + prop_set("/arm-io/disp0", "external", "", 0); + } + for (int i = 0; i < 2; i++) { + if (cfg.uuid[i]) + prop_str(nubs[i], "uuid", cfg.uuid[i]); + if (cfg.has_running[i]) + prop_u32(nubs[i], "running", cfg.running[i]); + } + int got = dt_t8132_external_display(); + printf("%s %s: %s\n", got == want ? "ok " : "FAIL", desc, + got ? "external handoff" : "M4 handoff"); + return got != want; +} + +int main(void) +{ + static const char PINNED[] = "B3BA603C-3CA7-3E21-B393-44EB20BD2AEF"; + static const char OTHER[] = "0F1E2D3C-4B5A-3968-8776-A5B4C3D2E1F0"; + int fails = 0; + + fails += check_case("headless disp0 external + 27.2 + dcpext0 pinned running", + (struct route_case){T8132, V27_2, true, true, {PINNED, PINNED}, + {1, 1}, {true, true}}, + 1); + fails += check_case("headless disp0 absent + 27.2 + dcpext1 pinned running", + (struct route_case){T8132, V27_2, false, false, {OTHER, PINNED}, + {0, 1}, {true, true}}, + 1); + fails += check_case("headless + 26.6.2 (dcp6_select_fw refuses even with pinned UUID)", + (struct route_case){T8132, V26_6_2, true, true, {PINNED, PINNED}, + {1, 1}, {true, true}}, + 0); + fails += check_case("headless + 27.0 (dcp6_select_fw refuses even with pinned UUID)", + (struct route_case){T8132, V27_0, true, true, {PINNED, PINNED}, + {1, 1}, {true, true}}, + 0); + fails += check_case("headless + 27.2 + other UUID (27.2.1)", + (struct route_case){T8132, V27_2, true, true, {OTHER, OTHER}, + {1, 1}, {true, true}}, + 0); + fails += check_case("headless + 27.2 + pinned UUID but not running", + (struct route_case){T8132, V27_2, true, true, {PINNED, PINNED}, + {0, 0}, {true, true}}, + 0); + fails += check_case("headless + 27.2 + missing UUID", + (struct route_case){T8132, V27_2, true, true, {NULL, NULL}, + {1, 1}, {true, true}}, + 0); + fails += check_case("internal panel (disp0 not external) + 27.2 + dcpext0 pinned running", + (struct route_case){T8132, V27_2, true, false, {PINNED, PINNED}, + {1, 1}, {true, true}}, + 0); + fails += check_case("wrong chip (T6041) + headless + 27.2 + dcpext0 pinned running", + (struct route_case){T6041, V27_2, true, true, {PINNED, PINNED}, + {1, 1}, {true, true}}, + 0); + return fails != 0; +} +''' + + +def function(source, name, kind="static int "): + start = source.index(kind + name + "(") + return source[start:source.index("\n}", start) + 2] + "\n" + + +def test_t8132_route_follows_adt_and_fw_state(tmp_path): + cc = host_cc() + if not shutil.which(cc[0]): + pytest.skip("no host C compiler") + src = tmp_path / "src" + src.mkdir() + code = (ROOT / "src/kboot_display_handoff.c").read_text() + if sys.platform == "darwin": + code = code.replace('section(".init.t6050")', 'section("__TEXT,__t6050_init")') + (src / "kboot_display_handoff.c").write_text(code) + shutil.copy(ROOT / "tests/t6041_display_host.c", src) + for header in (ROOT / "tests/t8122_display_host").glob("*.h"): + shutil.copy(header, src) + driver = src / "route_host.c" + driver.write_text(HARNESS) + binary = tmp_path / "t8132-route-host" + subprocess.run( + [*cc, "-std=gnu11", "-D_GNU_SOURCE", "-O1", "-g", "-Wall", "-Wno-unused-function", + "-Wno-format", "-Wno-format-truncation", "-fsanitize=address,undefined", + "-fno-omit-frame-pointer", "-fno-sanitize-recover=undefined", + "-I", str(src), "-I", str(ROOT / "src"), + str(driver), str(src / "kboot_display_handoff.c"), + *(str(ROOT / "src/libfdt" / f) for f in LIBFDT), "-o", str(binary)], + check=True, + ) + result = subprocess.run([str(binary)], capture_output=True, text=True, + env={"ASAN_OPTIONS": "detect_leaks=0", "PATH": "/usr/bin:/bin"}) + assert result.returncode == 0, result.stdout + result.stderr + + +def test_route_uses_shared_helper_and_single_uuid(): + kboot = (ROOT / "src/kboot.c").read_text() + handoff = (ROOT / "src/kboot_display_handoff.c").read_text() + uuid = "B3BA603C-3CA7-3E21-B393-44EB20BD2AEF" + assert uuid not in kboot + assert handoff.count(uuid) == 1 + body = function(handoff, "dt_t8132_external_display", "bool ") + assert "apple,j773g" not in body + assert "dcp6_adt_headless()" in body + assert "dcp6_select_fw(&dcp_soc_t8132)" in body + + +def test_dt_set_display_asks_before_the_m4_handoff(): + kboot = (ROOT / "src/kboot.c").read_text() + body = function(kboot, "dt_set_display", "static int ") + branch = body[body.index("if (chip_id == T8132) {"):] + branch = branch[:branch.index("dt_set_m4_dcp_handoff()")] + assert re.search(r"if \(dt_t8132_external_display\(\)\)\s+return dt_set_display_handoff\(dt\);", + branch), branch diff --git a/tests/python/test_t8132_j773g_27_display_handoff.py b/tests/python/test_t8132_j773g_27_display_handoff.py new file mode 100644 index 000000000..bf6e3298d --- /dev/null +++ b/tests/python/test_t8132_j773g_27_display_handoff.py @@ -0,0 +1,126 @@ +# SPDX-License-Identifier: MIT +"""T8132 Mac mini (J773g) display handoff on macOS 27.2 (26B123): external processors only. + +Failure model, recorded before src/kboot_display_handoff.c learnt the J773g: +- On 27.2, macOS drives the J773g's HDMI port from dcpext0. iBoot leaves dcp, dcpext0 and + dcpext1 running the same DCP image, B3BA603C-3CA7-3E21-B393-44EB20BD2AEF. The dcpext nubs have + four segments (__TEXT, __DATA, __OS_LOG, heap), and __TEXT is one buffer shared by all three + processors. /arm-io/dispextN clock-ids index /arm-io clock-frequencies from 256, like disp0's + (dispext0: 369 = 533333328 Hz, 432 = 0 Hz). +- Like the J575c, the board is headless: the internal DCP is never handed over. Its node, DARTs + and mailbox stay as the device tree has them, and it gets no marker, memory-region, clock or + firmware property. No MMIO is written at all: dart-dcp is not locked. +- Each apple,t8132-dcpext node the device tree describes, at /dcp@286e00000 or /dcp@48ae00000, + is handed over as the J575c's are. Requirements: + - the node and its DART (iommus <&dart 23>, an apple,t8110-dart) are status "disabled"; + - it has three disabled apple,asc-mem placeholders named text, data and heap; + - its nub has the pinned UUID, is pre-loaded and running, and has the four-segment layout; + - data and heap lie outside the kernel's RAM; + - its __TEXT is the first processor's. + Then: + - data and heap are filled and enabled with their DART alias; + - text points at the shared dcp-text region, which carries one iommu-addresses tuple per + processor; + - apple,firmware-version and -compat <27 2 0> and apple,firmware-uuid are published; + - the node and its DART stay disabled for the kernel's display gate. +- Unlike the J575c's, a J773g dcpext gets its own pipe's firmware clocks: + - fixed-clock providers, clock-names firmware-0..N and apple,clock-ids = the /arm-io/dispextN + clock-ids, where T6041's get empty apple,clock-ids. The firmware asks for them (D408), and + macOS answers with these rates. + - A /clock-dcp-firmware- node already in the device tree (the 26.6.2 tree has static + ones) is reused when it is a fixed-clock of the same rate; with another rate the processor + is refused. + - A single static `clocks` on the node is replaced. Existing clock-names or apple,clock-ids + refuse it. +- Any failed check refuses that processor only: its placeholders stay disabled and it gets no + firmware, UUID or clock property, and no clock node. The other processor is unaffected. +- Once a processor is published, the display subsystem (disp0 alias) and its DART are enabled, + with iBoot's boot framebuffer as the display's "framebuffer" memory-region. If the device tree + already enables them (the 26.6.2 J773g tree does), that step is refused and logged, and the + display node is left as it is; the published processors stay published. With no processor + published, the display is not touched. +- __OS_LOG of dcp, dcpext0 and dcpext1 is reserved, as on every supported boot. +- Nothing at all (no MMIO, the device tree byte for byte unchanged) unless: + - the chip is T8132 and the ADT describes a headless machine (/arm-io/disp0 has `external` or is + absent; an internal-panel T8132 gets nothing from this path, while any headless T8132 board + takes it without a board table entry); + - the OS firmware is 27.2 (26.6.2 and 27.0 get nothing); + - the internal nub runs the pinned UUID (26B131's 27.2.1 image gets nothing); + - /arm-io/disp0 has valid clock-ids; + - the device tree has the dcp and disp0 aliases on apple,t8132-dcp and + apple,t8132-display-subsystem nodes. +- A second call changes nothing. A device tree with no room refuses transactionally: no partial + clock nodes, properties or regions. +- T6041 (J616c/J614c/J575c) behaviour is unchanged (test_t6041_*_display_handoff.py). +""" + +import re + +import pytest +from test_t6041_display_handoff import ROOT, build_host, run + +HOST = "t8132_j773g_27_display_host.c" +SCENARIOS = re.findall(r'^ \{"([a-z0-9-]+)", ', (ROOT / "tests" / HOST).read_text(), re.M) + + +PUBLISHED = "dcpext0 firmware memory published" +BOTH = "dcpext1 firmware memory published" +DISPLAY = "display subsystem and its DART enabled for the external processors" +NOTHING = "nothing done" +# The log line of the check each scenario is meant to reach, so that none passes for another reason +REASONS = { + "publish": [PUBLISHED, BOTH, DISPLAY], + "other-headless-board": [PUBLISHED, BOTH, DISPLAY], + "dcpext0-only": [PUBLISHED, DISPLAY], + "repeat": ["3 __OS_LOG reservation(s) already present", "dcpext0 already has firmware clocks"], + "static-clock-nodes": [PUBLISHED, BOTH, DISPLAY], + "display-preenabled": [PUBLISHED, BOTH, 'display subsystem is not status "disabled"'], + "ext-uuid-mismatch": [PUBLISHED, "dcpext1: ADT image uuid is not B3BA603C-"], + "ext-not-running": [BOTH, "dcpext0: iBoot did not leave its four-segment firmware loaded"], + "ext-enabled": [BOTH, 'dcpext0 is not status "disabled"'], + "ext-dart-enabled": [BOTH, 'dcpext0 DART is not status "disabled"'], + "ext-wrong-compat": [BOTH, "dcpext0 is not apple,t8132-dcpext"], + "ext-no-placeholders": [BOTH, "dcpext0: no text, data and heap memory-region placeholders"], + "ext-text-not-shared": [PUBLISHED, "dcpext1: __TEXT 0x10284000000 is not the shared one"], + "ext-data-in-ram": [BOTH, "dcpext0: segment 1 0x10010000000+0x8000 at 0x10002490000 is kernel RAM"], + "ext-clock-missing": [BOTH, "27.2 /arm-io/dispext0 clock metadata is missing or invalid"], + "ext-clock-zero": [BOTH, "27.2 /arm-io/dispext0 primary clock frequency is zero"], + "ext-clock-names": [BOTH, "dcpext0 already has firmware clocks"], + "ext-clock-conflict": [BOTH, "/clock-dcp-firmware-369 exists with another rate"], + "ext-all-refused": ["dcpext0: iBoot did not leave", "dcpext1: iBoot did not leave"], + "wrong-chip": ["not a T8132 (chip 0x6041): " + NOTHING], + "j713": ["internal panel present: " + NOTHING], + "os-26-6-2": ["no qualified OS firmware for apple,t8132-dcp: " + NOTHING], + "os-27-0": ["no qualified OS firmware for apple,t8132-dcp: " + NOTHING], + "wrong-uuid": ["27.2 DCP image mismatch: " + NOTHING], + "disp0-clocks-missing": ["27.2 /arm-io/disp0 clock metadata is missing or invalid"], + "no-display-dt": ["the device tree does not describe the display block"], + "dt-full": ["could not add the __OS_LOG reservations", "dcpext0 firmware memory refused"], +} + + +def test_every_scenario_has_its_reason(): + assert sorted(REASONS) == sorted(SCENARIOS) + + +@pytest.fixture(scope="module") +def host(tmp_path_factory): + return build_host(tmp_path_factory.mktemp("t8132-27"), host_source=HOST) + + +def test_scenarios_cover_the_failure_model(): + for required in ("publish", "repeat", "display-preenabled", "static-clock-nodes", + "ext-uuid-mismatch", "ext-text-not-shared", "ext-clock-missing", + "ext-clock-conflict", "ext-all-refused", "j713", "os-26-6-2", + "wrong-uuid", "dt-full"): + assert required in SCENARIOS + + +@pytest.mark.parametrize("scenario", SCENARIOS) +def test_t8132_j773g_27_2_display_handoff(host, scenario): + result = run(host, scenario) + assert result.returncode == 0, result.stdout + result.stderr + assert result.stdout.rstrip().endswith(f"{scenario}: pass"), result.stdout + assert "FAIL" not in result.stdout, result.stdout + for reason in REASONS[scenario]: + assert reason in result.stdout, (reason, result.stdout) diff --git a/tests/t6041_display_host.c b/tests/t6041_display_host.c index 4096e95e1..1dabe8abd 100644 --- a/tests/t6041_display_host.c +++ b/tests/t6041_display_host.c @@ -726,6 +726,7 @@ static const char OTHER[] = "apple,j999s\0apple,t6040\0apple,arm-platform"; static const char J614S[] = "apple,j614s\0apple,t6040\0apple,arm-platform"; static const char J616C[] = "apple,j616c\0apple,t6041\0apple,arm-platform"; static const char J614C[] = "apple,j614c\0apple,t6041\0apple,arm-platform"; +static const char J575C[] = "apple,j575c\0apple,t6041\0apple,arm-platform"; /* ---- checks ---- */ @@ -989,9 +990,10 @@ static void refusal_check(const void *in, const void *dt, bool oslog) enum kind { PUBLISH, - NOTHING, /* refused by the chip, board, firmware or DT gate: no access at all, the DT - byte-identical */ - REFUSE, /* refused after the gates, __OS_LOG reserved */ + HEADLESS, /* no internal panel (ADT disp0 external): external processors + display enabled */ + NOTHING, /* refused by the chip, board, firmware or DT gate: no access at all, the DT + byte-identical */ + REFUSE, /* refused after the gates, __OS_LOG reserved */ REFUSE_NO_OSLOG, }; @@ -1022,6 +1024,7 @@ static const struct scenario scenarios[] = { {"j614c", PUBLISH, "a J614C (chip T6041) boot publishes"}, {"j614c-external", PUBLISH, "J614C external firmware memories come from the ADT"}, {"j614c-on-t6040", NOTHING, "chip T6040 with a J614C device tree"}, + {"j575c-headless", HEADLESS, "J575C headless (disp0 external): external processors only"}, {"no-display-dt", NOTHING, "a J616s device tree without the display block"}, {"no-dcp-alias", NOTHING, "a J616s device tree without the dcp alias"}, {"wrong-dcp-compat", NOTHING, "dcp is not apple,t6041-dcp"}, @@ -1095,17 +1098,20 @@ int main(int argc, char **argv) world_j616s(&o); static u8 dt[DT_SIZE], in[DT_SIZE]; - bool j616c = !strncmp(s, "j616c", 5), j614c = !strncmp(s, "j614c", 5); + bool j616c = !strncmp(s, "j616c", 5), j614c = !strncmp(s, "j614c", 5), + j575c = !strncmp(s, "j575c", 5); _Static_assert(sizeof(J616C) == sizeof(J616S), "root compatibles of equal length"); _Static_assert(sizeof(J614C) == sizeof(J616S), "root compatibles of equal length"); + _Static_assert(sizeof(J575C) == sizeof(J616S), "root compatibles of equal length"); const char *board = is(s, "wrong-board") ? OTHER : (is(s, "j614-no-optin") || is(s, "j614-optin")) ? J614S : j616c ? J616C : j614c ? J614C + : j575c ? J575C : J616S; build_dt(dt, board, sizeof(J616S)); if (is(s, "external") || is(s, "external-refuse") || is(s, "j616c-external") || - is(s, "j614c-external")) { + is(s, "j614c-external") || is(s, "j575c-headless")) { for (int i = 0; i < 2; i++) { build_ext(dt, i); world_ext(i); @@ -1116,8 +1122,14 @@ int main(int argc, char **argv) /* ADT side */ if (is(s, "wrong-chip") || (j616c && !is(s, "j616c-on-t6040")) || - (j614c && !is(s, "j614c-on-t6040"))) + (j614c && !is(s, "j614c-on-t6040")) || j575c) chip_id = T6041; + if (is(s, "j575c-headless")) { + prop_set("/arm-io/disp0", "external", "", 0); + fb_base = TB + 0x2a0000000ULL; + cur_boot_args.video.stride = 0x4000; + cur_boot_args.video.height = 0x200000 / 0x4000; + } if (is(s, "no-adt-nub")) node_drop("/arm-io/dcp/iop-dcp-nub"); if (is(s, "no-adt-dart-dcp")) @@ -1212,7 +1224,7 @@ int main(int argc, char **argv) check(exc_count == 0, "no MMIO access faulted"); if (is(s, "external") || is(s, "external-refuse") || is(s, "j616c-external") || - is(s, "j614c-external")) { + is(s, "j614c-external") || is(s, "j575c-headless")) { for (int i = 0; i < 2; i++) { char path[40]; snprintf(path, sizeof(path), "/dcp@%llx", (unsigned long long)(i ? EXT1 : EXT0)); @@ -1295,6 +1307,24 @@ int main(int argc, char **argv) check(mmio_writes == 1 && last_write == DART_DCP + 0x200, "the only MMIO write is the dart-dcp lock"); break; + case HEADLESS: { + int dcp_n = alias_node(dt, "dcp"), disp_n = alias_node(dt, "disp0"); + int dart_disp = fdt_node_offset_by_phandle(dt, PH_DART_DISP); + int fb_idx = fdt_stringlist_search(dt, disp_n, "memory-region-names", "framebuffer"); + int fb_reg = fb_idx >= 0 ? region_at(dt, disp_n, fb_idx) : -1; + u64 b = 0, sz = 0; + check(dcp_n >= 0 && !marker_set(dt, dcp_n) && status_is(dt, dcp_n, "disabled"), + "headless: internal DCP untouched without handoff marker"); + check(disp_n >= 0 && status_is(dt, disp_n, "okay") && dart_disp >= 0 && + status_is(dt, dart_disp, "okay"), + "headless: display subsystem and its DART enabled"); + check(fb_reg >= 0 && !fdt_node_check_compatible(dt, fb_reg, "framebuffer") && + reg_of(dt, fb_reg, &b, &sz) && b == fb_base && sz == 0x200000, + "headless: display framebuffer region uses iBoot boot framebuffer"); + oslog_check(dt, true); + check(mmio_writes == 0, "headless: no MMIO write (dart-dcp not locked)"); + break; + } case NOTHING: check(fdt_totalsize(dt) == fdt_totalsize(in) && !memcmp(in, dt, fdt_totalsize(in)), "nothing done: the device tree is byte for byte unchanged"); diff --git a/tests/t8122_display_host/soc.h b/tests/t8122_display_host/soc.h index 0e751873d..4fc7d17e1 100644 --- a/tests/t8122_display_host/soc.h +++ b/tests/t8122_display_host/soc.h @@ -8,4 +8,5 @@ #define T6040 0x6040 #define T6041 0x6041 #define T6050 0x6050 +#define T8132 0x8132 #endif diff --git a/tests/t8132_j773g_27_display_host.c b/tests/t8132_j773g_27_display_host.c new file mode 100644 index 000000000..f484c6d2f --- /dev/null +++ b/tests/t8132_j773g_27_display_host.c @@ -0,0 +1,660 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Host test of the T8132 Mac mini (J773g) display handoff on macOS 27.2 (26B123): the internal + * DCP is not handed over, dcpext0 and dcpext1 are, as on the J575c, each with its own pipe's + * firmware clocks. tests/python/test_t8132_j773g_27_display_handoff.py has the failure model. + * + * Reuses the fake ADT, PMGR, MMIO and checks of t6041_display_host.c, with the T8132 register + * blocks, stream IDs and nub layouts of the J773g's 27.2 ADT. Physical addresses of firmware + * segments are synthetic; nothing here is Apple firmware data. + * + * t8132-j773g-27-display-host list + * t8132-j773g-27-display-host + */ +#define main desktop26_main +#include "t6041_display_host.c" +#undef main + +#define J773G_UUID "B3BA603C-3CA7-3E21-B393-44EB20BD2AEF" +#define OTHER_UUID "0F1E2D3C-4B5A-3968-8776-A5B4C3D2E1F0" +#define J773G_DCP 0x30ae00000ULL +#define J773G_DART_DCP 0x30a340000ULL +#define J773G_DART_DSP 0x30a300000ULL +#define J773G_PMGR 0x380700000ULL /* synthetic */ +#define J773G_PS_DISP (J773G_PMGR + 0x10000) +#define J773G_FB (TB + 0x2a0000000ULL) +#define J773G_FB_SIZE 0x200000ULL +#define EXT_TEXT_PHYS (TB + 0x280000000ULL) +#define EXT_TEXT_REMAP (TB + 0x1c4c000ULL) +#define EXT_TEXT_SIZE 0x10000 + +static const char J773G[] = "apple,j773g\0apple,t8132\0apple,arm-platform"; +static const char J713[] = "apple,j713\0apple,t8132\0apple,arm-platform"; +static const char OTHER_T8132[] = "apple,j999g\0apple,t8132\0apple,arm-platform"; +static const char J773G_DISP[] = "apple,t8132-display-subsystem\0apple,t8132-display-diagnostics"; +static const char T8132_DART[] = "apple,t8132-dart\0apple,t8110-dart"; + +static const u64 ext_base[2] = {0x286e00000ULL, 0x48ae00000ULL}; +static const u64 ext_dart[2] = {0x286340000ULL, 0x48a340000ULL}; +static const u32 ext_clk[2][2] = {{369, 432}, {372, 433}}; +#define EXT_HZ 533333328u + +/* The internal nub: __TEXT, __DATA, __OS_LOG and three heaps, as on the 27.2 J773g */ +static const struct seg dcp_segs[6] = { + {EXT_TEXT_PHYS, 0, EXT_TEXT_REMAP, EXT_TEXT_SIZE, 1}, + {TB + 0x281000000ULL, 0x10000, TB + 0x2490000, 0x8000, 0}, + {TB + 0x24a4000ULL, 0x18000, TB + 0x24a4000ULL, 0x20000, 0xa}, + {TB + 0x281100000ULL, ~0ULL, TB + 0xb70000, 0x40000, 2}, + {TB + 0x281180000ULL, ~0ULL, TB + 0x1b88000, 0x4000, 2}, + {TB + 0x2811c0000ULL, ~0ULL, TB + 0x1b8c000, 0xc000, 2}, +}; + +static u64 ext_data(int i) +{ + return TB + 0x281200000ULL + i * 0x200000; +} +static u64 ext_heap(int i) +{ + return TB + 0x282200000ULL + i * 0x200000; +} +static u64 ext_oslog(int i) +{ + return TB + 0x28030000ULL + i * 0x40000; +} +/* Every processor has its own DART: data and heap are at the same DVAs on both, as on the Mac */ +#define EXT_DATA_REMAP (TB + 0x2490000ULL) +#define EXT_HEAP_REMAP (TB + 0x27bc000ULL) +#define EXT_DATA_SIZE 0x8000 +#define EXT_HEAP_SIZE 0x10000 + +static void world_j773g(void) +{ + chip_id = T8132; + os_firmware = (struct fw_version_info){V27_2, "27.2", {27, 2, 0}, 3, "mBoot-20457.41.3"}; + ram_base = TB + 0x800000; + ram_size = 0x200000000ULL - 0x800000; + prop_u64("/chosen", "dram-base", TB); + prop_u64("/chosen", "dram-size", 0x300000000ULL); + mcc_carveout_count = 0; + fb_base = J773G_FB; + cur_boot_args.video.stride = 0x4000; + cur_boot_args.video.height = J773G_FB_SIZE / 0x4000; + + const char *nub = "/arm-io/dcp/iop-dcp-nub"; + prop_set(nub, "segment-ranges", dcp_segs, sizeof(dcp_segs)); + prop_str(nub, "segment-names", "__TEXT;__DATA;__OS_LOG;unknown;unknown;unknown"); + prop_str(nub, "uuid", J773G_UUID); + prop_u32(nub, "pre-loaded", 1); + prop_u32(nub, "running", 1); + + reg_add("/arm-io/dcp", J773G_DCP, 0x88000); + prop_str("/arm-io/dcp", "compatible", "iop,ascwrap-v6"); + prop_str("/arm-io/dart-dcp", "compatible", "dart,t8110"); + reg_add("/arm-io/dart-dcp", J773G_DART_DCP, 0x20000); + prop_u64("/arm-io/dart-dcp", "vm-base", TB); + prop_u64("/arm-io/dart-dcp", "vm-size", 0x30000000000ULL); + prop_str("/arm-io/dart-disp0", "compatible", "dart,t8110"); + reg_add("/arm-io/dart-disp0", J773G_DART_DSP, 0xc000); + prop_u64("/arm-io/dart-disp0", "vm-base", TB); + prop_u64("/arm-io/dart-disp0", "vm-size", 0x30000000000ULL); + prop_str("/arm-io/disp0", "compatible", "disp0,t8132"); + prop_set("/arm-io/disp0", "external", "", 0); + + u32 hz[300] = {0}; + hz[364 - 256] = 300000000; + hz[ext_clk[0][0] - 256] = EXT_HZ; + hz[ext_clk[1][0] - 256] = EXT_HZ; + prop_set("/arm-io", "clock-frequencies", hz, sizeof(hz)); + u32 disp0_ids[] = {364, 431}; + prop_set("/arm-io/disp0", "clock-ids", disp0_ids, sizeof(disp0_ids)); + + nfdevs = 0; + fdev_add(0x10, "DISP_FE", false, J773G_PMGR + 0x208, 0); + fdev_add(0x11, "DISP_CPU", false, J773G_PS_DISP, 0x10); + fdev_add(0x12, "DCP_VIRT", true, 0, 0x11); + u32 dcp_cg[1] = {0x12}; + prop_set("/arm-io/dcp", "clock-gates", dcp_cg, sizeof(dcp_cg)); + + region_add(J773G_PMGR, 0x14000); + poke32(J773G_PS_DISP, 0xff); + poke32(J773G_PMGR + 0x208, 0xff); + region_add(J773G_DCP, 0x4000); + poke32(J773G_DCP + 0x44, 0x10); + poke32(J773G_DCP + 0x48, 0x3000ac); + region_add(J773G_DART_DCP, 0x4000); + region_add(J773G_DART_DSP, 0x4000); + poke32(J773G_DART_DCP + 0x200, 0x1e1); + poke32(J773G_DART_DSP + 0x200, 0x1e1); +} + +static void world_ext_j773g(int i) +{ + struct seg ext[4] = { + {EXT_TEXT_PHYS, 0, EXT_TEXT_REMAP, EXT_TEXT_SIZE, 1}, + {ext_data(i), 0x10000, EXT_DATA_REMAP, EXT_DATA_SIZE, 0}, + {ext_oslog(i), 0x18000, ext_oslog(i), 0x4000, 0xa}, + {ext_heap(i), ~0ULL, EXT_HEAP_REMAP, EXT_HEAP_SIZE, 2}, + }; + char path[96]; + snprintf(path, sizeof(path), "/arm-io/dcpext%d", i); + reg_add(path, ext_base[i], 0x88000); + snprintf(path, sizeof(path), "/arm-io/dart-dcpext%d", i); + reg_add(path, ext_dart[i], 0x20000); + snprintf(path, sizeof(path), "/arm-io/dcpext%d/iop-dcpext%d-nub", i, i); + prop_set(path, "segment-ranges", ext, sizeof(ext)); + prop_str(path, "segment-names", "__TEXT;__DATA;__OS_LOG;unknown"); + prop_str(path, "uuid", J773G_UUID); + prop_u32(path, "pre-loaded", 1); + prop_u32(path, "running", 1); + snprintf(path, sizeof(path), "/arm-io/dispext%d", i); + prop_set(path, "clock-ids", ext_clk[i], sizeof(ext_clk[i])); +} + +/* ---- the J773g device tree on 27.2, as t8132-dcp.dtsi and t8132-dcpext.dtsi describe it ---- */ + +enum { PH_J_DART_DCP = 1, PH_J_DART_DISP, PH_J_MBOX, PH_J_STATIC_CLK = 60 }; + +static int t8132_dart(void *dt, int parent, const char *name, u64 base, u32 ph) +{ + int n = OK(fdt_add_subnode(dt, parent, name)); + OK(fdt_setprop(dt, n, "compatible", T8132_DART, sizeof(T8132_DART))); + reg2(dt, n, base, 0x4000); + OK(fdt_setprop_u32(dt, n, "#iommu-cells", 1)); + fdt32_t range[4] = {cpu_to_fdt32(TB >> 32), 0, cpu_to_fdt32(0x10), 0}; + OK(fdt_setprop(dt, n, "apple,dma-range", range, sizeof(range))); + OK(fdt_setprop_string(dt, n, "status", "disabled")); + OK(fdt_setprop_u32(dt, n, "phandle", ph)); + return n; +} + +static void build_dt_j773g(void *dt, const char *root, size_t root_len) +{ + OK(fdt_create_empty_tree(dt, DT_SIZE)); + OK(fdt_setprop(dt, 0, "compatible", root, root_len)); + cells(dt, 0); + OK(fdt_add_subnode(dt, 0, "chosen")); + int resv = OK(fdt_add_subnode(dt, 0, "reserved-memory")); + cells(dt, resv); + OK(fdt_setprop_empty(dt, resv, "ranges")); + + int soc = OK(fdt_add_subnode(dt, 0, "soc")); + cells(dt, soc); + OK(fdt_setprop_empty(dt, soc, "ranges")); + t8132_dart(dt, soc, "iommu@30a340000", J773G_DART_DCP, PH_J_DART_DCP); + t8132_dart(dt, fdt_path_offset(dt, "/soc"), "iommu@30a300000", J773G_DART_DSP, PH_J_DART_DISP); + int mbox = OK(fdt_add_subnode(dt, fdt_path_offset(dt, "/soc"), "mailbox@30ae08000")); + OK(fdt_setprop_string(dt, mbox, "compatible", "apple,asc-mailbox-v4")); + OK(fdt_setprop_string(dt, mbox, "status", "disabled")); + OK(fdt_setprop_u32(dt, mbox, "phandle", PH_J_MBOX)); + + int dcp = OK(fdt_add_subnode(dt, fdt_path_offset(dt, "/soc"), "dcp@30ae00000")); + OK(fdt_setprop_string(dt, dcp, "compatible", "apple,t8132-dcp")); + reg2(dt, dcp, J773G_DCP, 0x4000); + iommus(dt, dcp, PH_J_DART_DCP, 23); + OK(fdt_setprop_u32(dt, dcp, "mboxes", PH_J_MBOX)); + OK(fdt_setprop_string(dt, dcp, "status", "disabled")); + int pio = OK(fdt_add_subnode(dt, dcp, "piodma")); + iommus(dt, pio, PH_J_DART_DISP, 16); + + int disp = OK(fdt_add_subnode(dt, 0, "display-subsystem")); + OK(fdt_setprop(dt, disp, "compatible", J773G_DISP, sizeof(J773G_DISP))); + iommus(dt, disp, PH_J_DART_DISP, 0); + OK(fdt_setprop_string(dt, disp, "status", "disabled")); + + int aliases = OK(fdt_add_subnode(dt, 0, "aliases")); + OK(fdt_setprop_string(dt, aliases, "dcp", "/soc/dcp@30ae00000")); + OK(fdt_setprop_string(dt, aliases, "disp0", "/display-subsystem")); + OK(fdt_setprop_string(dt, aliases, "disp0_piodma", "/soc/dcp@30ae00000/piodma")); +} + +static void ext_path(char *buf, size_t len, int i) +{ + snprintf(buf, len, "/soc/dcp@%llx", (unsigned long long)ext_base[i]); +} + +static void build_ext_j773g(void *dt, int i) +{ + char name[64], path[96]; + snprintf(name, sizeof(name), "iommu@%llx", (unsigned long long)ext_dart[i]); + t8132_dart(dt, fdt_path_offset(dt, "/soc"), name, ext_dart[i], 100 + i); + snprintf(name, sizeof(name), "dcp@%llx", (unsigned long long)ext_base[i]); + int dcp = OK(fdt_add_subnode(dt, fdt_path_offset(dt, "/soc"), name)); + OK(fdt_setprop_string(dt, dcp, "compatible", "apple,t8132-dcpext")); + fdt64_t reg[4] = {cpu_to_fdt64(ext_base[i]), cpu_to_fdt64(0x4000), + cpu_to_fdt64(i ? 0x288000000ULL : 0x284000000ULL), cpu_to_fdt64(0xc20000)}; + OK(fdt_setprop(dt, dcp, "reg", reg, sizeof(reg))); + iommus(dt, dcp, 100 + i, 23); + OK(fdt_setprop_u32(dt, dcp, "apple,dcp-index", i + 1)); + OK(fdt_setprop_string(dt, dcp, "status", "disabled")); + fdt32_t mr[3]; + static const char *const parts[] = {"text", "data", "heap"}; + for (int j = 0; j < 3; j++) { + snprintf(name, sizeof(name), "dcpext%d-%s", i, parts[j]); + int region = OK(fdt_add_subnode(dt, fdt_path_offset(dt, "/reserved-memory"), name)); + OK(fdt_setprop_string(dt, region, "compatible", "apple,asc-mem")); + reg2(dt, region, 0, 0x4000); + OK(fdt_setprop_empty(dt, region, "no-map")); + OK(fdt_setprop_string(dt, region, "status", "disabled")); + OK(fdt_setprop_u32(dt, region, "phandle", 110 + i * 3 + j)); + mr[j] = cpu_to_fdt32(110 + i * 3 + j); + } + ext_path(path, sizeof(path), i); + dcp = fdt_path_offset(dt, path); + OK(fdt_setprop(dt, dcp, "memory-region", mr, sizeof(mr))); + static const char names[] = "text\0data\0heap"; + OK(fdt_setprop(dt, dcp, "memory-region-names", names, sizeof(names))); +} + +/* The 26.6.2 J773g tree's static firmware clock for dispext0, on dcpext0 */ +static void add_static_ext_clock(void *dt, u32 hz) +{ + int clk = OK(fdt_add_subnode(dt, 0, "clock-dcp-firmware-369")); + OK(fdt_setprop_string(dt, clk, "compatible", "fixed-clock")); + OK(fdt_setprop_u32(dt, clk, "#clock-cells", 0)); + OK(fdt_setprop_u32(dt, clk, "clock-frequency", hz)); + OK(fdt_setprop_u32(dt, clk, "phandle", PH_J_STATIC_CLK)); + char path[96]; + ext_path(path, sizeof(path), 0); + OK(fdt_setprop_u32(dt, fdt_path_offset(dt, path), "clocks", PH_J_STATIC_CLK)); +} + +/* ---- checks ---- */ + +/* Every property of @path is the same in @in and @dt */ +static bool props_unchanged(const void *in, const void *dt, const char *path) +{ + int a = fdt_path_offset(in, path), b = fdt_path_offset(dt, path), prop, n = 0, m = 0; + if (a < 0 || b < 0) + return false; + fdt_for_each_property_offset(prop, in, a) + { + const char *name; + int len, olen; + const void *v = fdt_getprop_by_offset(in, prop, &name, &len); + const void *ov = fdt_getprop(dt, b, name, &olen); + if (!ov || olen != len || memcmp(v, ov, len)) + return false; + n++; + } + fdt_for_each_property_offset(prop, dt, b) m++; + return n == m; +} + +static bool resv_at(const void *dt, const char *prefix, u64 base, u64 size) +{ + char name[64]; + snprintf(name, sizeof(name), "%s@%llx", prefix, (unsigned long long)base); + int node = fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), name); + u64 b, s; + return node >= 0 && reg_of(dt, node, &b, &s) && b == base && s == size && + fdt_getprop(dt, node, "no-map", NULL); +} + +static bool tuple_for(const void *dt, int region, int dev, u64 dva, u64 size) +{ + u64 got_size = 0; + return iommu_dva(dt, region, dev, &got_size) == dva && got_size == size; +} + +static void check_clock(const void *dt, int ext, int k, u32 id, u32 hz) +{ + int len = 0; + const fdt32_t *clocks = fdt_getprop(dt, ext, "clocks", &len); + int node = clocks && len > k * 4 ? fdt_node_offset_by_phandle(dt, fdt32_ld(clocks + k)) : -1; + char want[40]; + snprintf(want, sizeof(want), "clock-dcp-firmware-%u", id); + const char *name = node < 0 ? NULL : fdt_get_name(dt, node, NULL); + const fdt32_t *rate = node < 0 ? NULL : fdt_getprop(dt, node, "clock-frequency", &len); + const fdt32_t *cc = node < 0 ? NULL : fdt_getprop(dt, node, "#clock-cells", NULL); + check(node >= 0 && fdt_parent_offset(dt, node) == 0 && name && !strcmp(name, want) && + !fdt_node_check_compatible(dt, node, "fixed-clock") && cc && !fdt32_ld(cc) && rate && + len == 4 && fdt32_ld(rate) == hz, + "clock %d is /%s, a fixed-clock at %u Hz", k, want, hz); +} + +static int clock_nodes(const void *dt, u32 id) +{ + int n = 0, node; + char want[40]; + snprintf(want, sizeof(want), "clock-dcp-firmware-%u", id); + fdt_for_each_subnode(node, dt, 0) + { + const char *name = fdt_get_name(dt, node, NULL); + if (name && !strcmp(name, want)) + n++; + } + return n; +} + +static void check_ext(const void *in, const void *dt, int i, bool want) +{ + char path[96]; + ext_path(path, sizeof(path), i); + int ext = fdt_path_offset(dt, path), len = 0; + const fdt32_t *mr = ext < 0 ? NULL : fdt_getprop(dt, ext, "memory-region", &len); + int dart = fdt_node_offset_by_phandle(dt, 100 + i); + char dart_path[64]; + snprintf(dart_path, sizeof(dart_path), "/soc/iommu@%llx", (unsigned long long)ext_dart[i]); + + if (!want) { + check(props_unchanged(in, dt, path) && props_unchanged(in, dt, dart_path), + "dcpext%d refused: its node and DART are unchanged", i); + for (int j = 0; mr && len == 12 && j < 3; j++) { + int region = fdt_node_offset_by_phandle(dt, fdt32_ld(&mr[j])); + check(region >= 0 && status_is(dt, region, "disabled") && + !fdt_getprop(dt, region, "iommu-addresses", NULL), + "dcpext%d refused: placeholder %d stays disabled", i, j); + } + check(clock_nodes(dt, ext_clk[i][0]) == clock_nodes(in, ext_clk[i][0]) && + clock_nodes(dt, ext_clk[i][1]) == clock_nodes(in, ext_clk[i][1]), + "dcpext%d refused: no clock node added for its pipe", i); + return; + } + + check(ext >= 0 && mr && len == 12 && status_is(dt, ext, "disabled") && dart >= 0 && + status_is(dt, dart, "disabled"), + "dcpext%d and its DART stay disabled for the display gate", i); + if (ext < 0 || !mr || len != 12) + return; + + const u32 ver[3] = {27, 2, 0}; + const char *uuid = fdt_getprop(dt, ext, "apple,firmware-uuid", NULL); + check(u32s_are(dt, ext, "apple,firmware-version", ver, 3) && + u32s_are(dt, ext, "apple,firmware-compat", ver, 3) && uuid && + !strcmp(uuid, J773G_UUID), + "dcpext%d: firmware 27.2.0 and the pinned UUID", i); + check(u32s_are(dt, ext, "apple,clock-ids", ext_clk[i], 2) && + fdt_stringlist_count(dt, ext, "clock-names") == 2 && + fdt_stringlist_search(dt, ext, "clock-names", "firmware-0") == 0 && + fdt_stringlist_search(dt, ext, "clock-names", "firmware-1") == 1, + "dcpext%d: apple,clock-ids are /arm-io/dispext%d's, named firmware-0/1", i, i); + check_clock(dt, ext, 0, ext_clk[i][0], EXT_HZ); + check_clock(dt, ext, 1, ext_clk[i][1], 0); + + int text = fdt_node_offset_by_phandle(dt, fdt32_ld(&mr[0])); + const char *tname = text < 0 ? NULL : fdt_get_name(dt, text, NULL); + char want_text[48]; + snprintf(want_text, sizeof(want_text), "dcp-text@%llx", (unsigned long long)EXT_TEXT_PHYS); + check(tname && !strcmp(tname, want_text) && + !fdt_node_check_compatible(dt, text, "apple,asc-mem") && + tuple_for(dt, text, ext, EXT_TEXT_REMAP, EXT_TEXT_SIZE), + "dcpext%d: text is the shared dcp-text region, mapped for this processor", i); + char ph_name[24]; + snprintf(ph_name, sizeof(ph_name), "dcpext%d-text", i); + check(fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), ph_name) < 0, + "dcpext%d: the text placeholder is gone", i); + const u64 phys[3] = {0, ext_data(i), ext_heap(i)}; + const u64 dva[3] = {0, EXT_DATA_REMAP, EXT_HEAP_REMAP}; + const u64 size[3] = {0, EXT_DATA_SIZE, EXT_HEAP_SIZE}; + for (int j = 1; j < 3; j++) { + int region = fdt_node_offset_by_phandle(dt, fdt32_ld(&mr[j])); + u64 b = 0, s = 0; + check(region >= 0 && status_is(dt, region, "okay") && reg_of(dt, region, &b, &s) && + b == phys[j] && s == size[j] && tuple_for(dt, region, ext, dva[j], size[j]), + "dcpext%d: %s is the ADT span, enabled, at its DART alias", i, + j == 1 ? "data" : "heap"); + } +} + +static void check_display(const void *in, const void *dt, bool want) +{ + int disp = alias_node(dt, "disp0"); + int dart = fdt_node_offset_by_phandle(dt, PH_J_DART_DISP); + if (!want) { + check(props_unchanged(in, dt, "/display-subsystem"), + "display subsystem left as the device tree has it"); + check(dart >= 0 && props_unchanged(in, dt, "/soc/iommu@30a300000"), + "display DART left as the device tree has it"); + return; + } + int fb = fdt_stringlist_search(dt, disp, "memory-region-names", "framebuffer"); + int region = fb >= 0 ? region_at(dt, disp, fb) : -1; + u64 b = 0, s = 0; + check(status_is(dt, disp, "okay") && dart >= 0 && status_is(dt, dart, "okay"), + "display subsystem and its DART enabled"); + check(region >= 0 && !fdt_node_check_compatible(dt, region, "framebuffer") && + reg_of(dt, region, &b, &s) && b == J773G_FB && s == J773G_FB_SIZE, + "the display's framebuffer memory-region is iBoot's boot framebuffer"); +} + +/* The internal DCP is not handed over: its node, DARTs and mailbox are as the DT had them */ +static void check_main_untouched(const void *in, const void *dt) +{ + check(props_unchanged(in, dt, "/soc/dcp@30ae00000") && + props_unchanged(in, dt, "/soc/dcp@30ae00000/piodma") && + props_unchanged(in, dt, "/soc/iommu@30a340000") && + props_unchanged(in, dt, "/soc/mailbox@30ae08000"), + "internal DCP, its piodma, DART and mailbox unchanged"); + check(mmio_writes == 0, "no MMIO write (dart-dcp not locked)"); +} + +static void check_oslogs(const void *dt, bool ext0, bool ext1) +{ + check(resv_at(dt, "dcp-oslog", dcp_segs[2].phys, ALIGN_UP((u64)dcp_segs[2].size, SZ_16K)), + "dcp __OS_LOG reserved"); + check(resv_at(dt, "dcp-oslog", ext_oslog(0), 0x4000) == ext0, "dcpext0 __OS_LOG %sreserved", + ext0 ? "" : "not "); + check(resv_at(dt, "dcp-oslog", ext_oslog(1), 0x4000) == ext1, "dcpext1 __OS_LOG %sreserved", + ext1 ? "" : "not "); +} + +/* ---- scenarios ---- */ + +enum j773g_kind { + J_PUBLISH, /* expectation per processor in the table */ + J_NOTHING, /* gate: no access at all, the DT byte for byte unchanged */ +}; + +struct j773g_scenario { + const char *name; + enum j773g_kind kind; + bool ext0, ext1, display; + const char *what; +}; + +static const struct j773g_scenario j773g_scenarios[] = { + {"publish", J_PUBLISH, true, true, true, "both external processors, the display enabled"}, + {"other-headless-board", J_PUBLISH, true, true, true, + "another headless T8132 board takes the path with no board table entry"}, + {"dcpext0-only", J_PUBLISH, true, false, true, "a tree with only dcpext0 described"}, + {"repeat", J_PUBLISH, true, true, true, "a second call changes nothing"}, + {"static-clock-nodes", J_PUBLISH, true, true, true, + "the 26.6.2 tree's static clock node and clocks on dcpext0 are reused"}, + {"display-preenabled", J_PUBLISH, true, true, false, + "the 26.6.2 tree enables the display: left alone, processors still published"}, + {"ext-uuid-mismatch", J_PUBLISH, true, false, true, "dcpext1 runs another image"}, + {"ext-not-running", J_PUBLISH, false, true, true, "dcpext0 is not running"}, + {"ext-enabled", J_PUBLISH, false, true, true, "dcpext0's node is enabled"}, + {"ext-dart-enabled", J_PUBLISH, false, true, true, "dcpext0's DART is enabled"}, + {"ext-wrong-compat", J_PUBLISH, false, true, true, "dcpext0 is not apple,t8132-dcpext"}, + {"ext-no-placeholders", J_PUBLISH, false, true, true, "dcpext0 has no memory-region"}, + {"ext-text-not-shared", J_PUBLISH, true, false, true, "dcpext1's __TEXT is its own"}, + {"ext-data-in-ram", J_PUBLISH, false, true, true, "dcpext0's __DATA is kernel RAM"}, + {"ext-clock-missing", J_PUBLISH, false, true, true, "/arm-io/dispext0 has no clock-ids"}, + {"ext-clock-zero", J_PUBLISH, false, true, true, "dispext0's first clock has no rate"}, + {"ext-clock-names", J_PUBLISH, false, true, true, "dcpext0 already has clock-names"}, + {"ext-clock-conflict", J_PUBLISH, false, true, true, + "/clock-dcp-firmware-369 exists with another rate"}, + {"ext-all-refused", J_PUBLISH, false, false, false, "no processor passes: display untouched"}, + {"wrong-chip", J_NOTHING, false, false, false, "chip T6041 with a J773g tree"}, + {"j713", J_NOTHING, false, false, false, + "a T8132 with an internal panel (disp0 not external) is left untouched"}, + {"os-26-6-2", J_NOTHING, false, false, false, "26.6.2 firmware"}, + {"os-27-0", J_NOTHING, false, false, false, "27.0 firmware"}, + {"wrong-uuid", J_NOTHING, false, false, false, "the internal nub runs another image"}, + {"disp0-clocks-missing", J_NOTHING, false, false, false, "/arm-io/disp0 has no clock-ids"}, + {"no-display-dt", J_NOTHING, false, false, false, "no dcp alias"}, + {"dt-full", J_PUBLISH, false, false, false, "no room: refused without partial edits"}, +}; + +int main(int argc, char **argv) +{ + if (argc == 2 && is(argv[1], "list")) { + for (size_t i = 0; i < ARRAY_SIZE(j773g_scenarios); i++) + printf("%s\n", j773g_scenarios[i].name); + return 0; + } + if (argc != 2) + return 2; + const struct j773g_scenario *sc = NULL; + for (size_t i = 0; i < ARRAY_SIZE(j773g_scenarios); i++) + if (is(argv[1], j773g_scenarios[i].name)) + sc = &j773g_scenarios[i]; + if (!sc) + return 2; + const char *s = sc->name; + + world_j773g(); + static u8 dt[DT_SIZE], in[DT_SIZE]; + _Static_assert(sizeof(J713) < sizeof(J773G), "J713 root fits"); + if (is(s, "j713")) { + build_dt_j773g(dt, J713, sizeof(J713)); + prop_drop("/arm-io/disp0", "external"); + } else if (is(s, "other-headless-board")) { + build_dt_j773g(dt, OTHER_T8132, sizeof(OTHER_T8132)); + } else { + build_dt_j773g(dt, J773G, sizeof(J773G)); + } + int next = is(s, "dcpext0-only") ? 1 : 2; + for (int i = 0; i < next; i++) { + build_ext_j773g(dt, i); + world_ext_j773g(i); + } + if (is(s, "dcpext0-only")) + world_ext_j773g(1); /* the ADT has it; the tree does not describe it */ + + char path[96]; + + /* ADT side */ + if (is(s, "wrong-chip")) + chip_id = T6041; + if (is(s, "os-26-6-2")) + os_firmware = + (struct fw_version_info){V26_6_2, "26.6.2", {26, 6, 2}, 3, "mBoot-18000.161.10"}; + if (is(s, "os-27-0")) + os_firmware = (struct fw_version_info){V27_0, "27.0", {27, 0, 0}, 3, "mBoot-20457.1.29"}; + if (is(s, "wrong-uuid")) + prop_str("/arm-io/dcp/iop-dcp-nub", "uuid", OTHER_UUID); + if (is(s, "ext-uuid-mismatch")) + prop_str("/arm-io/dcpext1/iop-dcpext1-nub", "uuid", OTHER_UUID); + if (is(s, "disp0-clocks-missing")) + prop_drop("/arm-io/disp0", "clock-ids"); + if (is(s, "ext-not-running")) + prop_u32("/arm-io/dcpext0/iop-dcpext0-nub", "running", 0); + if (is(s, "ext-text-not-shared") || is(s, "ext-data-in-ram")) { + int i = is(s, "ext-text-not-shared") ? 1 : 0; + struct seg ext[4] = { + {EXT_TEXT_PHYS, 0, EXT_TEXT_REMAP, EXT_TEXT_SIZE, 1}, + {ext_data(i), 0x10000, EXT_DATA_REMAP, EXT_DATA_SIZE, 0}, + {ext_oslog(i), 0x18000, ext_oslog(i), 0x4000, 0xa}, + {ext_heap(i), ~0ULL, EXT_HEAP_REMAP, EXT_HEAP_SIZE, 2}, + }; + if (i == 1) + ext[0].phys = TB + 0x284000000ULL; + else + ext[1].phys = TB + 0x10000000ULL; + snprintf(path, sizeof(path), "/arm-io/dcpext%d/iop-dcpext%d-nub", i, i); + prop_set(path, "segment-ranges", ext, sizeof(ext)); + } + if (is(s, "ext-clock-missing")) + prop_drop("/arm-io/dispext0", "clock-ids"); + if (is(s, "ext-clock-zero")) { + u32 ids[] = {380, 432}; + prop_set("/arm-io/dispext0", "clock-ids", ids, sizeof(ids)); + } + if (is(s, "ext-all-refused")) { + prop_u32("/arm-io/dcpext0/iop-dcpext0-nub", "running", 0); + prop_u32("/arm-io/dcpext1/iop-dcpext1-nub", "running", 0); + } + + /* device tree side */ + if (is(s, "static-clock-nodes")) + add_static_ext_clock(dt, EXT_HZ); + if (is(s, "ext-clock-conflict")) + add_static_ext_clock(dt, 600000000); +#define EXT0_NODE fdt_path_offset(dt, "/soc/dcp@286e00000") + if (is(s, "display-preenabled")) { + OK(fdt_setprop_string(dt, alias_node(dt, "disp0"), "status", "okay")); + OK(fdt_setprop_string(dt, fdt_node_offset_by_phandle(dt, PH_J_DART_DISP), "status", + "okay")); + OK(fdt_setprop_string(dt, alias_node(dt, "dcp"), "status", "okay")); + } + if (is(s, "ext-enabled")) + OK(fdt_setprop_string(dt, EXT0_NODE, "status", "okay")); + if (is(s, "ext-dart-enabled")) + OK(fdt_setprop_string(dt, fdt_node_offset_by_phandle(dt, 100), "status", "okay")); + if (is(s, "ext-wrong-compat")) + OK(fdt_setprop_string(dt, EXT0_NODE, "compatible", "apple,t6041-dcpext")); + if (is(s, "ext-no-placeholders")) { + OK(fdt_delprop(dt, EXT0_NODE, "memory-region")); + OK(fdt_delprop(dt, EXT0_NODE, "memory-region-names")); + } + if (is(s, "ext-clock-names")) + OK(fdt_setprop_string(dt, EXT0_NODE, "clock-names", "firmware-0")); + if (is(s, "no-display-dt")) + OK(fdt_delprop(dt, fdt_path_offset(dt, "/aliases"), "dcp")); + if (is(s, "dt-full")) { + OK(fdt_pack(dt)); + OK(fdt_open_into(dt, dt, fdt_totalsize(dt) + 256)); + } else { + OK(fdt_pack(dt)); + OK(fdt_open_into(dt, dt, DT_SIZE)); + } + memcpy(in, dt, fdt_totalsize(dt)); + + printf("== %s: %s\n", s, sc->what); + fflush(stdout); + mmio_reads = mmio_writes = 0; + int ret = dt_set_display_handoff(dt); + fflush(stdout); + check(ret == 0, "dt_set_display_handoff() returns 0"); + check(exc_count == 0, "no MMIO access faulted"); + + if (sc->kind == J_NOTHING) { + check(fdt_totalsize(dt) == fdt_totalsize(in) && !memcmp(in, dt, fdt_totalsize(in)), + "nothing done: the device tree is byte for byte unchanged"); + check(mmio_reads == 0 && mmio_writes == 0, "nothing done: no MMIO access at all"); + } else { + check_main_untouched(in, dt); + if (is(s, "dt-full")) { + for (int i = 0; i < 2; i++) + check_ext(in, dt, i, false); + check_display(in, dt, false); + } else { + check_oslogs(dt, true, true); + check_ext(in, dt, 0, sc->ext0); + if (!is(s, "dcpext0-only")) + check_ext(in, dt, 1, sc->ext1); + check_display(in, dt, sc->display); + } + if (is(s, "static-clock-nodes")) + check(clock_nodes(dt, ext_clk[0][0]) == 1 && + fdt_node_offset_by_phandle(dt, PH_J_STATIC_CLK) >= 0, + "the static clock node is reused, not duplicated"); + if (is(s, "ext-uuid-mismatch") || is(s, "ext-text-not-shared")) { + int text = -1; + char want[48]; + snprintf(want, sizeof(want), "dcp-text@%llx", (unsigned long long)EXT_TEXT_PHYS); + text = fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), want); + ext_path(path, sizeof(path), 1); + u64 sz = 0; + check(text >= 0 && iommu_dva(dt, text, fdt_path_offset(dt, path), &sz) == ~0ULL, + "the refused dcpext1 has no tuple on the shared text"); + } + if (is(s, "repeat")) { + static u8 first[DT_SIZE]; + memcpy(first, dt, fdt_totalsize(dt)); + int writes = mmio_writes; + fflush(stdout); + ret = dt_set_display_handoff(dt); + fflush(stdout); + check(ret == 0 && fdt_totalsize(first) == fdt_totalsize(dt) && + !memcmp(first, dt, fdt_totalsize(first)), + "second call returns 0 and changes nothing in the device tree"); + check(mmio_writes == writes, "second call makes no MMIO write"); + } + } + + printf("%s: %s\n", s, failures ? "FAIL" : "pass"); + return failures ? 1 : 0; +}