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/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.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/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); } 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 0ca0c5316..4fc10a502 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(); + if (ret) + reboot(); + wdt_s2r_guard_exit(&guard); +} + /* * 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 @@ -552,7 +586,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. */ @@ -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; }