From adbb8d2da983e2688f9b797a246cda030b63daeb Mon Sep 17 00:00:00 2001 From: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:31:43 -0800 Subject: [PATCH 1/4] kboot: J804: hand the running internal-panel DCP to Linux (opt-in) On J804 (MacBook Pro 14-inch, M6) iBoot leaves the internal panel's DCP running with its firmware mapped through locked DART tables. With j804.dcp=1 on the kernel command line, describe that state to Linux's DCP driver instead of leaving the panel on the simple-framebuffer: - check that the coprocessor runs, and that every page dart-dcp SID 23 and dart-disp0 SID 0 and SID 16 map belongs to a segment the ADT iop-dcp-nub segment-ranges name, at the right offset; - reserve the firmware segments for dcp, give disp0 and disp0_piodma the segments their tables map, with iommu-addresses; - publish apple,firmware-uuid from the ADT nub and enable dcp, disp0, disp0_piodma and their DARTs. T8152's OS log segment (flags 0xa), like T6030's and T8132's, runs at its physical address and is marked apple,dcp-os-log rather than mapped. The host tests that compile dt_reserve_asc_segments_at() define T8152 for that. Without j804.dcp=1, on any other board, or when a check fails, nothing is published and the display nodes stay disabled; the boot goes on. The tree is edited inside dt_display_transaction(), so a failure after the checks (firmware UUID, reserved memory, phandles, status writes) also leaves no partial handoff behind. An earlier version of this change, on an older base, was tested on a J804 with j804.dcp=1: Linux's DCP driver takes the panel over from simpledrm (eDP-1 3024x1890 at 120 Hz). Signed-off-by: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> --- src/kboot.c | 176 +++++++++++++++++++++- tests/python/test_isp_optional.py | 1 + tests/python/test_isp_reservation_full.py | 1 + tests/python/test_j713_dcpext_ready.py | 1 + tests/python/test_m4_dcp_handoff.py | 1 + tests/python/test_t8122_dcpext_ready.py | 1 + 6 files changed, 178 insertions(+), 3 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index 23e90d8e6..3c26a18b2 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -5050,13 +5050,13 @@ static int dt_reserve_asc_segments_at(int fdt_node, const char *adt_path, const uint32_t mem_phandle = fdt_get_phandle(dt, mem_node); /* - * The OS log segment (flags 0xa) of a T6030 or T8132 display coprocessor, + * The OS log segment (flags 0xa) of a T6030, T8132 or T8152 display coprocessor, * internal or external, runs at its physical address and is not reached * through the DART: reserve its RAM, marked as such, without an IOMMU * mapping. */ - if ((chip_id == T6030 || chip_id == T8132) && !strncmp(fdt_path, "dcp", 3) && - seg->unk == 0xa) + if ((chip_id == T6030 || chip_id == T8132 || chip_id == T8152) && + !strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa) ret = fdt_setprop_empty(dt, mem_node, "apple,dcp-os-log"); else ret = dt_device_set_reserved_mem(mem_node, node_name, dev_phandle, iova, seg_size); @@ -9417,6 +9417,173 @@ static int dt_set_j713_display_maps(const struct adt_segment_ranges *segments, u return 0; } +/* + * J804 (T8152) internal display handoff for Linux's DCP driver. Opt-in with the kernel argument + * j804.dcp=1; without it, or if any check or later step fails, nothing is published (the tree is + * edited in a dt_display_transaction()) and the display nodes stay disabled (the panel keeps the + * simple-framebuffer). + * + * iBoot leaves the DCP running with its firmware mapped through locked DART tables. The ADT + * iop-dcp-nub segment-ranges give each segment's physical address and, in "remap", its DVA in the + * DARTs' 1 TiB window, except __OS_LOG (flags 0xa), whose remap repeats the physical address. Every + * dart-dcp SID 23 page and every dart-disp0 SID 0 / SID 16 page is checked against those segments + * before anything is published: dcp gets one reserved region per segment, display (SID 0) and + * disp0_piodma (SID 16) get the flags-2 segments their tables map, with iommu-addresses. + */ +#define J804_DCP_COPROC 0x286e00000UL +#define J804_DART_DCP 0x286340000UL +#define J804_DART_DISP0 0x286300000UL +#define J804_DCP_SID 23 + +static int j804_dcp_page(u64 iova, u64 physical, void *opaque) +{ + struct j713_display_maps *maps = opaque; + for (u32 i = 0; i < maps->count; i++) { + const struct adt_segment_ranges *seg = &maps->segments[i]; + u64 start = seg->remap & ((1ULL << 36) - 1); + u64 size = ALIGN_UP((u64)seg->size, SZ_16K); + if (seg->unk == 0xa || iova < start || iova - start >= size) + continue; + if ((seg->remap >> 36) != 0x10 || physical != seg->phys + iova - start) + return -1; + maps->bytes[i] += SZ_16K; + return 0; + } + return -1; +} + +static bool dt_bootargs_have(const char *token) +{ + int node = fdt_path_offset(dt, "/chosen"), len = 0; + const char *args = node < 0 ? NULL : fdt_getprop(dt, node, "bootargs", &len); + size_t n = strlen(token); + + if (!args || len <= 0) + return false; + for (const char *p = args; (p = strstr(p, token)); p += n) + if ((p == args || dt_bootarg_space(p[-1])) && (!p[n] || dt_bootarg_space(p[n]))) + return true; + return false; +} + +static int dt_set_status_okay_iommu(const char *alias) +{ + int node = fdt_path_offset(dt, alias); + int dart = node < 0 ? -1 : dt_get_iommu_node(node, 0); + if (dart < 0 || fdt_setprop_string(dt, dart, "status", "okay")) + return -1; + node = fdt_path_offset(dt, alias); + if (node < 0 || fdt_setprop_string(dt, node, "status", "okay")) + return -1; + return 0; +} + +/* Runs in dt_display_transaction(): a failure at any step discards every edit it made. */ +static int dt_set_j804_dcp_handoff_one(unsigned int unused) +{ + (void)unused; + int nub = adt_path_offset(adt, "/arm-io/dcp/iop-dcp-nub"); + int chosen = adt_path_offset(adt, "/chosen"); + u64 dram_start, dram_size; + u32 len; + if (nub < 0 || chosen < 0 || ADT_GETPROP(adt, chosen, "dram-base", &dram_start) < 0 || + ADT_GETPROP(adt, chosen, "dram-size", &dram_size) < 0 || !dram_size) + bail("FDT: J804 DCP: missing ADT nub or DRAM bounds\n"); + const struct adt_segment_ranges *segments = adt_getprop(adt, nub, "segment-ranges", &len); + if (!segments || !len || len % sizeof(*segments) || len / sizeof(*segments) > 32) + bail("FDT: J804 DCP: invalid segment-ranges\n"); + u32 count = len / sizeof(*segments); + for (u32 i = 0; i < count; i++) { + u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K); + if (!size || (segments[i].phys | segments[i].remap) & (SZ_16K - 1) || + segments[i].phys < dram_start || segments[i].phys + size > dram_start + dram_size) + bail("FDT: J804 DCP: invalid segment %u\n", i); + } + + u32 cpu = read32(J804_DCP_COPROC + 0x44); + if (!(cpu & BIT(4))) + bail("FDT: J804 DCP: coprocessor is not running (CPU_CONTROL 0x%x)\n", cpu); + + /* Every page the running firmware maps must belong to a segment. */ + struct j713_display_maps dcpmap = {.segments = segments, .count = count}; + if (dart_walk_locked_t8110(J804_DART_DCP, 0x20000, J804_DCP_SID, dram_start, dram_size, + j804_dcp_page, &dcpmap)) + bail("FDT: J804 DCP: dart-dcp SID %u maps outside the firmware segments\n", J804_DCP_SID); + + const char *aliases[] = {"disp0", "disp0_piodma"}; + const u8 streams[] = {0, 16}; + struct j713_display_maps maps[2] = { + {.segments = segments, .count = count}, + {.segments = segments, .count = count}, + }; + for (u32 n = 0; n < 2; n++) { + if (dart_walk_locked_t8110(J804_DART_DISP0, 0xc000, streams[n], dram_start, dram_size, + j713_display_page, &maps[n])) + bail("FDT: J804 DCP: dart-disp0 SID %u maps outside the firmware segments\n", + streams[n]); + for (u32 i = 0; i < count; i++) + if (maps[n].bytes[i] && maps[n].bytes[i] != ALIGN_UP((u64)segments[i].size, SZ_16K)) + bail("FDT: J804 DCP: SID %u maps segment %u partially\n", streams[n], i); + } + + const char *uuid = adt_getprop(adt, nub, "uuid", &len); + if (!uuid || len != 37 || uuid[len - 1] || + fdt_setprop_string(dt, fdt_path_offset(dt, "dcp"), "apple,firmware-uuid", uuid)) + bail("FDT: J804 DCP: cannot publish the firmware UUID\n"); + + if (dt_reserve_asc_firmware("/arm-io/dcp/iop-dcp-nub", NULL, "dcp", true, 0)) + bail("FDT: J804 DCP: firmware reservation failed\n"); + + u32 phandles[2]; + for (u32 n = 0; n < 2; n++) { + int node = fdt_path_offset(dt, aliases[n]); + if (node < 0) + bail("FDT: J804 DCP: no %s alias\n", aliases[n]); + phandles[n] = fdt_get_phandle(dt, node); + if (!phandles[n] && (fdt_generate_phandle(dt, &phandles[n]) || + fdt_setprop_u32(dt, node, "phandle", phandles[n]))) + bail("FDT: J804 DCP: no phandle for %s\n", aliases[n]); + } + for (u32 i = 0; i < count; i++) { + if (!maps[0].bytes[i] && !maps[1].bytes[i]) + continue; + char name[64]; + snprintf(name, sizeof(name), "asc-firmware@%lx", segments[i].phys); + u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K); + int node = dt_get_or_add_reserved_mem(name, "apple,asc-mem", true, segments[i].phys, size); + if (node < 0) + return -1; + u32 phandle = fdt_get_phandle(dt, node); + for (u32 n = 0; n < 2; n++) { + if (!maps[n].bytes[i]) + continue; + node = fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), name); + if (node < 0 || + dt_device_set_reserved_mem(node, name, phandles[n], segments[i].remap, size) || + dt_device_add_mem_region(aliases[n], phandle, NULL)) + return -1; + } + } + + if (dt_set_status_okay_iommu("dcp") || dt_set_status_okay_iommu("disp0") || + dt_set_status_okay_iommu("disp0_piodma")) + bail("FDT: J804 DCP: could not enable the display nodes\n"); + printf("FDT: J804 DCP handoff published (%u segments, firmware %s)\n", count, uuid); + return 0; +} + +static int dt_set_j804_dcp_handoff(void) +{ + int dcp = fdt_path_offset(dt, "dcp"); + if (board_id != 0x22 || dcp < 0 || fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp")) + return 0; + if (!dt_bootargs_have("j804.dcp=1")) { + printf("FDT: J804 DCP handoff not requested (j804.dcp=1)\n"); + return 0; + } + return dt_display_transaction(dt_set_j804_dcp_handoff_one, 0); +} + static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine); /* @@ -14159,6 +14326,9 @@ static int dt_prepare_t8152(void) if (dt_set_serial_number() || dt_set_display_t8152() || dt_set_live_afk_t8152() || dt_set_gpu(dt)) return -1; + /* Fail-soft: a refused handoff leaves the display nodes disabled. */ + if (dt_set_j804_dcp_handoff()) + printf("FDT: J804 DCP handoff not published\n"); dt_set_dcpext_t8152(); } if (dt_set_dcp_state_t8152()) diff --git a/tests/python/test_isp_optional.py b/tests/python/test_isp_optional.py index 220637474..7969f5a26 100644 --- a/tests/python/test_isp_optional.py +++ b/tests/python/test_isp_optional.py @@ -42,6 +42,7 @@ def function(name): #define T8140 0x8140 #define T8142 0x8142 #define T8132 0x8132 +#define T8152 0x8152 #define SZ_16K 16384 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) diff --git a/tests/python/test_isp_reservation_full.py b/tests/python/test_isp_reservation_full.py index 08a4cca2d..e36390b75 100644 --- a/tests/python/test_isp_reservation_full.py +++ b/tests/python/test_isp_reservation_full.py @@ -31,6 +31,7 @@ def definition(signature): #define T6030 0x6030 #define T6041 0x6041 #define T8132 0x8132 +#define T8152 0x8152 /* Only the dram_only segment filter reads properties this way. */ #define ADT_GETPROP(a, n, k, v) ((size_t)0) static u32 chip_id; diff --git a/tests/python/test_j713_dcpext_ready.py b/tests/python/test_j713_dcpext_ready.py index 489f9d78b..f67bbc07a 100644 --- a/tests/python/test_j713_dcpext_ready.py +++ b/tests/python/test_j713_dcpext_ready.py @@ -45,6 +45,7 @@ def define(source, name): #define T8122 0x8122 #define T6030 0x6030 #define T8132 0x8132 +#define T8152 0x8152 #define T6000 0x6000 #define T6001 0x6001 #define T6002 0x6002 diff --git a/tests/python/test_m4_dcp_handoff.py b/tests/python/test_m4_dcp_handoff.py index cf3cb0f58..6fe6c453f 100644 --- a/tests/python/test_m4_dcp_handoff.py +++ b/tests/python/test_m4_dcp_handoff.py @@ -62,6 +62,7 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path): #define PACKED __attribute__((packed)) #define T6030 0x6030 #define T8132 0x8132 +#define T8152 0x8152 #define T8122 0x8122 #define SZ_16K 0x4000 #define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((u64)(a) - 1)) diff --git a/tests/python/test_t8122_dcpext_ready.py b/tests/python/test_t8122_dcpext_ready.py index 4233794c0..3fcbc3962 100644 --- a/tests/python/test_t8122_dcpext_ready.py +++ b/tests/python/test_t8122_dcpext_ready.py @@ -35,6 +35,7 @@ def function(name, kind="static int "): #define T8122 0x8122 #define T6030 0x6030 #define T8132 0x8132 +#define T8152 0x8152 #define T6000 0x6000 #define T6001 0x6001 #define T6002 0x6002 From 43862f0c209b56b33bcd2250c0161b43dc215c85 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:01:59 +1100 Subject: [PATCH 2/4] kboot: T8152: select the internal DCP handoff by its node, bases from the ADT Rework of the J804 internal-panel DCP handoff (aura#289, LiamRay11) for DjDeveloperr's review of 26de0ac2: - No boot argument and no board check. The handoff runs on every T8152 tree whose "dcp" alias names an apple,t8152-dcp node, inside dt_display_transaction(): a failed check or step publishes nothing and the display nodes stay disabled (the panel keeps simple-framebuffer). A J873g tree (apple,t8152-dcp-iboot) is not a target. The j804.dcp=1 switch and dt_bootargs_have() are gone, and the J804 names become T8152 ones. - The OS-log rule is keyed on the segment's own flag. A flags-0xa segment of any dcp* coprocessor is reserved as RAM, marked apple,dcp-os-log, without an IOMMU mapping; the T6030/T8132 chip list is gone. This also applies to every other DCP that reaches dt_reserve_asc_segments_at() with a flags-0xa segment: the external DCPs of T8112, T6020/T6021/T6022 and T8122 reserved through dt_reserve_dcpext_firmware(), and the M4/T6030 paths that already had it. Those segments were mapped at their remap address before. - The coprocessor and DART bases come from the ADT: the run check is asc_init("/arm-io/dcp") + asc_cpu_running() (ASC_CPU_CONTROL), and dart-dcp and dart-disp0 are walked at their ADT reg, with the reg size as the register window. The DCP stream (SID 23) and the 1 TiB DVA window test (remap >> 36 == 0x10) are named. The DCP stays as iBoot started it, so /chosen apple,dcp-rtkit-running stays absent here: dt_set_dcp_state_t8152() publishes it only for a DCP that aura itself started (aura#301). The call moves before dt_set_dcpext_t8152() in the minimal-boot branch, and the helpers sit next to the J713 display walk they share. Host tests: test_t8152_dcp_handoff_selector.py (selector, call site, ADT bases, rule) and test_dcp_os_log_flag.py (the flag rule on chips the list never named); test_t8152_audio_dt.py stubs the new call in the T8152 call order. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- src/kboot.c | 123 +++++++++--------- tests/python/test_dcp_os_log_flag.py | 100 ++++++++++++++ tests/python/test_isp_optional.py | 1 - tests/python/test_isp_reservation_full.py | 3 +- tests/python/test_j713_dcpext_ready.py | 1 - tests/python/test_m4_dcp_handoff.py | 1 - tests/python/test_t8122_dcpext_ready.py | 1 - tests/python/test_t8152_audio_dt.py | 3 +- .../python/test_t8152_dcp_handoff_selector.py | 58 +++++++++ 9 files changed, 226 insertions(+), 65 deletions(-) create mode 100644 tests/python/test_dcp_os_log_flag.py create mode 100644 tests/python/test_t8152_dcp_handoff_selector.py diff --git a/src/kboot.c b/src/kboot.c index 3c26a18b2..8319b98fc 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -5050,13 +5050,12 @@ static int dt_reserve_asc_segments_at(int fdt_node, const char *adt_path, const uint32_t mem_phandle = fdt_get_phandle(dt, mem_node); /* - * The OS log segment (flags 0xa) of a T6030, T8132 or T8152 display coprocessor, - * internal or external, runs at its physical address and is not reached - * through the DART: reserve its RAM, marked as such, without an IOMMU - * mapping. + * The OS log segment (flags 0xa) of a display coprocessor, internal or + * external, runs at its physical address and is not reached through the + * DART: reserve its RAM, marked as such, without an IOMMU mapping. The + * segment's own flag selects this, on every chip. */ - if ((chip_id == T6030 || chip_id == T8132 || chip_id == T8152) && - !strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa) + if (!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa) ret = fdt_setprop_empty(dt, mem_node, "apple,dcp-os-log"); else ret = dt_device_set_reserved_mem(mem_node, node_name, dev_phandle, iova, seg_size); @@ -9418,33 +9417,40 @@ static int dt_set_j713_display_maps(const struct adt_segment_ranges *segments, u } /* - * J804 (T8152) internal display handoff for Linux's DCP driver. Opt-in with the kernel argument - * j804.dcp=1; without it, or if any check or later step fails, nothing is published (the tree is - * edited in a dt_display_transaction()) and the display nodes stay disabled (the panel keeps the - * simple-framebuffer). + * T8152 internal display handoff for Linux's DCP driver, on every T8152 tree whose "dcp" alias is + * an apple,t8152-dcp node (the J804's). If any check or later step fails, nothing is published + * (the tree is edited in a dt_display_transaction()) and the display nodes stay disabled (the + * panel keeps the simple-framebuffer). A tree with the apple,t8152-dcp-iboot contract (J873g) is + * not a target. * * iBoot leaves the DCP running with its firmware mapped through locked DART tables. The ADT * iop-dcp-nub segment-ranges give each segment's physical address and, in "remap", its DVA in the * DARTs' 1 TiB window, except __OS_LOG (flags 0xa), whose remap repeats the physical address. Every - * dart-dcp SID 23 page and every dart-disp0 SID 0 / SID 16 page is checked against those segments - * before anything is published: dcp gets one reserved region per segment, display (SID 0) and - * disp0_piodma (SID 16) get the flags-2 segments their tables map, with iommu-addresses. + * dart-dcp DCP-stream page and every dart-disp0 SID 0 / SID 16 page is checked against those + * segments before anything is published: dcp gets one reserved region per segment, display (SID 0) + * and disp0_piodma (SID 16) get the flags-2 segments their tables map, with iommu-addresses. The + * coprocessor and both DARTs are found by their ADT reg. + * + * The DCP stays as iBoot started it, so apple,dcp-rtkit-running stays absent: + * dt_set_dcp_state_t8152() publishes it only for a DCP aura itself started. */ -#define J804_DCP_COPROC 0x286e00000UL -#define J804_DART_DCP 0x286340000UL -#define J804_DART_DISP0 0x286300000UL -#define J804_DCP_SID 23 +/* The DCP's stream on dart-dcp. */ +#define T8152_DCP_SID 23 +/* remap[63:36]: the DARTs' 1 TiB DVA window (0x10 << 36) the firmware segments run in. */ +#define T8152_DCP_DVA_SHIFT 36 +#define T8152_DCP_DVA_WINDOW 0x10 -static int j804_dcp_page(u64 iova, u64 physical, void *opaque) +static int t8152_dcp_page(u64 iova, u64 physical, void *opaque) { struct j713_display_maps *maps = opaque; for (u32 i = 0; i < maps->count; i++) { const struct adt_segment_ranges *seg = &maps->segments[i]; - u64 start = seg->remap & ((1ULL << 36) - 1); + u64 start = seg->remap & ((1ULL << T8152_DCP_DVA_SHIFT) - 1); u64 size = ALIGN_UP((u64)seg->size, SZ_16K); if (seg->unk == 0xa || iova < start || iova - start >= size) continue; - if ((seg->remap >> 36) != 0x10 || physical != seg->phys + iova - start) + if ((seg->remap >> T8152_DCP_DVA_SHIFT) != T8152_DCP_DVA_WINDOW || + physical != seg->phys + iova - start) return -1; maps->bytes[i] += SZ_16K; return 0; @@ -9452,18 +9458,15 @@ static int j804_dcp_page(u64 iova, u64 physical, void *opaque) return -1; } -static bool dt_bootargs_have(const char *token) +/* The register window of an ADT DART, by its reg; -1 when the ADT has none. */ +static int t8152_dcp_dart(const char *path, u64 *base, u64 *size) { - int node = fdt_path_offset(dt, "/chosen"), len = 0; - const char *args = node < 0 ? NULL : fdt_getprop(dt, node, "bootargs", &len); - size_t n = strlen(token); + int trace[8]; - if (!args || len <= 0) - return false; - for (const char *p = args; (p = strstr(p, token)); p += n) - if ((p == args || dt_bootarg_space(p[-1])) && (!p[n] || dt_bootarg_space(p[n]))) - return true; - return false; + if (adt_path_offset_trace(adt, path, trace) < 0 || + adt_get_reg(adt, trace, "reg", 0, base, size) < 0 || !*base || !*size) + return -1; + return 0; } static int dt_set_status_okay_iommu(const char *alias) @@ -9479,7 +9482,7 @@ static int dt_set_status_okay_iommu(const char *alias) } /* Runs in dt_display_transaction(): a failure at any step discards every edit it made. */ -static int dt_set_j804_dcp_handoff_one(unsigned int unused) +static int dt_set_t8152_dcp_handoff_one(unsigned int unused) { (void)unused; int nub = adt_path_offset(adt, "/arm-io/dcp/iop-dcp-nub"); @@ -9488,27 +9491,34 @@ static int dt_set_j804_dcp_handoff_one(unsigned int unused) u32 len; if (nub < 0 || chosen < 0 || ADT_GETPROP(adt, chosen, "dram-base", &dram_start) < 0 || ADT_GETPROP(adt, chosen, "dram-size", &dram_size) < 0 || !dram_size) - bail("FDT: J804 DCP: missing ADT nub or DRAM bounds\n"); + bail("FDT: T8152 DCP: missing ADT nub or DRAM bounds\n"); const struct adt_segment_ranges *segments = adt_getprop(adt, nub, "segment-ranges", &len); if (!segments || !len || len % sizeof(*segments) || len / sizeof(*segments) > 32) - bail("FDT: J804 DCP: invalid segment-ranges\n"); + bail("FDT: T8152 DCP: invalid segment-ranges\n"); u32 count = len / sizeof(*segments); for (u32 i = 0; i < count; i++) { u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K); if (!size || (segments[i].phys | segments[i].remap) & (SZ_16K - 1) || segments[i].phys < dram_start || segments[i].phys + size > dram_start + dram_size) - bail("FDT: J804 DCP: invalid segment %u\n", i); + bail("FDT: T8152 DCP: invalid segment %u\n", i); } - u32 cpu = read32(J804_DCP_COPROC + 0x44); - if (!(cpu & BIT(4))) - bail("FDT: J804 DCP: coprocessor is not running (CPU_CONTROL 0x%x)\n", cpu); + u64 dart_dcp, dart_dcp_size, dart_disp0, dart_disp0_size; + if (t8152_dcp_dart("/arm-io/dart-dcp", &dart_dcp, &dart_dcp_size) || + t8152_dcp_dart("/arm-io/dart-disp0", &dart_disp0, &dart_disp0_size)) + bail("FDT: T8152 DCP: no ADT reg for dart-dcp or dart-disp0\n"); + + asc_dev_t *asc = asc_init("/arm-io/dcp"); + bool running = asc && asc_cpu_running(asc); + asc_free(asc); + if (!running) + bail("FDT: T8152 DCP: coprocessor is not running\n"); /* Every page the running firmware maps must belong to a segment. */ struct j713_display_maps dcpmap = {.segments = segments, .count = count}; - if (dart_walk_locked_t8110(J804_DART_DCP, 0x20000, J804_DCP_SID, dram_start, dram_size, - j804_dcp_page, &dcpmap)) - bail("FDT: J804 DCP: dart-dcp SID %u maps outside the firmware segments\n", J804_DCP_SID); + if (dart_walk_locked_t8110(dart_dcp, dart_dcp_size, T8152_DCP_SID, dram_start, dram_size, + t8152_dcp_page, &dcpmap)) + bail("FDT: T8152 DCP: dart-dcp SID %u maps outside the firmware segments\n", T8152_DCP_SID); const char *aliases[] = {"disp0", "disp0_piodma"}; const u8 streams[] = {0, 16}; @@ -9517,32 +9527,32 @@ static int dt_set_j804_dcp_handoff_one(unsigned int unused) {.segments = segments, .count = count}, }; for (u32 n = 0; n < 2; n++) { - if (dart_walk_locked_t8110(J804_DART_DISP0, 0xc000, streams[n], dram_start, dram_size, + if (dart_walk_locked_t8110(dart_disp0, dart_disp0_size, streams[n], dram_start, dram_size, j713_display_page, &maps[n])) - bail("FDT: J804 DCP: dart-disp0 SID %u maps outside the firmware segments\n", + bail("FDT: T8152 DCP: dart-disp0 SID %u maps outside the firmware segments\n", streams[n]); for (u32 i = 0; i < count; i++) if (maps[n].bytes[i] && maps[n].bytes[i] != ALIGN_UP((u64)segments[i].size, SZ_16K)) - bail("FDT: J804 DCP: SID %u maps segment %u partially\n", streams[n], i); + bail("FDT: T8152 DCP: SID %u maps segment %u partially\n", streams[n], i); } const char *uuid = adt_getprop(adt, nub, "uuid", &len); if (!uuid || len != 37 || uuid[len - 1] || fdt_setprop_string(dt, fdt_path_offset(dt, "dcp"), "apple,firmware-uuid", uuid)) - bail("FDT: J804 DCP: cannot publish the firmware UUID\n"); + bail("FDT: T8152 DCP: cannot publish the firmware UUID\n"); if (dt_reserve_asc_firmware("/arm-io/dcp/iop-dcp-nub", NULL, "dcp", true, 0)) - bail("FDT: J804 DCP: firmware reservation failed\n"); + bail("FDT: T8152 DCP: firmware reservation failed\n"); u32 phandles[2]; for (u32 n = 0; n < 2; n++) { int node = fdt_path_offset(dt, aliases[n]); if (node < 0) - bail("FDT: J804 DCP: no %s alias\n", aliases[n]); + bail("FDT: T8152 DCP: no %s alias\n", aliases[n]); phandles[n] = fdt_get_phandle(dt, node); if (!phandles[n] && (fdt_generate_phandle(dt, &phandles[n]) || fdt_setprop_u32(dt, node, "phandle", phandles[n]))) - bail("FDT: J804 DCP: no phandle for %s\n", aliases[n]); + bail("FDT: T8152 DCP: no phandle for %s\n", aliases[n]); } for (u32 i = 0; i < count; i++) { if (!maps[0].bytes[i] && !maps[1].bytes[i]) @@ -9567,21 +9577,18 @@ static int dt_set_j804_dcp_handoff_one(unsigned int unused) if (dt_set_status_okay_iommu("dcp") || dt_set_status_okay_iommu("disp0") || dt_set_status_okay_iommu("disp0_piodma")) - bail("FDT: J804 DCP: could not enable the display nodes\n"); - printf("FDT: J804 DCP handoff published (%u segments, firmware %s)\n", count, uuid); + bail("FDT: T8152 DCP: could not enable the display nodes\n"); + printf("FDT: T8152 DCP handoff published (%u segments, firmware %s)\n", count, uuid); return 0; } -static int dt_set_j804_dcp_handoff(void) +/* The hardware selects the handoff: a "dcp" alias naming an apple,t8152-dcp node. */ +static int dt_set_t8152_dcp_handoff(void) { int dcp = fdt_path_offset(dt, "dcp"); - if (board_id != 0x22 || dcp < 0 || fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp")) - return 0; - if (!dt_bootargs_have("j804.dcp=1")) { - printf("FDT: J804 DCP handoff not requested (j804.dcp=1)\n"); + if (dcp < 0 || fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp")) return 0; - } - return dt_display_transaction(dt_set_j804_dcp_handoff_one, 0); + return dt_display_transaction(dt_set_t8152_dcp_handoff_one, 0); } static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine); @@ -14327,8 +14334,8 @@ static int dt_prepare_t8152(void) dt_set_gpu(dt)) return -1; /* Fail-soft: a refused handoff leaves the display nodes disabled. */ - if (dt_set_j804_dcp_handoff()) - printf("FDT: J804 DCP handoff not published\n"); + if (dt_set_t8152_dcp_handoff()) + printf("FDT: T8152 DCP handoff not published\n"); dt_set_dcpext_t8152(); } if (dt_set_dcp_state_t8152()) diff --git a/tests/python/test_dcp_os_log_flag.py b/tests/python/test_dcp_os_log_flag.py new file mode 100644 index 000000000..24ee7b707 --- /dev/null +++ b/tests/python/test_dcp_os_log_flag.py @@ -0,0 +1,100 @@ +# SPDX-License-Identifier: GPL-2.0-only +"""The DCP OS-log rule is keyed on the segment's own flag (0xa), on every chip.""" + +from pathlib import Path +import subprocess + + +def test_dcp_os_log_keyed_on_segment_flag(tmp_path): + repo = Path(__file__).resolve().parents[2] + source = (repo / "src/kboot.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;")] + def definition(signature): + start = source.index(signature) + return source[start:source.index("\n}", start) + 2] + + firmware = definition("static int dt_reserve_asc_segments_at(") + "\n" + \ + definition("static int dt_reserve_asc_segments(") + "\n" + \ + definition("static int dt_reserve_asc_firmware(") + harness = r''' +#include +#include +#include +#include +#include +typedef unsigned long u64; +typedef uint32_t u32; +#define SZ_16K 0x4000 +#define T6030 0x6030 +#define T6041 0x6041 +#define T8132 0x8132 +/* Only the dram_only segment filter reads properties this way. */ +#define ADT_GETPROP(a, n, k, v) ((size_t)0) +static u32 chip_id; +static unsigned int mappings, links, os_logs; +#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define bail(...) do { return -1; } while (0) +static int dt_device_dart_va_bits(int fdt_node, int adt_node, u32 *bits) { (void)fdt_node; (void)adt_node; (void)bits; return -1; } +#define PACKED __attribute__((packed)) +''' + layout + r''' +static struct adt_segment_ranges segment = {0x100000, 0x200000, 0, 0x4000, 0}; +static void *dt, *adt; +static int fdt_path_offset(const void *tree, const char *path) +{ (void)tree; (void)path; return 1; } +static int adt_path_offset(const void *tree, const char *path) +{ (void)tree; (void)path; return 1; } +static uint32_t fdt_get_phandle(const void *tree, int node) +{ (void)tree; (void)node; return 1; } +static int fdt_generate_phandle(const void *tree, u32 *value) +{ (void)tree; *value = 1; return 0; } +static int fdt_setprop_u32(void *tree, int node, const char *name, u32 value) +{ (void)tree; (void)node; (void)name; (void)value; return 0; } +static const void *adt_getprop(const void *tree, int node, const char *name, u32 *len) +{ (void)tree; (void)node; (void)name; *len = sizeof(segment); return &segment; } +static int dt_get_or_add_reserved_mem(const char *name, const char *compat, + bool no_map, u64 phys, u64 size) +{ (void)name; (void)compat; (void)no_map; (void)phys; (void)size; return 2; } +static int dt_device_set_reserved_mem(int node, const char *name, u32 handle, + u64 iova, u64 size) +{ (void)node; (void)name; (void)handle; (void)iova; (void)size; mappings++; return 0; } +static int fdt_node_offset_by_phandle(const void *tree, u32 handle) +{ (void)tree; (void)handle; return 1; } +static int fdt_appendprop_u32(void *tree, int node, const char *name, u32 value) +{ (void)tree; (void)node; (void)name; (void)value; links++; return 0; } +__attribute__((unused)) static const void *fdt_getprop(const void *tree, int node, const char *name, int *len) +{ (void)tree; (void)node; (void)name; (void)len; return "okay"; } +static int fdt_setprop_empty(void *tree, int node, const char *name) +{ (void)tree; (void)node; assert(!strcmp(name, "apple,dcp-os-log")); os_logs++; return 0; } +''' + firmware + r''' +int main(void) +{ + /* The flags-0xa segment of any DCP, on a chip no list names: RAM only, marked, no IOVA. */ + static const u32 chips[] = {0x8103, 0x8112, 0x6020, 0x8122, T6030, T8132, 0x8152}; + for (unsigned int i = 0; i < sizeof(chips) / sizeof(chips[0]); i++) { + chip_id = chips[i]; + segment.unk = 0xa; + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0)); + assert(os_logs == 1 && !mappings && links == 1); + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", false, 0)); + assert(os_logs == 1 && !mappings && links == 1); + /* Any other DCP segment, and a flags-0xa segment of another coprocessor, is mapped. */ + segment.unk = 2; + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0)); + assert(!os_logs && mappings == 1 && links == 1); + segment.unk = 0xa; + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/isp", NULL, "isp", false, 0)); + assert(!os_logs && mappings == 1 && links == 1); + } + return 0; +} +''' + binary = tmp_path / "dcp-os-log-flag" + subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-x", "c", "-", + "-o", str(binary)], input=harness, text=True, check=True) + subprocess.run([str(binary)], check=True) diff --git a/tests/python/test_isp_optional.py b/tests/python/test_isp_optional.py index 7969f5a26..220637474 100644 --- a/tests/python/test_isp_optional.py +++ b/tests/python/test_isp_optional.py @@ -42,7 +42,6 @@ def function(name): #define T8140 0x8140 #define T8142 0x8142 #define T8132 0x8132 -#define T8152 0x8152 #define SZ_16K 16384 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) diff --git a/tests/python/test_isp_reservation_full.py b/tests/python/test_isp_reservation_full.py index e36390b75..38bd7f9ae 100644 --- a/tests/python/test_isp_reservation_full.py +++ b/tests/python/test_isp_reservation_full.py @@ -31,10 +31,9 @@ def definition(signature): #define T6030 0x6030 #define T6041 0x6041 #define T8132 0x8132 -#define T8152 0x8152 /* Only the dram_only segment filter reads properties this way. */ #define ADT_GETPROP(a, n, k, v) ((size_t)0) -static u32 chip_id; +__attribute__((unused)) static u32 chip_id; static unsigned int mappings, links; #define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1)) /* Stub: placeholder-based ISP handoff isn't exercised here. */ diff --git a/tests/python/test_j713_dcpext_ready.py b/tests/python/test_j713_dcpext_ready.py index f67bbc07a..489f9d78b 100644 --- a/tests/python/test_j713_dcpext_ready.py +++ b/tests/python/test_j713_dcpext_ready.py @@ -45,7 +45,6 @@ def define(source, name): #define T8122 0x8122 #define T6030 0x6030 #define T8132 0x8132 -#define T8152 0x8152 #define T6000 0x6000 #define T6001 0x6001 #define T6002 0x6002 diff --git a/tests/python/test_m4_dcp_handoff.py b/tests/python/test_m4_dcp_handoff.py index 6fe6c453f..cf3cb0f58 100644 --- a/tests/python/test_m4_dcp_handoff.py +++ b/tests/python/test_m4_dcp_handoff.py @@ -62,7 +62,6 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path): #define PACKED __attribute__((packed)) #define T6030 0x6030 #define T8132 0x8132 -#define T8152 0x8152 #define T8122 0x8122 #define SZ_16K 0x4000 #define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((u64)(a) - 1)) diff --git a/tests/python/test_t8122_dcpext_ready.py b/tests/python/test_t8122_dcpext_ready.py index 3fcbc3962..4233794c0 100644 --- a/tests/python/test_t8122_dcpext_ready.py +++ b/tests/python/test_t8122_dcpext_ready.py @@ -35,7 +35,6 @@ def function(name, kind="static int "): #define T8122 0x8122 #define T6030 0x6030 #define T8132 0x8132 -#define T8152 0x8152 #define T6000 0x6000 #define T6001 0x6001 #define T6002 0x6002 diff --git a/tests/python/test_t8152_audio_dt.py b/tests/python/test_t8152_audio_dt.py index 98676fbc8..18a45b46e 100644 --- a/tests/python/test_t8152_audio_dt.py +++ b/tests/python/test_t8152_audio_dt.py @@ -93,6 +93,7 @@ static int dt_set_display_t8152(void) { return leaf("display"); } static int dt_set_live_afk_t8152(void) { return leaf("afk"); } static int dt_set_gpu(void *tree) { assert(tree == dt); return leaf("gpu"); } +static int dt_set_t8152_dcp_handoff(void) { return leaf("dcp_handoff"); } static void dt_set_dcpext_t8152(void) { leaf("dcpext"); } static int dt_set_dcp_state_t8152(void) { return leaf("dcp_state"); } static int dt_set_cpus(void) { return leaf("cpus"); } @@ -191,7 +192,7 @@ const u8 *cfg = fdt_getprop(dt, amp, "apple,speaker-config", &len); assert(cfg && len == 4 && cfg[1] == 0x0f); assert(!strcmp(calls, - "amp speaker_id serial display afk gpu dcpext dcp_state cpus mac " + "amp speaker_id serial display afk gpu dcp_handoff dcpext dcp_state cpus mac " "atc acio pcie dart fb2 legacy memory logs ")); /* 2. No CPMS profile, non-minimal mode: same audio setup, skips minimal-only steps. */ diff --git a/tests/python/test_t8152_dcp_handoff_selector.py b/tests/python/test_t8152_dcp_handoff_selector.py new file mode 100644 index 000000000..c96f7e47c --- /dev/null +++ b/tests/python/test_t8152_dcp_handoff_selector.py @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: GPL-2.0-only +"""T8152 internal DCP handoff: selected by the hardware, bases from the ADT, fail-closed.""" + +from pathlib import Path +import re + +SOURCE = (Path(__file__).resolve().parents[2] / "src/kboot.c").read_text() + + +def definition(signature): + start = SOURCE.index(signature) + return SOURCE[start:SOURCE.index("\n}\n", start) + 3] + + +def test_no_boot_argument_or_board_selector(): + assert "j804.dcp" not in SOURCE + assert "dt_bootargs_have" not in SOURCE + select = definition("static int dt_set_t8152_dcp_handoff(void)") + assert "board_id" not in select and "bootargs" not in select + # The dcp alias naming an apple,t8152-dcp node selects it, and the edit is one transaction. + assert 'fdt_path_offset(dt, "dcp")' in select + assert 'fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp")' in select + assert "dt_display_transaction(dt_set_t8152_dcp_handoff_one, 0)" in select + + +def test_called_fail_soft_in_the_minimal_boot_branch(): + prepare = definition("static int dt_prepare_t8152(void)") + minimal = prepare[prepare.index('"aurora,minimal-boot"'):prepare.index("dt_set_dcp_state_t8152()")] + call = minimal.index("if (dt_set_t8152_dcp_handoff())") + assert minimal.index("dt_set_gpu(dt)") < call < minimal.index("dt_set_dcpext_t8152();") + assert re.search(r"if \(dt_set_t8152_dcp_handoff\(\)\)\s*printf\(", minimal) + + +def test_bases_come_from_the_adt(): + one = definition("static int dt_set_t8152_dcp_handoff_one(unsigned int unused)") + # No coprocessor or DART address is built in. + assert not re.search(r"0x2[0-9a-f]{8}", one) + assert 'asc_init("/arm-io/dcp")' in one and "asc_cpu_running(asc)" in one + assert "asc_free(asc)" in one + assert 't8152_dcp_dart("/arm-io/dart-dcp", &dart_dcp, &dart_dcp_size)' in one + assert 't8152_dcp_dart("/arm-io/dart-disp0", &dart_disp0, &dart_disp0_size)' in one + assert "dart_walk_locked_t8110(dart_dcp, dart_dcp_size, T8152_DCP_SID" in one + assert "dart_walk_locked_t8110(dart_disp0, dart_disp0_size, streams[n]" in one + dart = definition("static int t8152_dcp_dart(const char *path, u64 *base, u64 *size)") + assert "adt_path_offset_trace(adt, path, trace)" in dart + assert 'adt_get_reg(adt, trace, "reg", 0, base, size)' in dart + # Named stream and DVA window. + assert "#define T8152_DCP_SID 23" in SOURCE + page = definition("static int t8152_dcp_page(u64 iova, u64 physical, void *opaque)") + assert "(seg->remap >> T8152_DCP_DVA_SHIFT) != T8152_DCP_DVA_WINDOW" in page + assert ">> 36" not in page + + +def test_os_log_rule_keyed_on_the_segment_flag(): + at = definition("static int dt_reserve_asc_segments_at(") + rule = at[at.rindex("if (", 0, at.index('"apple,dcp-os-log"')):at.index('"apple,dcp-os-log"')] + assert "chip_id" not in rule + assert '!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa' in rule From cf325bafb8647c2ef39c639f5ab74580ea2f5344 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:23:15 +1100 Subject: [PATCH 3/4] tests: T8152 call-order test: leave the new hand-off unrecorded tests/python/test_t8152_audio_dt.py asserts the exact dt_prepare_t8152() call sequence, and each of the four T8152 successor series (aura#286-#289 reworks) adds a call to it, so every pair of them would conflict on that one expected string. Stub this series' new calls without recording them, so the four merge in any order. Where each call sits is checked by the series' own call-site test instead. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- tests/python/test_t8152_audio_dt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/test_t8152_audio_dt.py b/tests/python/test_t8152_audio_dt.py index 18a45b46e..1e0fb9fb9 100644 --- a/tests/python/test_t8152_audio_dt.py +++ b/tests/python/test_t8152_audio_dt.py @@ -93,7 +93,7 @@ static int dt_set_display_t8152(void) { return leaf("display"); } static int dt_set_live_afk_t8152(void) { return leaf("afk"); } static int dt_set_gpu(void *tree) { assert(tree == dt); return leaf("gpu"); } -static int dt_set_t8152_dcp_handoff(void) { return leaf("dcp_handoff"); } +static int dt_set_t8152_dcp_handoff(void) { return 0; } static void dt_set_dcpext_t8152(void) { leaf("dcpext"); } static int dt_set_dcp_state_t8152(void) { return leaf("dcp_state"); } static int dt_set_cpus(void) { return leaf("cpus"); } @@ -192,7 +192,7 @@ const u8 *cfg = fdt_getprop(dt, amp, "apple,speaker-config", &len); assert(cfg && len == 4 && cfg[1] == 0x0f); assert(!strcmp(calls, - "amp speaker_id serial display afk gpu dcp_handoff dcpext dcp_state cpus mac " + "amp speaker_id serial display afk gpu dcpext dcp_state cpus mac " "atc acio pcie dart fb2 legacy memory logs ")); /* 2. No CPMS profile, non-minimal mode: same audio setup, skips minimal-only steps. */ From c8124aa6fa4374ae5b184abfb7544b9965fee9f2 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:12:19 +1100 Subject: [PATCH 4/4] kboot: T8152 DCP: OS log only where it runs at phys, checked DRAM bounds Two review follow-ups on the T8152 internal DCP handoff. The DCP OS-log rule (no chip list, keyed on the segment) now also needs the descriptor to say the log runs at its physical address: a flags-0xa segment of a dcp* coprocessor is reserved RAM-only and marked apple,dcp-os-log only when its remap equals its phys. A flags-0xa segment with a real remap keeps its DART mapping as before, so DCPs whose log is reached through the DART are not changed. The real-ADT fixtures all have remap == phys for that segment: T6030 (tests/python/test_m4_dcp_handoff.py), T8132 26.x and 27.2 (tests/t8132_dcpext_host.c), T8122 (tests/t8122_display_host.c) and T6041 (tests/t6041_display_host.c), and the J804 ADT does the same. A T8142 __OS_LOG can have a live DART alias that differs from its descriptor (dt_reserve_m5_display_segments()), which is why a remapped one must keep its mapping. The synthetic J713 dcpext fixture had a remapped log (0x20000000); it now repeats phys, as the real J713 ADTs do. dt_set_t8152_dcp_handoff_one() takes DRAM from dt_t8152_dram(): exact u64 dram-base and dram-size, a non-zero size and no wrap. Each segment's extent is checked by t8152_dcp_bad_segment() without forming unchecked ends (phys < start, size > dram size, phys - start > dram size - size), and phys + size and remap + size must not wrap. Host tests: test_dcp_os_log_flag.py covers remap == phys (RAM-only, on every chip) and a remapped flags-0xa segment (mapped); test_t8152_dcp_handoff_selector.py compiles t8152_dcp_bad_segment() against segments past the end, below the start, larger than DRAM, wrapping, unaligned and empty. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- src/kboot.c | 50 +++++++++---- tests/python/test_dcp_os_log_flag.py | 22 ++++-- tests/python/test_j713_dcpext_ready.py | 7 +- .../python/test_t8152_dcp_handoff_selector.py | 75 +++++++++++++++++++ 4 files changed, 132 insertions(+), 22 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index 8319b98fc..be7778368 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -5051,11 +5051,14 @@ static int dt_reserve_asc_segments_at(int fdt_node, const char *adt_path, const /* * The OS log segment (flags 0xa) of a display coprocessor, internal or - * external, runs at its physical address and is not reached through the - * DART: reserve its RAM, marked as such, without an IOMMU mapping. The - * segment's own flag selects this, on every chip. + * external, whose descriptor says it runs at its physical address (remap + * equal to phys) is not reached through the DART: reserve its RAM, marked + * as such, without an IOMMU mapping. The segment itself selects this, on + * every chip. A flags-0xa segment with a real remap keeps its mapping (a + * T8142 __OS_LOG can have a live DART alias, see + * dt_reserve_m5_display_segments()). */ - if (!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa) + if (!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa && seg->remap == seg->phys) ret = fdt_setprop_empty(dt, mem_node, "apple,dcp-os-log"); else ret = dt_device_set_reserved_mem(mem_node, node_name, dev_phandle, iova, seg_size); @@ -9481,27 +9484,46 @@ static int dt_set_status_okay_iommu(const char *alias) return 0; } +static int dt_t8152_dram(u64 *dram, u64 *size); + +/* + * Every firmware segment whole inside DRAM [dram_start, dram_start + dram_size), 16K-aligned at + * both its physical and its remap address, with neither end wrapping. Returns the index of the + * first segment that is not, or -1 when all are. + */ +static int t8152_dcp_bad_segment(const struct adt_segment_ranges *segments, u32 count, + u64 dram_start, u64 dram_size) +{ + for (u32 i = 0; i < count; i++) { + u64 phys = segments[i].phys, remap = segments[i].remap; + u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K); + if (!size || (phys | remap) & (SZ_16K - 1) || phys > UINT64_MAX - size || + remap > UINT64_MAX - size || phys < dram_start || size > dram_size || + phys - dram_start > dram_size - size) + return i; + } + return -1; +} + /* Runs in dt_display_transaction(): a failure at any step discards every edit it made. */ static int dt_set_t8152_dcp_handoff_one(unsigned int unused) { (void)unused; int nub = adt_path_offset(adt, "/arm-io/dcp/iop-dcp-nub"); - int chosen = adt_path_offset(adt, "/chosen"); u64 dram_start, dram_size; u32 len; - if (nub < 0 || chosen < 0 || ADT_GETPROP(adt, chosen, "dram-base", &dram_start) < 0 || - ADT_GETPROP(adt, chosen, "dram-size", &dram_size) < 0 || !dram_size) - bail("FDT: T8152 DCP: missing ADT nub or DRAM bounds\n"); + if (nub < 0) + bail("FDT: T8152 DCP: missing ADT nub\n"); + /* Exact u64 dram-base and dram-size, non-zero, not wrapping. */ + if (dt_t8152_dram(&dram_start, &dram_size)) + return -1; const struct adt_segment_ranges *segments = adt_getprop(adt, nub, "segment-ranges", &len); if (!segments || !len || len % sizeof(*segments) || len / sizeof(*segments) > 32) bail("FDT: T8152 DCP: invalid segment-ranges\n"); u32 count = len / sizeof(*segments); - for (u32 i = 0; i < count; i++) { - u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K); - if (!size || (segments[i].phys | segments[i].remap) & (SZ_16K - 1) || - segments[i].phys < dram_start || segments[i].phys + size > dram_start + dram_size) - bail("FDT: T8152 DCP: invalid segment %u\n", i); - } + int bad = t8152_dcp_bad_segment(segments, count, dram_start, dram_size); + if (bad >= 0) + bail("FDT: T8152 DCP: invalid segment %d\n", bad); u64 dart_dcp, dart_dcp_size, dart_disp0, dart_disp0_size; if (t8152_dcp_dart("/arm-io/dart-dcp", &dart_dcp, &dart_dcp_size) || diff --git a/tests/python/test_dcp_os_log_flag.py b/tests/python/test_dcp_os_log_flag.py index 24ee7b707..f7bfb6f67 100644 --- a/tests/python/test_dcp_os_log_flag.py +++ b/tests/python/test_dcp_os_log_flag.py @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -"""The DCP OS-log rule is keyed on the segment's own flag (0xa), on every chip.""" +"""The DCP OS-log rule is keyed on the segment itself (flag 0xa, remap == phys), on every chip.""" from pathlib import Path import subprocess @@ -70,25 +70,35 @@ def definition(signature): ''' + firmware + r''' int main(void) { - /* The flags-0xa segment of any DCP, on a chip no list names: RAM only, marked, no IOVA. */ static const u32 chips[] = {0x8103, 0x8112, 0x6020, 0x8122, T6030, T8132, 0x8152}; for (unsigned int i = 0; i < sizeof(chips) / sizeof(chips[0]); i++) { chip_id = chips[i]; + /* A flags-0xa DCP segment that runs at its physical address: RAM only, marked, no IOVA. */ segment.unk = 0xa; + segment.remap = segment.phys; mappings = links = os_logs = 0; - assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0)); + assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0)); assert(os_logs == 1 && !mappings && links == 1); mappings = links = os_logs = 0; - assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", false, 0)); + assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", true, 0)); assert(os_logs == 1 && !mappings && links == 1); + /* A flags-0xa DCP segment with a real remap keeps its mapping. */ + segment.remap = 0x20000000; + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0)); + assert(!os_logs && mappings == 1 && links == 1); + mappings = links = os_logs = 0; + assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", true, 0)); + assert(!os_logs && mappings == 1 && links == 1); /* Any other DCP segment, and a flags-0xa segment of another coprocessor, is mapped. */ + segment.remap = segment.phys; segment.unk = 2; mappings = links = os_logs = 0; - assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0)); + assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0)); assert(!os_logs && mappings == 1 && links == 1); segment.unk = 0xa; mappings = links = os_logs = 0; - assert(!dt_reserve_asc_firmware("/arm-io/isp", NULL, "isp", false, 0)); + assert(!dt_reserve_asc_firmware("/arm-io/isp", NULL, "isp", true, 0)); assert(!os_logs && mappings == 1 && links == 1); } return 0; diff --git a/tests/python/test_j713_dcpext_ready.py b/tests/python/test_j713_dcpext_ready.py index 489f9d78b..d58e06cd2 100644 --- a/tests/python/test_j713_dcpext_ready.py +++ b/tests/python/test_j713_dcpext_ready.py @@ -213,9 +213,12 @@ def define(source, name): u64 at = dram_base + 0x40000 * n; segments[n][0] = (struct adt_segment_ranges){.phys = at + 0x4000, .remap = 0x10000000, .size = 0x4000}; - /* The OS log segment, reserved by physical address. */ + /* + * The OS log segment, reserved by physical address: as in the real J713 ADTs + * (tests/t8132_dcpext_host.c), its descriptor's remap repeats phys. + */ segments[n][1] = (struct adt_segment_ranges){.phys = at + 0x10000, - .remap = 0x20000000, .size = 0x8000, .unk = 0xa}; + .remap = at + 0x10000, .size = 0x8000, .unk = 0xa}; } for (unsigned i = 0; i < ARRAY_SIZE(devices); i++) ps[i] = strstr(devices[i].name, "_CPU") ? GATED : ON; diff --git a/tests/python/test_t8152_dcp_handoff_selector.py b/tests/python/test_t8152_dcp_handoff_selector.py index c96f7e47c..77033558c 100644 --- a/tests/python/test_t8152_dcp_handoff_selector.py +++ b/tests/python/test_t8152_dcp_handoff_selector.py @@ -3,6 +3,7 @@ from pathlib import Path import re +import subprocess SOURCE = (Path(__file__).resolve().parents[2] / "src/kboot.c").read_text() @@ -56,3 +57,77 @@ def test_os_log_rule_keyed_on_the_segment_flag(): rule = at[at.rindex("if (", 0, at.index('"apple,dcp-os-log"')):at.index('"apple,dcp-os-log"')] assert "chip_id" not in rule assert '!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa' in rule + + +def test_os_log_rule_needs_the_log_to_run_at_its_physical_address(): + at = definition("static int dt_reserve_asc_segments_at(") + assert "seg->unk == 0xa && seg->remap == seg->phys" in at + + +def test_dram_bounds_are_exact_and_checked(): + one = definition("static int dt_set_t8152_dcp_handoff_one(unsigned int unused)") + # dt_t8152_dram(): exact u64 widths, non-zero size, no wrap. + assert "dt_t8152_dram(&dram_start, &dram_size)" in one + assert '"dram-base"' not in one and "dram_start + dram_size" not in one + assert "t8152_dcp_bad_segment(segments, count, dram_start, dram_size)" in one + dram = definition("static int dt_t8152_dram(u64 *dram, u64 *size)\n{") + assert '"dram-base", dram) != 8' in dram and '"dram-size", size) != 8' in dram + assert "*dram > UINT64_MAX - *size" in dram + + +def test_segment_extents(tmp_path): + header = (Path(__file__).resolve().parents[2] / "src/adt.h").read_text() + start = header.index("struct adt_segment_ranges {") + layout = header[start:header.index("} PACKED;", start) + len("} PACKED;")] + helper = SOURCE[SOURCE.index("static int t8152_dcp_bad_segment("):] + helper = helper[:helper.index("\n}\n") + 3] + harness = r''' +#include +#include +typedef uint64_t u64; +typedef uint32_t u32; +#define PACKED __attribute__((packed)) +#define SZ_16K 0x4000ULL +#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((u64)(a) - 1)) +''' + layout + "\n" + helper + r''' +#define TB 0x10000000000ULL +#define SEG(p, r, s) ((struct adt_segment_ranges){.phys = (p), .remap = (r), .size = (s)}) +int main(void) +{ + struct adt_segment_ranges s[2]; + u64 base = TB, size = 0x100000000ULL; + /* Whole segments inside DRAM, including one that ends exactly at its top. */ + s[0] = SEG(TB, TB, 0x8000); + s[1] = SEG(TB + size - 0x4000, 0x10000000000ULL, 0x4000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == -1); + /* Past the end, by one page. */ + s[1] = SEG(TB + size - 0x4000, TB, 0x8000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + /* Below the start. */ + s[1] = SEG(TB - 0x4000, TB, 0x4000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + /* Larger than DRAM. */ + s[1] = SEG(TB, TB, 0xffffc000u); + assert(t8152_dcp_bad_segment(&s[1], 1, base, 0x4000) == 0); + /* phys + size and remap + size wrap. */ + s[1] = SEG(0xffffffffffffc000ULL, TB, 0x8000); + assert(t8152_dcp_bad_segment(s, 2, 0, UINT64_MAX) == 1); + s[1] = SEG(TB, 0xffffffffffffc000ULL, 0x8000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + /* Unaligned, and empty. */ + s[1] = SEG(TB + 0x1000, TB, 0x4000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + s[1] = SEG(TB, TB + 0x1000, 0x4000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + s[1] = SEG(TB, TB, 0); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 1); + /* The first bad one is reported. */ + s[0] = SEG(TB - 0x4000, TB, 0x4000); + assert(t8152_dcp_bad_segment(s, 2, base, size) == 0); + return 0; +} +''' + binary = tmp_path / "t8152-dcp-segments" + subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-x", "c", "-", + "-o", str(binary)], input=harness, text=True, check=True) + subprocess.run([str(binary)], check=True)