From 30eef1aaf6a84c5c58fc06801d66bb37fe108438 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Thu, 3 Dec 2026 17:15:42 -0500 Subject: [PATCH 1/2] PM: suspend: fall back to s2idle when deep sleep is refused for retained PCIe-C When deep sleep is the default mem_sleep mode on Apple Silicon Macs whose deep sleep powers off CIO domains, a connected Thunderbolt device whose PCIe-C functions cannot be removed cleanly before sleep (such as an eGPU driven by nouveau, amdgpu, or snd_hda_intel, or an open external NVMe disk) causes apple_cio_pm_notify() to refuse PM_SUSPEND_PREPARE with -EOPNOTSUPP or -EBUSY. With s2idle as the default the same suspend succeeds because the USB4 router and PCIe-C link stay powered across s2idle. Add a PM_SUSPEND_FLAG_FALLBACK_S2IDLE flag checked by enter_state() when suspend_prepare(PM_SUSPEND_MEM) fails: if a notifier explicitly asked for an s2idle fallback, retry suspend_prepare() as PM_SUSPEND_TO_IDLE with PM_SUSPEND_FLAG_NO_PLATFORM set so the system suspends cleanly in s2idle while keeping mem_sleep_current at deep for subsequent suspends once the device is unplugged. Unflagged PM_SUSPEND_PREPARE errors are still returned unchanged. Add a non-destructive apple_pcie_tunnel_can_remove_orderly() pre-check invoked from a priority-120 ACIO PM notifier (ahead of CD321x at 100 and port teardown at 40) so sibling ports are never torn down before discovering that another port must stay up. Also ensure the remaining Apple PM_SUSPEND_PREPARE notifiers that test pm_suspend_default_s2idle() respect pm_suspend_no_platform() for both explicit freeze and s2idle fallback. Add host tests and mutation negative controls in tools/asahi/sleep-s2idle-fallback-tests.{c,py}. Signed-off-by: Satya Benson --- drivers/gpu/drm/apple/dcp.c | 3 +- drivers/pci/controller/pcie-apple.c | 92 +++- drivers/phy/apple/atc-t6050-combo.h | 3 +- drivers/thunderbolt/apple.c | 65 ++- include/linux/pci-apple.h | 1 + include/linux/suspend.h | 20 + kernel/power/suspend.c | 14 + tools/asahi/run-host-tests.py | 3 + tools/asahi/sleep-s2idle-fallback-tests.c | 488 +++++++++++++++++++++ tools/asahi/sleep-s2idle-fallback-tests.py | 111 +++++ 10 files changed, 776 insertions(+), 24 deletions(-) create mode 100644 tools/asahi/sleep-s2idle-fallback-tests.c create mode 100755 tools/asahi/sleep-s2idle-fallback-tests.py diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 83aa944329063..b373fd671338a 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -12824,7 +12824,8 @@ static int dcp_s2r_pm_notify(struct notifier_block *nb, unsigned long action, vo * brought back. The processor stays as it is until a reboot. */ if (action == PM_SUSPEND_PREPARE && dcp_j714s_ext(dcp->dev) && - !pm_suspend_default_s2idle() && READ_ONCE(dcp->crashed)) { + !pm_suspend_default_s2idle() && !pm_suspend_no_platform() && + READ_ONCE(dcp->crashed)) { dev_warn(dcp->dev, "the external display processor has crashed: sleep refused before anything is stopped (a reboot brings it back)\n"); return notifier_from_errno(-EBUSY); } diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 84b06ded5acdd..c7a693a0f8f58 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -5316,7 +5316,7 @@ static int apple_pcie_neo_pm_notify(struct notifier_block *nb, unsigned long act switch (action) { case PM_SUSPEND_PREPARE: - if (pm_suspend_default_s2idle()) + if (pm_suspend_default_s2idle() || pm_suspend_no_platform()) break; flush_work(&pcie->neo_radio_work); flush_work(&pcie->neo_recover_work); @@ -7126,6 +7126,72 @@ static int apple_pcie_tunnel_other_driver(struct pci_dev *pdev, void *data) return 1; } +static int apple_pcie_tunnel_check_removable(struct device *dev, + struct pci_host_bridge *bridge, + bool in_use_too, + struct apple_pcie_tunnel_use *use) +{ + const char *other = NULL; + unsigned int tries; + + pci_walk_bus(bridge->bus, apple_pcie_tunnel_other_driver, &other); + if (other) { + dev_info(dev, "PCIe-C: a function driven by %s is not removed for the sleep\n", + other); + return -EOPNOTSUPP; + } + /* A probe by udev right after the disk appeared closes again within moments. */ + for (tries = 0; tries < 10; tries++) { + use->disks = 0; + use->open = 0; + pci_walk_bus(bridge->bus, apple_pcie_tunnel_function_disks, use); + if (!use->open || in_use_too) + break; + msleep(50); + } + if (use->open && !in_use_too) { + dev_warn(dev, "PCIe-C: %u of %u disk(s) behind the tunnel in use: not removed for the sleep\n", + use->open, use->disks); + return -EBUSY; + } + return 0; +} + +/** + * apple_pcie_tunnel_can_remove_orderly - check if a tunnelled hierarchy can be removed for sleep + * @dev: the tunnelled host + * @in_use_too: also allow removal when a disk below it is open + * + * Non-destructive pre-check run before any port is torn down for deep sleep. + * + * Returns 0 if the hierarchy can be removed (or is already stopped), -EBUSY when + * a disk is open and @in_use_too is false, or -EOPNOTSUPP when a function has a + * driver other than nvme, igc, or pcieport. + */ +int apple_pcie_tunnel_can_remove_orderly(struct device *dev, bool in_use_too) +{ + struct pci_host_bridge *bridge = dev_get_drvdata(dev); + struct apple_pcie_tunnel_use use = { .bridge = bridge }; + struct apple_pcie *pcie; + int ret; + + if (!bridge || !bridge->bus) + return -ENODEV; + pcie = pci_host_bridge_priv(bridge); + if (!pcie->hw->tunneled) + return -EINVAL; + + pci_lock_rescan_remove(); + if (pcie->bus_stopped) { + pci_unlock_rescan_remove(); + return 0; + } + ret = apple_pcie_tunnel_check_removable(dev, bridge, in_use_too, &use); + pci_unlock_rescan_remove(); + return ret; +} +EXPORT_SYMBOL_GPL(apple_pcie_tunnel_can_remove_orderly); + /** * apple_pcie_tunnel_remove_orderly - remove a tunnelled hierarchy that is still reachable * @dev: the tunnelled host @@ -7149,8 +7215,7 @@ int apple_pcie_tunnel_remove_orderly(struct device *dev, bool in_use_too) struct apple_pcie_tunnel_use use = { .bridge = bridge }; struct pci_dev *pdev, *tmp; struct apple_pcie *pcie; - const char *other = NULL; - unsigned int tries; + int ret; if (!bridge || !bridge->bus) return -ENODEV; @@ -7163,25 +7228,10 @@ int apple_pcie_tunnel_remove_orderly(struct device *dev, bool in_use_too) pci_unlock_rescan_remove(); return 0; } - pci_walk_bus(bridge->bus, apple_pcie_tunnel_other_driver, &other); - if (other) { - pci_unlock_rescan_remove(); - dev_info(dev, "PCIe-C: a function driven by %s is not removed for the sleep\n", other); - return -EOPNOTSUPP; - } - /* A probe by udev right after the disk appeared closes again within moments. */ - for (tries = 0; tries < 10; tries++) { - use.disks = use.open = 0; - pci_walk_bus(bridge->bus, apple_pcie_tunnel_function_disks, &use); - if (!use.open || in_use_too) - break; - msleep(50); - } - if (use.open && !in_use_too) { + ret = apple_pcie_tunnel_check_removable(dev, bridge, in_use_too, &use); + if (ret) { pci_unlock_rescan_remove(); - dev_warn(dev, "PCIe-C: %u of %u disk(s) behind the tunnel in use: not removed for the sleep\n", - use.open, use.disks); - return -EBUSY; + return ret; } list_for_each_entry_safe(pdev, tmp, &bridge->bus->devices, bus_list) pci_stop_and_remove_bus_device(pdev); diff --git a/drivers/phy/apple/atc-t6050-combo.h b/drivers/phy/apple/atc-t6050-combo.h index 83f97bc835668..fd2dac5915b75 100644 --- a/drivers/phy/apple/atc-t6050-combo.h +++ b/drivers/phy/apple/atc-t6050-combo.h @@ -474,7 +474,8 @@ static int combo_pm_notify(struct notifier_block *nb, unsigned long action, void guard(mutex)(&dp->lock); switch (action) { case PM_SUSPEND_PREPARE: - if (pm_suspend_default_s2idle() || dp->faulted || dp->aon_syscore || + if (pm_suspend_default_s2idle() || pm_suspend_no_platform() || + dp->faulted || dp->aon_syscore || dp->combo_mode == COMBO_SAFE || !dp->domain[0].active) break; if (!apple_sleep_usb_aon_keep(dp->port, true)) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 429cfa75e93bb..2213baf5786d9 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -624,6 +624,7 @@ struct apple_cio { u64 link_lost_cycle_activity; /* remote activity of this domain at the last cycle */ bool link_lost_restart; /* protected by lock: cycle requested */ struct notifier_block pm_notifier; + struct notifier_block pm_notifier_check; struct notifier_block reboot_notifier; /* * T6050: the displays behind this port go at the start of the sleep's entry, before any @@ -4536,7 +4537,8 @@ static bool apple_cio_t6050_sleep(struct apple_cio *acio) return (of_machine_is_compatible("apple,j714s") || of_machine_is_compatible("apple,j716c")) && READ_ONCE(t6050_sleep_teardown) && acio->hw == &apple_cio_t6050_hw && - apple_sleep_v27_late() && !pm_suspend_default_s2idle(); + apple_sleep_v27_late() && !pm_suspend_default_s2idle() && + !pm_suspend_no_platform(); } /* @@ -4670,6 +4672,13 @@ static int apple_cio_sleep_teardown(struct apple_cio *acio) put_device(&pcie->dev); } if (ret) { + if (removes && (ret == -EOPNOTSUPP || ret == -EBUSY)) { + dev_info(acio->dev, + "PCIe devices behind this port stay for the sleep (%d): falling back to s2idle\n", + ret); + pm_set_suspend_fallback_s2idle(); + return ret; + } dev_warn(acio->dev, "PCIe devices behind this port stay for the sleep (%d): the port is not powered off\n", ret); @@ -4784,6 +4793,55 @@ static bool apple_cio_tbt_switch_sleep_stop(struct typec_thunderbolt_switch_dev return apple_cio_sleep_tears_down(typec_thunderbolt_switch_get_drvdata(sw)); } +/* + * Non-destructive pre-check ahead of every port and PD teardown: if any port + * whose deep sleep removes CIO domains carries a PCIe-C function that cannot + * be removed cleanly (such as a Thunderbolt eGPU or an open disk), refuse deep + * sleep before sibling ports are touched and ask enter_state() to fall back to + * s2idle, where the tunnel and PCIe-C link stay up. + */ +static int apple_cio_check_pcie_sleep(struct apple_cio *acio) +{ + struct platform_device *pcie; + int ret = 0; + + guard(mutex)(&acio->lock); + if (acio->removing || acio->resume_recovery_failed || !acio->current_cable_info) + return 0; + + guard(mutex)(&acio->pcie_tunnel_lock); + if (!READ_ONCE(acio->pcie_tunnel_requested) && !acio->pcie_tunnel_populated) + return 0; + + pcie = apple_cio_find_pcie_tunnel(acio); + if (pcie) { + ret = apple_pcie_tunnel_can_remove_orderly(&pcie->dev, false); + put_device(&pcie->dev); + } + if (ret == -EOPNOTSUPP || ret == -EBUSY) { + dev_info(acio->dev, + "PCIe devices behind this port stay for the sleep (%d): falling back to s2idle\n", + ret); + pm_set_suspend_fallback_s2idle(); + return ret; + } + return 0; +} + +static int apple_cio_pm_check_notify(struct notifier_block *nb, + unsigned long event, void *unused) +{ + struct apple_cio *acio = container_of(nb, struct apple_cio, + pm_notifier_check); + int ret; + + if (event != PM_SUSPEND_PREPARE || !apple_cio_sleep_removes_domains(acio)) + return NOTIFY_DONE; + + ret = apple_cio_check_pcie_sleep(acio); + return ret ? notifier_from_errno(ret) : NOTIFY_DONE; +} + static int apple_cio_pm_notify(struct notifier_block *nb, unsigned long event, void *unused) { @@ -5349,6 +5407,10 @@ static int apple_cio_probe(struct platform_device *pdev) if (acio->hw->phy_shared_with_dwc3) acio->pm_notifier.priority = 40; register_pm_notifier(&acio->pm_notifier); + /* Ahead of CD321x (100) and port teardown (40): pre-check non-removable PCIe-C. */ + acio->pm_notifier_check.notifier_call = apple_cio_pm_check_notify; + acio->pm_notifier_check.priority = 120; + register_pm_notifier(&acio->pm_notifier_check); /* Before the notifiers of the default priority, after the port controllers' (30). */ acio->pm_notifier_dp.notifier_call = apple_cio_dp_pm_notify; acio->pm_notifier_dp.priority = 20; @@ -5390,6 +5452,7 @@ static void apple_cio_remove(struct platform_device *pdev) apple_pcie_tunnel_unregister_notifier(&acio->pcie_notifier); unregister_pm_notifier(&acio->pm_notifier_dp); + unregister_pm_notifier(&acio->pm_notifier_check); unregister_pm_notifier(&acio->pm_notifier); disable_work_sync(&acio->sleep_dp_undo_work); disable_delayed_work_sync(&acio->sleep_restart_work); diff --git a/include/linux/pci-apple.h b/include/linux/pci-apple.h index 769f4d6a0f6a5..6eb807d983e27 100644 --- a/include/linux/pci-apple.h +++ b/include/linux/pci-apple.h @@ -18,6 +18,7 @@ void apple_pcie_tunnel_unregister_notifier(struct notifier_block *nb); bool apple_pcie_tunnel_needs_cold_init(struct device_node *np); int apple_pcie_tunnel_prepare(struct device *dev, struct device_node *tunnel); int apple_pcie_tunnel_quiesce(struct device *dev); +int apple_pcie_tunnel_can_remove_orderly(struct device *dev, bool in_use_too); int apple_pcie_tunnel_remove_orderly(struct device *dev, bool in_use_too); int apple_pcie_tunnel_restore(struct device *dev); int apple_pcie_tunnel_check_state(struct device *dev); diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 2fcb352878229..1d0f4e5107b02 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -161,6 +161,7 @@ extern unsigned int pm_dark_wake_irq; #define PM_SUSPEND_FLAG_FW_RESUME BIT(1) #define PM_SUSPEND_FLAG_NO_PLATFORM BIT(2) #define PM_SUSPEND_FLAG_DARK_WAKE BIT(3) +#define PM_SUSPEND_FLAG_FALLBACK_S2IDLE BIT(4) static inline void pm_suspend_clear_flags(void) { @@ -195,6 +196,11 @@ static inline void pm_set_dark_wake(void) pm_set_dark_wake_irq(0); } +static inline void pm_set_suspend_fallback_s2idle(void) +{ + pm_suspend_global_flags |= PM_SUSPEND_FLAG_FALLBACK_S2IDLE; +} + void pm_consume_dark_wake(void); /** @@ -275,6 +281,18 @@ static inline bool pm_dark_wake(void) return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_DARK_WAKE); } +/** + * pm_suspend_fallback_s2idle - Check if a refused deep sleep asked for s2idle. + * + * Return 'true' if a PM_SUSPEND_PREPARE notifier refused a platform sleep + * (PM_SUSPEND_MEM) because a device must stay powered across sleep and + * requested that enter_state() retry the suspend as PM_SUSPEND_TO_IDLE. + */ +static inline bool pm_suspend_fallback_s2idle(void) +{ + return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FALLBACK_S2IDLE); +} + /* Suspend-to-idle state machnine. */ enum s2idle_states { S2IDLE_STATE_NONE, /* Not suspended/suspending. */ @@ -329,6 +347,8 @@ static inline void pm_set_dark_wake_irq(unsigned int irq) {} static inline void pm_set_dark_wake(void) {} static inline void pm_consume_dark_wake(void) {} static inline bool pm_dark_wake(void) { return false; } +static inline void pm_set_suspend_fallback_s2idle(void) {} +static inline bool pm_suspend_fallback_s2idle(void) { return false; } static inline bool pm_suspend_default_s2idle(void) { return false; } static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {} diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c index 93de42d73120d..1f3350e6bc2de 100644 --- a/kernel/power/suspend.c +++ b/kernel/power/suspend.c @@ -641,6 +641,20 @@ static int enter_state(suspend_state_t state) if (state == PM_SUSPEND_TO_IDLE) pm_set_suspend_no_platform(); error = suspend_prepare(state); + if (error && state == PM_SUSPEND_MEM && pm_suspend_fallback_s2idle()) { +#ifdef CONFIG_PM_DEBUG + if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { + pr_warn("Unsupported test mode for suspend to idle fallback, please choose none/freezer/devices/platform.\n"); + goto Unlock; + } +#endif + pr_info("deep sleep refused (%d), falling back to s2idle\n", error); + state = PM_SUSPEND_TO_IDLE; + s2idle_begin(); + pm_suspend_clear_flags(); + pm_set_suspend_no_platform(); + error = suspend_prepare(state); + } if (error) goto Unlock; diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index a805fbfd76ff0..e844f020fa820 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -686,6 +686,9 @@ def mutations(flag, names, base, expect=KILL): 't8132-apcie-sleep-tests.py': [([], PASS)], 't8132-display-gate-tests.py': [([], PASS)], 't8132-sleep-refuse-tests.py': [([], PASS)], + 'sleep-s2idle-fallback-tests.py': [([], PASS)] + + mutations('--mutation', ('drop-fallback', 'drop-no-platform', + 'fallback-any-error', 'drop-precheck-priority'), []), 't8132-usb4-policy-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 'tb-cio-sleep-tests.py': [(out('--output'), PASS)], 'tb-dock-pm-tests.py': [(out('--output'), PASS)], diff --git a/tools/asahi/sleep-s2idle-fallback-tests.c b/tools/asahi/sleep-s2idle-fallback-tests.c new file mode 100644 index 0000000000000..21137c7029dc1 --- /dev/null +++ b/tools/asahi/sleep-s2idle-fallback-tests.c @@ -0,0 +1,488 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Host test for deep-sleep -> s2idle fallback when a tunneled PCIe-C device + * (e.g. a Thunderbolt eGPU or an open external NVMe disk) cannot be removed. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define BIT(n) (1U << (n)) +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define container_of(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) + +#define PM_SUSPEND_ON 0 +#define PM_SUSPEND_TO_IDLE 1 +#define PM_SUSPEND_STANDBY 2 +#define PM_SUSPEND_MEM 3 +#define PM_SUSPEND_MAX 4 +typedef int suspend_state_t; + +#define PM_SUSPEND_PREPARE 0x0003 +#define PM_POST_SUSPEND 0x0004 + +#define NOTIFY_DONE 0x0000 +#define NOTIFY_OK 0x0001 +#define NOTIFY_STOP_MASK 0x8000 +#define NOTIFY_BAD (NOTIFY_STOP_MASK | 0x0002) + +static inline int notifier_from_errno(int err) +{ + if (err) + return NOTIFY_STOP_MASK | (NOTIFY_OK - err); + return NOTIFY_OK; +} + +static inline int notifier_to_errno(int ret) +{ + ret &= ~NOTIFY_STOP_MASK; + return ret > NOTIFY_OK ? NOTIFY_OK - ret : 0; +} + +#define PM_SUSPEND_FLAG_FW_SUSPEND BIT(0) +#define PM_SUSPEND_FLAG_FW_RESUME BIT(1) +#define PM_SUSPEND_FLAG_NO_PLATFORM BIT(2) +#define PM_SUSPEND_FLAG_DARK_WAKE BIT(3) +#define PM_SUSPEND_FLAG_FALLBACK_S2IDLE BIT(4) + +static unsigned int pm_suspend_global_flags; +static unsigned int pm_dark_wake_irq; +static suspend_state_t mem_sleep_current = PM_SUSPEND_MEM; + +static const char * const __attribute__((unused)) mem_sleep_labels[] = { + [PM_SUSPEND_TO_IDLE] = "s2idle", + [PM_SUSPEND_STANDBY] = "shallow", + [PM_SUSPEND_MEM] = "deep", +}; + +static inline void pm_suspend_clear_flags(void) +{ + pm_suspend_global_flags = 0; + pm_dark_wake_irq = 0; +} + +static inline void pm_set_suspend_no_platform(void) +{ + pm_suspend_global_flags |= PM_SUSPEND_FLAG_NO_PLATFORM; +} + +static inline bool pm_suspend_no_platform(void) +{ + return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_NO_PLATFORM); +} + +static inline void pm_set_suspend_fallback_s2idle(void) +{ + pm_suspend_global_flags |= PM_SUSPEND_FLAG_FALLBACK_S2IDLE; +} + +static inline bool pm_suspend_fallback_s2idle(void) +{ + return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FALLBACK_S2IDLE); +} + +static inline bool pm_dark_wake(void) +{ + return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_DARK_WAKE); +} + +static bool pm_suspend_default_s2idle(void) +{ + return mem_sleep_current == PM_SUSPEND_TO_IDLE; +} + +static void pr_info(const char *fmt, ...) { (void)fmt; } +static void pr_warn(const char *fmt, ...) { (void)fmt; } +static void pm_pr_dbg(const char *fmt, ...) { (void)fmt; } +static void dev_info(const void *d, const char *fmt, ...) { (void)d; (void)fmt; } +static void dev_warn(const void *d, const char *fmt, ...) { (void)d; (void)fmt; } +static const char *TPS(const char *x) { return x; } +static void trace_suspend_resume(const char *a, int b, bool c) +{ + (void)a; + (void)b; + (void)c; +} +#define TEST_FREEZER 1 + +struct device_driver { + const char *name; +}; + +struct device { + struct device_driver *driver; + void *drvdata; +}; + +static void *dev_get_drvdata(const struct device *dev) +{ + return dev->drvdata; +} + +static void put_device(struct device *dev) +{ + (void)dev; +} + +struct platform_device { + struct device dev; +}; + +struct pci_dev { + struct device dev; + unsigned int disks; + unsigned int open_disks; +}; + +struct pci_bus { + struct pci_dev *devs[4]; + unsigned int ndevs; +}; + +struct apple_pcie_hw { + bool tunneled; +}; + +struct apple_pcie { + const struct apple_pcie_hw *hw; + bool bus_stopped; +}; + +struct pci_host_bridge { + struct pci_bus *bus; + struct apple_pcie pcie; +}; + +static struct apple_pcie *pci_host_bridge_priv(struct pci_host_bridge *bridge) +{ + return &bridge->pcie; +} + +struct apple_pcie_tunnel_use { + struct pci_host_bridge *bridge; + unsigned int disks, open; +}; + +static int apple_pcie_tunnel_function_disks(struct pci_dev *pdev, void *data) +{ + struct apple_pcie_tunnel_use *use = data; + + use->disks += pdev->disks; + use->open += pdev->open_disks; + return 0; +} + +static void pci_walk_bus(struct pci_bus *bus, + int (*cb)(struct pci_dev *, void *), void *userdata) +{ + for (unsigned int i = 0; i < bus->ndevs; i++) + if (cb(bus->devs[i], userdata)) + break; +} + +static void pci_lock_rescan_remove(void) {} +static void pci_unlock_rescan_remove(void) {} +static void msleep(unsigned int ms) { (void)ms; } + +struct mutex { + int locked; +}; + +static void mutex_lock(struct mutex *m) { m->locked++; } +static void mutex_unlock(struct mutex *m) { m->locked--; } +static bool mutex_trylock(struct mutex *m) { m->locked++; return true; } + +static void __guard_mutex_cleanup(struct mutex **m) +{ + if (*m) + mutex_unlock(*m); +} + +#define __GUARD_CAT2(a, b) a##b +#define __GUARD_CAT(a, b) __GUARD_CAT2(a, b) +#define guard(type) \ + struct mutex *__GUARD_CAT(__guard_, __COUNTER__) \ + __attribute__((cleanup(__guard_mutex_cleanup))) = \ + __guard_mutex_lock +static struct mutex *__guard_mutex_lock(struct mutex *m) +{ + mutex_lock(m); + return m; +} + +struct notifier_block { + int (*notifier_call)(struct notifier_block *nb, + unsigned long event, void *data); + int priority; +}; + +struct apple_cio_hw { + bool sleep_removes_domains; +}; + +struct apple_cio { + struct device *dev; + const struct apple_cio_hw *hw; + struct mutex lock; + struct mutex pcie_tunnel_lock; + bool removing; + bool resume_recovery_failed; + unsigned int current_cable_info; + bool pcie_tunnel_requested; + bool pcie_tunnel_populated; + struct platform_device *pcie_pdev; + struct notifier_block pm_notifier; + struct notifier_block pm_notifier_check; + unsigned int teardown_calls; +}; + +static struct platform_device *apple_cio_find_pcie_tunnel(struct apple_cio *acio) +{ + return acio->pcie_tunnel_populated ? acio->pcie_pdev : NULL; +} + +/* ACTUAL_PCIE_FUNCTIONS */ + +/* ACTUAL_ACIO_FUNCTIONS */ + +/* Simulated PM notifier chain sorted descending by priority. */ +static struct notifier_block *pm_chain[16]; +static unsigned int pm_chain_len; + +static void pm_chain_reset(void) +{ + pm_chain_len = 0; +} + +static void pm_chain_register(struct notifier_block *nb) +{ + unsigned int i = pm_chain_len++; + + assert(pm_chain_len <= 16); + while (i > 0 && pm_chain[i - 1]->priority < nb->priority) { + pm_chain[i] = pm_chain[i - 1]; + i--; + } + pm_chain[i] = nb; +} + +static int pm_notifier_call_chain_robust(unsigned long val_up, + unsigned long val_down) +{ + unsigned int i; + + for (i = 0; i < pm_chain_len; i++) { + int ret = pm_chain[i]->notifier_call(pm_chain[i], val_up, NULL); + + if (ret & NOTIFY_STOP_MASK) { + while (i-- > 0) + pm_chain[i]->notifier_call(pm_chain[i], val_down, NULL); + return notifier_to_errno(ret); + } + } + return 0; +} + +static struct mutex system_transition_mutex; +static bool sync_on_suspend_enabled; +static bool events_check_enabled; +static unsigned int s2idle_begin_calls; +static suspend_state_t entered_state; +static bool entered_no_platform; +static unsigned int entered_count; + +static bool valid_state(suspend_state_t state) +{ + return state == PM_SUSPEND_MEM; +} + +static void s2idle_begin(void) +{ + s2idle_begin_calls++; +} + +static int pm_sleep_fs_sync(void) +{ + return 0; +} + +static int suspend_prepare(suspend_state_t state) +{ + (void)state; + return pm_notifier_call_chain_robust(PM_SUSPEND_PREPARE, PM_POST_SUSPEND); +} + +static bool suspend_test(int level) +{ + (void)level; + return false; +} + +static int suspend_devices_and_enter(suspend_state_t state) +{ + entered_state = state; + entered_no_platform = pm_suspend_no_platform(); + entered_count++; + return 0; +} + +static void suspend_finish(void) +{ + for (unsigned int i = 0; i < pm_chain_len; i++) + pm_chain[i]->notifier_call(pm_chain[i], PM_POST_SUSPEND, NULL); +} + +/* ACTUAL_ENTER_STATE */ + +static int fake_cio_teardown_notify(struct notifier_block *nb, + unsigned long event, void *unused) +{ + struct apple_cio *acio = container_of(nb, struct apple_cio, pm_notifier); + + (void)unused; + if (event != PM_SUSPEND_PREPARE || !apple_cio_sleep_removes_domains(acio)) + return NOTIFY_DONE; + acio->teardown_calls++; + return NOTIFY_DONE; +} + +static int unrelated_err; +static int fake_unrelated_notify(struct notifier_block *nb, + unsigned long event, void *unused) +{ + (void)nb; + (void)unused; + /* Deep-only refusal that did not ask for s2idle fallback (e.g. hardware error). */ + if (event == PM_SUSPEND_PREPARE && !pm_suspend_no_platform() && unrelated_err) + return notifier_from_errno(unrelated_err); + return NOTIFY_DONE; +} + +static void reset_run(struct apple_cio *p0, struct apple_cio *p1) +{ + p0->teardown_calls = 0; + p1->teardown_calls = 0; + s2idle_begin_calls = 0; + entered_state = PM_SUSPEND_ON; + entered_no_platform = false; + entered_count = 0; + unrelated_err = 0; + mem_sleep_current = PM_SUSPEND_MEM; +} + +int main(void) +{ + static const struct apple_cio_hw t8112_hw = { .sleep_removes_domains = true }; + static const struct apple_pcie_hw tunneled_hw = { .tunneled = true }; + struct device_driver drv_nvme = { .name = "nvme" }; + struct device_driver drv_nouveau = { .name = "nouveau" }; + struct device_driver drv_hda = { .name = "snd_hda_intel" }; + struct device dev0 = {}, dev1 = {}; + + struct pci_dev nvme_dev = { .dev = { .driver = &drv_nvme }, .disks = 1, .open_disks = 0 }; + struct pci_dev egpu_dev = { .dev = { .driver = &drv_nouveau } }; + struct pci_dev hda_dev = { .dev = { .driver = &drv_hda } }; + + struct pci_bus bus0 = { .devs = { &nvme_dev }, .ndevs = 1 }; + struct pci_bus bus1 = { .devs = { &egpu_dev }, .ndevs = 1 }; + struct pci_host_bridge br0 = { .bus = &bus0, .pcie = { .hw = &tunneled_hw } }; + struct pci_host_bridge br1 = { .bus = &bus1, .pcie = { .hw = &tunneled_hw } }; + struct platform_device pcie0 = { .dev = { .drvdata = &br0 } }; + struct platform_device pcie1 = { .dev = { .drvdata = &br1 } }; + + struct apple_cio port0 = { + .dev = &dev0, .hw = &t8112_hw, + .current_cable_info = 1, + .pcie_tunnel_requested = true, .pcie_tunnel_populated = true, + .pcie_pdev = &pcie0, + }; + struct apple_cio port1 = { + .dev = &dev1, .hw = &t8112_hw, + .current_cable_info = 1, + .pcie_tunnel_requested = true, .pcie_tunnel_populated = true, + .pcie_pdev = &pcie1, + }; + struct notifier_block unrelated_nb = { + .notifier_call = fake_unrelated_notify, + .priority = 10, + }; + + port0.pm_notifier.notifier_call = fake_cio_teardown_notify; + port0.pm_notifier.priority = 40; + port0.pm_notifier_check.notifier_call = apple_cio_pm_check_notify; + port0.pm_notifier_check.priority = CHECK_PRIORITY; + + port1.pm_notifier.notifier_call = fake_cio_teardown_notify; + port1.pm_notifier.priority = 40; + port1.pm_notifier_check.notifier_call = apple_cio_pm_check_notify; + port1.pm_notifier_check.priority = CHECK_PRIORITY; + + pm_chain_reset(); + pm_chain_register(&port0.pm_notifier); + pm_chain_register(&port1.pm_notifier); + pm_chain_register(&port0.pm_notifier_check); + pm_chain_register(&port1.pm_notifier_check); + pm_chain_register(&unrelated_nb); + + /* 1. Multi-port: Port 0 idle NVMe + Port 1 eGPU (nouveau) -> s2idle, 0 teardowns. */ + reset_run(&port0, &port1); + assert(enter_state(PM_SUSPEND_MEM) == 0); + assert(entered_count == 1); + assert(entered_state == PM_SUSPEND_TO_IDLE); + assert(entered_no_platform); + assert(s2idle_begin_calls == 1); + assert(port0.teardown_calls == 0); + assert(port1.teardown_calls == 0); + assert(mem_sleep_current == PM_SUSPEND_MEM); + + /* 2. GPU HDMI-audio function (snd_hda_intel) alone also falls back to s2idle. */ + bus1.devs[0] = &hda_dev; + reset_run(&port0, &port1); + assert(enter_state(PM_SUSPEND_MEM) == 0); + assert(entered_state == PM_SUSPEND_TO_IDLE); + assert(entered_no_platform); + assert(port0.teardown_calls == 0 && port1.teardown_calls == 0); + + /* 3. Mounted/open TB NVMe disk (-EBUSY) falls back to s2idle without tearing down ports. */ + port1.current_cable_info = 0; + nvme_dev.open_disks = 1; + reset_run(&port0, &port1); + assert(enter_state(PM_SUSPEND_MEM) == 0); + assert(entered_state == PM_SUSPEND_TO_IDLE); + assert(entered_no_platform); + assert(port0.teardown_calls == 0 && port1.teardown_calls == 0); + + /* 4. Once eGPU is unplugged and disk closed, deep sleep proceeds normally. */ + nvme_dev.open_disks = 0; + reset_run(&port0, &port1); + assert(enter_state(PM_SUSPEND_MEM) == 0); + assert(entered_count == 1); + assert(entered_state == PM_SUSPEND_MEM); + assert(!entered_no_platform); + assert(s2idle_begin_calls == 0); + assert(port0.teardown_calls == 1); + + /* 5. Explicit freeze goes straight to s2idle with zero teardowns. */ + reset_run(&port0, &port1); + assert(enter_state(PM_SUSPEND_TO_IDLE) == 0); + assert(entered_state == PM_SUSPEND_TO_IDLE); + assert(entered_no_platform); + assert(s2idle_begin_calls == 1); + assert(port0.teardown_calls == 0); + + /* 6. Unrelated PM notifier error does NOT silently fall back to s2idle. */ + reset_run(&port0, &port1); + unrelated_err = -EBUSY; + assert(enter_state(PM_SUSPEND_MEM) == -EBUSY); + assert(entered_count == 0); + + puts("PASS: deep-refused s2idle fallback (eGPU, open TB disk, multi-port, errors)"); + return 0; +} diff --git a/tools/asahi/sleep-s2idle-fallback-tests.py b/tools/asahi/sleep-s2idle-fallback-tests.py new file mode 100755 index 0000000000000..08c3f5f57e7e0 --- /dev/null +++ b/tools/asahi/sleep-s2idle-fallback-tests.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Exercise deep-refused -> s2idle fallback with actual suspend/ACIO/PCIe-C bodies.""" +import argparse +from pathlib import Path +import re +import resource +import subprocess +import sys +import tempfile + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from sleep_test_utils import function # noqa: E402 + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +ROOT = Path(__file__).resolve().parents[2] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--mutation", choices=( + "drop-fallback", + "drop-no-platform", + "fallback-any-error", + "drop-precheck-priority", + )) + args = parser.parse_args() + + suspend = (ROOT / "kernel/power/suspend.c").read_text() + pcie = (ROOT / "drivers/pci/controller/pcie-apple.c").read_text() + acio = (ROOT / "drivers/thunderbolt/apple.c").read_text() + dcp = (ROOT / "drivers/gpu/drm/apple/dcp.c").read_text() + combo = (ROOT / "drivers/phy/apple/atc-t6050-combo.h").read_text() + + # Check notifier priority in production apple.c: pre-check > CD321x (100) > ACIO (40). + prio_m = re.search( + r"acio->pm_notifier_check\.priority\s*=\s*(\d+);", acio + ) + assert prio_m, "pm_notifier_check.priority not found" + check_prio = int(prio_m.group(1)) + assert check_prio > 100, f"pre-check priority {check_prio} must exceed CD321x (100)" + + # Every Apple PM_SUSPEND_PREPARE notifier checking pm_suspend_default_s2idle() + # must also respect pm_suspend_no_platform() for freeze and s2idle fallback. + for name, body in ( + ("apple_pcie_neo_pm_notify", function(pcie, "apple_pcie_neo_pm_notify")), + ("dcp_s2r_pm_notify", function(dcp, "dcp_s2r_pm_notify")), + ("combo_pm_notify", function(combo, "combo_pm_notify")), + ("apple_cio_t6050_sleep", function(acio, "apple_cio_t6050_sleep")), + ): + assert "pm_suspend_no_platform()" in body, f"{name} ignores pm_suspend_no_platform()" + + pcie_fns = "\n".join( + function(pcie, name) + for name in ( + "apple_pcie_tunnel_other_driver", + "apple_pcie_tunnel_check_removable", + "apple_pcie_tunnel_can_remove_orderly", + ) + ) + acio_fns = "\n".join( + function(acio, name) + for name in ( + "apple_cio_sleep_removes_domains", + "apple_cio_check_pcie_sleep", + "apple_cio_pm_check_notify", + ) + ) + enter_fn = function(suspend, "enter_state") + + if args.mutation == "drop-fallback": + old = "if (error && state == PM_SUSPEND_MEM && pm_suspend_fallback_s2idle()) {" + assert enter_fn.count(old) == 1 + enter_fn = enter_fn.replace(old, "if (false) {") + elif args.mutation == "drop-no-platform": + old = "\t\tpm_set_suspend_no_platform();\n\t\terror = suspend_prepare(state);" + assert enter_fn.count(old) == 1 + enter_fn = enter_fn.replace(old, "\t\terror = suspend_prepare(state);") + elif args.mutation == "fallback-any-error": + old = "if (error && state == PM_SUSPEND_MEM && pm_suspend_fallback_s2idle()) {" + assert enter_fn.count(old) == 1 + enter_fn = enter_fn.replace(old, "if (error && state == PM_SUSPEND_MEM) {") + elif args.mutation == "drop-precheck-priority": + check_prio = 20 + + fixture = Path(__file__).with_suffix(".c").read_text() + code = ( + fixture + .replace("/* ACTUAL_PCIE_FUNCTIONS */", pcie_fns) + .replace("/* ACTUAL_ACIO_FUNCTIONS */", acio_fns) + .replace("/* ACTUAL_ENTER_STATE */", enter_fn) + ) + with tempfile.TemporaryDirectory(prefix="s2idle-fallback-") as tmp: + c, exe = Path(tmp) / "test.c", Path(tmp) / "test" + c.write_text(code) + subprocess.run( + [ + "cc", "-std=gnu11", "-Wall", "-Wextra", "-Werror", + "-Wno-unused-function", "-Wno-unused-parameter", + "-DEXPORT_SYMBOL_GPL(sym)=", + f"-DCHECK_PRIORITY={check_prio}", + str(c), "-o", str(exe), + ], + check=True, + ) + run = subprocess.run([str(exe)]) + return run.returncode + + +if __name__ == "__main__": + sys.exit(main()) From 1f7f71c8cdf153d9fff106019de18dccb7e17b47 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Thu, 3 Dec 2026 17:15:42 -0500 Subject: [PATCH 2/2] arm64: dts: apple: t8112-j413-fw27: Enable deep sleep by default Add apple,sleep-tested to the 27.2 J413 (MacBook Air 13" M2) &sleep node so deep sleep is offered and selected by default without apple_sleep.deep=1 or apple_sleep.untested_commit=1, following the J414s/J613/J615 per-board pattern. Keep t8112_sleep_soc.untested set so J473 (Mac mini M2, where deep sleep commit resets in #4226), J415, J493, and legacy 13.5 J413 firmware stay on s2idle by default. Signed-off-by: Satya Benson --- arch/arm64/boot/dts/apple/t8112-j413-fw27.dts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t8112-j413-fw27.dts b/arch/arm64/boot/dts/apple/t8112-j413-fw27.dts index c81560b529253..31e2ab9d9a9c2 100644 --- a/arch/arm64/boot/dts/apple/t8112-j413-fw27.dts +++ b/arch/arm64/boot/dts/apple/t8112-j413-fw27.dts @@ -3,3 +3,8 @@ #include "t8112-j413.dts" #include "t8112-mtp-fw27.dtsi" #include "t8112-ane-fw27.dtsi" + +/* Through full sleeps and wakes: deep sleep by default on 27.2 J413. */ +&sleep { + apple,sleep-tested; +};