From 941977e0666d9af849ebba12df0c6099eb5b4536 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 17:08:07 +1100 Subject: [PATCH 1/7] dcp: choose the dptx-port message format from the firmware The dptx-port path behind dp2hdmi (M2 desktops, J873) was refused unless the OS firmware was 13.5, with T8152 exempted by chip. On a J473 with a 27.2 stub aura therefore never drives HDMI, and M2 desktops have no iBoot framebuffer, so the screen stays black until Linux. The firmware announces whether its dptx-port service speaks AFKv2, and the endpoint code already handles both formats. Decide from that instead: AFKv2 is driven on any firmware, the older format still only on 13.5, whose layouts are the only ones checked. The chip exemption goes away, as T8152's endpoint speaks AFKv2. Signed-off-by: Ryan Murray --- src/dcp.c | 20 ++++++++++++-------- src/dcp/dptx_port_ep.c | 5 +++++ src/dcp/dptx_port_ep.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/dcp.c b/src/dcp.c index 5b59a63ca..dc1d0bb7e 100644 --- a/src/dcp.c +++ b/src/dcp.c @@ -166,6 +166,18 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg) return -1; } + /* + * The firmware announces which message format its dptx-port service speaks. The AFKv2 + * format is the same on every firmware that has it. The older format's layouts were only + * checked against the 13.5 firmware, so other firmware with that format is not driven. + */ + if (!dcp_dptx_is_afkv2(dcp->dptx_ep) && os_firmware.version != V13_5) { + printf("dcp: dptx-port uses the pre-AFKv2 format, checked on 13.5 firmware only\n"); + dcp_dptx_shutdown(dcp->dptx_ep); + dcp_dpav_shutdown(dcp->dpav_ep); + return -1; + } + #ifdef RTKIT_SYSLOG // start system endpoint when extended logging is requested dcp->system_ep = dcp_system_init(dcp); @@ -271,14 +283,6 @@ dcp_dev_t *dcp_init(const display_config_t *cfg) dcp_init_quiesced = false; if (cfg && cfg->dptx_phy[0]) { - /* J873g reports mBoot-20457.1.29, which is not in the OS firmware - * table, and the same dptx-port endpoint works on its USB-C route. - * Keep the V13_5 restriction for the machines it was written for. */ - if (os_firmware.version != V13_5 && chip_id != T8152) { - printf("dcp: dtpx-port is only supported with V13_5 OS firmware.\n"); - return NULL; - } - strncpy(dcp_pmgr_dev, cfg->pmgr_dev, sizeof(dcp_pmgr_dev)); dcp_die = cfg->die; pmgr_adt_power_enable(cfg->dcp); diff --git a/src/dcp/dptx_port_ep.c b/src/dcp/dptx_port_ep.c index 2e76a558d..a037a7211 100644 --- a/src/dcp/dptx_port_ep.c +++ b/src/dcp/dptx_port_ep.c @@ -716,6 +716,11 @@ int dcp_dptx_disconnect(dcp_dptx_if_t *dptx, u32 port) return dptxport_set_hpd(dptx->port[port].service, false); } +bool dcp_dptx_is_afkv2(const dcp_dptx_if_t *dptx) +{ + return dptx && afk_epic_is_v2(dptx->epic); +} + dcp_dptx_if_t *dcp_dptx_init(dcp_dev_t *dcp, u32 num_dptxports) { dcp_dptx_if_t *dptx = calloc(1, sizeof(dcp_dptx_if_t)); diff --git a/src/dcp/dptx_port_ep.h b/src/dcp/dptx_port_ep.h index 2acd57653..309793331 100644 --- a/src/dcp/dptx_port_ep.h +++ b/src/dcp/dptx_port_ep.h @@ -54,6 +54,7 @@ enum dptx_link_rate { dcp_dptx_if_t *dcp_dptx_init(dcp_dev_t *dcp, u32 num_dptxports); int dcp_dptx_shutdown(dcp_dptx_if_t *dptx); +bool dcp_dptx_is_afkv2(const dcp_dptx_if_t *dptx); int dcp_dptx_connect(dcp_dptx_if_t *dptx, dptx_phy_t *phy, u32 die, u32 port); int dcp_dptx_hpd(dcp_dptx_if_t *dptx, u32 port, bool hpd); From 17582c4a516d0a7b29addbf50422341ae47597ef Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 21:33:37 +1100 Subject: [PATCH 2/7] dart: a locked display stream that bypasses only the DAPF still translates dart_init_adt_display() adopts a locked display stream only while it translates through its inherited tables. For T8110 it counted the DAPF bypass bit as "not translating", but that bit only skips the DMA address filter stage; the page tables still translate, and the rest of dart.c reads TCR that way (only BYPASS_DART and REMAP_EN end translation). With the 27.2 firmware, iBoot locks dart-dispext0 on the M2 Mac mini with TCR 0x5 (translate, DAPF bypass), so the DISP DART was refused and the external DCP could not be started. Signed-off-by: Ryan Murray --- src/dart.c | 7 +++-- tests/python/test_display_dart_view.py | 40 +++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/dart.c b/src/dart.c index 99d6b22cb..8490039ee 100644 --- a/src/dart.c +++ b/src/dart.c @@ -683,8 +683,11 @@ dart_dev_t *dart_init_adt_display(const char *path, int instance, int device) u32 tcr = read32(DART_TCR(dart)); bool enabled, translating; if (dart->type == DART_T8110) { - u32 mode = DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DART | - DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_REMAP_EN; + /* Bypassing only the DAPF filter stage leaves translation through the tables on, as the + * rest of this file reads TCR: iBoot locks dart-dispext0 that way (TCR 0x5) on firmware 27. + */ + u32 mode = + DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DART | DART_T8110_TCR_REMAP_EN; enabled = read32(dart->regs + DART_T8110_ENABLE_STREAMS + 4 * (dart->device >> 5)) & BIT(dart->device & 31); translating = (tcr & mode) == DART_T8110_TCR_TRANSLATE_ENABLE; diff --git a/tests/python/test_display_dart_view.py b/tests/python/test_display_dart_view.py index 5c46ff9fc..14a9bf019 100644 --- a/tests/python/test_display_dart_view.py +++ b/tests/python/test_display_dart_view.py @@ -793,6 +793,42 @@ def production_unit(): dart_shutdown(dart); assert(!live_objects && !heap_frees && legacy_l2_count == 1); } +/* ---- T8110-format display DART: locked by firmware with only the DAPF stage bypassed ---- */ +static void t8110_view(u32 tcr) +{ + /* As dart-dispext0 on an M2 desktop with the 27.2 firmware: PROTECT set, TCR 0x5. */ + reset(false, 0, 0, false); + chip_id = T8112; + nodes[DART_NODE].compat = "dart,t8110"; + *reg(REGS + DART_T8110_PROTECT) = DART_T8110_PROTECT_TTBR_TCR; + *reg(REGS + DART_T8110_ENABLE_STREAMS) = BIT(SID); + *reg(REGS + DART_T8110_TCR_OFF + 4 * SID) = tcr; + *reg(REGS + DART_T8110_TTBR_OFF + 4 * SID) = + DART_T8110_TTBR_VALID | FIELD_PREP(DART_T8110_TTBR_ADDR, PAGE(ROOT) >> DART_T8110_TTBR_SHIFT); +} +static void t8110_dapf_bypass_translates(void) +{ + t8110_view(DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DAPF); + dart_dev_t *dart = dart_init_adt_display(DCP_PATH, 0, SID); + assert(dart && dart->type == DART_T8110 && dart->locked && dart->l1[0] == at(ROOT)); + assert(!protected_writes() && !heap_used); + dart_shutdown(dart); + assert(!live_objects); +} +static void t8110_untranslated_refused(void) +{ + static const u32 tcrs[] = { + 0, + DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DART, + DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_REMAP_EN, + DART_T8110_TCR_TRANSLATE_ENABLE | DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART, + }; + for (unsigned int i = 0; i < ARRAY_SIZE(tcrs); i++) { + t8110_view(tcrs[i]); + assert(!dart_init_adt_display(DCP_PATH, 0, SID)); + assert(!heap_used && !live_objects); + } +} static void t6000_device_range(void) { /* A T6000-format DART has 32 streams: an out-of-range stream gets no handle, @@ -875,6 +911,8 @@ def production_unit(): {"t6000-locked-dcp", t6000_locked_dcp}, {"t6000-locked-refusals", t6000_locked_refusals}, {"t6000-device-range", t6000_device_range}, + {"t8110-dapf-bypass-translates", t8110_dapf_bypass_translates}, + {"t8110-untranslated-refused", t8110_untranslated_refused}, {"generic-locked-immutable", generic_locked_immutable}, {"fdt-locked-immutable", fdt_locked_immutable}, {"map-selected-root", map_selected_root}, @@ -935,7 +973,7 @@ def production_unit(): "admit-locked-translating", "refuse-device-range", "refuse-disabled-stream", "refuse-untranslated-stream", "stock-unlocked-view", "other-chip-same-rule", "gen3-unchanged", "disp-path-same-rule", "t6000-locked-dcp", "t6000-locked-refusals", - "t6000-device-range", + "t6000-device-range", "t8110-dapf-bypass-translates", "t8110-untranslated-refused", "generic-locked-immutable", "fdt-locked-immutable", "map-selected-root", "unmap-selected-root", "unmap-absent-allocates-nothing", "map-absent-root-refused", "map-new-l2-in-present-root", "map-conflict-rolls-back", From 2a9a9e13434e16649cd5cdea89293b13f7afd501 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 21:34:35 +1100 Subject: [PATCH 3/7] display: report HPD to the dptx-port when it reaches an AP gpio display_configure() told the dptx-port service about HPD only on T8152. The reason holds for any machine where the DP-to-HDMI converter's HPD goes to an AP gpio instead of the phy: a dptx-port service that speaks AFKv2 does not bring the port up until the AP reports HPD. The ADT describes that wiring: the converter's dp2hdmi-gpio node has function-hdmi_hpd. Decide from that and from the service's format. The J873g's ADT has it on dp2hdmi-gpio0 and its service speaks AFKv2, so it keeps its HPD report; the M2 Mac mini on the 27.2 firmware now gets one too, and its firmware then starts link training. The older format never needed the report and does not get it. Signed-off-by: Ryan Murray --- src/dcp.c | 11 +++++++++++ src/dcp.h | 2 ++ src/display.c | 7 ++----- tests/python/test_t8103_iboot_power_status.py | 1 + tests/python/test_t8152_display_fb.py | 2 ++ 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/dcp.c b/src/dcp.c index dc1d0bb7e..e0f7ff553 100644 --- a/src/dcp.c +++ b/src/dcp.c @@ -99,6 +99,7 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg) printf("dcp: failed to find dp2hdmi-gpio node '%s'\n", cfg->dp2hdmi_gpio); return -1; } + dcp->hpd_on_ap = adt_getprop(adt, node, "function-hdmi_hpd", NULL) != NULL; struct adt_function_smc_gpio dp2hdmi_pwr, hdmi_pwr; u32 pw8_size; const struct adt_function_smc_pw8 *pw8_dp2hdmi = @@ -194,6 +195,16 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg) return 0; } +/* + * Whether the firmware has to be told about HPD. When the converter's HPD reaches an AP gpio + * (function-hdmi_hpd on its dp2hdmi-gpio node) instead of the phy, a dptx-port service that speaks + * AFKv2 brings the port up only once the AP reports HPD. The older format never needed it. + */ +bool dcp_dptx_needs_ap_hpd(dcp_dev_t *dcp) +{ + return dcp->hpd_on_ap && dcp_dptx_is_afkv2(dcp->dptx_ep); +} + int dcp_connect_dptx(dcp_dev_t *dcp) { if (dcp->dptx_ep && dcp->phy) { diff --git a/src/dcp.h b/src/dcp.h index 535fe1112..662fd0f84 100644 --- a/src/dcp.h +++ b/src/dcp.h @@ -39,9 +39,11 @@ typedef struct dcp_dev { u32 die; u32 dp2hdmi_pwr_gpio; u32 hdmi_pwr_gpio; + bool hpd_on_ap; /* the converter's HPD reaches an AP gpio (ADT function-hdmi_hpd) */ } dcp_dev_t; int dcp_connect_dptx(dcp_dev_t *dcp); +bool dcp_dptx_needs_ap_hpd(dcp_dev_t *dcp); int dcp_work(dcp_dev_t *dcp); dcp_dev_t *dcp_init(const display_config_t *config); diff --git a/src/display.c b/src/display.c index d6be54bfe..f0f7e4364 100644 --- a/src/display.c +++ b/src/display.c @@ -595,11 +595,8 @@ int display_configure(const char *config) ret = dcp_connect_dptx(dcp); if (ret < 0) return ret; - /* The T8152 DCP only brings the port up once HPD is asserted. On the - * J873g HDMI path the sink's HPD reaches the AP gpio, so tell the - * firmware. */ - if (chip_id == T8152) - dcp_dptx_hpd(dcp->dptx_ep, 0, true); + if (dcp_dptx_needs_ap_hpd(dcp) && dcp_dptx_hpd(dcp->dptx_ep, 0, true)) + printf("display: telling the dptx-port about HPD failed\n"); } if (!display_is_external) { diff --git a/tests/python/test_t8103_iboot_power_status.py b/tests/python/test_t8103_iboot_power_status.py index d57b45aac..b33e7f74d 100644 --- a/tests/python/test_t8103_iboot_power_status.py +++ b/tests/python/test_t8103_iboot_power_status.py @@ -170,6 +170,7 @@ def unit(): static int display_start_dcp(void) { return 0; } static int dcp_connect_dptx(dcp_dev_t *d) { (void)d; assert(!"no DPTX on the internal panel"); return -1; } static int dcp_dptx_hpd(void *dptx, u32 port, bool hpd) { (void)dptx; (void)port; (void)hpd; assert(!"no DPTX on the internal panel"); return -1; } +static bool dcp_dptx_needs_ap_hpd(dcp_dev_t *d) { (void)d; assert(!"no DPTX on the internal panel"); return false; } static int dcp_work(dcp_dev_t *d) { (void)d; return 0; } static void mdelay(unsigned ms) { (void)ms; delays++; } static u64 get_ticks(void) { return 0; } diff --git a/tests/python/test_t8152_display_fb.py b/tests/python/test_t8152_display_fb.py index 515836d65..8892d7b8a 100644 --- a/tests/python/test_t8152_display_fb.py +++ b/tests/python/test_t8152_display_fb.py @@ -277,6 +277,8 @@ def unit(): } static int dcp_connect_dptx(dcp_dev_t *d) { (void)d; return 0; } static int dcp_dptx_hpd(void *ep, u32 port, bool hpd) { (void)ep; (void)port; assert(hpd); hpd_pokes++; return 0; } +/* The J873g ADT gives dp2hdmi-gpio0 function-hdmi_hpd and its dptx-port speaks AFKv2. */ +static bool dcp_dptx_needs_ap_hpd(dcp_dev_t *d) { (void)d; return chip_id == T8152; } static int dcp_work(dcp_dev_t *d) { (void)d; return 0; } static void mdelay(unsigned ms) { (void)ms; } static u64 get_ticks(void) { return 0; } From cc8b270a4b4b03843206900ecd06cbf9c658343e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 21:34:57 +1100 Subject: [PATCH 4/7] dptx-phy: only the ATC phy acts on route, drive and deactivate requests The AFKv2 dptx-port handler passes WILL/DID_CHANGE_LINK_CONFIG, SET_DRIVE_SETTINGS and DEACTIVATE to the phy, which accepted them only for T8152's two phys and failed them for every other phy type. On the M2 Mac mini (dptx-phy,t8112) with the 27.2 firmware, WILL_CHANGE_LINK_ CONFIG then failed every time and link training never started; with that fixed, SET_DRIVE_SETTINGS failed each training round. Only a phy behind an ATC port switches its route, takes new drive levels or powers down per link. A dedicated dptx-phy is wired to its display-crossbar slot when it is set up, as the T8152 HDMI phy already was. Accept those requests for it, which is what the pre-AFKv2 handler does for every phy. Signed-off-by: Ryan Murray --- src/dcp/dptx_phy.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/dcp/dptx_phy.c b/src/dcp/dptx_phy.c index d2dd33fd6..71cffd50b 100644 --- a/src/dcp/dptx_phy.c +++ b/src/dcp/dptx_phy.c @@ -527,12 +527,12 @@ u32 dptx_phy_get_active_lane_count(dptx_phy_t *phy) int dptx_phy_set_drive_settings(dptx_phy_t *phy, const dptx_drive_settings_t *settings, u32 count) { - /* The HDMI phy is not reprogrammed for drive settings; the request is - * only accepted. */ - if (phy && phy->type == DPTX_PHY_T8152_HDMI) - return 0; - if (!phy || phy->type != DPTX_PHY_T8152_ATC) + if (!phy) return -1; + /* Only the ATC phy is reprogrammed. A dedicated dptx-phy only accepts the request, as the + * pre-AFKv2 dptx-port handler does for every phy. */ + if (phy->type != DPTX_PHY_T8152_ATC) + return 0; return atc_phy_set_drive_settings(phy->atc, settings, count); } @@ -739,8 +739,14 @@ int dptx_phy_set_link_rate(dptx_phy_t *phy, u32 link_rate) int dptx_phy_deactivate(dptx_phy_t *phy) { - if (!phy || phy->type != DPTX_PHY_T8152_ATC) + if (!phy) return -1; + /* Only the ATC phy is powered down per link. A dedicated dptx-phy stays set up for its + * display-crossbar slot; the request is only accepted, as the pre-AFKv2 handler does. */ + if (phy->type != DPTX_PHY_T8152_ATC) { + phy->active_lanes = 0; + return 0; + } int ret = atc_phy_deactivate(phy->atc); if (!ret) phy->active_lanes = 0; @@ -749,11 +755,12 @@ int dptx_phy_deactivate(dptx_phy_t *phy) int dptx_phy_set_route(dptx_phy_t *phy, bool enable) { - /* The dedicated HDMI phy has no per-link route to switch. */ - if (phy && phy->type == DPTX_PHY_T8152_HDMI) - return 0; - if (!phy || phy->type != DPTX_PHY_T8152_ATC) + if (!phy) return -1; + /* Only a phy behind an ATC port switches its route per link. A dedicated dptx-phy is wired to + * its display-crossbar slot once, when it is set up. */ + if (phy->type != DPTX_PHY_T8152_ATC) + return 0; return atc_phy_set_route(phy->atc, enable); } From affa84b1d92c25f7c2dc98409cc05f8e6282db96 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 21:35:37 +1100 Subject: [PATCH 5/7] dptx-phy: T8112: report HBR2, the rate its replayed setup runs at dptx_phy_set_link_rate() does not program a rate for the T8112 dptx-phy: it replays the one register setup taken from a macOS trace (fc64fcc6). The AFKv2 handler reported HBR as the maximum and accepted only HBR, so the 27.2 firmware trained at HBR while the phy ran that setup, and clock recovery failed on every lane in every round. macOS trains the M2 Mac mini's HDMI converter at HBR2. With HBR2 reported, the firmware trains at HBR2 and the link comes up. Report HBR2 for this phy and generalise the "qualified only at HBR" rule to any phy with a single rate: ACTIVATE starts at that rate and SET_LINK_RATE accepts only it. Phys qualified at HBR behave as before. Signed-off-by: Ryan Murray --- src/dcp/dptx_phy.c | 20 +++++++++++++++++--- src/dcp/dptx_phy.h | 1 + src/dcp/dptx_port_ep.c | 10 ++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/dcp/dptx_phy.c b/src/dcp/dptx_phy.c index 71cffd50b..a7fc78bf1 100644 --- a/src/dcp/dptx_phy.c +++ b/src/dcp/dptx_phy.c @@ -764,11 +764,25 @@ int dptx_phy_set_route(dptx_phy_t *phy, bool enable) return atc_phy_set_route(phy->atc, enable); } -/* Highest link rate in MHz: the HDMI phy has PLL configurations up to HBR3 - * (macOS trains the PS190 at HBR3); everything else is qualified at HBR. */ +/* + * Highest link rate in MHz. The T8152 HDMI phy has PLL configurations up to HBR3 (macOS trains the + * PS190 at HBR3). The T8112 dptx-phy runs the one setup dptx_phy_set_link_rate() replays from a + * macOS trace, which trains at HBR2 (macOS trains the M2 Mac mini's HDMI converter at HBR2; HBR + * fails clock recovery on every lane). Everything else is qualified at HBR. + */ u32 dptx_phy_max_link_rate(dptx_phy_t *phy) { - return phy && phy->type == DPTX_PHY_T8152_HDMI ? 8100 : 2700; + if (phy && phy->type == DPTX_PHY_T8152_HDMI) + return 8100; + if (phy && phy->type == DPTX_PHY_T8112) + return 5400; + return 2700; +} + +/* Whether the phy runs at its highest rate only (every phy without its own PLL configurations). */ +bool dptx_phy_single_rate(dptx_phy_t *phy) +{ + return !phy || phy->type != DPTX_PHY_T8152_HDMI; } u32 dptx_phy_dcp_output(dptx_phy_t *phy) diff --git a/src/dcp/dptx_phy.h b/src/dcp/dptx_phy.h index 0dfd229aa..e8bece09c 100644 --- a/src/dcp/dptx_phy.h +++ b/src/dcp/dptx_phy.h @@ -21,6 +21,7 @@ u32 dptx_phy_get_active_lane_count(dptx_phy_t *phy); int dptx_phy_set_drive_settings(dptx_phy_t *phy, const dptx_drive_settings_t *settings, u32 count); int dptx_phy_set_link_rate(dptx_phy_t *phy, u32 link_rate); u32 dptx_phy_max_link_rate(dptx_phy_t *phy); +bool dptx_phy_single_rate(dptx_phy_t *phy); u32 dptx_phy_dcp_output(dptx_phy_t *phy); diff --git a/src/dcp/dptx_port_ep.c b/src/dcp/dptx_port_ep.c index a037a7211..639abdc00 100644 --- a/src/dcp/dptx_port_ep.c +++ b/src/dcp/dptx_port_ep.c @@ -557,7 +557,9 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, case DPTX_APCALL_ACTIVATE: if (dptx_phy_activate(port->phy)) return 0xe00002bc; /* kIOReturnError */ - port->link_rate = port->pending_link_rate = LINK_RATE_HBR; + port->link_rate = port->pending_link_rate = + dptx_phy_single_rate(port->phy) ? dptx_phy_max_link_rate(port->phy) / 270 + : LINK_RATE_HBR; return 0; case DPTX_APCALL_DEACTIVATE: if (dptx_phy_deactivate(port->phy)) @@ -582,9 +584,9 @@ static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, rate = ((const u8 *)data)[16]; if (rate * 270 > dptx_phy_max_link_rate(port->phy)) return 0xe00002c7; - /* A phy qualified only at HBR keeps that single rate. */ - if (dptx_phy_max_link_rate(port->phy) == 2700 && - (rate != LINK_RATE_HBR || port->link_rate != LINK_RATE_HBR)) + /* A phy qualified at a single rate keeps it. */ + if (dptx_phy_single_rate(port->phy) && + (rate * 270 != dptx_phy_max_link_rate(port->phy) || port->link_rate != rate)) return 0xe00002c7; if (dptx_phy_set_link_rate(port->phy, rate * 270)) return 0xe00002bc; From 2ec129547add01c314b31975a1326d28b4b1934d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 21:40:51 +1100 Subject: [PATCH 6/7] display: hand a DCP with a connected AFKv2 dptx-port on running Once a dptx-port that speaks AFKv2 has been connected, its DCP answers no power request. On the M2 Mac mini with the 27.2 firmware, after the HDMI modeset neither AP QUIESCED or IDLE nor IOP SLEEP is acknowledged, not with longer waits, not after HPD is dropped, the display released or powered off, nor after DEACTIVATE succeeds. dcp_shutdown() then keeps it as running and kboot refuses to boot the next stage ("Display handoff failed; returning to proxy"). Without a connected port the same DCP sleeps fine. Hand such a DCP on running instead, as the T8152 and T8132 displays already are: stop the iBoot interface and the other endpoints (dcp_handoff()) and leave RTKit, its buffers and DART mappings and the scanout running. The display stays lit, so U-Boot, GRUB and Linux's simpledrm keep showing the framebuffer aura set up. The DCP counts as running from then on, so this stage never starts it again, and kboot keeps this stage's heap, where its RTKit buffers live, out of the next stage's memory, as it already does for a post-M4 ANS. A kernel that takes over such a DCP has to adopt a running core, as the T8132 one does; today's kernel does not drive the 27.2 DCPs of T8112. Signed-off-by: Ryan Murray --- src/dcp.c | 20 +++ src/dcp.h | 2 + src/display.c | 28 ++++- src/display.h | 1 + src/kboot.c | 3 +- tests/python/test_display_dptx_hand_on.py | 118 ++++++++++++++++++ .../test_display_inherited_reservation.py | 2 +- 7 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 tests/python/test_display_dptx_hand_on.py diff --git a/src/dcp.c b/src/dcp.c index e0f7ff553..8af887d16 100644 --- a/src/dcp.c +++ b/src/dcp.c @@ -450,6 +450,26 @@ int dcp_handoff(dcp_dev_t *dcp) return ret; } +static bool dcp_handed_on; + +/* + * Hand a running DCP on to the next stage: its endpoints stopped (dcp_handoff()), RTKit, its + * buffers and DART mappings and the scanout left running. It counts as running from here on, so + * it is not started again and the next stage keeps its memory (kboot reserves the heap). + */ +int dcp_hand_on(dcp_dev_t *dcp) +{ + int ret = dcp_handoff(dcp); + dcp_running = true; + dcp_handed_on = !ret; + return ret; +} + +bool dcp_handed_on_live(void) +{ + return dcp_handed_on; +} + int dcp_shutdown(dcp_dev_t *dcp, bool sleep) { /* The ASCWrap-v8 sleep path needs a PMGR2 reset, which is not implemented diff --git a/src/dcp.h b/src/dcp.h index 662fd0f84..c014fa9ac 100644 --- a/src/dcp.h +++ b/src/dcp.h @@ -50,6 +50,8 @@ dcp_dev_t *dcp_init(const display_config_t *config); int dcp_shutdown(dcp_dev_t *dcp, bool sleep); int dcp_handoff(dcp_dev_t *dcp); +int dcp_hand_on(dcp_dev_t *dcp); +bool dcp_handed_on_live(void); bool dcp_left_running(void); u32 t8152_psd_state(u32 service); bool dcp_init_left_quiesced(void); diff --git a/src/display.c b/src/display.c index f0f7e4364..e23f8455f 100644 --- a/src/display.c +++ b/src/display.c @@ -1205,6 +1205,24 @@ static int display_handoff_t8152(void) return ret; } +/* + * Once a dptx-port that speaks AFKv2 has been connected, its DCP acknowledges no power request: + * on the M2 Mac mini with the 27.2 firmware neither AP QUIESCED or IDLE nor IOP SLEEP is answered, + * even after the display is released and powered off. It cannot be slept or quiesced, so it is + * handed on running, as the T8152 and T8132 displays are, with the display left lit: the next + * stage keeps showing the framebuffer. + */ +static bool display_dptx_hands_on(void) +{ + return display_is_dptx && dcp && dcp_dptx_is_afkv2(dcp->dptx_ep); +} + +/* Whether a DCP was handed on running; its buffers stay in this stage's heap. */ +bool display_handed_on_live(void) +{ + return dcp_handed_on_live(); +} + int display_shutdown(dcp_shutdown_mode mode) { if (chip_id == T8152 && mode == DCP_HANDOFF) @@ -1223,6 +1241,11 @@ int display_shutdown(dcp_shutdown_mode mode) /* An endpoint that does not stop stays registered, so dcp_shutdown() * retries it with the rest and reports the DCP as still running. */ dcp_ib_shutdown(iboot); + if (display_dptx_hands_on()) { + printf("display: handing the DCP on running (its dptx-port speaks AFKv2)\n"); + iboot = NULL; + return dcp_hand_on(dcp) < 0 ? -1 : 0; + } switch (mode) { case DCP_QUIESCED: printf("display: Quiescing DCP (unconditional)\n"); @@ -1243,6 +1266,7 @@ int display_shutdown(dcp_shutdown_mode mode) iboot = NULL; } - /* A DCP that did not stop at any point may still use its buffers. */ - return dcp_left_running() ? -1 : 0; + /* A DCP that did not stop at any point may still use its buffers; one handed on running + * keeps them on purpose. */ + return dcp_left_running() && !dcp_handed_on_live() ? -1 : 0; } diff --git a/src/display.h b/src/display.h index 229f999c3..195532f1f 100644 --- a/src/display.h +++ b/src/display.h @@ -29,6 +29,7 @@ char *display_afk_var(void); int display_shutdown(dcp_shutdown_mode mode); const display_config_t *display_get_config(void); bool display_dart_tables_inherited(void); +bool display_handed_on_live(void); /* The second display's framebuffer (J873g HDMI next to USB-C), base 0 if none. */ extern struct boot_video display_second_video; diff --git a/src/kboot.c b/src/kboot.c index 73c770ee5..c7e0502cc 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -12514,7 +12514,8 @@ int kboot_prepare_dt(void *fdt) * or powered off: they must stay out of Linux's allocator (J616C: ANS syslog lines in * page-cache pages at the SART-allowed heap addresses). */ - if (nvme_post_m4()) { + /* A DCP handed on running keeps its RTKit buffers in this stage's heap too. */ + if (nvme_post_m4() || display_handed_on_live()) { u64 ram_end, inherited = cur_boot_args.top_of_kernel_data; u64 high_water = heapblock_high_water(); diff --git a/tests/python/test_display_dptx_hand_on.py b/tests/python/test_display_dptx_hand_on.py new file mode 100644 index 000000000..b0d23c678 --- /dev/null +++ b/tests/python/test_display_dptx_hand_on.py @@ -0,0 +1,118 @@ +# SPDX-License-Identifier: MIT +"""display_shutdown() hands a DCP with a connected AFKv2 dptx-port on running. + +Once such a port has been connected, its DCP acknowledges no power request (M2 Mac mini, 27.2 +firmware), so display_shutdown() stops its endpoints and leaves it running instead of sleeping it. +Every other DCP keeps the sleep/quiesce it had. display_shutdown() and display_dptx_hands_on() are +taken from src/display.c; the DCP calls are stubs that record what was asked. +""" +from pathlib import Path +import os +import subprocess + +SOURCE = Path(os.environ.get("DART_DISPLAY_SOURCE", Path(__file__).resolve().parents[2])) + + +def function(text, signature): + start = text.index(signature) + depth = 0 + for i in range(text.index("{", start), len(text)): + if text[i] == "{": + depth += 1 + elif text[i] == "}": + depth -= 1 + if not depth: + return text[start:i + 1] + "\n" + raise AssertionError("unterminated " + signature) + + +HARNESS = r""" +#include +#include +#include +#include +#define T8112 0x8112 +#define T8132 0x8132 +#define T8140 0x8140 +#define T8152 0x8152 +typedef enum { DCP_QUIESCED, DCP_SLEEP_IF_EXTERNAL, DCP_SLEEP, DCP_HANDOFF } dcp_shutdown_mode; +typedef struct { bool afkv2; } dcp_dptx_if_t; +typedef struct { dcp_dptx_if_t *dptx_ep; bool endpoint_fails; } dcp_dev_t; +typedef struct { int unused; } dcp_iboot_if_t; +static unsigned chip_id = T8112; +static bool has_dcp = true, display_is_external = true, display_is_dptx; +static dcp_dev_t *dcp; +static dcp_iboot_if_t *iboot; +static bool running, handed_on; +static char calls[256]; +static void call(const char *what) { strcat(calls, what); strcat(calls, " "); } +static bool display_hands_on_live(void) { return chip_id == T8140 || chip_id == T8132 || chip_id == T8152; } +static int display_handoff_t8152(void) { call("t8152"); return 0; } +static bool dcp_dptx_is_afkv2(const dcp_dptx_if_t *ep) { return ep && ep->afkv2; } +static int dcp_ib_shutdown(dcp_iboot_if_t *ib) { (void)ib; call("ib"); return 0; } +static int dcp_shutdown(dcp_dev_t *d, bool sleep) { (void)d; call(sleep ? "sleep" : "quiesce"); return 0; } +static int dcp_hand_on(dcp_dev_t *d) +{ + call("hand-on"); + running = true; + handed_on = !d->endpoint_fails; + return d->endpoint_fails ? -1 : 0; +} +static bool dcp_handed_on_live(void) { return handed_on; } +static bool dcp_left_running(void) { return running; } +""" + +MAIN = r""" +static void reset(dcp_dev_t *d, dcp_iboot_if_t *ib, bool dptx, bool external) +{ + dcp = d; + iboot = ib; + display_is_dptx = dptx; + display_is_external = external; + running = handed_on = false; + calls[0] = 0; +} +int main(void) +{ + dcp_dptx_if_t v2 = {true}, v1 = {false}; + dcp_dev_t d2 = {&v2, false}, d1 = {&v1, false}, internal = {NULL, false}, stuck = {&v2, true}; + dcp_iboot_if_t ib; + + /* AFKv2 dptx-port: iBoot interface stopped, then handed on running; no sleep. */ + reset(&d2, &ib, true, true); + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == 0); + assert(!strcmp(calls, "ib hand-on ") && !iboot); + /* The Linux boundary asks again: nothing more to do, and still no failure. */ + calls[0] = 0; + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == 0 && !calls[0]); + + /* The pre-AFKv2 format (13.5 firmware) keeps the sleep. */ + reset(&d1, &ib, true, true); + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == 0 && !strcmp(calls, "ib sleep ")); + + /* An internal panel without a dptx-port keeps the quiesce. */ + reset(&internal, &ib, false, false); + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == 0 && !strcmp(calls, "ib quiesce ")); + + /* An endpoint that does not stop fails the hand-on, then and at the next boundary. */ + reset(&stuck, &ib, true, true); + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == -1 && !strcmp(calls, "ib hand-on ")); + assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == -1); + puts("ok"); + return 0; +} +""" + + +def test_afkv2_dptx_dcp_is_handed_on_running(tmp_path): + disp = (SOURCE / "src/display.c").read_text() + source = HARNESS + "".join(function(disp, signature) for signature in ( + "static bool display_dptx_hands_on(", + "int display_shutdown(dcp_shutdown_mode mode)", + )) + MAIN + exe = tmp_path / "hand-on" + subprocess.run(["cc", "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-Wno-switch", + "-x", "c", "-", "-o", str(exe)], input=source, text=True, check=True) + out = subprocess.run([str(exe)], capture_output=True, text=True, check=True).stdout + assert out.count("handing the DCP on running") == 2 + assert out.endswith("ok\n") diff --git a/tests/python/test_display_inherited_reservation.py b/tests/python/test_display_inherited_reservation.py index 3cbdba71f..8f0534d2e 100644 --- a/tests/python/test_display_inherited_reservation.py +++ b/tests/python/test_display_inherited_reservation.py @@ -22,7 +22,7 @@ def test_inherited_reservation(tmp_path): text = (SOURCE / "src/kboot.c").read_text() start = text.index(" u64 image_end = (u64)_end;", text.index("int kboot_prepare_dt(")) # Followed by the post-M4 retained-heap block. - end = text.index(" if (nvme_post_m4())", start) + end = text.index(" if (nvme_post_m4()", start) block = text[start:end] call = re.search( r" if \(dt_reserve_outside_exec_window\(\(u64\)_base, image_end - \(u64\)_base\)\)\n" From 3daee9293eab4ddbbfca023dd7f8dd4befdbba82 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 22 Nov 2026 22:37:21 +1100 Subject: [PATCH 7/7] display: carry live DCP ownership across aura stages Advertise live display support in the load header and pass the retained heap in a copy of the ADT. Preserve the inherited framebuffer without starting a second RTKit session. Reject raw proxy vectors while a live receiver is required. Complete pending live display handoffs before measuring retained memory. Keep the reservation clear of earlier FDT entries and require successful DT preparation before kernel entry. Signed-off-by: Ryan Murray --- src/chainload.c | 129 ++++++++++++++---- src/chainload.h | 1 + src/display.c | 36 ++++- src/display.h | 5 + src/kboot.c | 81 +++++++++-- src/proxy.c | 5 + src/start.S | 2 +- tests/python/test_chainload_run_base.py | 93 ++++++++++++- tests/python/test_ctxr_exec_window.py | 7 +- tests/python/test_display_dptx_hand_on.py | 14 +- tests/python/test_display_inherited_only.py | 1 + .../test_display_inherited_reservation.py | 45 +++++- tests/python/test_display_panel_absent.py | 1 + tests/python/test_smp_handoff_gate.py | 3 + tests/python/test_t8152_display_fb.py | 1 + 15 files changed, 369 insertions(+), 55 deletions(-) diff --git a/src/chainload.c b/src/chainload.c index 36371c7ad..95b890d18 100644 --- a/src/chainload.c +++ b/src/chainload.c @@ -37,7 +37,7 @@ _Static_assert(offsetof(struct chainload_header, run_base) == CHAINLOAD_HDR_RUN_ * Return how many bytes from its base an image needs to run at a run_base below its staged copy * (chainload.h), or 0 if it does not support that. */ -static u64 chainload_run_footprint(const void *image, size_t size) +static u64 chainload_run_footprint(const void *image, size_t size, u32 *flags) { struct chainload_header hdr; @@ -48,16 +48,38 @@ static u64 chainload_run_footprint(const void *image, size_t size) !(hdr.flags & CHAINLOAD_HDR_F_RUN_BASE) || hdr.run_base || !hdr.footprint || (hdr.footprint & (SZ_16K - 1)) || hdr.footprint > size) return 0; + if (flags) + *flags = hdr.flags; return hdr.footprint; } +#define LIVE_DCP_ADT_ENTRY_SIZE (sizeof(struct adt_property) + sizeof(u64)) + +/* A copied tree has one new property before its memory-map child. */ +static int chainload_set_adt_range(void *copy, int node, const char *name, u64 range[2]) +{ + if (!copy) + return adt_setprop(adt, node, name, range, 2 * sizeof(u64)); + + u32 size; + const u8 *value = adt_getprop(adt, node, name, &size); + if (!value || size != 2 * sizeof(u64)) + return -1; + size_t offset = value - (const u8 *)adt; + if (offset > cur_boot_args.devtree_size || size > cur_boot_args.devtree_size - offset) + return -1; + memcpy((u8 *)copy + offset + LIVE_DCP_ADT_ENTRY_SIZE, range, size); + return 0; +} + int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) { u64 new_base = (u64)_base; u64 image_size = size; u64 ram_end, ro_start = 0, ro_end = 0; - u64 live_heap = heapblock_high_water(); + u64 live_heap; u64 inherited_top = cur_boot_args.top_of_kernel_data; + u64 protected_heap_start = inherited_top; int ro_status = memory_fw_ro_range(&ro_start, &ro_end); printf("chainload: Preparing image...\n"); @@ -65,9 +87,21 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) if (!image || !size || ro_status < 0 || cur_boot_args.phys_base > UINT64_MAX - cur_boot_args.mem_size) return -1; + u32 target_flags = 0; + u64 target_footprint = chainload_run_footprint(image, size, &target_flags); + if (!(target_flags & CHAINLOAD_HDR_F_LIVE_DCP) && display_needs_live_receiver()) { + printf("chainload: next image cannot inherit the running DCP\n"); + return -1; + } + /* Capture any newly retained DCP buffers before laying out the next image. */ + if (display_prepare_live_handoff() < 0) + return -1; + live_heap = heapblock_high_water(); ram_end = cur_boot_args.phys_base + cur_boot_args.mem_size; + display_inherited_live_heap(&protected_heap_start); if (new_base < cur_boot_args.phys_base || new_base >= ram_end || live_heap > ram_end || - inherited_top > ram_end) + inherited_top > ram_end || protected_heap_start > inherited_top || + protected_heap_start < cur_boot_args.phys_base) return -1; for (size_t i = 0; i < var_cnt; i++) { @@ -108,6 +142,25 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) if (fw_bootargs_off > UINT64_MAX - SZ_16K) return -1; u64 fw_size = fw_bootargs_off + SZ_16K; + int chosen = -1, prop_offset = -1; + size_t adt_size = cur_boot_args.devtree_size; + u64 adt_off = fw_size; + if (display_handed_on_live()) { + /* Build a private ADT copy for the next stage. The current stage may retry. */ + chosen = adt_path_offset(adt, "/chosen"); + if (chosen < 0) + return -1; + if (adt_get_property_count(adt, chosen) <= 0) + return -1; + prop_offset = adt_first_property_offset(adt, chosen); + if (prop_offset < 0 || (size_t)prop_offset > adt_size || + adt_size > UINT32_MAX - LIVE_DCP_ADT_ENTRY_SIZE) + return -1; + adt_size += LIVE_DCP_ADT_ENTRY_SIZE; + if (fw_size > UINT64_MAX - ALIGN_UP(adt_size, SZ_16K)) + return -1; + fw_size += ALIGN_UP(adt_size, SZ_16K); + } if (image_size > UINT64_MAX - fw_size || new_base > UINT64_MAX - image_size - fw_size) return -1; @@ -123,7 +176,7 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) * base (start.S) while its payload, SEPFW and boot_args stay in the staged copy, below * the new high-water. */ - footprint = chainload_run_footprint(image, size); + footprint = target_footprint; if (!footprint || (new_base & (SZ_16K - 1)) || new_base >= ro_start || footprint > ro_start - new_base) { printf("chainload: image overlaps firmware RO and cannot run below it\n"); @@ -133,7 +186,8 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) } else if (packed_end > ram_end) { printf("chainload: image destination is invalid or overlaps firmware RO\n"); return -1; - } else if (live_heap > inherited_top && image_end > inherited_top && new_base < live_heap) { + } else if (live_heap > protected_heap_start && image_end > protected_heap_start && + new_base < live_heap) { /* * The next stage is larger than the room our caller left between our base and the heap * (a Stage 1 smaller than its Stage 2 plus the firmware block), so a copy to our base @@ -141,19 +195,19 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) * it is staged instead and move only its first footprint bytes to our base, which is * safe while those stay below the heap. */ - footprint = chainload_run_footprint(image, size); - if (!footprint || (new_base & (SZ_16K - 1)) || inherited_top <= new_base || - footprint > inherited_top - new_base) { + footprint = target_footprint; + if (!footprint || (new_base & (SZ_16K - 1)) || protected_heap_start <= new_base || + footprint > protected_heap_start - new_base) { printf("chainload: image span overlaps the live heap\n"); return -1; } must_stage = true; } - bool park_fw = - !run_base && !must_stage && - ((ro_status == 1 && packed_end > ro_start && image_end < ro_end) || - (live_heap > inherited_top && packed_end > inherited_top && image_end < live_heap)); + bool park_fw = !run_base && !must_stage && + ((ro_status == 1 && packed_end > ro_start && image_end < ro_end) || + (live_heap > protected_heap_start && packed_end > protected_heap_start && + image_end < live_heap)); u8 *parked = NULL; u64 fw_base = image_end; if (park_fw) { @@ -189,7 +243,7 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) copy_size + stub_size <= ram_end - staged && !(ro_status == 1 && staged < ro_end && staged + copy_size + stub_size > ro_start); if (!run_base && !park_fw && fits_staged) { - u64 run_footprint = chainload_run_footprint(image, size); + u64 run_footprint = target_footprint; if (run_footprint && !(new_base & (SZ_16K - 1)) && staged >= new_base + run_footprint && (ro_status != 1 || new_base + run_footprint <= ro_start || new_base >= ro_end)) { footprint = run_footprint; @@ -243,28 +297,42 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) high_water = max(high_water, ro_end); new_boot_args->top_of_kernel_data = high_water; - u64 new_sepfw[2] = {fw_base, sepfw[1]}; - if (adt_setprop(adt, anode, "SEPFW", new_sepfw, sizeof(new_sepfw)) < 0) { - free(new_image); - free(parked); - return -1; + u8 *next_adt = NULL; + if (chosen >= 0) { + next_adt = fw_data + adt_off; + struct adt_property *prop = (void *)(next_adt + prop_offset); + struct adt_node_hdr *node = (void *)(next_adt + chosen); + u64 next_adt_pa = fw_base + adt_off; + + memcpy(next_adt, adt, prop_offset); + memcpy(prop->name, DISPLAY_LIVE_ADT_PROP, sizeof(DISPLAY_LIVE_ADT_PROP)); + prop->size = sizeof(inherited_top); + memcpy(prop->value, &inherited_top, sizeof(inherited_top)); + memcpy(prop->value + sizeof(inherited_top), (u8 *)adt + prop_offset, + cur_boot_args.devtree_size - prop_offset); + node->property_count++; + new_boot_args->devtree = + (void *)(next_adt_pa - cur_boot_args.phys_base + cur_boot_args.virt_base); + new_boot_args->devtree_size = adt_size; + u64 tree_range[2] = {next_adt_pa, ALIGN_UP(adt_size, SZ_16K)}; + if (adt_getprop(adt, anode, "DeviceTree", NULL) && + chainload_set_adt_range(next_adt, anode, "DeviceTree", tree_range) < 0) + goto bad_adt_range; } + + u64 new_sepfw[2] = {fw_base, sepfw[1]}; + if (chainload_set_adt_range(next_adt, anode, "SEPFW", new_sepfw) < 0) + goto bad_adt_range; if (preoslog[1]) { preoslog[0] = fw_base + fw_preoslog_off; - if (adt_setprop(adt, anode, "preoslog", preoslog, sizeof(preoslog)) < 0) { - free(new_image); - free(parked); - return -1; - } + if (chainload_set_adt_range(next_adt, anode, "preoslog", preoslog) < 0) + goto bad_adt_range; } /* Point the memory map at the moved boot arguments, where the ADT lists them. */ u64 bootargs_map[2] = {fw_base + fw_bootargs_off, SZ_16K}; if (adt_getprop(adt, anode, "BootArgs", NULL) && - adt_setprop(adt, anode, "BootArgs", bootargs_map, sizeof(bootargs_map)) < 0) { - free(new_image); - free(parked); - return -1; - } + chainload_set_adt_range(next_adt, anode, "BootArgs", bootargs_map) < 0) + goto bad_adt_range; if (parked) { dc_cvac_range(parked, fw_size); sysop("dsb sy"); @@ -291,6 +359,11 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) next_stage.args[4] = new_base + CHAINLOAD_ENTRY_OFFSET; next_stage.restore_logo = false; return 0; + +bad_adt_range: + free(new_image); + free(parked); + return -1; } #ifdef CHAINLOADING diff --git a/src/chainload.h b/src/chainload.h index 9678258c6..f8870a951 100644 --- a/src/chainload.h +++ b/src/chainload.h @@ -15,6 +15,7 @@ #define CHAINLOAD_HDR_MAGIC 0x44414f4c41525541 /* "AURALOAD" */ #define CHAINLOAD_HDR_VERSION 1 #define CHAINLOAD_HDR_F_RUN_BASE 1 +#define CHAINLOAD_HDR_F_LIVE_DCP 2 /* consumes the live DCP marker in the ADT */ #define CHAINLOAD_HDR_FOOTPRINT_OFF 0x10 #define CHAINLOAD_HDR_RUN_BASE_OFF 0x18 #define CHAINLOAD_HDR_SIZE 0x20 diff --git a/src/display.c b/src/display.c index e23f8455f..c97acbf2b 100644 --- a/src/display.c +++ b/src/display.c @@ -418,13 +418,24 @@ static void display_stop_failed_dcp(void) display_dcp_state = !stopped; } +bool display_inherited_live_heap(u64 *start) +{ + int chosen = adt_path_offset(adt, "/chosen"); + u64 base; + if (chosen < 0 || ADT_GETPROP(adt, chosen, DISPLAY_LIVE_ADT_PROP, &base) != sizeof(base)) + return false; + if (start) + *start = base; + return true; +} + int display_start_dcp(void) { if (iboot) return 0; - if (dcp_left_running()) { - printf("display: a DCP did not stop; not starting it again\n"); + if (dcp_left_running() || display_inherited_live_heap(NULL)) { + printf("display: a DCP is still running; not starting it again\n"); return -1; } @@ -1123,6 +1134,12 @@ int display_init(void) else printf("display: Display is internal\n"); + if (display_inherited_live_heap(NULL)) { + printf("display: preserving live display from the previous stage (%ldx%ld)\n", + cur_boot_args.video.width, cur_boot_args.video.height); + return 0; + } + if (chip_id == T8140 || chip_id == T8132 || (chip_id == T6040 && board_id == 4 && adt_is_compatible(adt, 0, "J614sAP")) || /* J575c: iBoot leaves the internal DCP running with no panel and a dummy framebuffer; @@ -1217,6 +1234,21 @@ static bool display_dptx_hands_on(void) return display_is_dptx && dcp && dcp_dptx_is_afkv2(dcp->dptx_ep); } +bool display_needs_live_receiver(void) +{ + return dcp_handed_on_live() || display_inherited_live_heap(NULL) || + (iboot && display_dptx_hands_on() && !display_hands_on_live()); +} + +/* Stop the endpoints before callers lay out the next image or measure its reservations. */ +int display_prepare_live_handoff(void) +{ + if (!iboot || !display_dptx_hands_on()) + return 0; + + return display_shutdown(DCP_SLEEP_IF_EXTERNAL); +} + /* Whether a DCP was handed on running; its buffers stay in this stage's heap. */ bool display_handed_on_live(void) { diff --git a/src/display.h b/src/display.h index 195532f1f..c5fb271c2 100644 --- a/src/display.h +++ b/src/display.h @@ -29,8 +29,13 @@ char *display_afk_var(void); int display_shutdown(dcp_shutdown_mode mode); const display_config_t *display_get_config(void); bool display_dart_tables_inherited(void); +bool display_needs_live_receiver(void); +int display_prepare_live_handoff(void); bool display_handed_on_live(void); +#define DISPLAY_LIVE_ADT_PROP "aurora,dcp-retained-heap" +bool display_inherited_live_heap(u64 *start); + /* The second display's framebuffer (J873g HDMI next to USB-C), base 0 if none. */ extern struct boot_video display_second_video; diff --git a/src/kboot.c b/src/kboot.c index c7e0502cc..d7c69bba4 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -68,6 +68,7 @@ static void *dt = NULL; static int dt_bufsize = 0; static bool dt_ready; +static bool dt_retained_live_dcp; static void *initrd_start = NULL; static size_t initrd_size = 0; static char *chosen_params[MAX_CHOSEN_PARAMS][2]; @@ -756,6 +757,41 @@ static int dt_reserve_outside_exec_window(u64 start, u64 size) return 0; } +/* + * U-Boot may discard an entire memreserve entry that partially overlaps an earlier one. + * Add only uncovered intervals, including when the DT buffer or an individual DART table + * occupies part of the retained heap. + */ +static int dt_reserve_retained_span(u64 start, u64 end) +{ + int count = fdt_num_mem_rsv(dt); + if (count < 0) + return -1; + + while (start < end) { + u64 covered_end = start, gap_end = end; + for (int i = 0; i < count; i++) { + u64 base, size; + if (fdt_get_mem_rsv(dt, i, &base, &size) || size > UINT64_MAX - base) + return -1; + if (!size) + continue; + if (base <= start) + covered_end = max(covered_end, base + size); + else + gap_end = min(gap_end, base); + } + if (covered_end > start) { + start = covered_end; + continue; + } + if (dt_reserve_outside_exec_window(start, gap_end - start)) + return -1; + start = gap_end; + } + return 0; +} + /* Windows A to C. One inside the firmware RO range (T8140) is reserved with that range. */ static int dt_reserve_ctxr_low_windows(void) { @@ -12449,10 +12485,18 @@ int kboot_prepare_dt(void *fdt) } dt_ready = false; + dt_retained_live_dcp = false; if (!ctxr_exec_os_may_start()) return -1; + /* Finalize display ownership before measuring the heap that the DCP can still use. */ + if (display_prepare_live_handoff() < 0) + bail("FDT: display handoff failed\n"); + + bool local_dcp = display_handed_on_live(); + u64 display_heap_end = heapblock_high_water(); + /* Before the tree and anything kboot reserves are allocated */ kboot_decide_hibernate_layout(); @@ -12491,8 +12535,18 @@ int kboot_prepare_dt(void *fdt) if (dart_reserve_legacy_tables(dt, cur_boot_args.phys_base, cur_boot_args.mem_size)) bail("FDT: couldn't reserve inherited display translation tables\n"); - u64 image_end = (u64)_end; - if (display_dart_tables_inherited()) { + u64 image_start = (u64)_base, image_end = (u64)_end; + u64 inherited_dcp_heap; + bool inherited_dcp = display_inherited_live_heap(&inherited_dcp_heap); + if (inherited_dcp) { + u64 top = cur_boot_args.top_of_kernel_data; + if (inherited_dcp_heap < cur_boot_args.phys_base || inherited_dcp_heap >= top) + bail("FDT: invalid inherited display heap\n"); + image_start = min(image_start, ALIGN_DOWN(inherited_dcp_heap, SZ_16K)); + if (image_start < cur_boot_args.phys_base) + bail("FDT: inherited display heap overlaps the start of RAM\n"); + } + if (inherited_dcp || display_dart_tables_inherited()) { /* chainload_image carries the previous stage's live heap in this * high-water mark. Tables that stage published into a locked display * DART's inherited roots have no local record here, so keep the @@ -12515,32 +12569,31 @@ int kboot_prepare_dt(void *fdt) * page-cache pages at the SART-allowed heap addresses). */ /* A DCP handed on running keeps its RTKit buffers in this stage's heap too. */ - if (nvme_post_m4() || display_handed_on_live()) { + if (nvme_post_m4() || local_dcp) { u64 ram_end, inherited = cur_boot_args.top_of_kernel_data; - u64 high_water = heapblock_high_water(); + u64 high_water = nvme_post_m4() ? heapblock_high_water() : display_heap_end; if (!cur_boot_args.mem_size || cur_boot_args.phys_base > UINT64_MAX - cur_boot_args.mem_size) bail("FDT: invalid inherited RAM extent\n"); ram_end = cur_boot_args.phys_base + cur_boot_args.mem_size; - image_end = max(image_end, inherited); if ((u64)_base < cur_boot_args.phys_base || image_end < (u64)_base || image_end > ram_end || - high_water > ram_end) + inherited > ram_end || high_water > ram_end) bail("FDT: invalid retained memory extent\n"); u64 heap_top = max(inherited, high_water); if (heap_top > (u64)_end) { if (heap_top > UINT64_MAX - (SZ_16K - 1)) bail("FDT: retained heap extent overflows\n"); - u64 heap_start = ALIGN_DOWN((u64)_end, SZ_16K); + u64 heap_start = ALIGN_DOWN(image_end, SZ_16K); u64 heap_end = ALIGN_UP(heap_top, SZ_16K); - if (heap_end > ram_end || - dt_reserve_outside_exec_window(heap_start, heap_end - heap_start)) + if (heap_end > ram_end || dt_reserve_retained_span(heap_start, heap_end)) bail("FDT: couldn't reserve retained heap\n"); + image_end = heap_start; printf("FDT: retained heap [0x%lx, 0x%lx)\n", heap_start, heap_end); } } - if (dt_reserve_outside_exec_window((u64)_base, image_end - (u64)_base)) + if (dt_reserve_retained_span(image_start, image_end)) bail("FDT: couldn't add reservation for aura and inherited high-water\n"); /* @@ -12823,6 +12876,7 @@ int kboot_prepare_dt(void *fdt) kboot_withdraw_hibernate_layout("the heap came within 8 MiB of the fixed end")) return -1; + dt_retained_live_dcp = local_dcp || inherited_dcp; printf("FDT prepared at %p\n", dt); return 0; @@ -12952,6 +13006,13 @@ int kboot_boot(void *kernel) next_stage.entry = NULL; bail("kboot: refusing handoff while secondary callbacks are pending\n"); } + /* The proxy can configure a display between preparing and booting the tree. */ + if (display_prepare_live_handoff() < 0 || + (display_needs_live_receiver() && !dt_retained_live_dcp)) { + next_stage.entry = NULL; + bail("kboot: display handoff changed; prepare the device tree again\n"); + } + int ret = mcc_enable_cache(); if (ret && chip_id == T8140) { printf("kboot: refusing T8140 handoff after MCC cache failure\n"); diff --git a/src/proxy.c b/src/proxy.c index f924dffa9..007918c99 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -107,6 +107,11 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply) request->args[3], request->args[4]); break; case P_VECTOR: + if (display_needs_live_receiver()) { + printf("proxy: raw vector cannot hand over the running DCP\n"); + reply->status = S_ERROR; + break; + } // forcefully restore tps6598x IRQs usb_hpm_restore_irqs(1); iodev_console_flush(); diff --git a/src/start.S b/src/start.S index e4019420e..e4dbec5e2 100644 --- a/src/start.S +++ b/src/start.S @@ -92,7 +92,7 @@ _start: .Lload_header: .ascii "AURALOAD" .long CHAINLOAD_HDR_VERSION - .long CHAINLOAD_HDR_F_RUN_BASE + .long CHAINLOAD_HDR_F_RUN_BASE | CHAINLOAD_HDR_F_LIVE_DCP .quad _image_footprint .Lrun_base: .quad 0 diff --git a/tests/python/test_chainload_run_base.py b/tests/python/test_chainload_run_base.py index dc864f2f6..ad98c8f8f 100644 --- a/tests/python/test_chainload_run_base.py +++ b/tests/python/test_chainload_run_base.py @@ -54,10 +54,10 @@ def test_chainload_header_rejects_footprint_outside_file(tmp_path): CHAINLOAD_HDR_MAGIC, CHAINLOAD_HDR_VERSION, CHAINLOAD_HDR_F_RUN_BASE, 0x4000, 0 }; memcpy(image + CHAINLOAD_HDR_OFFSET, &hdr, sizeof(hdr)); - assert(chainload_run_footprint(image, sizeof(image)) == sizeof(image)); + assert(chainload_run_footprint(image, sizeof(image), NULL) == sizeof(image)); /* Even a complete header cannot describe bytes beyond the supplied file. */ - assert(chainload_run_footprint(image, sizeof(image) - 1) == 0); - assert(chainload_run_footprint(image, CHAINLOAD_HDR_OFFSET + sizeof(hdr) - 1) == 0); + assert(chainload_run_footprint(image, sizeof(image) - 1, NULL) == 0); + assert(chainload_run_footprint(image, CHAINLOAD_HDR_OFFSET + sizeof(hdr) - 1, NULL) == 0); return 0; } ''' @@ -67,7 +67,7 @@ def test_chainload_header_rejects_footprint_outside_file(tmp_path): def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): source = (REPO / "src/chainload.c").read_text() bodies = function(source, "chainload_run_footprint") + "\n" + \ - function(source, "chainload_image") + function(source, "chainload_set_adt_range") + "\n" + function(source, "chainload_image") harness = r''' #define _GNU_SOURCE #include @@ -94,6 +94,8 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): #define T8112 0x8112 #define T8122 0x8122 #define T8140 0x8140 +#define DISPLAY_LIVE_ADT_PROP "aurora,dcp-retained-heap" +#define LIVE_DCP_ADT_ENTRY_SIZE (sizeof(struct adt_property) + sizeof(u64)) #define T6000 0x6000 #define T6020 0x6020 #define T6030 0x6030 @@ -103,6 +105,8 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): struct boot_args { u16 revision, version; u64 virt_base, phys_base, mem_size, top_of_kernel_data; + void *devtree; + u32 devtree_size; u8 rest[0x300]; }; _Static_assert(offsetof(struct boot_args, phys_base) == 16, "boot_args layout"); @@ -124,8 +128,13 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): static int chip_id, ro_status, adt_node = 1, setprop_ret; static u64 ram, home, ro_start, ro_end, live_heap, heap_next, sepfw[2]; static u64 force_alloc, alloc_fail, allocs, frees, last_alloc; -static char adt_dummy, message[256]; -static void *adt = &adt_dummy; +static u8 adt_buf[128]; +#define TEST_SEPFW_OFF 64 +#define TEST_DEVTREE_OFF 80 +static char message[256]; +static void *adt = adt_buf; +static bool handed_on, inherited_dcp; +static u64 retained_heap_base; static u8 stub_code[STUB_SIZE]; static struct { void *entry; @@ -136,6 +145,19 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): #define _chainload_stub_start stub_code #define _chainload_stub_end (stub_code + STUB_SIZE) +struct adt_property { char name[32]; u32 size; u8 value[]; }; +struct adt_node_hdr { u32 property_count, child_count; }; +static int display_prepare_live_handoff(void) { return 0; } +static bool display_handed_on_live(void) { return handed_on; } +static bool display_needs_live_receiver(void) { return handed_on || inherited_dcp; } +static bool display_inherited_live_heap(u64 *start) +{ + if (inherited_dcp) + *start = retained_heap_base; + return inherited_dcp; +} +static int adt_get_property_count(const void *dt, int node) { return 1; } +static int adt_first_property_offset(const void *dt, int node) { return node + 8; } static u64 heapblock_high_water(void) { return live_heap; } static int memory_fw_ro_range(u64 *start, u64 *end) { @@ -147,13 +169,21 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): } static int adt_path_offset(const void *dt, const char *path) { - assert(dt == adt && !strcmp(path, "/chosen/memory-map")); + assert(dt == adt); + if (!strcmp(path, "/chosen")) + return 0; + assert(!strcmp(path, "/chosen/memory-map")); return adt_node; } /* This ADT has no preoslog and no BootArgs entry: their handling must leave the layout alone. */ static const void *adt_getprop(const void *dt, int node, const char *name, u32 *len) { assert(dt == adt && node == adt_node); + if (handed_on && (!strcmp(name, "SEPFW") || !strcmp(name, "DeviceTree"))) { + if (len) + *len = 2 * sizeof(u64); + return adt_buf + (!strcmp(name, "SEPFW") ? TEST_SEPFW_OFF : TEST_DEVTREE_OFF); + } assert(!strcmp(name, "preoslog") || !strcmp(name, "BootArgs")); return NULL; } @@ -247,6 +277,8 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): heap_next = live_heap; force_alloc = alloc_fail = allocs = frees = last_alloc = 0; setprop_ret = 0; + handed_on = inherited_dcp = false; + memset(adt_buf, 0, sizeof(adt_buf)); message[0] = 0; sepfw_src = ram + SEPFW_OFF; sepfw[0] = sepfw_src; @@ -668,6 +700,52 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): free(image); } +static void carries_live_display_in_chosen(void) +{ + setup(T8112, HEAP_USED); + handed_on = true; + struct adt_node_hdr *node = (void *)adt_buf; + node->property_count = 1; + struct adt_property *name = (void *)(node + 1); + memcpy(name->name, "name", sizeof("name")); + name->size = sizeof("chosen"); + memcpy(name->value, "chosen", sizeof("chosen")); + cur_boot_args.devtree_size = sizeof(adt_buf); + memcpy(adt_buf + TEST_SEPFW_OFF, sepfw, sizeof(sepfw)); + u64 original_tree_range[2] = {ram + ADT_OFF, sizeof(adt_buf)}; + memcpy(adt_buf + TEST_DEVTREE_OFF, original_tree_range, sizeof(original_tree_range)); + + u8 image[SZ_16K] = {0}; + struct chainload_header hdr = { + CHAINLOAD_HDR_MAGIC, CHAINLOAD_HDR_VERSION, CHAINLOAD_HDR_F_RUN_BASE, SZ_16K, 0 + }; + memcpy(image + CHAINLOAD_HDR_OFFSET, &hdr, sizeof(hdr)); + assert(chainload_image(image, sizeof(image), NULL, 0) == -1); + assert(!next_stage.entry); + hdr.flags |= CHAINLOAD_HDR_F_LIVE_DCP; + memcpy(image + CHAINLOAD_HDR_OFFSET, &hdr, sizeof(hdr)); + assert(chainload_image(image, sizeof(image), NULL, 0) == 0); + struct boot_args *ba = (void *)staged_view(next_stage.args[0]); + u64 dst = (u64)ba->devtree - ba->virt_base + ba->phys_base; + assert(dst >= live_heap || dst + ba->devtree_size <= cur_boot_args.top_of_kernel_data); + assert(ba->top_of_kernel_data >= live_heap); + assert(ba->top_of_kernel_data >= dst + ba->devtree_size); + struct adt_node_hdr *next = (void *)staged_view(dst); + struct adt_property *prop = (void *)(next + 1); + u64 retained; + assert(next->property_count == 2 && node->property_count == 1); + assert(!strcmp(prop->name, DISPLAY_LIVE_ADT_PROP) && prop->size == sizeof(retained)); + memcpy(&retained, prop->value, sizeof(retained)); + assert(retained == cur_boot_args.top_of_kernel_data); + assert(!memcmp(prop->value + sizeof(retained), name, sizeof(*name) + 8)); + const u8 *copied = (const u8 *)next + LIVE_DCP_ADT_ENTRY_SIZE; + u64 tree_range[2]; + memcpy(tree_range, copied + TEST_DEVTREE_OFF, sizeof(tree_range)); + assert(tree_range[0] == dst && tree_range[1] == SZ_16K); + assert(!memcmp(adt_buf + TEST_DEVTREE_OFF, original_tree_range, sizeof(tree_range))); + assert(!memcmp(adt_buf + TEST_SEPFW_OFF, sepfw, sizeof(sepfw))); +} + int main(void) { /* The failing combined stage 2 (aura + 115 DTBs + U-Boot + variables) */ @@ -690,6 +768,7 @@ def test_chainload_places_images_around_t8140_firmware_ro(tmp_path): t6000_stage1_before_larger_stage2(0x100088, 0x808000, 0x430000, true); /* m1n1 1.5.2 size */ t6000_stage1_before_larger_stage2(0x3b0000, 0x808000, 0x430000, false); /* m1n1 v2.0.0 size */ t6000_stage1_before_larger_stage2(0x430000, 0x808000, 0x430000, false); /* aura 1.0.0b1 */ + carries_live_display_in_chosen(); composed_image_is_placed(); puts("chainload run base matrix PASS"); return 0; diff --git a/tests/python/test_ctxr_exec_window.py b/tests/python/test_ctxr_exec_window.py index b5cb9d67d..497f4007d 100644 --- a/tests/python/test_ctxr_exec_window.py +++ b/tests/python/test_ctxr_exec_window.py @@ -628,7 +628,12 @@ def test_nothing_reserves_the_window_from_the_os(): source = kboot_source() # Each range that can span the window goes around it: the heap, the image, CTXR_D # and the T8132 high-water interval. - assert source.count("dt_reserve_outside_exec_window(") == 5 + prepare = function(source, "kboot_prepare_dt") + retained = function(source, "dt_reserve_retained_span") + assert "dt_reserve_retained_span(heap_start, heap_end)" in prepare + assert "dt_reserve_retained_span(image_start, image_end)" in prepare + assert "dt_reserve_outside_exec_window(start, gap_end - start)" in retained + assert "dt_reserve_outside_exec_window(start, end - start)" in prepare assert "dt_reserve_outside_exec_window(start, end - start))\n bail(\"FDT: couldn't reserve the T8132" in source # The firmware RO range stays reserved in every mode, before the EL1 guest goes its own way. ro = function(source, "dt_reserve_secondary_ro") diff --git a/tests/python/test_display_dptx_hand_on.py b/tests/python/test_display_dptx_hand_on.py index b0d23c678..4cb81170d 100644 --- a/tests/python/test_display_dptx_hand_on.py +++ b/tests/python/test_display_dptx_hand_on.py @@ -60,6 +60,7 @@ def function(text, signature): } static bool dcp_handed_on_live(void) { return handed_on; } static bool dcp_left_running(void) { return running; } +int display_shutdown(dcp_shutdown_mode mode); """ MAIN = r""" @@ -94,6 +95,14 @@ def function(text, signature): reset(&internal, &ib, false, false); assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == 0 && !strcmp(calls, "ib quiesce ")); + /* A modeset after main's initial shutdown still hands off before kboot measures the heap. */ + reset(&d2, &ib, true, true); + assert(!handed_on); + assert(display_prepare_live_handoff() == 0); + assert(handed_on && !iboot && !strcmp(calls, "ib hand-on ")); + reset(&d1, &ib, true, true); + assert(display_prepare_live_handoff() == 0 && !handed_on && !calls[0]); + /* An endpoint that does not stop fails the hand-on, then and at the next boundary. */ reset(&stuck, &ib, true, true); assert(display_shutdown(DCP_SLEEP_IF_EXTERNAL) == -1 && !strcmp(calls, "ib hand-on ")); @@ -108,11 +117,12 @@ def test_afkv2_dptx_dcp_is_handed_on_running(tmp_path): disp = (SOURCE / "src/display.c").read_text() source = HARNESS + "".join(function(disp, signature) for signature in ( "static bool display_dptx_hands_on(", - "int display_shutdown(dcp_shutdown_mode mode)", + "int display_prepare_live_handoff(void)", + "int display_shutdown(dcp_shutdown_mode mode)\n", )) + MAIN exe = tmp_path / "hand-on" subprocess.run(["cc", "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-Wno-switch", "-x", "c", "-", "-o", str(exe)], input=source, text=True, check=True) out = subprocess.run([str(exe)], capture_output=True, text=True, check=True).stdout - assert out.count("handing the DCP on running") == 2 + assert out.count("handing the DCP on running") == 3 assert out.endswith("ok\n") diff --git a/tests/python/test_display_inherited_only.py b/tests/python/test_display_inherited_only.py index 0fc25f406..da5e79d29 100644 --- a/tests/python/test_display_inherited_only.py +++ b/tests/python/test_display_inherited_only.py @@ -69,6 +69,7 @@ def function(text, signature): return NULL; } static int display_configure(const char *config) { (void)config; configures++; return 0; } +static bool display_inherited_live_heap(u64 *start) { (void)start; return false; } static int display_init_t8152(void) { t8152_inits++; return 0; } static void fb_clear_direct(void) {} static bool firmware_sfw_in_range(enum fw_version lo, enum fw_version hi) { (void)lo; (void)hi; return false; } diff --git a/tests/python/test_display_inherited_reservation.py b/tests/python/test_display_inherited_reservation.py index 8f0534d2e..63cc91e38 100644 --- a/tests/python/test_display_inherited_reservation.py +++ b/tests/python/test_display_inherited_reservation.py @@ -20,15 +20,18 @@ def test_inherited_reservation(tmp_path): text = (SOURCE / "src/kboot.c").read_text() - start = text.index(" u64 image_end = (u64)_end;", text.index("int kboot_prepare_dt(")) + start = text.index(" u64 image_start = (u64)_base, image_end = (u64)_end;", text.index("int kboot_prepare_dt(")) # Followed by the post-M4 retained-heap block. end = text.index(" if (nvme_post_m4()", start) block = text[start:end] call = re.search( - r" if \(dt_reserve_outside_exec_window\(\(u64\)_base, image_end - \(u64\)_base\)\)\n" + r" if \(dt_reserve_retained_span\(image_start, image_end\)\)\n" r" bail\([^\n]+\);", text[end:] ) assert call, "missing real aura image reservation" + reserve_start = text.index("static int dt_reserve_retained_span(") + reserve_end = text.index("\n}", reserve_start) + 2 + reserve = text[reserve_start:reserve_end] code = tmp_path / "inherited.c" code.write_text(r''' #include @@ -40,11 +43,20 @@ def test_inherited_reservation(tmp_path): typedef uint64_t u64; #define T8103 0x8103 #define T8112 0x8112 +#define SZ_16K 0x4000UL +#define ALIGN_DOWN(n, a) ((n) & ~((a) - 1)) +#define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define bail(...) do { printf(__VA_ARGS__); return -1; } while (0) static struct { u64 phys_base, mem_size, top_of_kernel_data; } cur_boot_args; static unsigned chip_id; static bool tables_inherited; +static u64 dcp_heap_base; +static bool display_inherited_live_heap(u64 *start) +{ + *start = dcp_heap_base; + return dcp_heap_base != 0; +} static bool display_dart_tables_inherited(void) { (void)chip_id; return tables_inherited; } static u64 image_base, image_end_address; #define _base ((void *)(uintptr_t)image_base) @@ -53,6 +65,7 @@ def test_inherited_reservation(tmp_path): static void *dt = tree; /* Without a window this is the plain call (test_ctxr_exec_window.py). */ #define dt_reserve_outside_exec_window(start, size) fdt_add_mem_rsv(dt, start, size) +''' + reserve + r''' static int reserve(void) { ''' + block + call.group(0) + r''' @@ -65,6 +78,7 @@ def test_inherited_reservation(tmp_path): memcpy(before, tree, sizeof(tree)); chip_id = T8103; tables_inherited = true; + dcp_heap_base = 0; cur_boot_args.phys_base = 0x800000000UL; cur_boot_args.mem_size = 0x20000000; image_base = cur_boot_args.phys_base + 0x100000; @@ -126,7 +140,30 @@ def test_inherited_reservation(tmp_path): range(image_end_address); reset(); tables_inherited = false; cur_boot_args.mem_size = 0; range(image_end_address); - puts("PASS inherited reservation 14 cases"); + /* A moved stage still reserves the earlier live heap, even without a locked DART. */ + reset(); + tables_inherited = false; + dcp_heap_base = image_base - SZ_16K; + assert(!reserve() && fdt_num_mem_rsv(tree) == 1); + assert(!fdt_get_mem_rsv(tree, 0, &address, &size)); + assert(address == dcp_heap_base && address + size == cur_boot_args.top_of_kernel_data); + reset(); + dcp_heap_base = cur_boot_args.top_of_kernel_data; + refused(); + /* Earlier reservations such as the FDT buffer and a locked-DART L2 must not + * cause U-Boot to discard the rest of the live display heap. */ + reset(); + u64 split = image_base + 2 * SZ_16K; + assert(!fdt_add_mem_rsv(tree, split, SZ_16K)); + assert(!fdt_add_mem_rsv(tree, split + 3 * SZ_16K, SZ_16K)); + assert(!reserve() && fdt_num_mem_rsv(tree) == 5); + assert(!fdt_get_mem_rsv(tree, 2, &address, &size)); + assert(address == image_base && size == 2 * SZ_16K); + assert(!fdt_get_mem_rsv(tree, 3, &address, &size)); + assert(address == split + SZ_16K && size == 2 * SZ_16K); + assert(!fdt_get_mem_rsv(tree, 4, &address, &size)); + assert(address == split + 4 * SZ_16K && address + size == cur_boot_args.top_of_kernel_data); + puts("PASS inherited reservation 17 cases"); return 0; } ''') @@ -144,4 +181,4 @@ def test_inherited_reservation(tmp_path): assert compiled.returncode == 0, compiled.stdout + compiled.stderr result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=30) assert result.returncode == 0, result.stdout + result.stderr - assert "PASS inherited reservation 14 cases" in result.stdout + assert "PASS inherited reservation 17 cases" in result.stdout diff --git a/tests/python/test_display_panel_absent.py b/tests/python/test_display_panel_absent.py index 67e9006da..78d8561fd 100644 --- a/tests/python/test_display_panel_absent.py +++ b/tests/python/test_display_panel_absent.py @@ -91,6 +91,7 @@ def function(text, signature): return NULL; } static int display_configure(const char *config) { (void)config; configures++; return 0; } +static bool display_inherited_live_heap(u64 *start) { (void)start; return false; } static int display_init_t8152(void) { return 0; } static void fb_clear_direct(void) {} static bool firmware_sfw_in_range(enum fw_version lo, enum fw_version hi) { (void)lo; (void)hi; return false; } diff --git a/tests/python/test_smp_handoff_gate.py b/tests/python/test_smp_handoff_gate.py index 09c0b4054..8af429644 100644 --- a/tests/python/test_smp_handoff_gate.py +++ b/tests/python/test_smp_handoff_gate.py @@ -52,6 +52,9 @@ def test_pending_calls_block_dt_preparation_and_kernel_publication(tmp_path): static _Noreturn void panic(const char *format, ...) { longjmp(refused, 1); } /* The T8152 handoff has its own path and is not modelled here. */ static int kboot_boot_t8152(void *kernel) { return -1; } +static bool dt_retained_live_dcp; +static int display_prepare_live_handoff(void) { return 0; } +static bool display_needs_live_receiver(void) { return false; } static int mcc_enable_cache(void) { hardware_calls++; return 0; } static void tunables_apply_static(void) {} static void clk_init(void) { if (mode == 2) spin_table[1].target = 0x1234; } diff --git a/tests/python/test_t8152_display_fb.py b/tests/python/test_t8152_display_fb.py index 8892d7b8a..4d6efbbad 100644 --- a/tests/python/test_t8152_display_fb.py +++ b/tests/python/test_t8152_display_fb.py @@ -284,6 +284,7 @@ def unit(): static u64 get_ticks(void) { return 0; } static u64 ticks_to_msecs(u64 t) { return t; } static void mmu_map_framebuffer(u64 a, u64 s) { (void)a; (void)s; } +static bool display_inherited_live_heap(u64 *start) { (void)start; return false; } static int adt_path_offset(void *a, const char *p) { (void)a; (void)p; return -1; } static int adt_setprop(void *a, int n, const char *p, void *v, size_t s) { (void)a; (void)n; (void)p; (void)v; (void)s; assert(0); return -1; } static bool adt_is_compatible(void *a, int n, const char *c) { (void)a; assert(!n && !strcmp(c, "J873gAP")); return is_j873g; }