diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index e6eaa106f88a1..8faa261ad895a 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -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) { @@ -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 */ @@ -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) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index ba1b57f03d76f..fcb4602e8cc38 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -111,6 +111,7 @@ #include #include #include +#include #include #include #include @@ -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; @@ -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) diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 39ab320931c91..1e4a52eb81c46 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -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 @@ -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) { } diff --git a/include/linux/suspend.h b/include/linux/suspend.h index 1d0f4e5107b02..49f8d3a6acc83 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -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); @@ -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 { diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c index 1f3350e6bc2de..d7a66ed40ecdd 100644 --- a/kernel/power/suspend.c +++ b/kernel/power/suspend.c @@ -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); diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index c0da74fb2a288..ca46a2970c078 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -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)], diff --git a/tools/asahi/sleep-deep-veto-tests.c b/tools/asahi/sleep-deep-veto-tests.c new file mode 100644 index 0000000000000..455a618e959ff --- /dev/null +++ b/tools/asahi/sleep-deep-veto-tests.c @@ -0,0 +1,349 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * A deep sleep that a driver vetoes before it starts: pm_suspend() takes s2idle for that one + * sleep, and deep again once nothing vetoes. The PM core, the sleep driver's veto list and the + * USB4 port's check are the production functions; the rest is stubbed. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef int suspend_state_t; +#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 +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) +#define GFP_KERNEL 0 +#define EXPORT_SYMBOL(x) +#define EXPORT_SYMBOL_GPL(x) +#define __maybe_unused __attribute__((unused)) +#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) + +struct list_head { + struct list_head *next, *prev; +}; + +#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } + +static void list_add_tail(struct list_head *node, struct list_head *head) +{ + node->prev = head->prev; + node->next = head; + head->prev->next = node; + head->prev = node; +} + +static void list_del(struct list_head *node) +{ + node->prev->next = node->next; + node->next->prev = node->prev; + node->next = NULL; + node->prev = NULL; +} + +static bool list_empty(const struct list_head *head) +{ + return head->next == head; +} + +#define list_for_each_entry(pos, head, member) \ + for (pos = container_of((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = container_of(pos->member.next, typeof(*pos), member)) + +/* A mutex taken twice, or released when free, is a bug in the code under test. */ +struct mutex { + bool held; +}; + +#define DEFINE_MUTEX(name) struct mutex name + +static struct mutex *mutex_guard_take(struct mutex *lock) +{ + assert(!lock->held); + lock->held = true; + return lock; +} + +static void mutex_guard_release(struct mutex **lock) +{ + assert((*lock)->held); + (*lock)->held = false; +} + +#define GUARD_NAME_(n) guard_##n +#define GUARD_NAME(n) GUARD_NAME_(n) +#define guard(type) \ + struct type *__attribute__((cleanup(type##_guard_release))) \ + GUARD_NAME(__COUNTER__) = type##_guard_take +#define scoped_guard(type, lock) \ + for (struct type *scope_ = type##_guard_take(lock); scope_; \ + type##_guard_release(&scope_), scope_ = NULL) + +/* A device with what devres needs: its allocations and actions, undone at unbind. */ +struct device { + const char *name; + void *drvdata; + void *allocs[4]; + unsigned int nallocs; + void (*actions[4])(void *data); + void *action_data[4]; + unsigned int nactions; +}; + +struct platform_device { + struct device dev; +}; + +static const char *dev_name(const struct device *dev) +{ + return dev->name; +} + +static void *dev_get_drvdata(const struct device *dev) +{ + return dev->drvdata; +} + +static void *devm_kzalloc(struct device *dev, size_t size, int gfp) +{ + void *p = calloc(1, size); + + (void)gfp; + assert(p && dev->nallocs < 4); + dev->allocs[dev->nallocs++] = p; + return p; +} + +static int devm_add_action_or_reset(struct device *dev, void (*action)(void *data), void *data) +{ + assert(dev->nactions < 4); + dev->actions[dev->nactions] = action; + dev->action_data[dev->nactions++] = data; + return 0; +} + +static void unbind(struct device *dev) +{ + while (dev->nactions--) + dev->actions[dev->nactions](dev->action_data[dev->nactions]); + dev->nactions = 0; + while (dev->nallocs--) + free(dev->allocs[dev->nallocs]); + dev->nallocs = 0; +} + +static char logged[4096]; + +static void log_line(const char *prefix, const char *fmt, ...) +{ + size_t len = strlen(logged); + va_list ap; + + va_start(ap, fmt); + snprintf(logged + len, sizeof(logged) - len, "%s: ", prefix); + len = strlen(logged); + vsnprintf(logged + len, sizeof(logged) - len, fmt, ap); + va_end(ap); +} + +#define pr_info(fmt, ...) log_line("PM", fmt, ##__VA_ARGS__) +#define dev_info(dev, fmt, ...) log_line(dev_name(dev), fmt, ##__VA_ARGS__) + +/* The PM core around pm_suspend(). */ +struct platform_suspend_ops { + bool (*deep_vetoed)(void); +}; + +static const char *const mem_sleep_labels[] = { + [PM_SUSPEND_TO_IDLE] = "s2idle", + [PM_SUSPEND_STANDBY] = "shallow", + [PM_SUSPEND_MEM] = "deep", +}; + +static const struct platform_suspend_ops *suspend_ops __maybe_unused; +static suspend_state_t entered = PM_SUSPEND_ON; +static int enter_error, saved_errno = 1; + +static int enter_state(suspend_state_t state) +{ + entered = state; + return enter_error; +} + +static void dpm_save_errno(int error) +{ + saved_errno = error; +} + +/* The sleep driver. */ +struct apple_sleep_soc { + bool apcie_ports_host; +}; + +struct apple_sleep { + const struct apple_sleep_soc *soc; + struct device *dev; + bool apcie_ports_unowned; +}; + +static struct apple_sleep *apple_sleep; +static LIST_HEAD(sleep_deep_vetoes); +static DEFINE_MUTEX(sleep_deep_veto_lock); +static bool apcie_ports_host_owned; +static unsigned int asked; + +/* A USB4 port. */ +struct apple_cio { + struct mutex lock, pcie_tunnel_lock; + bool removing, resume_recovery_failed; + u32 current_cable_info; + bool pcie_tunnel_requested, pcie_tunnel_populated; +}; + +static struct platform_device tunnel_host = { .dev = { .name = "530000000.pcie" } }; +static bool tunnel_host_found = true; +static int tunnel_removable = 0, tunnel_refs; +static struct apple_cio *tunnel_acio; + +static struct platform_device *apple_cio_find_pcie_tunnel(struct apple_cio *acio) +{ + /* Under both of the port's locks, as the teardown looks it up. */ + assert(acio->lock.held && acio->pcie_tunnel_lock.held); + if (!acio->pcie_tunnel_populated || !tunnel_host_found) + return NULL; + tunnel_refs++; + return &tunnel_host; +} + +static void put_device(struct device *dev) +{ + assert(dev == &tunnel_host.dev && tunnel_refs > 0); + tunnel_refs--; +} + +static int apple_pcie_tunnel_can_remove_orderly(struct device *dev, bool in_use_too) +{ + assert(dev == &tunnel_host.dev && !in_use_too); + assert(tunnel_acio && tunnel_acio->lock.held && tunnel_acio->pcie_tunnel_lock.held); + return tunnel_removable; +} + +/* ACTUAL_FUNCTIONS */ + +static bool counted_vetoed(void) +{ + asked++; + return apple_sleep_deep_vetoed(); +} + +static const struct platform_suspend_ops apple_ops = { .deep_vetoed = counted_vetoed }; +static const struct platform_suspend_ops other_ops = { 0 }; + +#define sleep_as(state, expect, why) sleep_as_at(state, expect, why, __LINE__) + +static void sleep_as_at(suspend_state_t state, suspend_state_t expect, const char *why, int line) +{ + char want[64]; + + logged[0] = '\0'; + entered = PM_SUSPEND_ON; + assert(pm_suspend(state) == enter_error); + assert(saved_errno == enter_error); + snprintf(want, sizeof(want), "PM: suspend entry (%s)\n", mem_sleep_labels[expect]); + if (entered != expect || !strstr(logged, want)) { + fprintf(stderr, "FAIL line %d: %s: asked %s, entered %s\n%s", line, why, + mem_sleep_labels[state], mem_sleep_labels[entered], logged); + abort(); + } + assert(!strstr(logged, "vetoed") == (expect == state)); + printf("ok - %s\n", why); +} + +int main(void) +{ + static const struct apple_sleep_soc t8132 = { .apcie_ports_host = true }; + static const struct apple_sleep_soc other = { 0 }; + struct device sleep_dev = { .name = "23d2b0000.sleep" }; + struct device pmp = { .name = "2a0000000.pmp" }; + struct device port = { .name = "501ac0000.cio" }; + struct apple_cio acio = { .current_cable_info = 0x1 }; + struct apple_sleep s = { .soc = &other, .dev = &sleep_dev }; + + /* A platform without the callback: deep stays deep. */ + suspend_ops = &other_ops; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "no callback, deep"); + + /* A driver that knows at its probe, before the sleep driver's: it still counts. */ + assert(devm_apple_sleep_deep_veto(&pmp, NULL) == 0); + apple_sleep = &s; + suspend_ops = &apple_ops; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_TO_IDLE, "static veto: s2idle instead of deep"); + assert(strstr(logged, "deep sleep vetoed by 2a0000000.pmp")); + + /* A sleep that isn't deep asks nothing and stays what it was. */ + asked = 0; + sleep_as(PM_SUSPEND_TO_IDLE, PM_SUSPEND_TO_IDLE, "s2idle stays s2idle"); + sleep_as(PM_SUSPEND_STANDBY, PM_SUSPEND_STANDBY, "shallow stays shallow"); + assert(!asked); + + /* An error from the s2idle sleep is still the caller's. */ + enter_error = -EBUSY; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_TO_IDLE, "s2idle error returned"); + enter_error = 0; + + /* Unbound, it vetoes no more. */ + unbind(&pmp); + assert(list_empty(&sleep_deep_vetoes)); + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "veto gone: deep again"); + + /* T8132 with no host that sets its PCIe ports up again after a deep sleep. */ + s.soc = &t8132; + s.apcie_ports_unowned = true; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_TO_IDLE, "orphaned PCIe ports: s2idle"); + apple_sleep_apcie_ports_owned(true); + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "host owns the ports: deep"); + s.soc = &other; + + /* A USB4 port asks its PCIe tunnel each time. */ + port.drvdata = &acio; + tunnel_acio = &acio; + assert(devm_apple_sleep_deep_veto(&port, apple_cio_deep_vetoed) == 0); + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "port, no PCIe tunnel: deep"); + acio.pcie_tunnel_populated = true; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "port, removable tunnel: deep"); + tunnel_removable = -EOPNOTSUPP; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_TO_IDLE, "eGPU behind the port: s2idle"); + assert(strstr(logged, "deep sleep vetoed by 501ac0000.cio")); + tunnel_removable = -EBUSY; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_TO_IDLE, "disk in use behind the port: s2idle"); + acio.pcie_tunnel_populated = false; + acio.pcie_tunnel_requested = true; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "tunnel asked for, not up yet: deep"); + acio.pcie_tunnel_populated = true; + tunnel_host_found = false; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "no tunnelled host: deep"); + tunnel_host_found = true; + acio.current_cable_info = 0; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "nothing plugged in: deep"); + acio.current_cable_info = 0x1; + acio.removing = true; + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "port going away: deep"); + acio.removing = false; + unbind(&port); + assert(list_empty(&sleep_deep_vetoes)); + sleep_as(PM_SUSPEND_MEM, PM_SUSPEND_MEM, "port unbound: deep"); + + assert(!tunnel_refs && !acio.lock.held && !acio.pcie_tunnel_lock.held); + return 0; +} diff --git a/tools/asahi/sleep-deep-veto-tests.py b/tools/asahi/sleep-deep-veto-tests.py new file mode 100755 index 0000000000000..761a9b72d4736 --- /dev/null +++ b/tools/asahi/sleep-deep-veto-tests.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Build and run a deep sleep that a driver vetoes before it starts: "mem" goes to s2idle.""" +from pathlib import Path +import argparse +import re +import resource +import subprocess +import sys +import tempfile + +# python3 -I, as run-host-tests.py runs it, leaves this directory off sys.path. +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from sleep_test_utils import function, structure # noqa: E402 + +MUTATIONS = { + # The PM core takes deep sleep whatever the platform says. + "core-ignores-veto": ("pm_suspend", "state = PM_SUSPEND_TO_IDLE;", ";"), + # Only the sleep driver's own check counts, no driver's veto. + "drivers-ignored": ("apple_sleep_deep_vetoed", "veto->vetoes(veto->dev)", "false"), + # A port whose PCIe functions can't be removed doesn't say so. + "acio-silent": ("apple_cio_deep_vetoed", "return ret;", "return false;"), +} + +parser = argparse.ArgumentParser() +parser.add_argument("--mutation", choices=sorted(MUTATIONS)) +args = parser.parse_args() + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +root = Path(__file__).resolve().parents[2] +sleep = (root / "drivers/soc/apple/sleep.c").read_text() +core = (root / "kernel/power/suspend.c").read_text() +acio = (root / "drivers/thunderbolt/apple.c").read_text() + +functions = {name: function(core, name) for name in ("pm_suspend",)} +functions |= { + name: function(sleep, name) + for name in ( + "sleep_apcie_ports_orphaned", + "apple_sleep_apcie_ports_owned", + "apple_sleep_deep_veto_drop", + "devm_apple_sleep_deep_veto", + "apple_sleep_deep_vetoed", + ) +} +functions["apple_cio_deep_vetoed"] = function(acio, "apple_cio_deep_vetoed") + +if args.mutation: + name, old, new = MUTATIONS[args.mutation] + assert functions[name].count(old) == 1, args.mutation + functions[name] = functions[name].replace(old, new) + +# Both of the sleep driver's tables answer the PM core. +for table in ("apple_sleep_ops", "apple_sleep_early_ops"): + body = re.search( + r"^static const struct platform_suspend_ops " + table + r" = \{(.*?)^\};", + sleep, + re.M | re.S, + ) + assert body and ".deep_vetoed = apple_sleep_deep_vetoed," in body[1], table + +# Only a port that a deep sleep takes down, and whose own driver stops it, asks. +assert re.search( + r"if \(acio->hw->sleep_removes_domains && !acio->hw->strict_power_cycle\) \{\s*" + r"ret = devm_apple_sleep_deep_veto\(dev, apple_cio_deep_vetoed\);", + function(acio, "apple_cio_probe"), +) + +fixture = Path(__file__).with_suffix(".c").read_text() +actual = structure(sleep, "apple_sleep_deep_veto") + "".join(functions.values()) +with tempfile.TemporaryDirectory(prefix="sleep-deep-veto-") as tmp: + c, exe = Path(tmp) / "test.c", Path(tmp) / "test" + c.write_text(fixture.replace("/* ACTUAL_FUNCTIONS */", actual)) + subprocess.run( + ["cc", "-std=gnu11", "-Wall", "-Werror", str(c), "-o", str(exe)], + check=True, + ) + # A failed check prints its line; the exit status says it failed. + sys.exit(1 if subprocess.run([str(exe)]).returncode else 0)