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
66 changes: 66 additions & 0 deletions drivers/soc/apple/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,70 @@ void apple_sleep_apcie_ports_owned(bool owned)
}
EXPORT_SYMBOL_GPL(apple_sleep_apcie_ports_owned);

/*
* A driver that knows, before a sleep starts, that it would refuse a deep one. Kept out of
* struct apple_sleep so that driver may probe before this one.
*/
struct apple_sleep_deep_veto {
struct list_head node;
struct device *dev;
bool (*vetoes)(struct device *dev);
};

static LIST_HEAD(sleep_deep_vetoes);
static DEFINE_MUTEX(sleep_deep_veto_lock);

static void apple_sleep_deep_veto_drop(void *data)
{
struct apple_sleep_deep_veto *veto = data;

guard(mutex)(&sleep_deep_veto_lock);
list_del(&veto->node);
}

int devm_apple_sleep_deep_veto(struct device *dev, bool (*vetoes)(struct device *dev))
{
struct apple_sleep_deep_veto *veto;

veto = devm_kzalloc(dev, sizeof(*veto), GFP_KERNEL);
if (!veto)
return -ENOMEM;
veto->dev = dev;
veto->vetoes = vetoes;
scoped_guard(mutex, &sleep_deep_veto_lock)
list_add_tail(&veto->node, &sleep_deep_vetoes);

return devm_add_action_or_reset(dev, apple_sleep_deep_veto_drop, veto);
}
EXPORT_SYMBOL_GPL(devm_apple_sleep_deep_veto);

/*
* Asked by pm_suspend() for a deep sleep, before anything is frozen: what is known now to
* refuse it. That sleep then goes to s2idle, which needs none of it. What is found only
* later, as a device's suspend or the checks at .enter, still refuses the deep sleep.
*/
static bool apple_sleep_deep_vetoed(void)
{
struct apple_sleep *s = apple_sleep;
struct apple_sleep_deep_veto *veto;

/* The same check as .begin's, which stays for a host that leaves after this one. */
if (sleep_apcie_ports_orphaned(s)) {
dev_info(s->dev, "deep sleep vetoed: no host sets the PCIe ports up again after one\n");
return true;
}

guard(mutex)(&sleep_deep_veto_lock);
list_for_each_entry(veto, &sleep_deep_vetoes, node) {
if (!veto->vetoes || veto->vetoes(veto->dev)) {
dev_info(s->dev, "deep sleep vetoed by %s\n", dev_name(veto->dev));
return true;
}
}

return false;
}

/* One APCIe port's host set it up again after a real sleep: the next commit may stop it. */
void apple_sleep_apcie_port_up(unsigned int idx)
{
Expand Down Expand Up @@ -7917,6 +7981,7 @@ static const struct platform_suspend_ops apple_sleep_ops = {
.enter = apple_sleep_enter,
.wake = apple_sleep_report,
.end = apple_sleep_end,
.deep_vetoed = apple_sleep_deep_vetoed,
};

/* For a SoC with early_context: the same, with the scopes its entry needs held */
Expand All @@ -7930,6 +7995,7 @@ static const struct platform_suspend_ops apple_sleep_early_ops = {
.enter = apple_sleep_enter,
.wake = apple_sleep_report,
.end = apple_sleep_end,
.deep_vetoed = apple_sleep_deep_vetoed,
};

static int apple_sleep_read_wake_sources(struct apple_sleep *s)
Expand Down
34 changes: 34 additions & 0 deletions drivers/thunderbolt/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
#include <linux/soc/apple/dcp-v27-sleep.h>
#include <linux/soc/apple/pmgr-pwrstate.h>
#include <linux/soc/apple/rtkit.h>
#include <linux/soc/apple/sleep.h>
#include <linux/soc/apple/dp-tunnel.h>
#include <linux/soc/apple/tunable.h>
#include <linux/spinlock.h>
Expand Down Expand Up @@ -4647,6 +4648,32 @@ static int apple_cio_sleep_release_displays(struct apple_cio *acio, bool pause)
return hweight32(asked);
}

