diff --git a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml index 25228d7c80251..bb1c214d5e072 100644 --- a/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml +++ b/Documentation/devicetree/bindings/soc/apple/apple,t8140-sleep.yaml @@ -19,16 +19,17 @@ description: | properties: compatible: enum: + - apple,t8122-sleep - apple,t8132-sleep - apple,t8140-sleep - apple,t8142-sleep reg: - minItems: 25 + minItems: 21 maxItems: 41 reg-names: - minItems: 25 + minItems: 21 maxItems: 41 apple,wake-enable: @@ -105,6 +106,41 @@ required: - apple,display-coprocessors allOf: + - if: + properties: + compatible: + contains: + const: apple,t8122-sleep + then: + required: + - apple,wake-enable + properties: + reg: + minItems: 21 + maxItems: 21 + reg-names: + items: + - const: ps + - const: pwrgate + - const: cpu-start + - const: cllt + - const: cluster + - const: commit + - const: wdt + - const: spmi + - const: spmi-hpm + - const: wake + - const: ps-aon + - const: mcc-a + - const: mcc-b + - const: mcc-c + - const: mcc-d + - const: apcie-axi + - const: apcie-rc + - const: apcie-clkgen + - const: apcie-phy + - const: apcie-phy-ip + - const: apcie-port0 - if: properties: compatible: diff --git a/arch/arm64/boot/dts/apple/t8122-j613-j615.dtsi b/arch/arm64/boot/dts/apple/t8122-j613-j615.dtsi index f891967d74d04..98df2180607c0 100644 --- a/arch/arm64/boot/dts/apple/t8122-j613-j615.dtsi +++ b/arch/arm64/boot/dts/apple/t8122-j613-j615.dtsi @@ -503,3 +503,13 @@ }; }; }; + +&mcc { + apple,boot-dma-devices = <&pmp>; + status = "okay"; +}; + +&sleep { + apple,debug-port = <&hpm0>; + status = "okay"; +}; diff --git a/arch/arm64/boot/dts/apple/t8122-peripherals.dtsi b/arch/arm64/boot/dts/apple/t8122-peripherals.dtsi index cfeb98ce06c76..b740469f7578a 100644 --- a/arch/arm64/boot/dts/apple/t8122-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t8122-peripherals.dtsi @@ -282,12 +282,9 @@ status = "disabled"; }; - mcc: memory-controller@2d03c0000 { + mcc: memory-controller@220000000 { compatible = "apple,t8122-mcc", "apple,mcc"; - reg = <0x2 0xd03c0000 0x0 0x20000>, - <0x2 0xd0478000 0x0 0x149c>, - <0x2 0xd0780000 0x0 0x4000>, - <0x2 0x20000000 0x0 0x2000000>, + reg = <0x2 0x20000000 0x0 0x2000000>, <0x2 0x22000000 0x0 0x2000000>; status = "disabled"; }; diff --git a/arch/arm64/boot/dts/apple/t8122.dtsi b/arch/arm64/boot/dts/apple/t8122.dtsi index ad7382ac89ea4..c3e777c9ff688 100644 --- a/arch/arm64/boot/dts/apple/t8122.dtsi +++ b/arch/arm64/boot/dts/apple/t8122.dtsi @@ -558,6 +558,7 @@ mboxes = <&sep_mbox>; mbox-names = "mbox"; iommus = <&sep_dart 0>; + power-domains = <&ps_sep>, <&ps_gpio>; status = "disabled"; }; @@ -893,6 +894,41 @@ /* child nodes are added in t8122-pmgr.dtsi */ }; + sleep: sleep-controller@2d0700000 { + compatible = "apple,t8122-sleep"; + reg = <0x2 0xd0700000 0x0 0x14000>, + <0x2 0xd0724000 0x0 0x4000>, + <0x2 0xd0734000 0x0 0x10>, + <0x2 0xd0768000 0x0 0x40>, + <0x2 0xd0780000 0x0 0x8>, + <0x2 0xe42d4000 0x0 0x4>, + <0x2 0xe42b0000 0x0 0x30>, + <0x2 0xe4714000 0x0 0x100>, + <0x2 0xe4908000 0x0 0x100>, + <0x2 0xe4004000 0x0 0x48>, + <0x2 0xe4280000 0x0 0x4000>, + <0x2 0x20024000 0x0 0x1000>, + <0x2 0x20064000 0x0 0x1000>, + <0x2 0x20158000 0x0 0x1000>, + <0x2 0x20400000 0x0 0x1000>, + <0x5 0x9c000000 0x0 0x2000>, + <0x5 0x91000000 0x0 0x1000>, + <0x5 0x9e088000 0x0 0x3000>, + <0x5 0x9e000000 0x0 0x10000>, + <0x5 0x9e040000 0x0 0x10000>, + <0x5 0x94008000 0x0 0x5000>; + reg-names = "ps", "pwrgate", "cpu-start", "cllt", + "cluster", "commit", "wdt", "spmi", + "spmi-hpm", "wake", "ps-aon", + "mcc-a", "mcc-b", "mcc-c", "mcc-d", + "apcie-axi", "apcie-rc", "apcie-clkgen", + "apcie-phy", "apcie-phy-ip", "apcie-port0"; + apple,wake-enable = <155>; + apple,display-coprocessors = <&dcp>; + apple,pinctrl = <&pinctrl_ap>; + status = "disabled"; + }; + pmp_mbox: mbox@2d0c08000 { compatible = "apple,t8122-asc-mailbox", "apple,asc-mailbox-v4"; reg = <0x2 0xd0c08000 0x0 0x4000>; diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index cb77aa8eabd5a..5c7d6a7229aaf 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -941,6 +941,8 @@ struct apple_dcp { struct delayed_work secure_poll_wq; /* Stopped and not restarted, which outlives a suspend that gets refused. */ bool s2r_fw_down; + /* Stopped cold for firmware-driven sleep, with no restart until reboot. */ + bool sleep_cold_stopped; /* * Its last system suspend left the firmware running and its domains on * (wakeup path), see dcp_sleep_keeps_firmware(). Cleared by the next diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 4e48c4574624f..f6b8edddc0f38 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -10428,11 +10428,25 @@ static void dcp_dpms_power_off(struct apple_dcp *dcp) __dcp_dpms_power_off(dcp); } +/* + * Internal 26.6 DCP firmware stops cold when firmware-driven deep sleep will + * cut display power, because there is no cold-restart path for it yet. In a + * sleep dry run the coprocessor is left running and takes the ordinary suspend + * path instead. + */ +static bool dcp_sleep_owned(struct apple_dcp *dcp) +{ + return IS_ENABLED(CONFIG_APPLE_SOC_SLEEP) && dcp->fw_compat == DCP_FIRMWARE_V_26_6 && + !dcp->external && apple_sleep_commit_stops_coprocs() && !apple_sleep_dry_run(); +} + bool dcp_s2r_firmware_stopped(struct platform_device *pdev) { struct apple_dcp *dcp = platform_get_drvdata(pdev); - return READ_ONCE(dcp->s2r_fw_down); + /* Whichever of this and the display subsystem suspends first: nothing commits to it. */ + return READ_ONCE(dcp->s2r_fw_down) || READ_ONCE(dcp->sleep_cold_stopped) || + (pm_suspend_via_firmware() && dcp_sleep_owned(dcp)); } #include "dcp-v27-sleep.inc" @@ -10551,6 +10565,31 @@ static int dcp_platform_suspend(struct device *dev) if (pm_suspend_via_firmware() && dcp_v27_sleep_selected(dcp)) return dcp_v27_sleep_suspend(dcp); + if (READ_ONCE(dcp->sleep_cold_stopped)) + return 0; + if (pm_suspend_via_firmware() && dcp_sleep_owned(dcp)) { + u32 control = readl(dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL); + struct apple_rtkit *rtk = dcp->rtk ?: dcp_v26_rtkit(dcp); + + dev_warn(dev, "display coprocessor stopped cold for sleep (control %#x); panel stays off until reboot\n", + control); + WRITE_ONCE(dcp->quiescing, true); + /* + * Run the 26.6 firmware's panel-off and sleep-entry calls, then + * request RTKit AP and IOP idle before halting the coprocessor. + */ + dcp_v26_sleep_calls(dcp, 2); + if (!IS_ERR_OR_NULL(rtk)) { + int ret = apple_rtkit_idle_with(rtk, dcp_secure_port_idle, dcp); + + if (ret) + dev_warn(dev, "RTKit idle before cold stop failed: %d\n", ret); + } + WRITE_ONCE(dcp->sleep_cold_stopped, true); + dcp_s2r_cpu_stop(dcp); + return 0; + } + /* * J813 uses the same qualified stop as DPMS off. Probe already gates * its derived power layout on appledrm.s2r_untested; without this @@ -10722,6 +10761,10 @@ static int dcp_platform_resume(struct device *dev) if (dcp_v27_sleep_selected(dcp) && READ_ONCE(dcp->sleep_v27_closed)) return dcp_v27_sleep_resume(dcp); + /* Stopped cold, with no restart path until reboot. */ + if (READ_ONCE(dcp->sleep_cold_stopped)) + return 0; + if (dcp->s2r_stopped) return dcp_s2r_resume(dcp); diff --git a/drivers/gpu/drm/apple/iomfb_v26_6.c b/drivers/gpu/drm/apple/iomfb_v26_6.c index 95128654acf30..2f0b1ffa1ae08 100644 --- a/drivers/gpu/drm/apple/iomfb_v26_6.c +++ b/drivers/gpu/drm/apple/iomfb_v26_6.c @@ -554,6 +554,24 @@ static int dcp_v26_callback(void *cookie, u32 tag, } if (tag == D(576) && SHAPE(4, 0)) return 0; + /* + * Callbacks the firmware sends when the display is powered down (A473, observed in + * macOS's panel-off and in Linux's sleep calls), answered as the H17P and M4 drivers + * answer them: a no-op (D2), swap_complete_intent_gated (D592, a no-op), + * powerUpDART (D572, success) and get_calendar_time_ms (D209). + */ + if ((tag == D(2) && SHAPE(0, 0)) || (tag == D(592) && SHAPE(20, 0))) + return 0; + if (tag == D(572) && SHAPE(4, 4)) { + if (v26->dcp) + dcp_s2r_dart_power(v26->dcp, get_unaligned_le32(in)); + memset(out, 0, 4); + return 0; + } + if (tag == D(209) && SHAPE(0, 8)) { + put_unaligned_le64(ktime_get_real_ns() / NSEC_PER_MSEC, out); + return 0; + } if (tag == D(300) && SHAPE(16, 0)) { dcp_v26_publish(v26, in); return 0; @@ -1774,7 +1792,8 @@ void iomfb_v26_6_flush(struct apple_dcp *dcp, struct drm_crtc *crtc, p = crtc->primary->state; fb = p && p->visible ? p->fb : NULL; dcp->swap_start = ktime_get(); - if (!dcp->v26 || !dcp_v26_present(dcp->v26, fb, dcp->notch_height, &presented, cs)) { + if (READ_ONCE(dcp->sleep_cold_stopped) || !dcp->v26 || + !dcp_v26_present(dcp->v26, fb, dcp->notch_height, &presented, cs)) { dcp_v26_cancel_event(dcp); return; } @@ -1928,3 +1947,81 @@ void iomfb_v26_6_poweroff(struct apple_dcp *dcp) dcp_v26_present(dcp->v26, NULL, 0, NULL, NULL); } } + +/* + * Calls the 26.6 firmware receives from macOS before a system sleep, in the order and + * with the inputs observed on its mailbox and RPC memory. + * The panel-off group: A414 (8 zero bytes; reply 1), A478 (reply 1) twice, A476(1), + * A478, A412 (reply 1), A473 (16 zero bytes; reply 8 zero bytes), A500 (0, 0). + * The sleep-entry group: A478, A476(1), A473 (bytes 8, 11 and 12 set to 1), A115 + * (reply 0), A500 (0, 1). @groups: 1 sends the sleep-entry group only, 2 sends the + * panel-off group first, then the sleep-entry group. Every reply is compared with + * the expected firmware reply; a mismatch returns -EPROTO. + */ +static int dcp_v26_sleep_call(struct apple_dcp_v26 *v26, u32 tag, const void *in, u32 in_size, + const void *want, u32 want_size, bool *same) +{ + u8 out[8] = {}; + int ret; + + ret = dcp_v26_call(v26, tag, in, in_size, want_size ? out : NULL, want_size, 0); + if (!ret && want_size && memcmp(out, want, want_size)) { + dev_warn(v26->dev, "sleep call %#x reply %*ph, expected %*ph\n", tag, want_size, + out, want_size, want); + *same = false; + } + return ret; +} + +int dcp_v26_sleep_calls(struct apple_dcp *dcp, int groups) +{ + static const u8 zero16[16], one[4] = { 1 }, one8[8] = { 1 }; + static const u8 a473_entry[16] = { [8] = 1, [11] = 1, [12] = 1 }; + static const u8 a500_off[4] = { 0, 0 }, a500_entry[4] = { 0, 1 }; + struct apple_dcp_v26 *v26 = dcp->v26; + bool same = true; + int ret = 0; + + if (!v26 || !v26->started) + return -ENODEV; + mutex_lock(&v26->lock); + if (groups >= 2) { + ret = dcp_v26_sleep_call(v26, A(414), zero16, 8, one8, 8, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(478), NULL, 0, one, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(478), NULL, 0, one, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(476), one, 4, NULL, 0, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(478), NULL, 0, one, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(412), NULL, 0, one, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(473), zero16, 16, zero16, 8, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(500), a500_off, 4, NULL, 0, &same); + dev_dbg(v26->dev, "panel-off calls before sleep: %d\n", ret); + } + if (!ret) + ret = dcp_v26_sleep_call(v26, A(478), NULL, 0, one, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(476), one, 4, NULL, 0, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(473), a473_entry, 16, zero16, 8, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(115), NULL, 0, zero16, 4, &same); + if (!ret) + ret = dcp_v26_sleep_call(v26, A(500), a500_entry, 4, NULL, 0, &same); + mutex_unlock(&v26->lock); + if (!ret && !same) + ret = -EPROTO; + dev_dbg(v26->dev, "sleep calls (groups %d): %d, replies %s expected\n", groups, ret, + same ? "matched" : "differed from"); + return ret; +} + +struct apple_rtkit *dcp_v26_rtkit(struct apple_dcp *dcp) +{ + return dcp->v26 ? dcp->v26->rtk : NULL; +} diff --git a/drivers/gpu/drm/apple/iomfb_v26_6.h b/drivers/gpu/drm/apple/iomfb_v26_6.h index 2831ee2f79345..1b856a09eb21f 100644 --- a/drivers/gpu/drm/apple/iomfb_v26_6.h +++ b/drivers/gpu/drm/apple/iomfb_v26_6.h @@ -11,6 +11,7 @@ #include struct apple_dcp; +struct apple_rtkit; struct apple_dcp_v26; struct dcp_v26_board; struct device; @@ -51,5 +52,8 @@ void iomfb_v26_6_flush(struct apple_dcp *dcp, struct drm_crtc *crtc, struct drm_atomic_commit *state); void iomfb_v26_6_poweron(struct apple_dcp *dcp); void iomfb_v26_6_poweroff(struct apple_dcp *dcp); +/* Pre-sleep shutdown calls and the session's RTKit for firmware-driven sleep. */ +int dcp_v26_sleep_calls(struct apple_dcp *dcp, int groups); +struct apple_rtkit *dcp_v26_rtkit(struct apple_dcp *dcp); #endif /* __APPLE_IOMFB_V26_6_H__ */ diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index b33e64ae38bd8..15c8eeba74147 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -236,6 +236,8 @@ struct apple_nvme_hw { u32 sleep_shn; /* The ANS is never reset through PMGR here, so no reset is named. */ bool reset_optional; + /* Pre-M4 controller that warm-idles and warm-restarts across deep sleep. */ + bool warm_sleep; bool secure_reg_layout; /* Also send the NVMe shutdown notification (CC.SHN) on removal. */ bool shutdown_notify; @@ -275,6 +277,7 @@ struct apple_nvme { bool sleep_restarting; ktime_t sleep_run_time; bool sleep_registered; + bool t8122_warm_sleep; unsigned long recovery_flags; void __iomem *mmio_coproc; @@ -1897,6 +1900,7 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) container_of(work, struct apple_nvme, ctrl.reset_work); enum nvme_ctrl_state state = nvme_ctrl_state(&anv->ctrl); bool inherited = !anv->owns_rtkit; + bool warm_restarted = false; if (anv->hw->needs_ioq_registers) { mutex_lock(&anv->disable_lock); @@ -2003,6 +2007,11 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) goto out; goto rtkit_ready; } + if (READ_ONCE(anv->sleep_restarted)) { + WRITE_ONCE(anv->sleep_restarted, false); + warm_restarted = true; + goto rtkit_ready; + } /* reset the controller if it is enabled */ if (anv->ctrl.ctrl_config & NVME_CC_ENABLE) apple_nvme_disable_legacy(anv, false); @@ -2049,6 +2058,14 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) if (ret) goto out; + /* + * T8122 shares the ANS reset with SART: replay saved SART + * entries after deasserting reset before starting ANS (no-op + * on cold boot when replay_valid is false). + */ + if (anv->sart) + apple_sart_replay(anv->sart); + writel(APPLE_ANS_COPROC_CPU_CONTROL_RUN, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); @@ -2088,12 +2105,14 @@ static void apple_nvme_reset_work_impl(struct work_struct *work) * queues. Woken this way, it would ignore the queue registers below, * which may only change while CC.EN is clear. Disable it first. */ - if (inherited && !anv->hw->needs_ioq_registers) { + if ((inherited || warm_restarted) && !anv->hw->needs_ioq_registers) { anv->ctrl.cap = readq(anv->mmio_nvme + NVME_REG_CAP); anv->ctrl.ctrl_config = readl(anv->mmio_nvme + NVME_REG_CC); if (anv->ctrl.ctrl_config & NVME_CC_ENABLE) { dev_info(anv->dev, - "controller handed over enabled, disabling it\n"); + "controller %s enabled, disabling it\n", + warm_restarted ? "retained after warm restart" : + "handed over"); ret = nvme_disable_ctrl(&anv->ctrl, false); if (ret) goto out; @@ -3137,6 +3156,36 @@ static void apple_nvme_shutdown(struct platform_device *pdev) * macOS. The wait for on doesn't: macOS rings the SEP a few ms after ANS's HELLO, before ANS * reports on, so the reset work waits, after the SEP's own resume has had its turn. */ +static void apple_nvme_t8122_warm_resume(struct apple_nvme *anv) +{ + void __iomem *control = anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL; + int ret; + + if (!xchg(&anv->t8122_warm_sleep, false)) + return; + + /* + * A sleep refused before the commit left ANS powered, so a warm + * restart without a power cycle can hang; fall back to the cold PMGR + * reset path in apple_nvme_reset_work_impl(). + */ + if (!pm_resume_via_firmware()) + return; + + apple_sleep_restore_pins(); + if (anv->sart) + apple_sart_replay(anv->sart); + apple_rtkit_set_warm_epmap_mask(anv->rtk, BIT(0)); + anv->sleep_run_time = ktime_get(); + ret = apple_rtkit_sleep_return_start(anv->rtk, control); + if (ret) { + dev_err(anv->dev, "ANS warm restart failed: %d\n", ret); + anv->sleep_failed = true; + } else { + WRITE_ONCE(anv->sleep_restarting, true); + } +} + static int apple_nvme_resume_early(struct device *dev) { struct apple_nvme *anv = dev_get_drvdata(dev); @@ -3147,7 +3196,11 @@ static int apple_nvme_resume_early(struct device *dev) READ_ONCE(anv->m5.phase) != APPLE_M5_ANS_LIVE) return apple_nvme_m5_resume_early(anv); - if (!anv->hw->needs_ioq_registers || !anv->suspended || !anv->sleep_idled) + if (!anv->hw->needs_ioq_registers) { + apple_nvme_t8122_warm_resume(anv); + return 0; + } + if (!anv->suspended || !anv->sleep_idled) return 0; anv->sleep_idled = false; if (anv->sleep_stop) @@ -3232,6 +3285,11 @@ static int apple_nvme_resume(struct device *dev) WRITE_ONCE(anv->suspended, false); return nvme_try_sched_reset(&anv->ctrl); } + apple_nvme_t8122_warm_resume(anv); + if (READ_ONCE(anv->sleep_failed)) { + dev_crit(dev, "holding all I/O: ANS is down after the sleep, reboot to recover\n"); + return 0; + } return nvme_reset_ctrl(&anv->ctrl); } @@ -3392,6 +3450,21 @@ static int apple_nvme_suspend(struct device *dev) } if (apple_rtkit_is_running(anv->rtk)) { + if (anv->hw->warm_sleep && + pm_suspend_target_state == PM_SUSPEND_MEM && + pm_suspend_via_firmware() && + !apple_sleep_dry_run()) { + anv->sleep_failed = false; + anv->sleep_restarted = false; + ret = apple_rtkit_idle(anv->rtk); + if (ret) { + dev_err(dev, "ANS warm idle failed: %d\n", ret); + return ret; + } + writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); + WRITE_ONCE(anv->t8122_warm_sleep, true); + return 0; + } ret = apple_rtkit_shutdown(anv->rtk); writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL); @@ -3417,6 +3490,13 @@ static const struct apple_nvme_hw apple_nvme_t8103_hw = { .max_queue_depth = 64, }; +static const struct apple_nvme_hw apple_nvme_t8122_hw = { + .has_lsq_nvmmu = true, + .has_queue_count = true, + .warm_sleep = true, + .max_queue_depth = 64, +}; + /* * The post-M4 (T8132 and later) I/O queue setup follows Yureka Lilian's T8132 * support in m1n1 (commit 53f8ee9b54ba, "nvme: support T8132"). @@ -3528,6 +3608,7 @@ static const struct of_device_id apple_nvme_of_match[] = { { .compatible = "apple,t6050-nvme-ans2", .data = &apple_nvme_t6050_hw }, { .compatible = "apple,t8015-nvme-ans2", .data = &apple_nvme_t8015_hw }, { .compatible = "apple,t8103-nvme-ans2", .data = &apple_nvme_t8103_hw }, + { .compatible = "apple,t8122-nvme-ans2", .data = &apple_nvme_t8122_hw }, { .compatible = "apple,t8132-nvme-ans2", .data = &apple_nvme_t8132_hw }, { .compatible = "apple,t8140-nvme-ans2", .data = &apple_nvme_t8140_hw }, { .compatible = "apple,t8142-nvme-ans2", .data = &apple_nvme_t8142_hw }, diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 824b94d04caa7..1d556e26828c8 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -7140,9 +7140,18 @@ static int apple_pcie_suspend_noirq(struct device *dev) return ret; } if (!pcie->hw->tunneled) { - if (apple_pcie_s2r_reinit(pcie) && pm_suspend_via_firmware()) + bool s2r_reinit = apple_pcie_s2r_reinit(pcie) && pm_suspend_via_firmware(); + + if (s2r_reinit) apple_pcie_hib_save(pcie); - if (pm_suspend_no_platform() && pcie->num_pds) + /* + * Keep the host's power domains on across genpd noirq suspend + * for s2idle and firmware sleeps: on an abandoned or dry-run + * firmware sleep the commit never runs, so gating the domain + * here would leave the controller unprogrammed with no wake + * sequence to reinitialize it. A real commit powers it down. + */ + if ((pm_suspend_no_platform() || s2r_reinit) && pcie->num_pds) return apple_pcie_hold_domains(pcie); return 0; } diff --git a/drivers/soc/apple/aop.rs b/drivers/soc/apple/aop.rs index 4d0f1de643013..c851602cc228b 100644 --- a/drivers/soc/apple/aop.rs +++ b/drivers/soc/apple/aop.rs @@ -3196,15 +3196,27 @@ const HW_CFG_T6041: AopHwConfig = AopHwConfig { /// the DART's DAPF programmed and its windows reserved alongside the firmware /// segments. const HW_CFG_T8132: AopHwConfig = HW_CFG_T6040; -/// M3-family AOP (T8122, T6030 and T6031): as T6040. iBoot leaves the AOP -/// halted with its external code, data and log in DRAM, which the bootloader -/// reserves in `memory-region`; the 27.2 firmware reads its `__EDATA` base -/// from `ED0p`. A bootloader that reserves none leaves it to start as -/// `apple,t6020-aop`, as before. -const HW_CFG_T8122: AopHwConfig = AopHwConfig { +/// M3-family AOP without SoC-specific sleep endpoints (T6030 and T6031): as +/// T6040. iBoot leaves the AOP halted with its external code, data and log in +/// DRAM, which the bootloader reserves in `memory-region`; the 27.2 firmware +/// reads its `__EDATA` base from `ED0p`. A bootloader that reserves none leaves +/// it to start as `apple,t6020-aop`, as before. +const HW_CFG_T6030: AopHwConfig = AopHwConfig { without_fw_regions: Some(&HW_CFG_T6020), ..HW_CFG_T6040 }; +/// T8122 fallback when the bootloader supplies no firmware `memory-region`: +/// keeps the T6020 boot path while still stopping the T8122 sleep endpoints. +const HW_CFG_T8122_NO_FW_REGIONS: AopHwConfig = AopHwConfig { + sleep_endpoints: &[0x2b, 0x2a, 0x25, 0x27, 0x20, 0x23, 0x29, 0x24, 0x21, 0x28, 0x26, 0x22], + ..HW_CFG_T6020 +}; +/// T8122 (M3): as `HW_CFG_T6030`, plus T8122's twelve deep-sleep AFK endpoints. +const HW_CFG_T8122: AopHwConfig = AopHwConfig { + sleep_endpoints: &[0x2b, 0x2a, 0x25, 0x27, 0x20, 0x23, 0x29, 0x24, 0x21, 0x28, 0x26, 0x22], + without_fw_regions: Some(&HW_CFG_T8122_NO_FW_REGIONS), + ..HW_CFG_T6040 +}; /// T8140: the firmware is started with the boot arguments the bootloader /// left, boots through the setup port and speaks EPIC version 4. Of the /// advertised AFK endpoints, the application map is 0x20 misc, 0x21 @@ -3335,8 +3347,8 @@ kernel::of_device_table!( (of::DeviceId::new(c_str!("apple,t8112-aop")), &HW_CFG_T8112), (of::DeviceId::new(c_str!("apple,t6000-aop")), &HW_CFG_T6000), (of::DeviceId::new(c_str!("apple,t8122-aop")), &HW_CFG_T8122), - (of::DeviceId::new(c_str!("apple,t6030-aop")), &HW_CFG_T8122), - (of::DeviceId::new(c_str!("apple,t6031-aop")), &HW_CFG_T8122), + (of::DeviceId::new(c_str!("apple,t6030-aop")), &HW_CFG_T6030), + (of::DeviceId::new(c_str!("apple,t6031-aop")), &HW_CFG_T6030), (of::DeviceId::new(c_str!("apple,t6020-aop")), &HW_CFG_T6020), (of::DeviceId::new(c_str!("apple,t8140-aop")), &HW_CFG_T8140), (of::DeviceId::new(c_str!("apple,t6041-aop")), &HW_CFG_T6041), diff --git a/drivers/soc/apple/mcc.c b/drivers/soc/apple/mcc.c index 8bcdfc740eeaa..d962b9f0de328 100644 --- a/drivers/soc/apple/mcc.c +++ b/drivers/soc/apple/mcc.c @@ -377,7 +377,8 @@ static int apple_mcc_probe(struct platform_device *pdev) INIT_LIST_HEAD(&mcc->boot_dma.link); mcc->boot_dma.dev = dev; mcc->boot_dma.lost = apple_mcc_boot_dma_lost; - mcc->retained = of_device_is_compatible(dev->of_node, "apple,t8132-mcc"); + mcc->retained = of_device_is_compatible(dev->of_node, "apple,t8122-mcc") || + of_device_is_compatible(dev->of_node, "apple,t8132-mcc"); mcc->ways_off = of_device_is_compatible(dev->of_node, "apple,t8142-mcc"); while (platform_get_resource(pdev, IORESOURCE_MEM, mcc->nr_planes)) { struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, mcc->nr_planes); @@ -538,6 +539,7 @@ static const struct dev_pm_ops apple_mcc_pm_ops = { }; static const struct of_device_id apple_mcc_of_match[] = { + { .compatible = "apple,t8122-mcc" }, { .compatible = "apple,t8132-mcc" }, { .compatible = "apple,t8140-mcc" }, { .compatible = "apple,t8142-mcc" }, diff --git a/drivers/soc/apple/pmp.rs b/drivers/soc/apple/pmp.rs index c8b9219e51c01..9bff46da17cd2 100644 --- a/drivers/soc/apple/pmp.rs +++ b/drivers/soc/apple/pmp.rs @@ -1020,6 +1020,14 @@ const HW_CFG_T6050: PmpHwConfig = PmpHwConfig { confirm_ioreg_len: false, }; +/// T8122: the sleep engine reloads `__TEXT` from DRAM at the wake, and only `__DATA` +/// (the ADT's second segment, offset 0x36000, size 0x47000) has to be kept. When the +/// boot loader supplies `apple,sram-keep`, both offset and size come from the DT instead. +const HW_CFG_T8122: PmpHwConfig = PmpHwConfig { + sram_backup: Some((0x36000, 0x47000)), + ..HW_CFG_DEFAULT +}; + /// T8132: over a sleep only the ADT's second segment (0x68000 bytes, starting at 0x3c000 on /// 26.6.2/25G83 and 0x3d000 on 27.2/26B123 per ADT `segment-ranges` and SRAM header word 0x234) /// needs to be kept. When the boot loader supplies `apple,sram-keep`, both offset and size come @@ -1045,6 +1053,7 @@ kernel::of_device_table!( OF_TABLE, ::IdInfo, [ + (of::DeviceId::new(c"apple,t8122-pmp-v2"), &HW_CFG_T8122), (of::DeviceId::new(c"apple,t8132-pmp-v2"), &HW_CFG_T8132), (of::DeviceId::new(c"apple,t8140-pmp-v2"), &HW_CFG_T8140), (of::DeviceId::new(c"apple,t8142-pmp-v2"), &HW_CFG_T8142), diff --git a/drivers/soc/apple/sleep-t8122.h b/drivers/soc/apple/sleep-t8122.h new file mode 100644 index 0000000000000..957b4fc4b7744 --- /dev/null +++ b/drivers/soc/apple/sleep-t8122.h @@ -0,0 +1,1256 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * T8122 (Apple M3) system suspend-to-RAM support. + * + * Implements the T8122 sleep register map, display/APCIE/ATC/MCC shutdown and + * wake sequences, HID4/EHID4-unblocked PoP/PoC cache clean before deep sleep, + * and CYC_OVRD mode-3 EL1 deep WFI arming. + */ + +#define T8122_PS_GPIO 0x120 +#define T8122_PS_DISP_SYS 0x1d8 +#define T8122_PS_GFX 0x1e0 +#define T8122_PS_DISP_FE 0x208 +#define T8122_PS_SIO_CPU 0x210 +#define T8122_PS_APCIE 0x3f0 +#define T8122_PS_ANS 0x3f8 +#define T8122_PS_ATC0_COMMON 0x400 +#define T8122_PS_ATC1_COMMON 0x408 +#define T8122_PS_APCIE_GP 0x440 +#define T8122_PS_APCIE_ST 0x448 +#define T8122_PS_APCIE_PHY_SW 0x4a0 +#define T8122_PS_SEP 0xc00 +#define T8122_PS_DISP_CPU 0x10000 + +#define T8122_PS_AUTO_IDLE 0x1400000f + +#define T8122_AON_ATC0_USB_AON 0xb0 +#define T8122_AON_ATC1_USB_AON 0xb8 +#define T8122_AON_ATC0_USB 0xc0 +#define T8122_AON_ATC1_USB 0xc8 + +#define T8122_APCIE_PHY_CTRL_RESET BIT(7) +#define T8122_APCIE_PHY_CTRL_UP BIT(9) + +#define T8122_PWRGATE_E 0x80 +#define T8122_PWRGATE_P 0x88 +#define T8122_PWRGATE_DISP_A 0xcac +#define T8122_PWRGATE_DISP_B 0xcd0 +#define T8122_PWRGATE_DISP_C 0xcf0 + +#define T8122_CLUSTER_E 0x0 +#define T8122_CLUSTER_P 0x4 +#define T8122_CLLT_CTL 0x0 +#define T8122_CLLT_LAST 0x24 + +#define T8122_SYS_HID4 sys_reg(3, 0, 15, 4, 0) +#define T8122_SYS_EHID4 sys_reg(3, 0, 15, 4, 1) +#define T8122_HID4_DISABLE_DC_MVA BIT_ULL(11) +#define T8122_HID4_DISABLE_DC_SW_L2 BIT_ULL(44) +#define T8122_HID4_DC_MASK (T8122_HID4_DISABLE_DC_MVA | T8122_HID4_DISABLE_DC_SW_L2) +#define T8122_MPIDR_P_CORE BIT_ULL(16) + +#define T8122_SYS_CYC_OVRD sys_reg(3, 5, 15, 5, 0) +#define T8122_CYC_WFI_MODE GENMASK_ULL(25, 24) + +/* Every PMGR power-state word in the T8122 device tree. */ +static const u32 t8122_ps_all[] = { + 0x100, 0x108, 0x110, 0x118, 0x120, 0x128, 0x130, 0x138, 0x140, 0x148, + 0x150, 0x158, 0x160, 0x168, 0x170, 0x178, 0x180, 0x188, 0x190, 0x198, + 0x1a8, 0x1b0, 0x1b8, 0x1d8, 0x1e0, 0x1e8, 0x1f0, 0x200, 0x208, 0x210, + 0x218, 0x220, 0x228, 0x230, 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, + 0x268, 0x270, 0x278, 0x280, 0x288, 0x290, 0x298, 0x2a0, 0x2a8, 0x2b0, + 0x2b8, 0x2c0, 0x2c8, 0x2d0, 0x2d8, 0x2e0, 0x2e8, 0x2f0, 0x2f8, 0x300, + 0x308, 0x310, 0x318, 0x330, 0x338, 0x340, 0x348, 0x358, 0x360, 0x368, + 0x370, 0x378, 0x380, 0x388, 0x390, 0x398, 0x3a0, 0x3a8, 0x3b0, 0x3b8, + 0x3c0, 0x3f0, 0x3f8, 0x400, 0x408, 0x410, 0x418, 0x420, 0x428, 0x430, + 0x438, 0x440, 0x448, 0x450, 0x458, 0x460, 0x468, 0x470, 0x478, 0x480, + 0x488, 0x490, 0x498, 0x4a0, 0x4a8, 0x4b0, 0x4b8, 0x4c0, 0xc00, 0x4000, + 0x4008, 0x4010, 0x4018, 0x4020, 0x4028, 0x4030, 0x4038, 0x4040, 0x4048, 0x4050, + 0x4058, 0x4060, 0x8000, 0x8008, 0x8010, 0x8018, 0x8020, 0x10000, +}; + +/* + * On at macOS's commit and off once Linux's drivers have suspended: + * DWI, GPIO, SOC_RC, SPI_P, UART_P, AUDIO_P, UART_N, UART0. Parents first. + */ +static const u32 t8122_ps_macos_on[] = { + 0x118, 0x120, 0x168, 0x278, 0x280, 0x288, 0x2d0, 0x2d8, +}; + +/* Off at macOS's commit and left clock-gated by the bootloader. */ +static const u32 t8122_ps_macos_off[] = { + 0x108, 0x138, 0x140, 0x148, 0x150, 0x158, 0x178, 0x228, 0x258, 0x260, + 0x268, 0x270, 0x2f8, 0x300, 0x308, 0x378, 0x380, 0x388, 0x390, 0x3b8, +}; + +static const u32 t8122_ps_aon_all[] = { + 0x0, 0x58, 0x60, 0x68, 0x70, 0x78, 0x80, 0x88, + 0x90, 0x98, 0xa0, 0xa8, 0xb0, 0xb8, 0xc0, 0xc8, +}; + +static const u32 t8122_ps_wake_order[] = { + 0xc00, 0x100, 0x110, 0x118, 0x120, 0x128, 0x130, 0x3f0, + 0x3f8, 0x440, 0x448, 0x450, 0x458, 0x4a0, 0x400, 0x408, +}; + +static const u32 t8122_ps_aon_wake_order[] = { 0x90 }; + +static const struct apple_sleep_word t8122_stray[] = { + { "isp_sys", 0x1e8 }, { "i2c1", 0x238 }, { "i2c2", 0x240 }, { "i2c3", 0x248 }, + { "spi2", 0x2a8 }, { "qspi", 0x2c8 }, { "sio_cpu", 0x210 }, { "apcie_phy_sw", 0x4a0 }, +}; + +static struct { + void __iomem *hpm_notify; + void __iomem *mcc[4]; + void __iomem *apcie_axi; + void __iomem *apcie_rc; + void __iomem *apcie_clkgen; + void __iomem *apcie_phy; + void __iomem *apcie_phy_ip; + void __iomem *apcie_port0; + u32 notify_saved[2]; + bool notify_staged; + bool matched; +} t8122; + +static __always_inline bool t8122_is_pcore(void) +{ + return !!(read_sysreg(mpidr_el1) & T8122_MPIDR_P_CORE); +} + +static __always_inline u64 t8122_read_hid4(bool pcore) +{ + return pcore ? read_sysreg_s(T8122_SYS_HID4) + : read_sysreg_s(T8122_SYS_EHID4); +} + +static __always_inline void t8122_write_hid4(bool pcore, u64 val) +{ + if (pcore) + write_sysreg_s(val, T8122_SYS_HID4); + else + write_sysreg_s(val, T8122_SYS_EHID4); + isb(); +} + +/* + * Clean [start, end) past T8122's L2 into SLC/DRAM via clean-only dc cvadp + * (FEAT_DPB2) or dc cvap (FEAT_DPB). Must run with HID4/EHID4 DISABLE_DC_MVA + * cleared so the maintenance instructions are not ignored as NOPs. + */ +static __always_inline void t8122_clean_range_slc(unsigned long start, unsigned long end) +{ + unsigned long line = 4UL << ((read_sysreg(ctr_el0) >> 16) & 0xf); + unsigned int dpb = read_sysreg(id_aa64isar1_el1) & 0xf; + unsigned long addr = start & ~(line - 1); + + if (dpb >= 2) { + for (; addr < end; addr += line) + asm volatile("sys #3, c7, c13, #1, %0" : : "r"(addr) : "memory"); + } else if (dpb >= 1) { + for (; addr < end; addr += line) + asm volatile("sys #3, c7, c12, #1, %0" : : "r"(addr) : "memory"); + } else { + for (; addr < end; addr += line) + asm volatile("dc civac, %0" : : "r"(addr) : "memory"); + } + dsb(sy); +} + +/* + * Register-only dc csw across all implemented data/unified cache levels + * (L1 + L2 on T8122). Must run with HID4/EHID4 DISABLE_DC_SW_L2 cleared and + * zero stack writes so L1/L2 stay clean through dsb(sy). + */ +static __always_inline void t8122_dcache_clean_all_levels(void) +{ + u64 r_clidr, r_ccidx, r_lvl, r_ctype, r_ccsidr, r_lshift; + u64 r_way, r_mset, r_wshift, r_set, r_tmp; + + asm volatile( + "dsb sy\n\t" + "mrs %[clidr], clidr_el1\n\t" + "mrs %[ccidx], id_aa64mmfr2_el1\n\t" + "ubfx %[ccidx], %[ccidx], #20, #4\n\t" + "mov %[lvl], #0\n\t" + "1:\n\t" + "cmp %[lvl], #7\n\t" + "b.hs 5f\n\t" + "add %[tmp], %[lvl], %[lvl], lsl #1\n\t" + "lsr %[ctype], %[clidr], %[tmp]\n\t" + "and %[ctype], %[ctype], #7\n\t" + "cbz %[ctype], 5f\n\t" + "cmp %[ctype], #2\n\t" + "b.lo 4f\n\t" + "cmp %[ctype], #4\n\t" + "b.hi 4f\n\t" + "lsl %[tmp], %[lvl], #1\n\t" + "msr csselr_el1, %[tmp]\n\t" + "isb\n\t" + "mrs %[ccsidr], ccsidr_el1\n\t" + "and %[lshift], %[ccsidr], #7\n\t" + "add %[lshift], %[lshift], #4\n\t" + "cbnz %[ccidx], 20f\n\t" + "ubfx %[way], %[ccsidr], #3, #10\n\t" + "ubfx %[mset], %[ccsidr], #13, #15\n\t" + "b 21f\n\t" + "20:\n\t" + "ubfx %[way], %[ccsidr], #3, #21\n\t" + "ubfx %[mset], %[ccsidr], #32, #24\n\t" + "21:\n\t" + "clz %w[wshift], %w[way]\n\t" + "2:\n\t" + "mov %[set], %[mset]\n\t" + "lsl %[ctype], %[way], %[wshift]\n\t" + "orr %[ctype], %[ctype], %[lvl], lsl #1\n\t" + "3:\n\t" + "lsl %[tmp], %[set], %[lshift]\n\t" + "orr %[tmp], %[ctype], %[tmp]\n\t" + "dc csw, %[tmp]\n\t" + "subs %[set], %[set], #1\n\t" + "b.ge 3b\n\t" + "subs %[way], %[way], #1\n\t" + "b.ge 2b\n\t" + "dsb sy\n\t" + "4:\n\t" + "add %[lvl], %[lvl], #1\n\t" + "b 1b\n\t" + "5:\n\t" + "msr csselr_el1, xzr\n\t" + "dsb sy\n\t" + "isb\n\t" + : [clidr] "=&r"(r_clidr), + [ccidx] "=&r"(r_ccidx), + [lvl] "=&r"(r_lvl), + [ctype] "=&r"(r_ctype), + [ccsidr] "=&r"(r_ccsidr), + [lshift] "=&r"(r_lshift), + [way] "=&r"(r_way), + [mset] "=&r"(r_mset), + [wshift] "=&r"(r_wshift), + [set] "=&r"(r_set), + [tmp] "=&r"(r_tmp) + : + : "cc", "memory"); +} + +static bool t8122_linear_va_valid(unsigned long addr) +{ + pgd_t *pgdp = pgd_offset_k(addr); + pgd_t pgd = READ_ONCE(*pgdp); + p4d_t *p4dp, p4d; + pud_t *pudp, pud; + pmd_t *pmdp, pmd; + pte_t *ptep; + + if (pgd_none(pgd) || pgd_bad(pgd)) + return false; + p4dp = p4d_offset(pgdp, addr); + p4d = READ_ONCE(*p4dp); + if (p4d_none(p4d) || p4d_bad(p4d)) + return false; + pudp = pud_offset(p4dp, addr); + pud = READ_ONCE(*pudp); + if (pud_none(pud)) + return false; + if (pud_leaf(pud)) + return pud_valid(pud); + if (pud_bad(pud)) + return false; + pmdp = pmd_offset(pudp, addr); + pmd = READ_ONCE(*pmdp); + if (pmd_none(pmd)) + return false; + if (pmd_leaf(pmd)) + return pmd_valid(pmd); + if (pmd_bad(pmd)) + return false; + ptep = pte_offset_kernel(pmdp, addr); + return ptep && pte_valid(READ_ONCE(*ptep)); +} + +static void t8122_clean_used_ram(void) +{ + struct memblock_region *reg; + + for_each_mem_region(reg) { + unsigned long pfn, end_pfn; + unsigned long run_start = 0, run_end = 0; + + if (memblock_is_nomap(reg)) + continue; + pfn = PFN_UP(reg->base); + end_pfn = PFN_DOWN(reg->base + reg->size); + while (pfn < end_pfn) { + struct page *page; + unsigned long va; + + if (!pfn_valid(pfn)) { + if (run_start) { + t8122_clean_range_slc(run_start, run_end); + run_start = 0; + } + pfn++; + continue; + } + page = pfn_to_page(pfn); + if (PageBuddy(page)) { + unsigned long order = page_private(page); + unsigned long nr = 1; + + if (order <= MAX_PAGE_ORDER && + !(pfn & ((1UL << order) - 1))) + nr = 1UL << order; + if (nr > end_pfn - pfn) + nr = end_pfn - pfn; + if (run_start) { + t8122_clean_range_slc(run_start, run_end); + run_start = 0; + } + pfn += nr; + continue; + } + if (PageOffline(page)) { + if (run_start) { + t8122_clean_range_slc(run_start, run_end); + run_start = 0; + } + pfn++; + continue; + } + va = (unsigned long)page_address(page); + if (va && t8122_linear_va_valid(va)) { + if (run_start && va == run_end) { + run_end += PAGE_SIZE; + } else { + if (run_start) + t8122_clean_range_slc(run_start, run_end); + run_start = va; + run_end = va + PAGE_SIZE; + } + } else if (run_start) { + t8122_clean_range_slc(run_start, run_end); + run_start = 0; + } + pfn++; + } + if (run_start) + t8122_clean_range_slc(run_start, run_end); + } + dsb(sy); +} + +/* + * Clean the mapped kernel image (_text.._end). Under CTR_EL0.IDC=1 and + * rodata=full, patched text/rodata aliases are unmapped in the linear map and + * must be cleaned via kernel VAs so DRAM matches patched code on wake. + */ +static void t8122_clean_kernel_image(void) +{ + unsigned long start = (unsigned long)_text & PAGE_MASK; + unsigned long end = PAGE_ALIGN((unsigned long)_end); + unsigned long va, run_start = 0, run_end = 0; + + for (va = start; va < end; va += PAGE_SIZE) { + if (t8122_linear_va_valid(va)) { + if (run_start && va == run_end) { + run_end += PAGE_SIZE; + } else { + if (run_start) + t8122_clean_range_slc(run_start, run_end); + run_start = va; + run_end = va + PAGE_SIZE; + } + } else if (run_start) { + t8122_clean_range_slc(run_start, run_end); + run_start = 0; + } + } + if (run_start) + t8122_clean_range_slc(run_start, run_end); + dsb(sy); +} + +static void t8122_clean_percpu_slc(void) +{ + unsigned int cpu; + + for_each_possible_cpu(cpu) { + unsigned long area = (unsigned long)__per_cpu_start + per_cpu_offset(cpu); + + t8122_clean_range_slc(area, area + (__per_cpu_end - __per_cpu_start)); + } +} + +static void t8122_clean_all_kptables(void) +{ + unsigned int i, j, k, l; + pgd_t *pgd_base = swapper_pg_dir; + + t8122_clean_range_slc((unsigned long)pgd_base, + (unsigned long)pgd_base + PAGE_SIZE); + for (i = 0; i < PTRS_PER_PGD; i++) { + pgd_t pgd = READ_ONCE(pgd_base[i]); + p4d_t *p4d_base; + + if (pgd_none(pgd) || pgd_bad(pgd) || pgd_leaf(pgd)) + continue; + p4d_base = p4d_offset(&pgd_base[i], 0); + if ((unsigned long)p4d_base != (unsigned long)&pgd_base[i]) + t8122_clean_range_slc((unsigned long)p4d_base, + (unsigned long)p4d_base + PAGE_SIZE); + for (j = 0; j < PTRS_PER_P4D; j++) { + p4d_t p4d = READ_ONCE(p4d_base[j]); + pud_t *pud_base; + + if (p4d_none(p4d) || p4d_bad(p4d) || p4d_leaf(p4d)) + continue; + pud_base = pud_offset(&p4d_base[j], 0); + if ((unsigned long)pud_base != (unsigned long)&p4d_base[j]) + t8122_clean_range_slc((unsigned long)pud_base, + (unsigned long)pud_base + PAGE_SIZE); + for (k = 0; k < PTRS_PER_PUD; k++) { + pud_t pud = READ_ONCE(pud_base[k]); + pmd_t *pmd_base; + + if (pud_none(pud) || pud_bad(pud) || pud_leaf(pud)) + continue; + pmd_base = pmd_offset(&pud_base[k], 0); + if ((unsigned long)pmd_base != (unsigned long)&pud_base[k]) + t8122_clean_range_slc((unsigned long)pmd_base, + (unsigned long)pmd_base + PAGE_SIZE); + for (l = 0; l < PTRS_PER_PMD; l++) { + pmd_t pmd = READ_ONCE(pmd_base[l]); + pte_t *pte_base; + + if (pmd_none(pmd) || pmd_bad(pmd) || pmd_leaf(pmd)) + continue; + pte_base = pte_offset_kernel(&pmd_base[l], 0); + if (pte_base) + t8122_clean_range_slc((unsigned long)pte_base, + (unsigned long)pte_base + PAGE_SIZE); + } + } + } + } + dsb(sy); +} + +static void t8122_clean_va_ptables(unsigned long start, unsigned long len) +{ + unsigned long addr = start & PAGE_MASK; + unsigned long end = PAGE_ALIGN(start + len); + + for (; addr < end; addr += PAGE_SIZE) { + pgd_t *pgdp = pgd_offset_k(addr); + p4d_t *p4dp; + pud_t *pudp; + pmd_t *pmdp; + pte_t *ptep; + + t8122_clean_range_slc((unsigned long)pgdp & PAGE_MASK, + ((unsigned long)pgdp & PAGE_MASK) + PAGE_SIZE); + if (pgd_none(*pgdp) || pgd_bad(*pgdp)) + continue; + p4dp = p4d_offset(pgdp, addr); + if ((unsigned long)p4dp != (unsigned long)pgdp) + t8122_clean_range_slc((unsigned long)p4dp & PAGE_MASK, + ((unsigned long)p4dp & PAGE_MASK) + PAGE_SIZE); + if (p4d_none(*p4dp) || p4d_bad(*p4dp)) + continue; + pudp = pud_offset(p4dp, addr); + t8122_clean_range_slc((unsigned long)pudp & PAGE_MASK, + ((unsigned long)pudp & PAGE_MASK) + PAGE_SIZE); + if (pud_none(*pudp) || pud_bad(*pudp) || pud_leaf(*pudp)) + continue; + pmdp = pmd_offset(pudp, addr); + t8122_clean_range_slc((unsigned long)pmdp & PAGE_MASK, + ((unsigned long)pmdp & PAGE_MASK) + PAGE_SIZE); + if (pmd_none(*pmdp) || pmd_bad(*pmdp) || pmd_leaf(*pmdp)) + continue; + ptep = pte_offset_kernel(pmdp, addr); + if (ptep) + t8122_clean_range_slc((unsigned long)ptep & PAGE_MASK, + ((unsigned long)ptep & PAGE_MASK) + PAGE_SIZE); + } +} + +static void t8122_clean_state_poc(struct apple_sleep *s) +{ + unsigned int i, stash_n = mpidr_hash_size(); + + t8122_clean_range_slc((unsigned long)swapper_pg_dir, + (unsigned long)swapper_pg_dir + PAGE_SIZE); + t8122_clean_range_slc((unsigned long)idmap_pg_dir, + (unsigned long)idmap_pg_dir + 3 * PAGE_SIZE); + t8122_clean_va_ptables((unsigned long)current, sizeof(*current)); + t8122_clean_va_ptables((unsigned long)task_stack_page(current), THREAD_SIZE); + t8122_clean_va_ptables((unsigned long)s, sizeof(*s)); + t8122_clean_va_ptables((unsigned long)&t8122, sizeof(t8122)); + if (sleep_save_stash) { + unsigned long base = (unsigned long)sleep_save_stash; + + t8122_clean_va_ptables(base, stash_n * sizeof(*sleep_save_stash)); + t8122_clean_range_slc(base, base + stash_n * sizeof(*sleep_save_stash)); + for (i = 0; i < stash_n; i++) { + unsigned long entry = READ_ONCE(sleep_save_stash[i]); + + if (entry) { + t8122_clean_va_ptables(entry, sizeof(struct sleep_stack_data)); + t8122_clean_range_slc(entry, entry + sizeof(struct sleep_stack_data)); + } + } + } + t8122_clean_range_slc((unsigned long)&mpidr_hash, + (unsigned long)(&mpidr_hash + 1)); + t8122_clean_range_slc((unsigned long)&sleep_save_stash, + (unsigned long)(&sleep_save_stash + 1)); + t8122_clean_range_slc((unsigned long)s, (unsigned long)(s + 1)); + t8122_clean_range_slc((unsigned long)&t8122, (unsigned long)(&t8122 + 1)); + if (s->mbox) + t8122_clean_range_slc((unsigned long)s->mbox, (unsigned long)(s->mbox + 1)); + if (s->ps_commit && s->soc->ps_all.count) + t8122_clean_range_slc((unsigned long)s->ps_commit, + (unsigned long)(s->ps_commit + s->soc->ps_all.count)); + if (s->ps_aon_commit && s->soc->ps_aon_all.count) + t8122_clean_range_slc((unsigned long)s->ps_aon_commit, + (unsigned long)(s->ps_aon_commit + s->soc->ps_aon_all.count)); + if (s->ps_on_saved && s->soc->ps_macos_on.count) + t8122_clean_range_slc((unsigned long)s->ps_on_saved, + (unsigned long)(s->ps_on_saved + s->soc->ps_macos_on.count)); + if (s->ps_off_saved && s->soc->ps_macos_off.count) + t8122_clean_range_slc((unsigned long)s->ps_off_saved, + (unsigned long)(s->ps_off_saved + s->soc->ps_macos_off.count)); + if (s->pins_saved && s->npins) + t8122_clean_range_slc((unsigned long)s->pins_saved, + (unsigned long)(s->pins_saved + s->npins)); + t8122_clean_range_slc((unsigned long)current, + (unsigned long)current + sizeof(*current)); + t8122_clean_range_slc((unsigned long)task_stack_page(current), + (unsigned long)task_stack_page(current) + THREAD_SIZE); + dsb(sy); +} + +static void __iomem *t8122_map(struct apple_sleep *s, const char *name, struct resource *out) +{ + struct device_node *np = s->dev->of_node; + struct resource res; + void __iomem *base; + int index; + + index = of_property_match_string(np, "reg-names", name); + if (index < 0) + return IOMEM_ERR_PTR(index); + if (of_address_to_resource(np, index, &res)) + return IOMEM_ERR_PTR(-EINVAL); + /* Never unmapped: the driver has no unbind, as the common map. */ + base = ioremap_np(res.start, resource_size(&res)); + if (!base) + return IOMEM_ERR_PTR(-ENOMEM); + if (out) + *out = res; + + return base; +} + +static int t8122_sleep_map_regs(struct apple_sleep *s) +{ + static const struct { + const char *name; + size_t offset; + } regs[] = { + { "pwrgate", offsetof(struct apple_sleep, pwrgate) }, + { "cpu-start", offsetof(struct apple_sleep, cpu_start) }, + { "cllt", offsetof(struct apple_sleep, cllt) }, + { "cluster", offsetof(struct apple_sleep, cluster) }, + { "commit", offsetof(struct apple_sleep, commit) }, + { "wdt", offsetof(struct apple_sleep, wdt) }, + { "spmi", offsetof(struct apple_sleep, spmi) }, + { "wake", offsetof(struct apple_sleep, wake) }, + }; + static const char *const mcc_names[4] = { "mcc-a", "mcc-b", "mcc-c", "mcc-d" }; + unsigned int i; + bool pcore; + u64 hid4_orig; + int ret; + + s->ps = t8122_map(s, "ps", &s->ps_res); + if (IS_ERR(s->ps)) + return dev_err_probe(s->dev, PTR_ERR(s->ps), "no ps registers\n"); + for (i = 0; i < ARRAY_SIZE(regs); i++) { + void __iomem **field = (void __iomem **)((u8 *)s + regs[i].offset); + + *field = t8122_map(s, regs[i].name, NULL); + if (IS_ERR(*field)) + return dev_err_probe(s->dev, PTR_ERR(*field), "no %s registers\n", + regs[i].name); + } + t8122.hpm_notify = t8122_map(s, "spmi-hpm", NULL); + if (IS_ERR(t8122.hpm_notify)) + return dev_err_probe(s->dev, PTR_ERR(t8122.hpm_notify), "no spmi-hpm registers\n"); + + s->ps_aon = t8122_map(s, "ps-aon", NULL); + if (IS_ERR(s->ps_aon)) { + dev_warn(s->dev, "no ps-aon registers: USB/debug domains will not be lowered\n"); + s->ps_aon = NULL; + } + + ret = apple_sleep_read_wake_sources(s); + if (ret) + return ret; + + for (i = 0; i < ARRAY_SIZE(mcc_names); i++) { + t8122.mcc[i] = t8122_map(s, mcc_names[i], NULL); + if (IS_ERR(t8122.mcc[i])) { + dev_warn(s->dev, "no %s registers: MCC channel step-down unavailable\n", + mcc_names[i]); + t8122.mcc[i] = NULL; + } + } + + { + static const struct { + const char *name; + void __iomem **slot; + } apcie_regs[] = { + { "apcie-axi", &t8122.apcie_axi }, + { "apcie-rc", &t8122.apcie_rc }, + { "apcie-clkgen", &t8122.apcie_clkgen }, + { "apcie-phy", &t8122.apcie_phy }, + { "apcie-phy-ip", &t8122.apcie_phy_ip }, + { "apcie-port0", &t8122.apcie_port0 }, + }; + + for (i = 0; i < ARRAY_SIZE(apcie_regs); i++) { + *apcie_regs[i].slot = t8122_map(s, apcie_regs[i].name, NULL); + if (IS_ERR(*apcie_regs[i].slot)) + return dev_err_probe(s->dev, PTR_ERR(*apcie_regs[i].slot), + "no %s registers\n", apcie_regs[i].name); + } + } + + /* Ensure the EL1 deep-WFI stub is clean in DRAM before secondary cores jump to it. */ + pcore = t8122_is_pcore(); + hid4_orig = t8122_read_hid4(pcore); + t8122_write_hid4(pcore, hid4_orig & ~T8122_HID4_DISABLE_DC_MVA); + t8122_clean_range_slc((unsigned long)t8142_el1_wfi_stub, + (unsigned long)t8142_el1_wfi_stub_end); + t8122_write_hid4(pcore, hid4_orig); + + s->driver_down = devm_bitmap_zalloc(s->dev, resource_size(&s->ps_res) / 4, GFP_KERNEL); + if (!s->driver_down) + return -ENOMEM; + if (apple_sleep_alloc_books(s)) + return -ENOMEM; + + return 0; +} + +static void t8122_ps_lower_if_on(struct apple_sleep *s, u32 off, u32 first, u32 second) +{ + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + off))) + return; + if (first) + writel(first, s->ps + off); + writel(second, s->ps + off); +} + +static int t8122_sleep_display_off(struct apple_sleep *s) +{ + u64 start, hz = arch_timer_get_cntfrq(); + bool off = false; + + if (FIELD_GET(PS_ACTUAL, readl(s->ps + T8122_PS_DISP_CPU))) { + writel(0x0f0000ff, s->ps + T8122_PS_DISP_CPU); + writel(3, s->pwrgate + T8122_PWRGATE_DISP_A); + writel(0, s->pwrgate + T8122_PWRGATE_DISP_B); + writel(0, s->pwrgate + T8122_PWRGATE_DISP_C); + writel(0x0f0003f0, s->ps + T8122_PS_DISP_CPU); + } + t8122_ps_lower_if_on(s, T8122_PS_DISP_FE, 0, 0x23f0); + t8122_ps_lower_if_on(s, T8122_PS_DISP_SYS, 0, 0x23f0); + + start = read_sysreg(cntvct_el0); + while (read_sysreg(cntvct_el0) - start < hz / 10) { + if (!FIELD_GET(PS_ACTUAL, readl(s->ps + T8122_PS_DISP_CPU))) { + off = true; + break; + } + cpu_relax(); + } + if (!off) { + dev_err(s->dev, "refused: DISP_CPU stayed on: %#x\n", + readl(s->ps + T8122_PS_DISP_CPU)); + return -EBUSY; + } + + return 0; +} + +static void t8122_sleep_match(struct apple_sleep *s) +{ + const struct apple_sleep_soc *soc = s->soc; + int i; + + t8122.matched = true; + for (i = 0; i < soc->ps_macos_on.count; i++) { + u32 offset = soc->ps_macos_on.offset[i]; + + s->ps_on_saved[i] = readl(s->ps + offset); + if (FIELD_GET(PS_ACTUAL, s->ps_on_saved[i]) == 0xf) + continue; + sleep_ps_raise_start(s, offset); + } + for (i = 0; i < soc->ps_macos_off.count; i++) { + u32 offset = soc->ps_macos_off.offset[i]; + + s->ps_off_saved[i] = readl(s->ps + offset); + if (!sleep_ps_on(s, offset)) + continue; + sleep_ps_lower(s, offset); + } + for (i = 0; i < soc->ps_macos_off.count; i++) + sleep_ps_wait_off(s, soc->ps_macos_off.offset[i]); +} + +static void t8122_sleep_match_undo(struct apple_sleep *s) +{ + const struct apple_sleep_soc *soc = s->soc; + int i; + + if (!t8122.matched) + return; + t8122.matched = false; + if (s->returned) + return; + for (i = soc->ps_macos_off.count - 1; i >= 0; i--) + if (FIELD_GET(PS_ACTUAL, s->ps_off_saved[i])) + sleep_ps_raise(s, soc->ps_macos_off.offset[i]); + for (i = soc->ps_macos_on.count - 1; i >= 0; i--) { + u32 val = s->ps_on_saved[i]; + + if (!FIELD_GET(PS_ACTUAL, val)) + sleep_ps_lower(s, soc->ps_macos_on.offset[i]); + else if (FIELD_GET(PS_ACTUAL, val) != 0xf) + writel(val & ~PS_FLAGS, s->ps + soc->ps_macos_on.offset[i]); + } +} + +static void t8122_apcie_down(struct apple_sleep *s) +{ + void __iomem *axi600, *clk, *ctrl, *run, *phy0, *port0; + u32 val, b_phy0; + int ret; + + if (!t8122.apcie_axi || !t8122.apcie_clkgen || !t8122.apcie_phy || + !t8122.apcie_phy_ip || !t8122.apcie_port0) + return; + + ret = sleep_ps_raise_start(s, T8122_PS_APCIE); + ret |= sleep_ps_raise_start(s, T8122_PS_ANS); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_GP); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_ST); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_PHY_SW); + if (ret) { + dev_warn(s->dev, "APCIE power-down raise failed: %d\n", ret); + return; + } + + axi600 = t8122.apcie_axi + 0x600; + clk = t8122.apcie_clkgen + APCIE_CLK_CTRL; + ctrl = t8122.apcie_phy + APCIE_PHY_CTRL; + run = t8122.apcie_phy + APCIE_PHY_RUN; + phy0 = t8122.apcie_phy + 0xc000; + port0 = t8122.apcie_port0; + + /* Port 0 PHY + PERST shutdown */ + b_phy0 = readl(phy0); + if (b_phy0 & 0xc0000603U) { + writel(b_phy0 & ~0xc0000000U, phy0); + writel(readl(axi600) & ~BIT(16), axi600); + writel(readl(port0 + 0x13c) | 0x100, port0 + 0x13c); + writel(0, port0 + APCIE_PORT_PERST); + writel(readl(port0 + 0x13c) & ~0x100, port0 + 0x13c); + writel(readl(phy0) & ~BIT(10), phy0); + writel(readl(phy0) & ~BIT(9), phy0); + writel(readl(phy0) | BIT(4), phy0); + writel(readl(phy0) & ~BIT(0), phy0); + readl_poll_timeout_atomic(phy0, val, !(val & BIT(2)), 1, APCIE_POLL_US); + writel(readl(phy0) & ~BIT(1), phy0); + readl_poll_timeout_atomic(phy0, val, !(val & BIT(3)), 1, APCIE_POLL_US); + writel(readl(port0 + APCIE_PORT_APPCLK) & ~BIT(0), port0 + APCIE_PORT_APPCLK); + writel(readl(axi600) | BIT(16), axi600); + } + + /* Common PHY + PHY IP + AXI + CIO3PLL shutdown */ + writel(readl(t8122.apcie_phy_ip + 0x6c) & ~0xcU, t8122.apcie_phy_ip + 0x6c); + writel(readl(t8122.apcie_phy_ip + 0x7c) | 0x0f000000U, t8122.apcie_phy_ip + 0x7c); + writel(readl(ctrl) | T8122_APCIE_PHY_CTRL_RESET, ctrl); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK1REQ, ctrl); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK1ACK), 1, APCIE_POLL_US); + writel(readl(ctrl) & ~APCIE_PHY_CTRL_CLK0REQ, ctrl); + readl_poll_timeout_atomic(ctrl, val, !(val & APCIE_PHY_CTRL_CLK0ACK), 1, APCIE_POLL_US); + writel(readl(run) & ~APCIE_PHY_RUN_EN2, run); + writel(readl(run) & ~APCIE_PHY_RUN_EN, run); + writel(readl(axi600) & ~BIT(16), axi600); + writel(readl(clk) & ~APCIE_CLK_RUN, clk); + writel(readl(clk) | APCIE_CLK_OFF, clk); + writel(readl(clk) & ~APCIE_GEN4_CLK_EN, clk); + readl_poll_timeout_atomic(clk, val, !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); + writel(readl(clk) | APCIE_CLK_OFF2, clk); + + writel(T8122_PS_AUTO_IDLE, s->ps + T8122_PS_APCIE_PHY_SW); + readl_poll_timeout_atomic(s->ps + T8122_PS_APCIE_PHY_SW, val, + FIELD_GET(PS_ACTUAL, val) == 4, 1, 5000); + sleep_ps_raise_finish(s, T8122_PS_APCIE); + sleep_ps_raise_finish(s, T8122_PS_ANS); + sleep_ps_raise_finish(s, T8122_PS_APCIE_GP); + sleep_ps_raise_finish(s, T8122_PS_APCIE_ST); +} + +static void t8122_sleep_prepare_regs(struct apple_sleep *s) +{ + t8122_sleep_match(s); + t8122_apcie_down(s); + apple_sleep_pmu_write(s->spmi, s->soc->pmu_usid, + s->soc->pmu_sleep_reg, s->soc->pmu_sleep_val); + t8122.notify_saved[0] = readl(s->spmi + SPMI_NOTIFY_ENABLE); + t8122.notify_saved[1] = readl(t8122.hpm_notify + SPMI_NOTIFY_ENABLE); + t8122.notify_staged = true; + writel(s->soc->spmi_notify_sleep, s->spmi + SPMI_NOTIFY_ENABLE); + writel(s->soc->spmi_notify_sleep, t8122.hpm_notify + SPMI_NOTIFY_ENABLE); +} + +static void t8122_sleep_undo_regs(struct apple_sleep *s) +{ + t8122_sleep_match_undo(s); + if (!t8122.notify_staged) + return; + writel(t8122.notify_saved[0], s->spmi + SPMI_NOTIFY_ENABLE); + writel(t8122.notify_saved[1], t8122.hpm_notify + SPMI_NOTIFY_ENABLE); + t8122.notify_staged = false; +} + +static void t8122_aon_lower(struct apple_sleep *s, u32 off) +{ + u32 val = readl(s->ps_aon + off); + + if (val & PS_AUTO_ENABLE) { + val &= ~(PS_AUTO_ENABLE | PS_FLAGS); + writel(val, s->ps_aon + off); + } + writel((val & ~PS_TARGET) | PS_FLAGS, s->ps_aon + off); +} + +static void t8122_sleep_atc_off(struct apple_sleep *s) +{ + static const struct { + u32 usb, usb_aon, common; + } port[] = { + { T8122_AON_ATC0_USB, T8122_AON_ATC0_USB_AON, T8122_PS_ATC0_COMMON }, + { T8122_AON_ATC1_USB, T8122_AON_ATC1_USB_AON, T8122_PS_ATC1_COMMON }, + }; + unsigned int i; + + if (!s->ps_aon) + return; + for (i = 0; i < ARRAY_SIZE(port); i++) { + t8122_aon_lower(s, port[i].usb); + t8122_aon_lower(s, port[i].usb_aon); + sleep_ps_lower(s, port[i].common); + sleep_ps_wait_off(s, port[i].common); + } +} + +/* + * Step down T8122 MCC channel enable/stage registers after SLC write-back + * (handled by drivers/soc/apple/mcc.c in suspend_noirq) and CPU cache clean. + */ +static const struct { u8 page; u16 off; bool wide; } t8122_mcc_words[] = { + { 2, 0x000, true }, { 0, 0x000, true }, { 1, 0x000, true }, { 2, 0x800, true }, + { 0, 0x800, true }, { 1, 0x800, true }, { 3, 0x584, false }, { 3, 0x588, false }, +}; +static const char t8122_mcc_order[] = + "ABABABABABABAAAAAAAAABABABAABABABABABABABACACCCCCCCCCCCCCCCDDDEDEDFDFDDGGGGGGGGGHH"; + +static void t8122_mcc_channel_stop(void) +{ + unsigned int i; + + if (!t8122.mcc[0] || !t8122.mcc[1] || !t8122.mcc[2] || !t8122.mcc[3]) + return; + for (i = 0; t8122_mcc_order[i]; i++) { + unsigned int w = t8122_mcc_order[i] - 'A'; + void __iomem *reg = t8122.mcc[t8122_mcc_words[w].page] + t8122_mcc_words[w].off; + + if (t8122_mcc_words[w].wide) { + u64 q = readq(reg); + + if (q) + writeq(q & (q - 1), reg); + } else { + u32 l = readl(reg); + + if (l) + writel(l & (l - 1), reg); + } + } +} + +static void t8122_sleep_commit_regs(struct apple_sleep *s) +{ + bool pcore; + u64 hid4_orig; + + sleep_ps_save(s); + t8122_sleep_atc_off(s); + writel(0, s->wdt + WDT_WD0_CTL); + + /* + * Mask and save AIC state before the full cache clean so the saved + * interrupt masks reach DRAM before the clusters power off. + */ + aic_sleep_mask_all(); + + if (s->mbox) { + s->resumes_before = READ_ONCE(s->mbox->resumes); + WRITE_ONCE(s->mbox->resume_pa, __pa_symbol(s->soc->ops->resume)); + } + + pcore = t8122_is_pcore(); + hid4_orig = t8122_read_hid4(pcore); + t8122_write_hid4(pcore, hid4_orig & ~T8122_HID4_DC_MASK); + t8122_clean_used_ram(); + t8122_clean_kernel_image(); + t8122_clean_percpu_slc(); + t8122_clean_all_kptables(); + t8122_clean_state_poc(s); + t8122_dcache_clean_all_levels(); + if (s->mbox) + t8122_clean_range_slc((unsigned long)s->mbox, + (unsigned long)(s->mbox + 1)); + t8122_clean_range_slc((unsigned long)current, + (unsigned long)current + sizeof(*current)); + t8122_clean_range_slc((unsigned long)task_stack_page(current), + (unsigned long)task_stack_page(current) + THREAD_SIZE); + dsb(sy); + t8122_write_hid4(pcore, hid4_orig); + + t8122_mcc_channel_stop(); + + writel(PWRGATE_ON, s->pwrgate + T8122_PWRGATE_E); + writel(PWRGATE_ON, s->pwrgate + T8122_PWRGATE_P); + writel(0, s->cluster + T8122_CLUSTER_E); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntp_ctl_el0); + write_sysreg(ARCH_TIMER_CTRL_IT_MASK, cntv_ctl_el0); + sysreg_clear_set_s(SYS_APL_DEEP_SLEEP, 0, DEEP_SLEEP_ENABLE); + writel(0, s->cluster + T8122_CLUSTER_P); + writel(0x2500, s->cllt + T8122_CLLT_CTL); + writel(0, s->cllt + T8122_CLLT_CTL); + writel(0x1c17, s->cllt + T8122_CLLT_LAST); +} + +/* + * Common apple_sleep_commit() writes s->mbox->resume_pa and calls + * dcache_clean_poc() right before pre_commit, which is a NOP on T8122 while + * HID4.DISABLE_DC_MVA is set. Clean s->mbox and s with DISABLE_DC_MVA cleared. + */ +static void t8122_sleep_pre_commit(struct apple_sleep *s) +{ + bool pcore = t8122_is_pcore(); + u64 hid4_orig = t8122_read_hid4(pcore); + + t8122_write_hid4(pcore, hid4_orig & ~T8122_HID4_DISABLE_DC_MVA); + if (s->mbox) + t8122_clean_range_slc((unsigned long)s->mbox, + (unsigned long)(s->mbox + 1)); + t8122_clean_range_slc((unsigned long)s, (unsigned long)(s + 1)); + dsb(sy); + t8122_write_hid4(pcore, hid4_orig); +} + +/* AIC was already masked and saved before the cache clean in commit_regs. */ +static void t8122_aic_mask_noop(void) +{ +} + +static void t8122_deep_wfi_arm(void) +{ + sysreg_clear_set_s(T8122_SYS_CYC_OVRD, 0, T8122_CYC_WFI_MODE); + isb(); + sysreg_clear_set_s(SYS_APL_DEEP_SLEEP, 0, DEEP_SLEEP_ENABLE); + isb(); + if (is_kernel_in_hyp_mode()) { + dsb(sy); + t8142_el1_wfi(); + } +} + +static int t8122_raise_apcie(struct apple_sleep *s) +{ + static const struct { u32 off, val; } axi[] = { + { 0x0034, 0x0000ffff }, { 0x0038, 0x0000ffff }, { 0x0000, 0x00000211 }, + { 0x000c, 0x00016800 }, { 0x0010, 0x0000b400 }, { 0x0108, 0x00100031 }, + { 0x010c, 0x00016800 }, { 0x0110, 0x0000b400 }, { 0x0600, 0xc0010001 }, + { 0x0c00, 0x01ffffff }, { 0x0d48, 0x00200000 }, { 0x0e58, 0x00800010 }, + { 0x1000, 0x00000101 }, + }; + static const struct { u32 off, val; } cio3pll[] = { + { 0x2200, 0x00081f55 }, { 0x2224, 0x00000818 }, { 0x2228, 0x00000b18 }, + { 0x224c, 0x1f800096 }, { 0x22e4, 0x01024201 }, { 0x22fc, 0x030b40b4 }, + }; + static const struct { u32 off, val; } phy_ip[] = { + { 0x0028, 0x00800800 }, { 0x006c, 0x0000004c }, { 0x0080, 0x80010001 }, { 0x0094, 0x00000001 }, + { 0x1204, 0x0000004c }, { 0x1000, 0x2a2383c8 }, { 0x102c, 0x04454447 }, { 0x5080, 0x294d8190 }, + { 0x5088, 0x00560800 }, { 0x508c, 0x294d8190 }, { 0x5094, 0x00060800 }, { 0x50a0, 0x00554800 }, + { 0x50ac, 0x00554800 }, { 0x50b8, 0x00554800 }, { 0x5200, 0x0008155e }, { 0x5224, 0x00000818 }, + { 0x5240, 0x04010f69 }, { 0x52b0, 0x10300811 }, { 0x52fc, 0x03078078 }, { 0x531c, 0x00000002 }, + { 0x608c, 0x65918280 }, { 0x6224, 0x00000818 }, { 0x6228, 0x00000b18 }, { 0x6230, 0x0001e80f }, + { 0x624c, 0x1f80009d }, { 0xa000, 0x080001aa }, { 0xa008, 0x0000000a }, { 0xa00c, 0x0000000a }, + { 0xa010, 0x0000000a }, { 0xa014, 0x0000000a }, { 0xa038, 0x00000271 }, { 0xa040, 0x000000c8 }, + { 0xa04c, 0x00000bff }, { 0xa050, 0x00000001 }, { 0xa068, 0x00000c41 }, { 0xa070, 0x55b00820 }, + { 0xa084, 0x3e8f4240 }, { 0xa088, 0x00000fff }, { 0xa10c, 0x000f4240 }, { 0xa11c, 0x000f30dc }, + { 0xa178, 0x00300000 }, { 0xa0c0, 0x14e051f8 }, { 0xa1a4, 0x00000901 }, { 0xa1b0, 0x00000000 }, + { 0xa1b8, 0x00000000 }, { 0xa1e8, 0x00100000 }, { 0x903c, 0x018c3509 }, { 0x90d8, 0x0102068d }, + { 0x90dc, 0x12004408 }, { 0x91e0, 0x00002008 }, { 0x9288, 0x0801172a }, { 0x9280, 0xcccccccc }, + { 0x9000, 0x1e000000 }, { 0x9280, 0xccc8ccc1 }, { 0x928c, 0x00000003 }, { 0xb02c, 0x01980062 }, + { 0xb098, 0x05028000 }, { 0xc01c, 0x000006a1 }, { 0xc080, 0x540c6831 }, { 0xc08c, 0x10be0c1f }, + { 0xc0a0, 0x021a1780 }, { 0xc0ec, 0x00000000 }, { 0xd008, 0x0000000c }, { 0xd014, 0x04840408 }, + }; + void __iomem *ctrl, *run, *ready, *common; + unsigned int i; + u32 val; + int ret; + + if (!t8122.apcie_axi || !t8122.apcie_rc || !t8122.apcie_clkgen || + !t8122.apcie_phy || !t8122.apcie_phy_ip) + return -ENODEV; + + ret = sleep_ps_raise_start(s, T8122_PS_APCIE); + ret |= sleep_ps_raise_start(s, T8122_PS_ANS); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_GP); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_ST); + ret |= sleep_ps_raise_start(s, T8122_PS_APCIE_PHY_SW); + if (ret) + return ret; + + for (i = 0; i < ARRAY_SIZE(axi); i++) + writel(axi[i].val, t8122.apcie_axi + axi[i].off); + + writel(0, t8122.apcie_rc + APCIE_RC_ZERO); + + writel(0x239, t8122.apcie_clkgen + APCIE_CLK_CTRL); + writel(0x80000030, t8122.apcie_clkgen + APCIE_CLK_START); + writel(0x80000239, t8122.apcie_clkgen + APCIE_CLK_CTRL); + ret = readl_poll_timeout_atomic(t8122.apcie_clkgen + APCIE_CLK_CTRL, val, + !(val & APCIE_CLK_BUSY), 1, APCIE_POLL_US); + if (ret) + return ret; + for (i = 0; i < ARRAY_SIZE(cio3pll); i++) + writel(cio3pll[i].val, t8122.apcie_clkgen + cio3pll[i].off); + writel(0x239, t8122.apcie_clkgen + APCIE_CLK_CTRL); + + ctrl = t8122.apcie_phy + APCIE_PHY_CTRL; + run = t8122.apcie_phy + APCIE_PHY_RUN; + ready = t8122.apcie_phy + APCIE_PHY_READY; + common = t8122.apcie_phy + APCIE_PHY_COMMON; + + writel(0x03010030, t8122.apcie_phy + 0x800c); + writel(0x0400c030, t8122.apcie_phy + 0x8010); + writel(readl(ctrl) | APCIE_PHY_CTRL_CLK0REQ, ctrl); + ret = readl_poll_timeout_atomic(ctrl, val, val & APCIE_PHY_CTRL_CLK0ACK, 1, APCIE_POLL_US); + if (ret) + return ret; + writel(readl(ctrl) | APCIE_PHY_CTRL_CLK1REQ, ctrl); + ret = readl_poll_timeout_atomic(ctrl, val, val & APCIE_PHY_CTRL_CLK1ACK, 1, APCIE_POLL_US); + if (ret) + return ret; + writel(readl(ctrl) & ~T8122_APCIE_PHY_CTRL_RESET, ctrl); + writel(readl(run) | APCIE_PHY_RUN_EN, run); + + for (i = 0; i < ARRAY_SIZE(phy_ip); i++) + writel(phy_ip[i].val, t8122.apcie_phy_ip + phy_ip[i].off); + + writel(readl(run) | APCIE_PHY_RUN_EN2, run); + ret = readl_poll_timeout_atomic(ready, val, val & BIT(0), 1, APCIE_POLL_US); + if (ret) + return ret; + writel(readl(common) | BIT(0), common); + writel(readl(ctrl) | T8122_APCIE_PHY_CTRL_UP, ctrl); + + writel(0x140, t8122.apcie_rc + APCIE_RC_READY_CFG); + writel(1, t8122.apcie_rc + APCIE_RC_READY_START); + return readl_poll_timeout_atomic(t8122.apcie_rc + APCIE_RC_READY_STAT, val, + val & BIT(0), 1, APCIE_POLL_US); +} + +static void t8122_port0_wake(struct apple_sleep *s) +{ + static const struct { u32 off, val; } port0_init[] = { + { 0x088, 0x00000110 }, { 0x100, 0xffffffff }, { 0x148, 0xffffffff }, + { 0x210, 0xffffffff }, { 0x080, 0x00000000 }, { 0x084, 0x00000000 }, + { 0x104, 0xfffffff0 }, { 0x124, 0x00000100 }, { 0x16c, 0x00000000 }, + { 0x13c, 0x00000010 }, { 0x800, 0x00100100 }, { 0x808, 0x001000ff }, + { 0x82c, 0x00000000 }, { 0x130, 0x03000000 }, { 0x140, 0x00000010 }, + { 0x144, 0x00253770 }, { 0x21c, 0x00000000 }, { 0x834, 0x00000000 }, + { 0x83c, 0x00000000 }, { 0x140, 0x00000011 }, { 0x8a4, 0x00002020 }, + }; + void __iomem *port = t8122.apcie_port0; + unsigned int i; + u32 val; + + writel(readl(t8122.apcie_axi + 0x600) & ~BIT(16), t8122.apcie_axi + 0x600); + + for (i = 0; i < ARRAY_SIZE(port0_init); i++) + writel(port0_init[i].val, port + port0_init[i].off); + + writel(0x00100101, port + APCIE_PORT_APPCLK); + writel(1, port + APCIE_PORT_PERST); + readl_poll_timeout_atomic(port + 0x804, val, val & BIT(0), 1, APCIE_POLL_US); + writel(0x00100001, port + APCIE_PORT_APPCLK); + writel(3, port + 0x4020); + + writel(readl(t8122.apcie_axi + 0x600) | BIT(16), t8122.apcie_axi + 0x600); + + sleep_apcie_port_clock(port, t8122.apcie_phy + 0xc000); +} + +static void t8122_sleep_wake(struct apple_sleep *s) +{ + u32 val; + + writel(CLUSTER_ON, s->cluster + T8122_CLUSTER_E); + s->cluster_e_ret = readl_poll_timeout_atomic(s->cluster + T8122_CLUSTER_E, val, + val & CLUSTER_E_READY, 1, + CLUSTER_TIMEOUT_US); + writel(CLUSTER_ON, s->cluster + T8122_CLUSTER_P); + + writel(PWRGATE_ON, s->pwrgate + s->soc->pwrgate_wake[0]); + writel(PWRGATE_ON, s->pwrgate + s->soc->pwrgate_wake[1]); + + if (s->ps_aon) + sleep_ps_rewake(s); + /* Ensure GPIO domain is on before apple_sleep_restore_pins(). */ + sleep_ps_raise(s, T8122_PS_GPIO); + s->pins_lost = s->pins_saved_valid; + + if (t8122.apcie_axi) { + if (!t8122_raise_apcie(s) && t8122.apcie_port0 && t8122.apcie_phy) + t8122_port0_wake(s); + sleep_ps_raise_finish(s, T8122_PS_APCIE); + sleep_ps_raise_finish(s, T8122_PS_ANS); + sleep_ps_raise_finish(s, T8122_PS_APCIE_GP); + sleep_ps_raise_finish(s, T8122_PS_APCIE_ST); + sleep_ps_raise_finish(s, T8122_PS_APCIE_PHY_SW); + } + + val = readl(s->ps + T8122_PS_SEP); + if (val & PS_DEV_DISABLE) + writel(val & ~PS_DEV_DISABLE, s->ps + T8122_PS_SEP); + + writel(PWRGATE_ON, s->pwrgate + T8122_PWRGATE_E); + writel(PWRGATE_ON, s->pwrgate + T8122_PWRGATE_P); +} + +static const struct apple_sleep_gate_coproc t8122_gate_coprocs[] = { + { "/soc/dcp@28ec00000", true, T8122_PS_DISP_CPU }, + { "/soc/pmp@2d0500000", true }, + { "/soc/gpu@292400000", true, T8122_PS_GFX }, + { "/soc/sep@29e400000" }, + { "/soc/nvme@30dcc0000" }, + { "/soc/smc@2ec400000" }, +}; + +static const struct apple_sleep_wake_name t8122_wake_names[] = { + { 3, "AOP.CPUWakeupAP" }, { 12, "AOP.GPIO.IRQ6" }, { 51, "AOP.OutboxNotEmpty" }, + { 74, "AOP.SPMI0.SW3" }, { 83, "AOP.SPMI1.SW3" }, { 87, "ATC0.CIOWakeup" }, + { 88, "ATC0.USBWakeup" }, { 89, "ATC1.CIOWakeup" }, { 90, "ATC1.USBWakeup" }, + { 100, "MTP.DOCK.CHANNELS.AP0.IRQ" }, { 107, "MTP.GPIO.IRQ4" }, { 109, "MTP.GPIO.IRQ6" }, + { 128, "NUB.APWakeupTime" }, { 129, "NUB.APWatchdog" }, { 143, "NUB.GPIO.IRQ6" }, + { 155, "NUB.SPMI0.SW3" }, { 165, "NUB.SPMI1.SW3" }, { 179, "NUB.WakeupAP" }, + { 184, "SMC.CPUWakeupAP" }, { 208, "SMC.OutboxNotEmpty" }, +}; + +static const u32 t8122_wake_required[] = { 155 }; + +static int t8122_sleep_cpu_prepare(u64 mpidr) +{ + return 0; +} + +static void t8122_sleep_cpu_restore(u64 mpidr) +{ +} + +static const struct apple_sleep_ops t8122_sleep_ops = { + .deep_wfi_arm = t8122_deep_wfi_arm, + .resume = cpu_resume, + .aic_mask_all = t8122_aic_mask_noop, + .aic_ack = aic_sleep_ack, + .aic_restore = aic_sleep_restore, + .map_regs = t8122_sleep_map_regs, + .display_off = t8122_sleep_display_off, + .prepare_regs = t8122_sleep_prepare_regs, + .undo_regs = t8122_sleep_undo_regs, + .commit_regs = t8122_sleep_commit_regs, + .pre_commit = t8122_sleep_pre_commit, + .wake = t8122_sleep_wake, + .cpu_prepare = t8122_sleep_cpu_prepare, + .cpu_restore = t8122_sleep_cpu_restore, +}; + +static const struct apple_sleep_soc t8122_sleep_soc = { + .ops = &t8122_sleep_ops, + .untested = true, + .pen_deep = true, + .el1_wfi = true, + .no_pmu_notes = true, + .display_no_restart = true, + .early_context = true, + .stray = t8122_stray, + .nstray = ARRAY_SIZE(t8122_stray), + .wake_mask = 0x08, + .wake_status = 0x24, + .wake_words = 7, + .wake_last_word = GENMASK(16, 0), + .wake_names = t8122_wake_names, + .nwake_names = ARRAY_SIZE(t8122_wake_names), + .no_wd2 = true, + .early_cpu_wfi = true, + .debug_cut_after_stops = true, + .boot_mpidr = 0x10100, + .cpu_count = 8, + .wake_required = t8122_wake_required, + .nwake_required = ARRAY_SIZE(t8122_wake_required), + .boot_wfi_delay_ms = 2, + .ndisp = 1, + .nmcc = 4, + .cluster_cpu = { T8122_CLUSTER_E, T8122_CLUSTER_P }, + .ncluster_cpu = 2, + .cpu_clusters = 2, + .cpu_cores_per_cluster = 4, + .cpu_start_stride = 4, + .pwrgate_wake = { 0x000, 0x040 }, + .pwrgate_cpm = { T8122_PWRGATE_E, T8122_PWRGATE_P }, + .pmu_usid = 0xe, + .pmu_sleep_reg = 0xf701, + .pmu_sleep_val = 0x50, + .spmi_notify_sleep = 0x2000001, + .ps_aon_debug_switch = 0xa8, + .ps = { + .disp = { { T8122_PS_DISP_CPU, T8122_PS_DISP_FE, T8122_PS_DISP_SYS } }, + .sep = T8122_PS_SEP, + .ans = T8122_PS_ANS, + .apcie_sys_gp = T8122_PS_ANS, + }, + .ps_all = SLEEP_WORDS(t8122_ps_all), + .ps_aon_all = SLEEP_WORDS(t8122_ps_aon_all), + .ps_wake_order = SLEEP_WORDS(t8122_ps_wake_order), + .ps_aon_wake_order = SLEEP_WORDS(t8122_ps_aon_wake_order), + .ps_macos_on = SLEEP_WORDS(t8122_ps_macos_on), + .ps_macos_off = SLEEP_WORDS(t8122_ps_macos_off), + .gate_coprocs = t8122_gate_coprocs, + .ngate_coprocs = ARRAY_SIZE(t8122_gate_coprocs), +}; diff --git a/drivers/soc/apple/sleep-test.c b/drivers/soc/apple/sleep-test.c index 56df6219d5677..5cafa2300b02a 100644 --- a/drivers/soc/apple/sleep-test.c +++ b/drivers/soc/apple/sleep-test.c @@ -12,6 +12,7 @@ static const char *const apple_sleep_test_profiles[] = { "apple,t8142-sleep", "apple,t6050-sleep", "apple,t6050-j716c-sleep", + "apple,t8122-sleep", }; static bool apple_sleep_test_has(const struct apple_sleep_words *all, u32 offset) @@ -59,7 +60,8 @@ static void apple_sleep_test_known_profiles(struct kunit *test) KUNIT_EXPECT_NOT_NULL_MSG(test, id->data, "%s has no data", id->compatible); KUNIT_ASSERT_LT(test, n, ARRAY_SIZE(apple_sleep_test_profiles)); KUNIT_EXPECT_STREQ(test, id->compatible, apple_sleep_test_profiles[n]); - if (!strcmp(id->compatible, "apple,t8142-sleep")) { + if (!strcmp(id->compatible, "apple,t8142-sleep") || + !strcmp(id->compatible, "apple,t8122-sleep")) { const struct apple_sleep_soc *soc = id->data; KUNIT_ASSERT_NOT_NULL(test, soc); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 7f93efef9ec5a..dc2fc2151b2c0 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -52,6 +52,8 @@ #include #include #include +#include +#include #include #include #include @@ -86,6 +88,7 @@ #include #include #include +#include #include #include #include @@ -666,6 +669,19 @@ struct apple_sleep_soc { bool early_context; /* The SoC's operations arm and read its wake sources: none of the common wake words */ bool own_wake; + /* Secondary and boot cores drop to EL1 with MMU off for their final deep WFI. */ + bool el1_wfi; + /* WD2 is not this SoC's measured sleep safety net. */ + bool no_wd2; + /* Keep the debug link until the coprocessors have stopped. */ + bool debug_cut_after_stops; + /* The PMU scratch the step notes are written to is unmeasured on this SoC: none are. */ + bool no_pmu_notes; + /* + * The first display's driver stops its coprocessor cold for a sleep and can't restart + * it. A dry run leaves it running and doesn't count it among the refusals. + */ + bool display_no_restart; bool strict_gate_inventory; /* * No PMP runs and its report is disabled: the PTD entries the commit drops have no @@ -773,6 +789,12 @@ struct apple_sleep_soc { unsigned int nwake_names; /* The sources in the wake block's last word */ u32 wake_last_word; + /* + * Sources the tree's apple,wake-enable must name, or the sleep is refused: the ones a + * timed wake arrives on, so a sleep can't be entered that only a person can end. + */ + const u32 *wake_required; + unsigned int nwake_required; /* The encoders' system power words: their domains need the AVE complex up */ const u32 *ave_domains; unsigned int nave_domains; @@ -1014,6 +1036,8 @@ struct apple_sleep { /* This wake came back down the deep path, not the DRAM-kept one. */ bool deep_ran; bool regs_staged, wake_masks_saved, entry_prepared, entry_committed; + /* An untested SoC's sleep that stops before the commit, as .begin found the switch. */ + bool dry_run; u64 smc_idle_sent, smc_idle_ack, timer_at; struct { struct device *dev; @@ -1142,6 +1166,18 @@ module_param_named(untested_commit, sleep_untested_commit, bool, 0644); MODULE_PARM_DESC(untested_commit, "Untested SoC: let a deep sleep through to the commit (else a dry run that refuses)"); +/* + * Whether this sleep is an untested SoC's dry run. An early_context SoC's drivers ask the same + * answer in their .suspend, so it is the one .begin latched: the switch can't change under a + * sleep. The common entry reads the switch where it stops, as before. + */ +static bool apple_sleep_dry(const struct apple_sleep *s) +{ + if (!s->soc->untested) + return false; + return s->soc->early_context ? s->dry_run : !READ_ONCE(sleep_untested_commit); +} + /* * Untested SoCs: each step of a sleep in PMU scratch, readable after a reset. J815's ADT * pmu-main info-scrpad is 0x8000 + 0x2800; aurora-silicon/linux#1664 (T6041) notes its steps @@ -1668,6 +1704,15 @@ bool apple_sleep_commit_stops_coprocs(void) } EXPORT_SYMBOL_GPL(apple_sleep_commit_stops_coprocs); +/* This firmware sleep stops before its commit: a driver must stop nothing it can't restart. */ +bool apple_sleep_dry_run(void) +{ + struct apple_sleep *s = READ_ONCE(apple_sleep); + + return s && pm_suspend_via_firmware() && apple_sleep_dry(s); +} +EXPORT_SYMBOL_GPL(apple_sleep_dry_run); + /* * The last thing that can fail on the way to the commit: no CPU but this one is up and * interrupts are off, so no lock can be held. A coprocessor stopped here comes back only @@ -3358,6 +3403,10 @@ static bool apple_sleep_gate_failed(struct apple_sleep *s) /* Idled below full power, or stopped: its registers read only at full power. */ if (g->asc && g->ps && !sleep_asc_running(s, g->ps, g->asc)) continue; + /* Left running by a dry run, see apple_sleep_refuse(). */ + if (s->soc->display_no_restart && s->display[0].np && g->np == s->display[0].np && + apple_sleep_dry(s)) + continue; if (g->asc && g->ps && (g->allow_unstarted || s->soc->regions)) dev_err(s->dev, "%s didn't stop: control %#x\n", g->name, readl(g->asc + ASC_CPU_CONTROL)); @@ -3422,6 +3471,24 @@ static void sleep_wake_masks(struct apple_sleep *s, u32 *val) sleep_wake_unmask(s, val, s->wake_enable[i]); } +/* Every source the SoC requires is among the tree's. A SoC that requires none admits any. */ +static bool apple_sleep_wake_admitted(const struct apple_sleep_soc *soc, const u32 *enabled, + int count) +{ + unsigned int i; + int j; + + for (i = 0; i < soc->nwake_required; i++) { + for (j = 0; j < count; j++) + if (enabled[j] == soc->wake_required[i]) + break; + if (j >= count) + return false; + } + + return true; +} + /* The last checks, then the stray words lowered: what has stopped may not return until a wake. */ static int apple_sleep_refuse(struct apple_sleep *s) { @@ -3465,6 +3532,15 @@ static int apple_sleep_refuse(struct apple_sleep *s) dev_err(s->dev, "refused: no wake source enabled in the device tree\n"); refused = true; } + if (!apple_sleep_wake_admitted(s->soc, s->wake_enable, s->wake_enable_count)) { + dev_err(s->dev, "refused: the device tree doesn't enable the timed wake's source\n"); + refused = true; + } + /* A required source can be neither armed nor read back without its block. */ + if (s->soc->nwake_required && !s->wake) { + dev_err(s->dev, "refused: the wake block isn't mapped\n"); + refused = true; + } sleep_note(s, 0x21); /* Its first config access after the power loss would hang on the untrained link. */ @@ -3518,6 +3594,11 @@ static int apple_sleep_refuse(struct apple_sleep *s) } if (s->display[i].bound && sleep_asc_running(s, ps, s->display[i].asc) && (!apple_sleep_v27_late() || sleep_v27_final_check(i))) { + /* Only a sleep that commits has its driver stop it: there is no restart. */ + if (i == 0 && s->soc->display_no_restart && apple_sleep_dry(s)) { + dev_info(s->dev, "dry run: the display coprocessor is left running; a sleep that commits stops it cold, until a reboot\n"); + continue; + } dev_err(s->dev, "refused: a bound display coprocessor is still running\n"); refused = true; } @@ -3532,7 +3613,7 @@ static int apple_sleep_refuse(struct apple_sleep *s) sleep_note(s, 0x28); /* A dry run, which stops right after this, writes no power word (as in b27). */ - return apple_sleep_lower_stray(s, s->soc->untested && !READ_ONCE(sleep_untested_commit)); + return apple_sleep_lower_stray(s, apple_sleep_dry(s)); } static void sleep_wake_arm(struct apple_sleep *s) @@ -3817,7 +3898,8 @@ static int sleep_hpm_sleep(struct apple_sleep *s) */ static int sleep_hpm_wait_contract(struct apple_sleep *s, int *gone_ms) { - ktime_t start = ktime_get(); + /* The raw counter: the late cut runs with timekeeping suspended. */ + u64 start = read_sysreg(cntvct_el0), hz = arch_timer_get_cntfrq(); bool wake = true, up; u32 val = 0; int ms, ret; @@ -3830,7 +3912,7 @@ static int sleep_hpm_wait_contract(struct apple_sleep *s, int *gone_ms) *gone_ms = -1; mdelay(HPM_RESET_FIRST_MS); for (;;) { - ms = ktime_ms_delta(ktime_get(), start); + ms = div64_u64((read_sysreg(cntvct_el0) - start) * 1000, hz); if (ms >= HPM_CONTRACT_MS) return -ETIMEDOUT; ret = wake ? sleep_hpm_cmd(s, SPMI_OP_WAKEUP, 0, NULL, NULL) : 0; @@ -3852,7 +3934,7 @@ static int sleep_hpm_wait_contract(struct apple_sleep *s, int *gone_ms) /* A live debug link holds the debug switch domain on. */ static bool sleep_debug_link_up(struct apple_sleep *s) { - return s->hpm_spmi && + return s->hpm_spmi && s->ps_aon && FIELD_GET(PS_ACTUAL, readl(s->ps_aon + s->soc->ps_aon_debug_switch)); } @@ -3876,7 +3958,9 @@ static bool apple_sleep_usb_down(struct apple_sleep *s) if (!sleep_debug_link_up(s)) return false; - dev_info(s->dev, "debug link: cutting it for the sleep\n"); + /* The late cut prints nothing after it: the console is the link. */ + dev_info(s->dev, "debug link: cutting it for the sleep%s\n", + s->soc->debug_cut_after_stops ? ", after the coprocessor stops" : ""); enable = readl(s->hpm_spmi + SPMI_NOTIFY_ENABLE); writel(enable & ~SPMI_NOTIFY_REPLY, s->hpm_spmi + SPMI_NOTIFY_ENABLE); /* A reset that the partner doesn't come back from is done again. */ @@ -3891,11 +3975,13 @@ static bool apple_sleep_usb_down(struct apple_sleep *s) WRITE_ONCE(sleep_debug_link_cut, true); ms = sleep_hpm_wait_contract(s, &gone); if (ms >= 0) { - dev_info(s->dev, "debug link: contract back %d ms after reset %d (gone at %d ms)\n", - ms, i, gone); + if (!s->soc->debug_cut_after_stops) + dev_info(s->dev, "debug link: contract back %d ms after reset %d (gone at %d ms)\n", + ms, i, gone); break; } - dev_warn(s->dev, "debug link: no contract with the partner after reset %d\n", i); + if (!s->soc->debug_cut_after_stops) + dev_warn(s->dev, "debug link: no contract with the partner after reset %d\n", i); } if (!ret) ret = sleep_hpm_sleep(s); @@ -3908,7 +3994,12 @@ static bool apple_sleep_usb_down(struct apple_sleep *s) bool apple_sleep_debug_link_cut(void) { - return apple_sleep ? apple_sleep_usb_down(apple_sleep) : false; + struct apple_sleep *s = apple_sleep; + + /* The entry cuts it itself, after its stops. */ + if (!s || s->soc->debug_cut_after_stops) + return false; + return apple_sleep_usb_down(s); } EXPORT_SYMBOL_GPL(apple_sleep_debug_link_cut); @@ -3916,7 +4007,7 @@ bool apple_sleep_debug_link_up(void) { struct apple_sleep *s = apple_sleep; - return s && !s->usb_down_ran && sleep_debug_link_up(s); + return s && !s->soc->debug_cut_after_stops && !s->usb_down_ran && sleep_debug_link_up(s); } EXPORT_SYMBOL_GPL(apple_sleep_debug_link_up); @@ -5055,7 +5146,8 @@ static void sleep_spmi_sources_report(struct apple_sleep *s) static bool sleep_notes_on(struct apple_sleep *s) { - return s && s->spmi && s->soc->untested && READ_ONCE(sleep_pmu_notes); + return s && s->spmi && s->soc->untested && !s->soc->no_pmu_notes && + READ_ONCE(sleep_pmu_notes); } /* @@ -5626,8 +5718,8 @@ static int apple_sleep_commit(unsigned long unused) early = s->soc->early_cpu_wfi && READ_ONCE(sleep_early_wfi); if (early) { /* Released before the stop: each core goes from its stop bit to its deep WFI. */ - spin_table_pen_deep_el1(s->soc->ops->deep_wfi_arm == t8142_deep_wfi_arm && - is_kernel_in_hyp_mode() && READ_ONCE(sleep_el1_wfi) >= 2); + spin_table_pen_deep_el1(s->soc->pen_deep && is_kernel_in_hyp_mode() && + (s->soc->el1_wfi || READ_ONCE(sleep_el1_wfi) >= 2)); spin_table_release_stopped(); } s->cpus_stop_ret = spin_table_stop_parked(); @@ -5669,8 +5761,8 @@ static int apple_sleep_commit(unsigned long unused) /* Each core's deep WFI comes after the commit, this one last. No SPMI from here on. */ if (!early) { - spin_table_pen_deep_el1(s->soc->ops->deep_wfi_arm == t8142_deep_wfi_arm && - is_kernel_in_hyp_mode() && READ_ONCE(sleep_el1_wfi) >= 2); + spin_table_pen_deep_el1(s->soc->pen_deep && is_kernel_in_hyp_mode() && + (s->soc->el1_wfi || READ_ONCE(sleep_el1_wfi) >= 2)); spin_table_release_stopped(); } if (s->soc->boot_wfi_delay_ms) @@ -6145,6 +6237,31 @@ static void apple_sleep_display_then_gpu(struct apple_sleep *s) apple_sleep_coprocs_stop(s, true); } +/* + * An untested SoC with early_context stops here unless asked, as the common entry does: the + * refusals have run and passed, no stray word was lowered, and no core is held, no display + * word lowered, no SMC notice sent from here and nothing stopped. What is left that can refuse + * without changing anything runs too, so the dry run reports it: each coprocessor's own last + * check and the SoC's final check. + */ +static int apple_sleep_enter_dry(struct apple_sleep *s) +{ + int ret = apple_sleep_coprocs_check(s); + + if (!ret && s->soc->ops->final_check) + ret = s->soc->ops->final_check(s); + if (!ret && pm_wakeup_pending()) + ret = -EBUSY; + apple_sleep_stray_restore(s); + if (ret) { + dev_info(s->dev, "dry run: refused at the last checks: %d\n", ret); + return ret; + } + dev_info(s->dev, + "dry run: every check passed; refusing before the first stop and the commit (untested SoC, apple_sleep.untested_commit=1 lets it through)\n"); + return -EAGAIN; +} + /* * The entry of a SoC with early_context, called with the boot core's context saved. All * returning checks precede enclave/final-stop. @@ -6165,6 +6282,8 @@ static int apple_sleep_enter_prepare(struct apple_sleep *s) ret = apple_sleep_refuse(s); if (ret) return ret; + if (apple_sleep_dry(s)) + return apple_sleep_enter_dry(s); ret = apple_sleep_cpus_hold(s); if (!ret) { step = "display prepare"; @@ -6670,6 +6789,8 @@ static int apple_sleep_begin(suspend_state_t state) if (ret) return ret; + apple_sleep->dry_run = apple_sleep->soc->untested && !READ_ONCE(sleep_untested_commit); + if (apple_sleep->soc->ops->begin) { ret = apple_sleep->soc->ops->begin(apple_sleep); if (ret) @@ -6682,7 +6803,7 @@ static int apple_sleep_begin(suspend_state_t state) apple_sleep->smc_woke_alone = false; sleep_pins_save(apple_sleep); } - { + if (!apple_sleep->soc->no_wd2) { unsigned long flags; spin_lock_irqsave(&apple_sleep_wdt_lock, flags); @@ -6706,7 +6827,7 @@ static void apple_sleep_end(void) /* Nothing left by now after a wake; the platform wake takes it */ if (pm_suspend_via_firmware() && apple_sleep->soc->ops->pmu_wake) apple_sleep->soc->ops->pmu_wake(apple_sleep); - if (pm_suspend_via_firmware()) + if (pm_suspend_via_firmware() && !apple_sleep->soc->no_wd2) apple_sleep_wdt_end(apple_sleep->wdt); } @@ -7168,7 +7289,8 @@ static const struct platform_suspend_ops apple_sleep_ops = { /* For a SoC with early_context: the same, with the scopes its entry needs held */ static const struct platform_suspend_ops apple_sleep_early_ops = { - .valid = suspend_valid_only_mem, + /* An untested SoC's deep sleep is offered only with apple_sleep.deep=1. */ + .valid = apple_sleep_valid, .begin = apple_sleep_begin, .prepare = apple_sleep_prepare, .prepare_late = apple_sleep_late_scope_begin, @@ -8119,6 +8241,9 @@ static int apple_sleep_probe(struct platform_device *pdev) return dev_err_probe(dev, ret, "cannot control CPU power\n"); if (s->soc->pen_deep) spin_table_pen_deep_arm(true); + if (s->soc->el1_wfi) + dcache_clean_poc((unsigned long)t8142_el1_wfi_stub, + (unsigned long)t8142_el1_wfi_stub_end); /* Last: the exported helpers take it as a probed driver, and a failure frees s. */ apple_sleep = s; @@ -8139,13 +8264,15 @@ static int apple_sleep_probe(struct platform_device *pdev) READ_ONCE(sleep_el1_wfi) >= 2 ? "EL1 on every core" : "EL1 on the boot core", &(phys_addr_t){ __pa_symbol(t8142_el1_wfi_stub) }); } - dev_info(dev, "probe: pwrgate wake %#x %#x cpm %#x %#x, commit %#x, clusters %#x %#x\n", - readl(s->pwrgate + s->soc->pwrgate_wake[0]), - readl(s->pwrgate + s->soc->pwrgate_wake[1]), - readl(s->pwrgate + s->soc->pwrgate_cpm[0]), - readl(s->pwrgate + s->soc->pwrgate_cpm[1]), readl(s->commit), - readl(s->cluster + s->soc->cluster_cpu[0]), - readl(s->cluster + s->soc->cluster_cpu[1])); + /* The common layout's words: a SoC with its own map names none of them. */ + if (!s->soc->ops->map_regs) + dev_info(dev, "probe: pwrgate wake %#x %#x cpm %#x %#x, commit %#x, clusters %#x %#x\n", + readl(s->pwrgate + s->soc->pwrgate_wake[0]), + readl(s->pwrgate + s->soc->pwrgate_wake[1]), + readl(s->pwrgate + s->soc->pwrgate_cpm[0]), + readl(s->pwrgate + s->soc->pwrgate_cpm[1]), readl(s->commit), + readl(s->cluster + s->soc->cluster_cpu[0]), + readl(s->cluster + s->soc->cluster_cpu[1])); dev_warn(dev, "sleep data derived for this SoC, untested: deep sleep is %s, and %s\n", READ_ONCE(sleep_deep) ? "offered (apple_sleep.deep=1)" : "off (apple_sleep.deep=1 offers it)", READ_ONCE(sleep_untested_commit) ? "the commit is allowed" : @@ -9849,10 +9976,12 @@ static const struct apple_sleep_soc t8142_sleep_soc = { .wake_last_word = GENMASK(11, 0), }; +#include "sleep-t8122.h" + /* * Qualified profiles expose system sleep. Inventory-only profiles match for a * precise refusal and cannot reach hardware preparation or suspend_set_ops(). - * T8142 remains opt-in and requires its separate physical-commit gate. + * T8142 and T8122 remain opt-in and require their separate physical-commit gate. */ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8140-sleep", .data = &t8140_sleep_soc }, @@ -9860,6 +9989,7 @@ static const struct of_device_id apple_sleep_of_match[] = { { .compatible = "apple,t8142-sleep", .data = &t8142_sleep_soc }, { .compatible = "apple,t6050-sleep", .data = &t6050_sleep_soc }, { .compatible = "apple,t6050-j716c-sleep", .data = &t6050_j716c_sleep_soc }, + { .compatible = "apple,t8122-sleep", .data = &t8122_sleep_soc }, { } }; diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 760369ea2a756..f66c192a2ae3e 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -192,6 +192,20 @@ static const struct apple_cio_hw apple_cio_t8103_hw = { .dp = &apple_dpin_m1, }; +/* + * T8122 shares the T8103 ACIO register layout and DP IN policy, while deep + * sleep removes the CIO power domains and shares the ATC PHY with DWC3. Tear + * down a connected router before tasks freeze (ahead of DWC3's PM notifier) + * and request fresh cable state immediately on wake; s2idle and freeze leave + * the port up. + */ +static const struct apple_cio_hw apple_cio_t8122_hw = { + .sleep_removes_domains = true, + .restart_on_wake = true, + .phy_shared_with_dwc3 = true, + .dp = &apple_dpin_m1, +}; + static const struct apple_cio_hw apple_cio_t6020_hw = { .dp = &apple_dpin_m2, }; @@ -4896,6 +4910,10 @@ static const struct of_device_id apple_acio_match[] = { .compatible = "apple,t6030-usb4-acio", .data = &apple_cio_t6030_hw, }, + { + .compatible = "apple,t8122-usb4-acio", + .data = &apple_cio_t8122_hw, + }, { .compatible = "apple,t6020-usb4-acio", .data = &apple_cio_t6020_hw, diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index d2642a9c8ffb4..aea4996a464b1 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -150,6 +150,9 @@ bool apple_sleep_coproc_stopped(struct device *dev); */ bool apple_sleep_commit_stops_coprocs(void); +/* This firmware sleep stops before its commit: a driver must stop nothing it can't restart. */ +bool apple_sleep_dry_run(void); + /* * Untested SoCs: a coprocessor's last stop notes its own step in PMU scratch (kept across a * WD2 reset), next to the sleep driver's step and the coprocessor's slot. @@ -402,6 +405,11 @@ static inline bool apple_sleep_commit_stops_coprocs(void) return false; } +static inline bool apple_sleep_dry_run(void) +{ + return false; +} + static inline void apple_sleep_usb_set_asleep(bool asleep) { } diff --git a/tools/asahi/pciec-policy-tests.py b/tools/asahi/pciec-policy-tests.py index eb05f79c90501..3e753abfdba22 100644 --- a/tools/asahi/pciec-policy-tests.py +++ b/tools/asahi/pciec-policy-tests.py @@ -670,6 +670,181 @@ def test_pm_order(out, acio): print('PASS negative control:', name) +T8122_SLEEP_FIXTURE = r''' +#include +#include +#include +#include +#include +struct apple_dpin_policy { int unused; }; +static const struct apple_dpin_policy apple_dpin_m1; +struct apple_cio_hw { + bool sleep_removes_domains; + bool restart_on_wake; + bool phy_shared_with_dwc3; + const struct apple_dpin_policy *dp; +}; +struct notifier_block { int (*notifier_call)(void); int priority; }; +struct apple_cio { const struct apple_cio_hw *hw; struct notifier_block pm_notifier; }; +struct dwc3_apple { struct notifier_block pm_nb; }; +struct match_entry { const char *compatible; const struct apple_cio_hw *data; }; +static bool default_s2idle, no_platform; +static bool pm_suspend_default_s2idle(void) { return default_s2idle; } +static bool pm_suspend_no_platform(void) { return no_platform; } +/* PRODUCTION_HW */ +static const struct match_entry match_table[] = { +/* MATCH_ENTRIES */ + { NULL, NULL }, +}; +/* PRODUCTION_FN */ +static const struct apple_cio_hw *match_of(const char *const *compat) +{ + for (size_t i = 0; compat[i]; i++) + for (size_t j = 0; match_table[j].compatible; j++) + if (!strcmp(compat[i], match_table[j].compatible)) + return match_table[j].data; + return NULL; +} +static struct notifier_block *chain[2]; +static int count; +static bool phy_off, pci_off; +static int register_pm_notifier(struct notifier_block *nb) +{ + int i = count++; + assert(count <= 2); + while (i && chain[i - 1]->priority < nb->priority) { + chain[i] = chain[i - 1]; + i--; + } + chain[i] = nb; + return 0; +} +static int apple_cio_pm_notify(void) +{ + assert(!phy_off); + pci_off = true; + return 0; +} +static int dwc3_apple_pm_notify(void) +{ + assert(pci_off); + phy_off = true; + return 0; +} +static void add_cio(struct apple_cio *acio) +{ + /* ACIO_REGISTRATION */ +} +static void add_dwc(struct dwc3_apple *appledwc) +{ + int ret; + /* DWC_REGISTRATION */ + assert(!ret); +} +int main(void) +{ + static const char *const t8122_compat[] = { + "apple,t8122-usb4-acio", "apple,t8103-usb4-acio", NULL + }; + static const char *const t8103_compat[] = { + "apple,t8103-usb4-acio", NULL + }; + const struct apple_cio_hw *t8122 = match_of(t8122_compat); + const struct apple_cio_hw *t8103 = match_of(t8103_compat); + assert(t8122 == &apple_cio_t8122_hw); + assert(t8103 == &apple_cio_t8103_hw); + assert(t8122->sleep_removes_domains); + assert(t8122->restart_on_wake); + assert(t8122->phy_shared_with_dwc3); + assert(t8122->dp == &apple_dpin_m1); + + struct apple_cio acio_t8122 = { .hw = t8122 }; + struct apple_cio acio_t8103 = { .hw = t8103 }; + + /* mem_sleep=deep + mem tears down T8122 ACIO, leaves T8103 unchanged. */ + default_s2idle = false; + no_platform = false; + assert(apple_cio_sleep_removes_domains(&acio_t8122)); + assert(!apple_cio_sleep_removes_domains(&acio_t8103)); + + /* s2idle keeps the port up. */ + default_s2idle = true; + no_platform = false; + assert(!apple_cio_sleep_removes_domains(&acio_t8122)); + + /* freeze keeps the port up even when mem_sleep=deep is the default. */ + default_s2idle = false; + no_platform = true; + assert(!apple_cio_sleep_removes_domains(&acio_t8122)); + + /* Shared ATC PHY: ACIO PM notifier must run before DWC3's. */ + struct dwc3_apple dwc = { 0 }; + add_dwc(&dwc); + add_cio(&acio_t8122); + for (int i = 0; i < count; i++) + assert(!chain[i]->notifier_call()); + assert(pci_off && phy_off); + + puts("PASS: T8122 ACIO deep-sleep teardown, freeze/s2idle preservation, and DWC3 PM order"); + return 0; +} +''' + + +def test_t8122_cio_sleep(out, acio): + dwc = (ROOT / 'drivers/usb/dwc3/dwc3-apple.c').read_text() + hw_defs = [] + for sym in ('apple_cio_t8103_hw', 'apple_cio_t8122_hw'): + m = re.search(r'^static const struct apple_cio_hw ' + sym + + r' = \{.*?\n\};', acio, re.M | re.S) + assert m, sym + hw_defs.append(m.group()) + match_block = re.search( + r'static const struct of_device_id apple_acio_match\[\] = \{.*?\n\};', + acio, re.S) + assert match_block + entries = re.findall( + r'\.compatible\s*=\s*"([^"]+)"\s*,\s*\.data\s*=\s*(&apple_cio_\w+_hw)', + match_block.group()) + match_lines = [f' {{ "{c}", {d} }},' for c, d in entries + if c in ('apple,t8122-usb4-acio', 'apple,t8103-usb4-acio')] + assert len(match_lines) == 2, match_lines + code = T8122_SLEEP_FIXTURE + code = code.replace('/* PRODUCTION_HW */', '\n'.join(hw_defs)) + code = code.replace('/* MATCH_ENTRIES */', '\n'.join(match_lines)) + code = code.replace('/* PRODUCTION_FN */', + function(acio, 'apple_cio_sleep_removes_domains')) + code = code.replace('/* ACIO_REGISTRATION */', pm_registration( + acio, 'acio->pm_notifier.notifier_call = apple_cio_pm_notify;', + 'register_pm_notifier(&acio->pm_notifier);')) + code = code.replace('/* DWC_REGISTRATION */', pm_registration( + dwc, 'appledwc->pm_nb.notifier_call = dwc3_apple_pm_notify;', + 'ret = register_pm_notifier(&appledwc->pm_nb);')) + muts = { + 't8122-drop-match': (' { "apple,t8122-usb4-acio", &apple_cio_t8122_hw },', ''), + 't8122-drop-sleep-removes-domains': ('\t.sleep_removes_domains = true,\n', ''), + 't8122-drop-phy-shared': ('\t.phy_shared_with_dwc3 = true,\n', ''), + } + cases = {'t8122-cio-sleep': code} + for name, (old, new) in muts.items(): + assert code.count(old) == 1, name + cases[name] = code.replace(old, new) + for name, body in cases.items(): + source, binary = out / (name + '.c'), out / name + source.write_text(body) + subprocess.run(['cc', '-std=gnu11', '-Wall', '-Wextra', '-Werror', + str(source), '-o', str(binary)], check=True) + run = subprocess.run([str(binary)], capture_output=True, text=True) + (out / (name + '.log')).write_text(run.stdout + run.stderr) + if name == 't8122-cio-sleep': + assert run.returncode == 0, run.stderr + print(run.stdout, end='') + else: + assert run.returncode < 0 and 'Assertion' in run.stderr, ( + name, run.returncode, run.stderr) + print('PASS negative control:', name) + + def main(): parser = argparse.ArgumentParser() parser.add_argument('--out', type=Path) @@ -743,6 +918,7 @@ def main(): out = args.out or Path(tmp) out.mkdir(parents=True, exist_ok=True) test_pm_order(out, acio) + test_t8122_cio_sleep(out, acio) cases = { 'production': code, 'm2-fallback': code.replace(