Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions arch/arm64/boot/dts/apple/t8112-j413-fw27.dts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
3 changes: 2 additions & 1 deletion drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
92 changes: 71 additions & 21 deletions drivers/pci/controller/pcie-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion drivers/phy/apple/atc-t6050-combo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
65 changes: 64 additions & 1 deletion drivers/thunderbolt/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

/*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions include/linux/pci-apple.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions include/linux/suspend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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) {}
Expand Down
14 changes: 14 additions & 0 deletions kernel/power/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down
Loading
Loading