/*
* Asked before a deep sleep starts. The PCIe functions behind this port that the teardown
* can't remove (a driver it doesn't remove, or a disk in use) would have it refuse that sleep
* at PM_SUSPEND_PREPARE; s2idle keeps the port and its functions up.
*/
static bool apple_cio_deep_vetoed(struct device *dev)
{
struct apple_cio *acio = dev_get_drvdata(dev);
struct platform_device *pcie;
int ret = 0;

guard(mutex)(&acio->lock);
if (acio->removing || acio->resume_recovery_failed || !acio->current_cable_info)
return false;
guard(mutex)(&acio->pcie_tunnel_lock);
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)
dev_info(dev, "PCIe devices behind this port can't be removed for a deep sleep (%d)\n",
ret);
return ret;
}

static int apple_cio_sleep_teardown(struct apple_cio *acio)
{
ktime_t start = ktime_get(), stop;
Expand Down Expand Up @@ -5196,6 +5223,13 @@ static int apple_cio_probe(struct platform_device *pdev)
if (!acio->hw)
return -EINVAL;

/* A deep sleep takes this port down, and its teardown may refuse that. */
if (acio->hw->sleep_removes_domains && !acio->hw->strict_power_cycle) {
ret = devm_apple_sleep_deep_veto(dev, apple_cio_deep_vetoed);
if (ret)
return ret;
}

acio->type7 = acio->hw->type7;
if (acio->type7 &&
of_property_count_u32_elems(acio->np, "apple,tunable-rc") <= 0)
Expand Down
13 changes: 13 additions & 0 deletions include/linux/soc/apple/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ void apple_sleep_coproc_unregister(struct device *dev);
*/
bool apple_sleep_request_deep_once(void);

/*
* Until @dev's driver unbinds: a deep sleep would be refused for @dev, as @vetoes says when
* asked before each one starts (always when NULL). Such a sleep goes to s2idle instead.
* @vetoes runs under a lock that the unbind also takes: it must not take device_lock(@dev).
*/
int devm_apple_sleep_deep_veto(struct device *dev, bool (*vetoes)(struct device *dev));

/*
* For a registered coprocessor: the last of its stop that may still refuse the sleep, run
* with interrupts off before any coprocessor's @last_stop. A negative return refuses, and the
Expand Down Expand Up @@ -402,6 +409,12 @@ static inline bool apple_sleep_request_deep_once(void)
return false;
}

static inline int devm_apple_sleep_deep_veto(struct device *dev,
bool (*vetoes)(struct device *dev))
{
return 0;
}

static inline void apple_sleep_coproc_unregister(struct device *dev)
{
}
Expand Down
10 changes: 10 additions & 0 deletions include/linux/suspend.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ typedef int __bitwise suspend_state_t;
* Called by the PM core if the suspending of devices fails.
* This callback is optional and should only be implemented by platforms
* which require special recovery actions in that situation.
*
* @deep_vetoed: Whether the platform already knows it would refuse the
* %PM_SUSPEND_MEM transition about to start, for example because a driver
* that cannot take part in it has said so.
* Called by pm_suspend() before tasks are frozen and before @begin(). If
* it returns true, the system suspends to idle instead, which needs nothing
* from the platform. A refusal found only once the transition has started
* still fails it.
* This callback is optional.
*/
struct platform_suspend_ops {
int (*valid)(suspend_state_t state);
Expand All @@ -129,6 +138,7 @@ struct platform_suspend_ops {
bool (*suspend_again)(void);
void (*end)(void);
void (*recover)(void);
bool (*deep_vetoed)(void);
};

struct platform_s2idle_ops {
Expand Down
7 changes: 7 additions & 0 deletions kernel/power/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,13 @@ int pm_suspend(suspend_state_t state)
if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
return -EINVAL;

/* Before anything is frozen: suspend-to-idle needs nothing from the platform. */
if (state == PM_SUSPEND_MEM && suspend_ops && suspend_ops->deep_vetoed &&
suspend_ops->deep_vetoed()) {
pr_info("deep sleep vetoed by the platform, suspending to idle\n");
state = PM_SUSPEND_TO_IDLE;
}

pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
error = enter_state(state);
dpm_save_errno(error);
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 @@ -690,6 +690,9 @@ def mutations(flag, names, base, expect=KILL):
'sleep-s2idle-fallback-tests.py': [([], PASS)]
+ mutations('--mutation', ('drop-fallback', 'drop-no-platform',
'fallback-any-error', 'drop-precheck-priority'), []),
# A deep sleep that a driver vetoes before it starts goes to s2idle instead.
'sleep-deep-veto-tests.py': [([], PASS)]
+ mutations('--mutation', ('core-ignores-veto', 'drivers-ignored', 'acio-silent'), []),
'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