From a1f20632d2670076e35b64c311ef56588f03a9ea Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 17:52:45 -0500 Subject: [PATCH 1/4] kboot: gpu: publish the T8103 (G13G) firmware 27.2 handoff Recognize the headerless T8103 (G13G) firmware 27.2 (26B131) image in firmware.c and extend the G14G 27.2 handoff in kboot_gpu26.c to cover T8103: - Allow the 3-segment preloaded firmware layout on T8103 where the __DATA segment flags word is 0 (0x10 on G14/G15). - Make ADT perf-states-sram optional on T8103 (which has no perf-states-sram property and derives SRAM voltage from apple,min-sram-microvolt in the device tree). - Mark the GPU power domain both apple,no-auto-pm and apple,sleep-retained when publishing the 27.2 handoff so Linux PMGR does not auto power-gate or suspend-gate ps_gfx under the live ASC. Signed-off-by: Erik Bourget --- src/firmware.c | 5 +++ src/kboot_gpu.c | 10 ++++-- src/kboot_gpu26.c | 88 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 76 insertions(+), 27 deletions(-) diff --git a/src/firmware.c b/src/firmware.c index 41c9853aa..2998bd8a2 100644 --- a/src/firmware.c +++ b/src/firmware.c @@ -383,6 +383,11 @@ static const struct gfx_fw_image gfx_fw_images[] = { {T8122, V14_8_3, "g15g build c0", {0xdf, 0x69, 0x7f, 0x05, 0xf6, 0xb5, 0x33, 0xef, 0xa1, 0x37, 0x61, 0xc4, 0xa7, 0x3d, 0x66, 0x6a}, 0x5d54a, 0x1e8, 0x64000, true}, + /* + * T8103 (M1, G13G): the headerless image iBoot loads on macOS 27.2 (26B131), seen on the + * MacBook Air (J313). Linux's g14_v27_2 backend starts it. + */ + {T8103, V27_2, "g13g 27.2", {0}, 0, 0, 0x50000, false, "2FF15E57-6B75-3421-922F-EE7EE927AC22"}, /* * T8112 (M2, G14G): the headerless image iBoot loads on macOS 27.2 (26B123), seen on the * MacBook Air (J413). Linux's g14_v27_2 backend starts it. diff --git a/src/kboot_gpu.c b/src/kboot_gpu.c index 99a1f7a26..f9b6ce2f4 100644 --- a/src/kboot_gpu.c +++ b/src/kboot_gpu.c @@ -4542,6 +4542,7 @@ int dt_set_gpu(void *dt) return dt_set_gpu_t6041(dt); bool has_cs_afr = false; + bool has_sram_pstates = true; int (*calc_power)(u32 count, u32 table_count, const struct perf_state *core, const struct perf_state *sram, const struct aux_perf_states *cs, u32 *max_pwr, float *core_leak, float *sram_leak, float *cs_leak, float *afr_leak); @@ -4552,6 +4553,7 @@ int dt_set_gpu(void *dt) switch (chip_id) { case T8103: + has_sram_pstates = false; calc_power = calc_power_t8103; break; case T6022: @@ -4676,8 +4678,12 @@ int dt_set_gpu(void *dt) gpu_fail("ADT: GPU: invalid perf-states length\n"); perf_states_sram = adt_getprop(adt, sgx, "perf-states-sram", &perf_states_len); - /* The 27.2 backend takes each operating point's SRAM voltage from the handoff. */ - if (handoff && !perf_states_sram) + /* + * The 27.2 backend takes each operating point's SRAM voltage from the handoff on SoCs whose + * ADT publishes perf-states-sram (T8112); T8103 has no such table and derives SRAM voltage + * from apple,min-sram-microvolt in the device tree. + */ + if (handoff && has_sram_pstates && !perf_states_sram) gpu_fail("ADT: GPU: perf-states-sram missing\n"); if (perf_states_sram && perf_states_len != sizeof(*perf_states) * perf_state_count * perf_state_table_count) diff --git a/src/kboot_gpu26.c b/src/kboot_gpu26.c index 44c3436b2..16823b21b 100644 --- a/src/kboot_gpu26.c +++ b/src/kboot_gpu26.c @@ -97,6 +97,8 @@ struct gpu26_target { int (*calibrate)(void *dt); bool fast_die; bool exact_regions; + bool zero_data_flag_3seg; /* __DATA flags in the 3-segment layout (0 on G13, 0x10 on G14/G15) */ + bool sram_optional; }; static const struct gpu26_target t8122_target = { @@ -216,8 +218,11 @@ static int gpu26_read_adt(const struct gpu26_target *t, struct t8122_gpu_fw *fw) /* Without a fixed base, the segments only have to run on from the first one. */ if (!i && !next_va && s.iova && !(s.iova & (SZ_16K - 1))) next_va = s.iova; + u32 expected_flag = (segments == 3 && i == 1 && t->zero_data_flag_3seg) + ? 0 + : t8122_gpu_layouts[segments].flags[i]; if (s.phys != s.remap || !s.size || s.size & (SZ_16K - 1) || s.size > T8122_GPU_MAX_SEG || - s.unk != t8122_gpu_layouts[segments].flags[i]) + s.unk != expected_flag) bail("ADT: GPU: %s gfx-asc segment %d is not a plain preloaded segment\n", t->tag, i); if (s.iova != next_va) bail("ADT: GPU: %s firmware segment %d at unexpected VA 0x%lx (expected 0x%lx)\n", @@ -878,13 +883,23 @@ int dt_set_gpu_t6030_27(void *dt, const struct fw_version_info *image) } /* - * G14G (T8112) on firmware whose GPU interface the kernel starts itself (on 27.2, Linux's - * g14_v27_2 backend). The caller has identified the image iBoot loaded (firmware_gfx_version()) - * and has no initdata layout for it, and has already published the operating points. The ADT - * must describe the same handoff as on T8122: four UAT regions in /arm-io/sgx and the preloaded - * gfx-asc segments, the __DATA_SHARED_RO tail matching the sgx gfx-data-shared-ro pair. Their - * firmware VAs are not fixed here: they must follow on from the first segment's. + * G13G (T8103) and G14G (T8112) on firmware whose GPU interface the kernel starts itself (on 27.2, + * Linux's g14_v27_2 backend). The caller has identified the image iBoot loaded + * (firmware_gfx_version()) and has no initdata layout for it, and has already published the + * operating points. The ADT must describe the same handoff as on T8122: four UAT regions in + * /arm-io/sgx and the preloaded gfx-asc segments, the __DATA_SHARED_RO tail matching the sgx + * gfx-data-shared-ro pair. Their firmware VAs are not fixed here: they must follow on from the + * first segment's. */ +static const struct gpu26_target g13g_target = { + .tag = "G13G", + .sgx_compat = "gpu,t8103", + .gpu_compat = "apple,agx-g13g", + .mbox_compat = "apple,asc-mailbox-v4", + .zero_data_flag_3seg = true, + .sram_optional = true, +}; + static const struct gpu26_target g14g_target = { .tag = "G14G", .sgx_compat = "gpu,t8112", @@ -902,43 +917,66 @@ struct g14g_handoff { static int g14g_gpu_publish(void *dt, void *arg) { const struct g14g_handoff *h = arg; + const char *tag = h->fw.t->tag; if (gpu26_publish_regions(dt, &h->fw)) return -1; /* - * The kernel's 27.2 backend reads the SRAM voltage of each operating point from - * /chosen/aurora-gpu-resources/sgx adt-perf-states-sram, the ADT property as it is. + * On SoCs whose ADT carries perf-states-sram (T8112), the kernel's 27.2 backend reads each + * operating point's SRAM voltage from /chosen/aurora-gpu-resources/sgx adt-perf-states-sram. + * T8103 has no perf-states-sram in its ADT and uses apple,min-sram-microvolt instead. */ - int chosen = fdt_path_offset(dt, "/chosen"); - int res = chosen < 0 ? chosen : fdt_subnode_offset(dt, chosen, "aurora-gpu-resources"); - if (chosen >= 0 && res == -FDT_ERR_NOTFOUND) - res = fdt_add_subnode(dt, chosen, "aurora-gpu-resources"); - int sgx = res < 0 ? res : fdt_subnode_offset(dt, res, "sgx"); - if (res >= 0 && sgx == -FDT_ERR_NOTFOUND) - sgx = fdt_add_subnode(dt, res, "sgx"); - if (sgx < 0 || fdt_setprop(dt, sgx, "adt-perf-states-sram", h->sram, h->sram_len)) - bail("FDT: GPU: G14G cannot publish /chosen/aurora-gpu-resources/sgx\n"); + if (h->sram && h->sram_len) { + int chosen = fdt_path_offset(dt, "/chosen"); + int res = chosen < 0 ? chosen : fdt_subnode_offset(dt, chosen, "aurora-gpu-resources"); + if (chosen >= 0 && res == -FDT_ERR_NOTFOUND) + res = fdt_add_subnode(dt, chosen, "aurora-gpu-resources"); + int sgx = res < 0 ? res : fdt_subnode_offset(dt, res, "sgx"); + if (res >= 0 && sgx == -FDT_ERR_NOTFOUND) + sgx = fdt_add_subnode(dt, res, "sgx"); + if (sgx < 0 || fdt_setprop(dt, sgx, "adt-perf-states-sram", h->sram, h->sram_len)) + bail("FDT: GPU: %s cannot publish /chosen/aurora-gpu-resources/sgx\n", tag); + } if (gpu26_publish_version(dt, h->fw.t, h->image) || gpu26_enable(dt, (void *)h->fw.t)) return -1; + /* + * The firmware keeps running through suspend-to-idle, so pmgr must retain the GPU domain + * across system sleep instead of gating it under the live ASC. + */ + int domain = gpu26_phandle_node(dt, h->fw.t, "power-domains"); + if (domain < 0 || fdt_setprop_empty(dt, domain, "apple,sleep-retained")) + bail("FDT: GPU: %s cannot mark the GPU power domain apple,sleep-retained\n", tag); + for (int i = 0; i < T8122_GPU_REGIONS; i++) - printf("FDT: GPU: G14G %s: 0x%lx+0x%lx (no-map)\n", t8122_gpu_names[i], h->fw.base[i], + printf("FDT: GPU: %s %s: 0x%lx+0x%lx (no-map)\n", tag, t8122_gpu_names[i], h->fw.base[i], h->fw.size[i]); - printf("FDT: GPU: G14G firmware %s handoff published, GPU enabled\n", h->image->string); + printf("FDT: GPU: %s firmware %s handoff published, GPU enabled\n", tag, h->image->string); return 0; } int dt_set_gpu_g14g_handoff(void *dt, const struct fw_version_info *image) { + int sgx = adt_path_offset(adt, "/arm-io/sgx"); + if (sgx < 0) + return -1; + + const struct gpu26_target *target; + if (adt_is_compatible(adt, sgx, g13g_target.sgx_compat)) + target = &g13g_target; + else if (adt_is_compatible(adt, sgx, g14g_target.sgx_compat)) + target = &g14g_target; + else + return -1; + struct g14g_handoff h = {.image = image}; - if (gpu26_read_adt(&g14g_target, &h.fw)) + if (gpu26_read_adt(target, &h.fw)) return -1; - int sgx = adt_path_offset(adt, "/arm-io/sgx"); - h.sram = sgx < 0 ? NULL : adt_getprop(adt, sgx, "perf-states-sram", &h.sram_len); - if (!h.sram || !h.sram_len) - bail("ADT: GPU: G14G perf-states-sram missing; handoff refused\n"); + h.sram = adt_getprop(adt, sgx, "perf-states-sram", &h.sram_len); + if (!target->sram_optional && (!h.sram || !h.sram_len)) + bail("ADT: GPU: %s perf-states-sram missing; handoff refused\n", target->tag); return t8122_fdt_transaction(dt, g14g_gpu_publish, &h); } From 3526be041a5115bed6edebb241d6de042eeb5900 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 12:45:15 -0500 Subject: [PATCH 2/4] exception: keep DAIF masked and set VBAR_EL2 when returning from S2R Once the S2R mailbox is published, a core reset brings the cores back through this image on their way back to the OS that slept. That OS owns every interrupt source by then, so exception_initialize() keeps debug, SError, IRQ and FIQ masked instead of unmasking them on the boot core. At EL2 it now writes VBAR_EL2 by name, before any other register access, instead of VBAR_EL1. VBAR_EL1 reached EL2's vector base only through the E2H alias, which a core reset need not have set up yet; VBAR_EL2 is the same register wherever that alias was in place, so nothing changes for a normal boot. smp_s2r_return_to_os() keeps its explicit mask; its comment now says why. Signed-off-by: Erik Bourget --- src/exception.c | 16 ++++++++++++++-- src/smp.c | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/exception.c b/src/exception.c index e973ac76b..fcadf385d 100644 --- a/src/exception.c +++ b/src/exception.c @@ -9,6 +9,7 @@ #include "gxf.h" #include "iodev.h" #include "memory.h" +#include "smp.h" #include "sptm_virt.h" #include "sptm_virt_priv.h" #include "uart.h" @@ -138,8 +139,14 @@ static const char *get_exception_level(void) void exception_initialize(void) { + /* + * At EL2, name VBAR_EL2 itself: VBAR_EL1 reaches it only through the E2H alias, which a core + * reset (a wake from a system sleep) need not have set up by this point. + */ if (in_el3()) msr(VBAR_EL3, _vectors_start); + else if (in_el2()) + msr(VBAR_EL2, _vectors_start); else msr(VBAR_EL1, _vectors_start); @@ -168,9 +175,14 @@ void exception_initialize(void) } /* Initial T8152 bring-up polls KIS without configuring the AIC. A core reset while the - * virtualized SPTM's guest is live must not take the guest's interrupts at EL2. */ - if (is_boot_cpu() && chip_id != T8152 && !sptm_virt_guest_live) + * virtualized SPTM's guest is live must not take the guest's interrupts at EL2. A core back + * from a system sleep returns to the OS that slept, which owns every interrupt source by + * then: nothing may be taken here on the way back. */ + bool s2r = smp_s2r_published(); + if (is_boot_cpu() && chip_id != T8152 && !sptm_virt_guest_live && !s2r) msr(DAIF, 0 << 6); // Enable SError, IRQ and FIQ + else if (s2r) + msr(DAIF, 0xf << 6); // Mask debug, SError, IRQ and FIQ else msr(DAIF, 3 << 6); // Disable IRQ and FIQ diff --git a/src/smp.c b/src/smp.c index 0ca0c5316..ba8efeee1 100644 --- a/src/smp.c +++ b/src/smp.c @@ -552,7 +552,7 @@ void smp_s2r_return_to_os(void) { u64 entry = s2r_mailbox.resume_pa; - /* exception_initialize() unmasked everything. */ + /* exception_initialize() left everything masked; the OS expects it so. */ msr(DAIF, 0xf << 6); /* A reset the OS didn't arm: reboot rather than guess what state it's in. */ From 8893cb5800067e7ea253e51b3050d49329b4f1f4 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Tue, 24 Nov 2026 12:45:24 -0500 Subject: [PATCH 3/4] pcie, smp: re-initialize PCIe complex on S2R wake before returning to the OS A system sleep takes the PCIe complex's power with the rest of the SoC. Where kboot hands the OS the ADT's APCIe tables (pcie_apcie_tables_fdt(), an ANS with nvme-secure-bar: M4 on), the OS brings the block back itself. Elsewhere, T8103 among them, only aura's boot-time setup describes the complex, and after an S2R wake the M1 MacBook Air (J313) resumed into a complex that was no longer set up. The gate is what the ADT says and what aura actually did, not a chip. The boot core, back from the sleep and on its way to the OS, sets the complex up again only if all of these hold (pcie_needs_sleep_reinit()): - a controller came up at boot through pcie_init_controller(), so a SoC whose PCIe comes up another way (T6050) is never replayed; - kboot did not hand the OS the APCIe tables (it records whether it did, pcie_set_os_restores_apcie()); - the ADT has an ANS without nvme-secure-bar (pcie_sleep_reinit_possible()), so an M4 or later Mac is never replayed, even if its table export failed. The saved J313 ADT has no nvme-secure-bar; the J713, J714s and J716c ADTs have it. pcie_reinit_after_sleep() replays only the controllers that came up at boot, each from the ADT node it came up from (recorded in its slot), and fails if any one of them fails or none came up. pcie_init() tries /arm-io/apcie and /arm-io/apcie0 against the same slot, and the replay does not repeat that. On the replay the APCIe controller's RC word at +4, which boot clears, is read first and written only if the sleep left it set; boot is unchanged. smp_s2r_pcie_reinit(): - turns the console off first: nobody drains it now, and a full FIFO holds the core; - runs under a watchdog, wdt_s2r_guard_enter()/exit(). It reads the watchdogs before any write. If the OS left one of the two it uses armed (the primary with its reset enable, as Linux's T8103 sleep does, or the third, at +0x2c of the primary block, with its enable), that covers the step and nothing is written. Otherwise, on the ADT's version 2 layout with the secondary watchdog off, it borrows the primary for 30 s (Linux's default period), reads it back, and afterwards restores the control, count and alarm it read. The secondary is never written. Without a watchdog either way, the step doesn't run and the Mac reboots; - powers ANS's domains before the PHY comes up, as they already are at boot; - reboots if the complex doesn't come back, rather than resuming the OS into a state it doesn't know. So that a reboot from that step always works, the S2R mailbox is published only where the step is ruled out by the ADT or the watchdog layout it needs is there (wdt_s2r_guard_supported(), which also keeps the primary's base in memory for wdt_reboot()). Otherwise aurora,s2r-unavailable says why. kboot publishes the mailbox before it sets PCIe up, so that check uses the ADT part of the gate. The PCIe sequence host test covers the replay: the same MMIO accesses and polls as boot for the one controller that came up, the RC word read first and cleared only when set, failure propagation, nothing to replay when nothing came up, and the gate's cases (tables handed over, an ANS with nvme-secure-bar, no ANS, PCIe up without a replayable controller). The SMP handoff gate host test stubs the new kboot call. Signed-off-by: Erik Bourget --- src/kboot.c | 17 +++++- src/pcie.c | 87 ++++++++++++++++++++++++--- src/pcie.h | 7 +++ src/smp.c | 36 +++++++++++ src/wdt.c | 87 +++++++++++++++++++++++++++ src/wdt.h | 10 +++ tests/python/test_pcie_sequence.py | 83 +++++++++++++++++++++++++ tests/python/test_smp_handoff_gate.py | 1 + 8 files changed, 318 insertions(+), 10 deletions(-) diff --git a/src/kboot.c b/src/kboot.c index 164fe76be..6628f0fd1 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -48,6 +48,7 @@ #include "usb_dwc3.h" #include "utils.h" #include "vsprintf.h" +#include "wdt.h" #include "xnuboot.h" #include "libfdt/libfdt.h" @@ -742,6 +743,18 @@ static int dt_set_s2r_mailbox(int node) return 0; } + /* + * A wake may have to set PCIe up again before the OS runs (smp_s2r_pcie_reinit()), which it + * does only under a watchdog that can still reset the SoC. kboot sets PCIe up after this, so + * decide from the ADT alone whether that step can come up at all. + */ + if (pcie_sleep_reinit_possible() && !wdt_s2r_guard_supported()) { + printf("FDT: no S2R mailbox: no watchdog for the wake's PCIe bring-up\n"); + fdt_setprop_string(dt, node, "aurora,s2r-unavailable", + "no watchdog for the wake's PCIe bring-up"); + return 0; + } + u64 mailbox = smp_s2r_publish(); if (fdt_setprop_u64(dt, node, "aurora,s2r-mailbox", mailbox)) bail("FDT: couldn't set aurora,s2r-mailbox\n"); @@ -14030,8 +14043,10 @@ int kboot_boot(void *kernel) return -1; } /* Linux's deep sleep needs them to bring ANS's APCIe block back. */ - if (!ret && pcie_apcie_tables_fdt(dt, dt_bufsize) < 0) + int apcie_tables = ret ? 0 : pcie_apcie_tables_fdt(dt, dt_bufsize); + if (apcie_tables < 0) printf("kboot: APCIe tables not published; deep sleep stays refused\n"); + pcie_set_os_restores_apcie(apcie_tables > 0); if (chip_id == T8140) { if (ret < 0) { printf("kboot: refusing T8140 handoff after partial PCIe setup\n"); diff --git a/src/pcie.c b/src/pcie.c index 12dc289fd..32d798a3d 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -270,6 +270,8 @@ struct state { bool initialized; bool dirty; u32 active_ports; + /* The ADT node this controller last came up from, for pcie_reinit_after_sleep() */ + const char *up_path; }; static struct state controllers[NUM_CONTROLLERS]; @@ -371,8 +373,11 @@ static u32 pcie_phy_up(const struct state *state) return BIT(27); } -/* With ans_off, only read the windows, stop the shared PHY and power ANS's gates off. */ -static int pcie_init_controller(int controller, const char *path, bool ans_off) +/* + * With ans_off, only read the windows, stop the shared PHY and power ANS's gates off. With wake, + * the controller comes up again after a system sleep (pcie_reinit_after_sleep()). + */ +static int pcie_init_controller(int controller, const char *path, bool ans_off, bool wake) { struct state *state = &controllers[controller]; int adt_path[8]; @@ -627,7 +632,8 @@ static int pcie_init_controller(int controller, const char *path, bool ans_off) /* ??? */ if (state->pcie_regs->type == APCIE_T8140) printf("pcie: T8140 RC reg[1] setup\n"); - if (controller == APCIE) + /* On the way back from a sleep, read first: written only if the sleep left it set. */ + if (controller == APCIE && (!wake || read32(state->rc_base + 0x4))) write32(state->rc_base + 0x4, 0); if (!adt_getprop(adt, adt_offset, "apcie-common-tunables", NULL)) { @@ -1110,6 +1116,7 @@ static int pcie_init_controller(int controller, const char *path, bool ans_off) printf("pcie: Initialized controller %d\n", controller); state->initialized = true; state->dirty = false; + state->up_path = path; return 0; } @@ -1134,7 +1141,7 @@ int pcie_init(void) printf("pcie: T8140 controller absent\n"); return 1; } - int ret = pcie_init_controller(APCIE, path, false); + int ret = pcie_init_controller(APCIE, path, false, false); if (!ret) { pcie_initialized = true; return 0; @@ -1153,10 +1160,10 @@ int pcie_init(void) return ret; } - success |= pcie_init_controller(APCIE, "/arm-io/apcie", false) == 0; - success |= pcie_init_controller(APCIE, "/arm-io/apcie0", false) == 0; - success |= pcie_init_controller(APCIE_GE0, "/arm-io/apcie-ge0", false) == 0; - success |= pcie_init_controller(APCIE_GE1, "/arm-io/apcie-ge1", false) == 0; + success |= pcie_init_controller(APCIE, "/arm-io/apcie", false, false) == 0; + success |= pcie_init_controller(APCIE, "/arm-io/apcie0", false, false) == 0; + success |= pcie_init_controller(APCIE_GE0, "/arm-io/apcie-ge0", false, false) == 0; + success |= pcie_init_controller(APCIE_GE1, "/arm-io/apcie-ge1", false, false) == 0; if (success) pcie_initialized = true; @@ -1164,6 +1171,68 @@ int pcie_init(void) return success ? 0 : -1; } +/* Set by kboot once it has handed the OS the APCIe tables (pcie_apcie_tables_fdt()). */ +static bool pcie_os_restores_apcie; + +void pcie_set_os_restores_apcie(bool restores) +{ + pcie_os_restores_apcie = restores; +} + +/* + * Whether the ADT leaves a system sleep's PCIe bring-up to aura at all: an ANS without + * nvme-secure-bar does not run on the APCIe block (before M4), so the OS is never handed the + * APCIe tables for it. Known before kboot sets PCIe up, for the S2R mailbox's own check. + */ +bool pcie_sleep_reinit_possible(void) +{ + int ans = adt_path_offset(adt, "/arm-io/ans"); + + return ans >= 0 && !adt_get_property(adt, ans, "nvme-secure-bar"); +} + +/* + * A system sleep takes the PCIe complex's power with the rest of the SoC. An OS that was handed + * the APCIe tables brings the block back itself. Without them, only this image's boot setup + * describes the complex, so if that setup brought a controller up, it has to run again before + * the OS does: ANS's power domains first (pcie_init() runs at boot with them already on), then + * the complex. Only controllers set up by pcie_init_controller() can be replayed; a SoC whose + * PCIe came up another way (T6050) or whose ANS runs on the APCIe block never is. + */ +bool pcie_needs_sleep_reinit(void) +{ + bool up = false; + + for (int i = 0; i < NUM_CONTROLLERS; i++) + up |= controllers[i].up_path != NULL; + return up && pcie_initialized && !pcie_os_restores_apcie && pcie_sleep_reinit_possible(); +} + +/* + * pcie_needs_sleep_reinit()'s step: each controller that came up at boot, from the ADT node it + * came up from, and nothing else. Every one of them must come up again. + */ +int pcie_reinit_after_sleep(void) +{ + int count = 0; + + for (int i = 0; i < NUM_CONTROLLERS; i++) { + struct state *state = &controllers[i]; + + if (!state->up_path) + continue; + count++; + if (pcie_init_controller(i, state->up_path, false, true)) { + pcie_initialized = false; + return -1; + } + } + if (!count) + return -1; + pcie_initialized = true; + return 0; +} + /* For a stage done with a post-M4 ANS, which runs on the APCIe PHY: the PHY and ANS powered off. */ static const char *pcie_ans_path(void) { @@ -1246,7 +1315,7 @@ int pcie_ans_power_off(void) return 0; } pcie_initialized = false; - return pcie_init_controller(APCIE, pcie_ans_path(), true); + return pcie_init_controller(APCIE, pcie_ans_path(), true, false); } int pcie_ans_power_on(void) diff --git a/src/pcie.h b/src/pcie.h index 172e1f568..d5a53a17b 100644 --- a/src/pcie.h +++ b/src/pcie.h @@ -23,5 +23,12 @@ int pcie_ans_power_on(void); int pcie_disable_j713_fdt(void *dt); /* Post-M4: copy the APCIe tables from the ADT to the DT's PCIe host and its port. */ int pcie_apcie_tables_fdt(void *dt, int capacity); +/* Whether the OS was handed those tables, so that it brings the APCIe block back itself */ +void pcie_set_os_restores_apcie(bool restores); +/* Whether the ADT can leave a system sleep's PCIe bring-up to aura (before M4) */ +bool pcie_sleep_reinit_possible(void); +/* After a system sleep, before the OS runs again: whether the complex is aura's to set up */ +bool pcie_needs_sleep_reinit(void); +int pcie_reinit_after_sleep(void); #endif diff --git a/src/smp.c b/src/smp.c index ba8efeee1..d5f40e6dd 100644 --- a/src/smp.c +++ b/src/smp.c @@ -6,13 +6,16 @@ #include "aic_regs.h" #include "cpu_regs.h" #include "dockchannel_uart.h" +#include "iodev.h" #include "malloc.h" #include "memory.h" +#include "pcie.h" #include "pmgr.h" #include "soc.h" #include "string.h" #include "types.h" #include "utils.h" +#include "wdt.h" #include "xnuboot.h" #define CPU_START_OFF_S5L8960X 0x30000 @@ -542,6 +545,37 @@ bool smp_s2r_published(void) return s2r_mailbox.magic == AURA_S2R_MAGIC; } +/* The bring-up below takes well under a second at boot; Linux's default watchdog period. */ +#define S2R_PCIE_WDT_SECONDS 30 + +/* + * Where the PCIe complex's setup is this image's alone (pcie_needs_sleep_reinit()), the sleep + * took it: set it up again before the OS runs. Nobody drains the console now and a full FIFO + * holds the core, so the console goes first. The step runs only under a watchdog: the OS's, if + * it left one armed (Linux's T8103 sleep does, from its commit to the wake), or one borrowed for + * the step and put back after. Without either, or if the complex doesn't come back, the SoC is + * in no state the OS knows: reboot rather than resume into it. + */ +static void smp_s2r_pcie_reinit(void) +{ + struct wdt_guard guard; + + if (!pcie_needs_sleep_reinit()) + return; + + for (iodev_id_t id = 0; id < IODEV_NUM; id++) + iodev_set_usage(id, iodev_get_usage(id) & ~USAGE_CONSOLE); + + if (wdt_s2r_guard_enter(&guard, S2R_PCIE_WDT_SECONDS)) + reboot(); + int ret = pmgr_adt_power_enable("/arm-io/ans"); + if (!ret) + ret = pcie_reinit_after_sleep(); + wdt_s2r_guard_exit(&guard); + if (ret) + reboot(); +} + /* * The boot core back from a sleep, with the OS live: nothing here may rerun the boot * flow. The OS gets EL2h with the MMU and caches off and interrupts masked, as at @@ -567,6 +601,8 @@ void smp_s2r_return_to_os(void) if (!armed) reboot(); + smp_s2r_pcie_reinit(); + s2r_mailbox.resume_pa = 0; s2r_mailbox.resumes++; sysop("dsb sy"); diff --git a/src/wdt.c b/src/wdt.c index d508acd2b..d478bfee4 100644 --- a/src/wdt.c +++ b/src/wdt.c @@ -2,6 +2,7 @@ #include "wdt.h" #include "adt.h" +#include "memory.h" #include "types.h" #include "utils.h" @@ -9,6 +10,12 @@ #define WDT_ALARM 0x14 #define WDT_CTL 0x1c +#define WDT_CTL_RESET_EN BIT(2) + +/* The block's third watchdog, which an OS arms where it doesn't use the primary */ +#define WDT2_CTL 0x2c +#define WDT2_CTL_ENABLE BIT(0) + static u64 wdt_base = 0; void wdt_disable(void) @@ -66,6 +73,86 @@ void wdt_reboot(void) write32(wdt_base + WDT_CTL, 4); } +/* + * The ADT's version 2 layout that wdt_s2r_guard_enter() needs: the primary block (both watchdogs + * an OS may arm) and the secondary. The primary's base is kept for wdt_reboot() and cleaned to + * memory, so that a wake, with the caches off, can still reset the SoC through it. + */ +bool wdt_s2r_guard_supported(void) +{ + int path[8]; + int node = adt_path_offset_trace(adt, "/arm-io/wdt", path); + u64 primary, primary_size, secondary, secondary_size; + u32 version; + + if (node < 0 || ADT_GETPROP(adt, node, "wdt-version", &version) < 0 || version != 2 || + adt_get_reg(adt, path, "reg", 0, &primary, &primary_size) || (primary & 3) || + primary_size < WDT2_CTL + 4 || + adt_get_reg(adt, path, "reg", 2, &secondary, &secondary_size) || secondary_size != 4) + return false; + wdt_base = primary; + dc_cvac_range(&wdt_base, sizeof(wdt_base)); + return true; +} + +/* + * A dead-man for a step on the way back from a system sleep, with the OS live. The watchdogs + * are read before anything is written. If the OS left one of the two it uses armed (the + * primary, as Linux's T8103 sleep does, or the third), that covers the step and nothing is + * written. + * Otherwise, on the ADT's version 2 layout with the secondary watchdog off (wdt_arm()'s + * conditions), the primary is borrowed: armed for the given time and read back. -1: the step + * has no watchdog and must not run. + */ +int wdt_s2r_guard_enter(struct wdt_guard *guard, unsigned seconds) +{ + int path[8]; + int node = adt_path_offset_trace(adt, "/arm-io/wdt", path); + u64 primary, primary_size, secondary, secondary_size; + u32 version; + + guard->borrowed = false; + if (seconds < 1 || seconds > 178 || node < 0 || + adt_get_reg(adt, path, "reg", 0, &primary, &primary_size) || (primary & 3) || + primary_size < WDT2_CTL + 4) + return -1; + + guard->base = primary; + guard->ctl = read32(primary + WDT_CTL); + guard->count = read32(primary + WDT_COUNT); + guard->alarm = read32(primary + WDT_ALARM); + if ((guard->ctl & WDT_CTL_RESET_EN) || (read32(primary + WDT2_CTL) & WDT2_CTL_ENABLE)) + return 0; + + if (ADT_GETPROP(adt, node, "wdt-version", &version) < 0 || version != 2 || + adt_get_reg(adt, path, "reg", 2, &secondary, &secondary_size) || secondary_size != 4 || + read32(secondary)) + return -1; + + u32 alarm = seconds * 24000000u; + guard->borrowed = true; + write32(primary + WDT_CTL, 0); + write32(primary + WDT_COUNT, 0); + write32(primary + WDT_ALARM, alarm); + write32(primary + WDT_CTL, WDT_CTL_RESET_EN); + sysop("dsb sy"); + if (read32(primary + WDT_ALARM) != alarm || !(read32(primary + WDT_CTL) & WDT_CTL_RESET_EN)) + return -1; + return 0; +} + +/* A borrowed primary watchdog back as it was read; the secondary is never touched. */ +void wdt_s2r_guard_exit(const struct wdt_guard *guard) +{ + if (!guard->borrowed) + return; + write32(guard->base + WDT_CTL, 0); + write32(guard->base + WDT_COUNT, guard->count); + write32(guard->base + WDT_ALARM, guard->alarm); + write32(guard->base + WDT_CTL, guard->ctl); + sysop("dsb sy"); +} + static int wdt_arm(unsigned seconds) { int path[8]; diff --git a/src/wdt.h b/src/wdt.h index 018635178..0ce5f498d 100644 --- a/src/wdt.h +++ b/src/wdt.h @@ -8,6 +8,16 @@ void wdt_disable(void); void wdt_reboot(void); int wdt_arm_seconds(unsigned seconds); + +/* wdt_s2r_guard_enter(): the primary watchdog as read, and whether it was borrowed */ +struct wdt_guard { + bool borrowed; + u64 base; + u32 ctl, count, alarm; +}; +bool wdt_s2r_guard_supported(void); +int wdt_s2r_guard_enter(struct wdt_guard *guard, unsigned seconds); +void wdt_s2r_guard_exit(const struct wdt_guard *guard); /* The longest the 24 MHz counter allows, for each stage of a trial boot */ #define WDT_TRIAL_SECONDS 178 int wdt_arm_trial(void); diff --git a/tests/python/test_pcie_sequence.py b/tests/python/test_pcie_sequence.py index 5749468e5..af96ae08b 100644 --- a/tests/python/test_pcie_sequence.py +++ b/tests/python/test_pcie_sequence.py @@ -64,12 +64,24 @@ def test_t8140_sequence(tmp_path): /* pci-bridge0's apcie-port and reg (phys.hi first), each with its length; 0 is absent. */ static u32 bridge_port, bridge_port_len, bridge_reg[5], bridge_reg_len; static u32 phy_ctrl, port_ctrl, appclk; +/* /arm-io/ans, and whether it has nvme-secure-bar (an ANS on the APCIe block) */ +static int ans_present, ans_secure_bar; +/* The RC window's word at +4, and how often it was read and written */ +#define RC_MISC 0x394000004ULL +static u32 rc_misc; +static int rc_misc_reads, rc_misc_writes; static int adt_path_offset(const void *a, const char *path) { if (!strcmp(path, "/arm-io/apcie")) return 1; if (!strcmp(path, "/arm-io/apcie/pci-bridge0")) return 2; + if (!strcmp(path, "/arm-io/ans") && ans_present) return 3; return -1; } +static const void *adt_get_property(const void *a, int node, const char *prop) +{ + return node == 3 && !strcmp(prop, "nvme-secure-bar") && ans_secure_bar ? &ans_secure_bar + : NULL; +} static int adt_path_offset_trace(const void *a, const char *path, int *trace) { trace[0] = adt_path_offset(a, path); @@ -146,11 +158,19 @@ def test_t8140_sequence(tmp_path): if (addr == PHY + 4) return 0x11; if (addr == PORT_PHY) return port_ctrl; if (addr == PORT + 0x800) return appclk; + if (addr == RC_MISC) { + rc_misc_reads++; + return rc_misc; + } return 0; } static void write32(u64 addr, u32 val) { access_mmio(addr); + if (addr == RC_MISC) { + rc_misc_writes++; + rc_misc = val; + } if (addr == PHY || addr == PORT_PHY) { u32 *ctrl = addr == PHY ? &phy_ctrl : &port_ctrl; if ((*ctrl & 0x10) && !(val & 0x10)) assert((*ctrl & 0xf) == 0xf); @@ -229,6 +249,19 @@ def test_t8140_sequence(tmp_path): regs[7][1] = 0x8000; phy_ctrl = port_ctrl = 0x10; appclk = 0; + ans_present = 1; + ans_secure_bar = 0; + rc_misc = 0; + rc_misc_reads = rc_misc_writes = 0; + pcie_set_os_restores_apcie(false); +} +/* A system sleep: the hardware loses its state, aura's memory keeps controllers[]. */ +static void power_cycle(void) +{ + powered = common_mode = rc_ready = port_tunables = 0; + reset_delay = pll = auspma = port_run = 0; + phy_ctrl = port_ctrl = 0x10; + appclk = 0; } static void expect_failure(void) { @@ -283,6 +316,56 @@ def test_t8140_sequence(tmp_path): fail_tunable = 1; assert(pcie_init() == -1 && pll == 1 && auspma == 0 && !port_run); + /* + * After a system sleep: the controller that came up at boot comes up again from the node it + * came up from, with the same MMIO accesses and polls, and nothing else is tried. A step that + * fails fails the whole bring-up; with nothing up at boot there is nothing to bring back. + */ + reset_model(); + chip_id = 0x8132; + controller_compat = "apcie,t8132"; + rc_misc = 0x5; + assert(pcie_init() == 0 && !strcmp(controllers[APCIE].up_path, "/arm-io/apcie")); + assert(!controllers[APCIE_GE0].up_path && !controllers[APCIE_GE1].up_path); + /* Boot writes the RC word without reading it, as it always has. */ + assert(rc_misc == 0 && rc_misc_reads == 0 && rc_misc_writes == 1); + /* Replayed: no tables for the OS and an ANS off the APCIe block (T8103). */ + assert(pcie_needs_sleep_reinit()); + int boot_accesses = accesses, boot_polls = polls; + power_cycle(); + /* Left clear by the sleep: read, not written. */ + assert(pcie_reinit_after_sleep() == 0 && pcie_initialized && port_run); + assert(controllers[APCIE].initialized && controllers[APCIE].active_ports == 1); + assert(rc_misc_reads == 1 && rc_misc_writes == 1); + assert(accesses == 2 * boot_accesses && polls == 2 * boot_polls); + /* Left set: read, then cleared. */ + power_cycle(); + rc_misc = 0x5; + assert(pcie_reinit_after_sleep() == 0 && rc_misc == 0); + assert(rc_misc_reads == 2 && rc_misc_writes == 2); + /* Not replayed: an OS that got the tables, an ANS on the APCIe block, or no ANS. */ + pcie_set_os_restores_apcie(true); + assert(!pcie_needs_sleep_reinit()); + pcie_set_os_restores_apcie(false); + ans_secure_bar = 1; + assert(!pcie_needs_sleep_reinit() && !pcie_sleep_reinit_possible()); + ans_secure_bar = 0; + ans_present = 0; + assert(!pcie_needs_sleep_reinit() && !pcie_sleep_reinit_possible()); + ans_present = 1; + assert(pcie_needs_sleep_reinit()); + power_cycle(); + fail_poll = polls + 1; + assert(pcie_reinit_after_sleep() == -1 && !pcie_initialized); + reset_model(); + assert(pcie_reinit_after_sleep() == -1 && accesses == 0); + /* + * PCIe up without a controller set up here (T6050's own bring-up): nothing to replay, even + * with no tables handed over and an ANS off the APCIe block. + */ + pcie_initialized = true; + assert(pcie_sleep_reinit_possible() && !pcie_needs_sleep_reinit()); + /* * ANS through its storage gates alone: no APCIe block power or MMIO, so the GP ports (N1 on * T8142) stay as they are. T8142: AppleT8142PCIe's storage half, on in ADT order. diff --git a/tests/python/test_smp_handoff_gate.py b/tests/python/test_smp_handoff_gate.py index 8af429644..594e26bb6 100644 --- a/tests/python/test_smp_handoff_gate.py +++ b/tests/python/test_smp_handoff_gate.py @@ -66,6 +66,7 @@ def test_pending_calls_block_dt_preparation_and_kernel_publication(tmp_path): /* No modelled chip takes the T6050 PCIe handoff. */ static int pcie_t6050_handoff_fdt(void *p) { abort(); } static int pcie_apcie_tables_fdt(void *p, int capacity) { return 0; } +static void pcie_set_os_restores_apcie(bool restores) {} static int fdt_node_check_compatible(void *p, int node, const char *compatible) { return 1; } static int dt_disable_j713_pcie(void) { return 0; } static int dt_disable_t8140_pcie(void) { return 0; } From f5ddfff29a5ef2a07fe959eacce41c946002effc Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Wed, 25 Nov 2026 03:31:00 -0500 Subject: [PATCH 4/4] smp: keep S2R PCIe watchdog armed until failure reboot In smp_s2r_pcie_reinit(), only exit the borrowed S2R watchdog guard on the success path before returning to the OS, keeping the watchdog armed across reboot() if PCIe re-initialization fails. Signed-off-by: Erik Bourget --- src/smp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smp.c b/src/smp.c index d5f40e6dd..4fc10a502 100644 --- a/src/smp.c +++ b/src/smp.c @@ -571,9 +571,9 @@ static void smp_s2r_pcie_reinit(void) int ret = pmgr_adt_power_enable("/arm-io/ans"); if (!ret) ret = pcie_reinit_after_sleep(); - wdt_s2r_guard_exit(&guard); if (ret) reboot(); + wdt_s2r_guard_exit(&guard); } /*