From 689f2f91cc94da196482d1717a89604c6fac14c2 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:42:11 -0600 Subject: [PATCH 01/22] soc: apple: vote GPU activity to the PMP so memory leaves idle DVFS on t600x On the M1 Max (t6001) the PMP keeps memory at its idle floor for a busy GPU until the AGX power request, bit 9 of the PMP report's PS-REQ word, is set. Nothing sets it on Linux, so GPU reads top out at 241 GB/s. The same chip reads 361 GB/s on macOS. Report GPU activity to the PMP. The GPU driver calls apple_pmp_agx_activity_begin() when a submission starts and apple_pmp_agx_activity_end() when it finishes. pmp-report sets bit 9 when the first operation starts and clears it 100 ms after the last one ends. It also clears it at once when the GPU driver unbinds or the report goes away, so the request is never left set past the GPU's power-domain release. The register sequence is the one apple_pmp_report_set_state() already uses for report entries: read 0xf80, write 0x107c0, poll 0x1000, status 0x10, 50 ms acknowledge. A set that is not acknowledged is cleared again and not retried for a second. The vote is off by default. pmp_report.agx_vote=1 turns it on, and pmp_report.agx_vote_idle_ms (default 100) sets the hold. With it off nothing is written. It applies to apple,t6001 only; t6000 is untested. The decisions (set, clear, hold, retry, wrap of the 32-bit millisecond clock) are plain functions in pmp-agx-vote.h with no kernel dependency. Setting the bit by hand with a test module on a 16-inch M1 Max moved a GPU read microbenchmark from 241 to 368 GB/s. This patch has not yet been run on hardware. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/gpu/drm/asahi/driver.rs | 3 + drivers/gpu/drm/asahi/gpu.rs | 5 + drivers/pmdomain/apple/pmp-agx-vote.h | 122 ++++++++++++++++++ drivers/pmdomain/apple/pmp-report.c | 172 ++++++++++++++++++++++++++ include/linux/soc/apple/pmp-report.h | 20 +++ rust/bindings/bindings_helper.h | 1 + 6 files changed, 323 insertions(+) create mode 100644 drivers/pmdomain/apple/pmp-agx-vote.h diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index 80582ece5d2391..6eff6f06bf4338 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -231,6 +231,9 @@ impl platform::Driver for AsahiPlatformDriver { _pdev: &'bound platform::Device>, this: Pin<&Self::Data<'bound>>, ) { + // SAFETY: Takes no arguments, process context. Clears the T6001 AGX request in the PMP report if the pmp_report.agx_vote + // parameter set it; a no-op everywhere else. + unsafe { bindings::apple_pmp_agx_activity_release() }; if let Some(gpu) = this.g17.as_ref() { // Driver data is dropped after devres, so CPU control must happen in unbind. gpu.shutdown(); diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index 84d82bb1880ef2..60acb7a2d5b376 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -1284,6 +1284,9 @@ impl GpuManager::ver { .with(|raw, _inner| raw.pending_submissions.fetch_add(1, Ordering::Acquire)); mod_dev_dbg!(self.dev, "OP start (pending: {})\n", val + 1); + // SAFETY: Takes no arguments and does nothing unless pmp_report.agx_vote is set on a T6001. The matching end call is made + // by OpGuard's Drop, and the guard is created right below, so every path out of here (including a failed kick) pairs it. + unsafe { kernel::bindings::apple_pmp_agx_activity_begin() }; let guard = OpGuard(self.clone()); self.kick_firmware()?; Ok(guard) @@ -1804,5 +1807,7 @@ impl GpuManagerPriv for GpuManager::ver { .with(|raw, _inner| raw.pending_submissions.fetch_sub(1, Ordering::Release)); mod_dev_dbg!(self.dev, "OP end (pending: {})\n", val - 1); + // SAFETY: Pairs with the begin call in start_op(); callable from any context. + unsafe { kernel::bindings::apple_pmp_agx_activity_end() }; } } diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h new file mode 100644 index 00000000000000..70ad71c514e395 --- /dev/null +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * When the T6001 AGX request (PS-REQ bit 9 of the PMP report) is set and cleared. + * + * The GPU driver counts operations in flight (begin/end). The request is set when the first operation starts and cleared + * idle_ms after the last one ends, so a decode stream (a burst every ~10 ms) holds one vote and an idle GPU drops it. All state + * changes are in these functions; the caller serializes them with one spinlock and does the register write between + * agx_vote_decide() and agx_vote_applied(). Times are milliseconds from any wrapping clock. + */ +#ifndef PMP_AGX_VOTE_H +#define PMP_AGX_VOTE_H + +#ifdef __KERNEL__ +#include +/* jiffies_to_msecs() returns u32: the clock wraps every 49.7 days, and every compare below is done in 32 bits. */ +typedef u32 agx_ms_t; +typedef s32 agx_sms_t; +#else +#include +#include +typedef uint32_t agx_ms_t; +typedef int32_t agx_sms_t; +#endif + +/* After a set that was not acknowledged (or a PMP that is not running), wait this long before trying again. */ +#define AGX_VOTE_RETRY_MS 1000 + +struct agx_vote { + bool enabled; /* the module parameter */ + bool voted; /* bit 9 is set and was acknowledged */ + unsigned int users; /* GPU operations in flight */ + agx_ms_t idle_ms; /* keep the vote this long after the last operation ends */ + agx_ms_t idle_at; /* when users last fell to 0 */ + unsigned int failures; /* consecutive failed sets; the backoff applies only while this is nonzero */ + agx_ms_t retry_at; /* with failures: no new set before this */ +}; + +enum agx_vote_action { + AGX_VOTE_NONE, + AGX_VOTE_SET, + AGX_VOTE_CLEAR, +}; + +static inline bool agx_vote_reached(agx_ms_t now, agx_ms_t t) +{ + return (agx_sms_t)(now - t) >= 0; +} + +/* An operation starts. True: run the reconcile step now. */ +static inline bool agx_vote_begin(struct agx_vote *v) +{ + v->users++; + return v->enabled && !v->voted; +} + +/* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */ +static inline agx_ms_t agx_vote_end(struct agx_vote *v, agx_ms_t now) +{ + if (!v->users) /* unbalanced end: ignored */ + return 0; + if (--v->users) + return 0; + v->idle_at = now; + if (!v->enabled || !v->voted) + return 0; + return v->idle_ms ? v->idle_ms : 1; +} + +/* What the reconcile step must do now. */ +static inline enum agx_vote_action agx_vote_decide(const struct agx_vote *v, agx_ms_t now) +{ + if (!v->enabled) + return v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE; + if (v->users) + return !v->voted && (!v->failures || agx_vote_reached(now, v->retry_at)) ? AGX_VOTE_SET : AGX_VOTE_NONE; + if (v->voted && agx_vote_reached(now, v->idle_at + v->idle_ms)) + return AGX_VOTE_CLEAR; + return AGX_VOTE_NONE; +} + +/* Delay in ms until the reconcile step must run again, 0 when no timer is needed. */ +static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now) +{ + agx_ms_t at; + + if (!v->enabled) + return 0; + if (v->users && !v->voted && v->failures) + at = v->retry_at; + else if (!v->users && v->voted) + at = v->idle_at + v->idle_ms; + else + return 0; + return agx_vote_reached(now, at) ? 0 : at - now; +} + +/* Record the outcome of the register write for `action` (ret 0: acknowledged). */ +static inline void agx_vote_applied(struct agx_vote *v, enum agx_vote_action action, int ret, agx_ms_t now) +{ + if (action == AGX_VOTE_SET) { + if (ret) { + v->failures++; + v->retry_at = now + AGX_VOTE_RETRY_MS; + } else { + v->voted = true; + v->failures = 0; + } + } else if (action == AGX_VOTE_CLEAR) { + v->voted = false; /* the write was made; the caller logs an unacknowledged clear */ + } +} + +/* The GPU driver unbinds: drop the vote now. True when bit 9 is set and the caller must write the clear. */ +static inline bool agx_vote_release(struct agx_vote *v) +{ + bool was_voted = v->voted; + + v->voted = false; + return was_voted; +} + +#endif /* PMP_AGX_VOTE_H */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 9aac3ced17cda1..fe2aa14788b11b 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -31,6 +31,7 @@ #include #include +#include "pmp-agx-vote.h" #include "pmp-report-validation.h" #define PMP_REPORT_READY 0x1 @@ -80,6 +81,18 @@ struct apple_pmp_report { }; static const struct apple_pmp_report_offsets apple_pmp_offsets_t6030; +static const struct apple_pmp_report_offsets apple_pmp_offsets_t600x; + +/* T6001 AGX request (see the engine below set_state): AGX is soc-device record 9 on the T6001. */ +#define APPLE_PMP_T6001_AGX_ID 9 +static bool agx_vote; +static struct agx_vote apple_pmp_agx_vote_state = {.idle_ms = 100}; +static DEFINE_SPINLOCK(apple_pmp_agx_vote_lock); +/* Serializes the vote work, the unbind release and the report's removal. */ +static DEFINE_MUTEX(apple_pmp_agx_vote_mutex); +static void apple_pmp_agx_vote_fn(struct work_struct *work); +static DECLARE_DELAYED_WORK(apple_pmp_agx_vote_work, apple_pmp_agx_vote_fn); +static void apple_pmp_agx_vote_stop(void); /* Bound report regions, protected by RCU until managed teardown drains readers. */ static LIST_HEAD(apple_pmp_reports); @@ -168,6 +181,11 @@ static void apple_pmp_report_release(void *data) /* Withdraw child providers while their power callbacks can still use MMIO. */ of_platform_depopulate(rep->dev); + if (rep->offsets == &apple_pmp_offsets_t600x) { + cancel_delayed_work_sync(&apple_pmp_agx_vote_work); + guard(mutex)(&apple_pmp_agx_vote_mutex); + apple_pmp_agx_vote_stop(); + } mutex_lock(&apple_pmp_reports_lock); if (!list_empty(&rep->list)) list_del_rcu(&rep->list); @@ -1295,6 +1313,160 @@ static int apple_pmp_report_set_state(struct apple_pmp_report *rep, u32 id, 50000); } +/* + * T6001 AGX request. T2 showed that setting PS-REQ bit 9 (AGX, soc-device record 9) lifts the GPU read roof from 241 to 368 GB/s: the PMP + * leaves memory at its idle floor for a GPU that is busy but has not asked. The GPU driver reports operations in flight + * (apple_pmp_agx_activity_begin/end); the request is set while any operation runs and cleared agx_vote_idle_ms after the last one ends, at + * GPU unbind and when this report goes away. The decisions are in pmp-agx-vote.h (unit tested); this is the glue. + * Off by default (pmp_report.agx_vote): with it off nothing is written. + */ +static unsigned int agx_vote_idle_ms = 100; + +static agx_ms_t apple_pmp_agx_now_ms(void) +{ + return jiffies_to_msecs(jiffies); +} + +static void apple_pmp_agx_vote_kick(unsigned long delay_ms) +{ + mod_delayed_work(system_wq, &apple_pmp_agx_vote_work, msecs_to_jiffies(delay_ms)); +} + +static int apple_pmp_agx_vote_param_set(const char *val, const struct kernel_param *kp) +{ + int ret = param_set_bool(val, kp); + + if (!ret) + apple_pmp_agx_vote_kick(0); /* switching off clears at once, switching on votes if the GPU is busy */ + return ret; +} + +static const struct kernel_param_ops apple_pmp_agx_vote_ops = { + .set = apple_pmp_agx_vote_param_set, + .get = param_get_bool, +}; +module_param_cb(agx_vote, &apple_pmp_agx_vote_ops, &agx_vote, 0644); +MODULE_PARM_DESC(agx_vote, "T6001: set the AGX power request in the PMP report while the GPU has work in flight (default: off)"); +module_param(agx_vote_idle_ms, uint, 0644); +MODULE_PARM_DESC(agx_vote_idle_ms, "Keep the AGX request this many ms after the last GPU operation ends (default: 100)"); + +/* Write the AGX request of the bound T6001 report: -ENODEV without one, -EAGAIN (nothing written) when the PMP does not run. */ +static int apple_pmp_agx_write(bool enable, bool no_ack) +{ + struct apple_pmp_report *rep; + int ret = -ENODEV; + + if (!of_machine_is_compatible("apple,t6001")) + return ret; + rcu_read_lock(); + list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { + if (rep->offsets != &apple_pmp_offsets_t600x) + continue; + if (enable && !(readq(rep->base + rep->offsets->status) & PMP_REPORT_READY)) + ret = -EAGAIN; + else + ret = apple_pmp_report_set_state(rep, APPLE_PMP_T6001_AGX_ID, enable, no_ack); + break; + } + rcu_read_unlock(); + return ret; +} + +/* Clear a request that is still set. Caller holds apple_pmp_agx_vote_mutex. */ +static void apple_pmp_agx_vote_stop(void) +{ + unsigned long flags; + bool voted; + + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + voted = agx_vote_release(&apple_pmp_agx_vote_state); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + if (voted) + apple_pmp_agx_write(false, true); +} + +static void apple_pmp_agx_vote_fn(struct work_struct *work) +{ + struct agx_vote *v = &apple_pmp_agx_vote_state; + enum agx_vote_action act; + unsigned long flags; + agx_ms_t now, next = 0; + int ret = 0; + + guard(mutex)(&apple_pmp_agx_vote_mutex); + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + v->enabled = READ_ONCE(agx_vote); + v->idle_ms = READ_ONCE(agx_vote_idle_ms); + now = apple_pmp_agx_now_ms(); + act = agx_vote_decide(v, now); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + + if (act != AGX_VOTE_NONE) { + const bool set = act == AGX_VOTE_SET; + + ret = apple_pmp_agx_write(set, false); + if (ret == -ETIMEDOUT) { + pr_err("pmp_report: AGX request %s not acknowledged, clearing\n", set ? "set" : "clear"); + apple_pmp_agx_write(false, true); + } + if (!set) + ret = 0; /* a clear is final: the state is "not voted" whatever happened */ + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + agx_vote_applied(v, act, ret, now); + next = agx_vote_next(v, apple_pmp_agx_now_ms()); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + } else { + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + next = agx_vote_next(v, now); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + } + if (next) + apple_pmp_agx_vote_kick(next); +} + +void apple_pmp_agx_activity_begin(void) +{ + struct agx_vote *v = &apple_pmp_agx_vote_state; + unsigned long flags; + bool kick; + + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + v->enabled = READ_ONCE(agx_vote); + kick = agx_vote_begin(v); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + if (kick) + apple_pmp_agx_vote_kick(0); +} +EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_begin); + +void apple_pmp_agx_activity_end(void) +{ + struct agx_vote *v = &apple_pmp_agx_vote_state; + unsigned long flags; + agx_ms_t delay; + + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + v->enabled = READ_ONCE(agx_vote); + v->idle_ms = READ_ONCE(agx_vote_idle_ms); + delay = agx_vote_end(v, apple_pmp_agx_now_ms()); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + if (delay) + apple_pmp_agx_vote_kick(delay); +} +EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_end); + +/* + * The GPU driver unbinds: clear the request now instead of waiting out the idle time, so it is never left set past the GPU's + * power-domain release (the T6030 fix clears before it releases its vote). Process context. + */ +void apple_pmp_agx_activity_release(void) +{ + cancel_delayed_work_sync(&apple_pmp_agx_vote_work); + guard(mutex)(&apple_pmp_agx_vote_mutex); + apple_pmp_agx_vote_stop(); +} +EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_release); + /* Find and check the report before taking the GPU vote; optionally update it. */ static int apple_pmp_report_t6030_gfx(struct device *supplier, const bool *enable) { diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 1d0689f31c2039..7fb5327a7b9646 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -21,7 +21,27 @@ int apple_pmp_report_set_t6030_gfx(struct device *supplier, bool enable); int apple_pmp_report_wait_ready(struct device_node *entry, unsigned long timeout); int apple_pmp_report_fast_die_effort(const struct device_node *np, unsigned int lane, u8 *effort); +/* + * T6001: the GPU driver reports each operation in flight. With the pmp_report.agx_vote parameter set, the AGX request (PS-REQ bit 9) is + * set while any operation runs and cleared agx_vote_idle_ms after the last one ends. Parameter off: no effect. Both may be called from + * any context. release() clears the request at once (GPU driver unbind), process context. + */ +void apple_pmp_agx_activity_begin(void); +void apple_pmp_agx_activity_end(void); +void apple_pmp_agx_activity_release(void); #else +static inline void apple_pmp_agx_activity_begin(void) +{ +} + +static inline void apple_pmp_agx_activity_end(void) +{ +} + +static inline void apple_pmp_agx_activity_release(void) +{ +} + static inline int apple_pmp_report_wait_supplier_ready(struct device *supplier, unsigned int timeout_ms) { diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index f981774335c7ca..15046badff5f37 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -102,6 +102,7 @@ #include #include #include +#include #include #include #include From f7ad8cffcacb77bcba84adbfb67965e8ea865bb0 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:56:22 -0600 Subject: [PATCH 02/22] pmdomain: apple: pmp-report: export a locked, acknowledged update of one power request bit The PS-REQ word of the PMP report is shared by every client of the PMP: the report entries (display, ISP, video decode, the AFNC IOAs), the T6001 AGX vote (bit 9) and, out of tree, the ANE clock level of omarchy-ane (bit 10). The report entries and the AGX vote read-modify-write the word under rep->lock. A module outside this driver could only map the window and read-modify-write it without that lock, so a concurrent write of another bit could be lost. apple_pmp_report_set_request(report_np, id, enable, no_ack) is the one writer a client outside this file may use. It calls the same apple_pmp_report_set_state() the entries use, so it takes the same lock and does the same acknowledge poll (at most 50 ms). The caller owns the bit it asks for. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-report.c | 43 ++++++++++++++++++++++++++++ include/linux/soc/apple/pmp-report.h | 12 ++++++++ 2 files changed, 55 insertions(+) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index fe2aa14788b11b..915191daa21f92 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -1523,6 +1523,49 @@ int apple_pmp_report_set_t6030_gfx(struct device *supplier, bool enable) } EXPORT_SYMBOL_GPL(apple_pmp_report_set_t6030_gfx); +/** + * apple_pmp_report_set_request() - set or clear one bit of the PMP power request word of a bound report + * @report_np: device tree node of the report region (compatible apple,t6000-pmp-v2-report and its + * siblings) + * @id: bit number of the request, the index of the soc-device record, 0 to 63 (ANE0 is 10 and AGX + * is 9 on the T6001) + * @enable: set or clear the bit + * @no_ack: do not wait for the acknowledge + * + * One read-modify-write of the word, made under the report's lock: the same lock every report + * entry and the AGX vote take, so requests for different bits cannot lose each other. Unless + * @no_ack, waits (atomic poll, at most 50 ms) until the acknowledge word shows the bit as + * requested. A PMP that does not run has nothing to acknowledge: the request is written and 0 is + * returned, as for a report entry. + * + * The caller owns the bit it requests; this function does not arbitrate between callers of the + * same bit. On the T6001 bit 9 belongs to the AGX vote (pmp_report.agx_vote), so other callers + * must not use it. + * + * Context: process context (the acknowledge wait is a busy poll of up to 50 ms). + * Return: 0, -ENODEV if no report is bound to @report_np, -EINVAL if @id is out of range, + * -ETIMEDOUT if the acknowledge did not follow. + */ +int apple_pmp_report_set_request(const struct device_node *report_np, unsigned int id, + bool enable, bool no_ack) +{ + struct apple_pmp_report *rep; + int ret = -ENODEV; + + if (id > 63) + return -EINVAL; + rcu_read_lock(); + list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { + if (rep->dev->of_node == report_np) { + ret = apple_pmp_report_set_state(rep, id, enable, no_ack); + break; + } + } + rcu_read_unlock(); + return ret; +} +EXPORT_SYMBOL_GPL(apple_pmp_report_set_request); + static int apple_pmp_report_entry_set_state(struct generic_pm_domain *genpd, bool enable) { struct apple_pmp_report_entry *ent = genpd_to_apple_pmp_report_entry(genpd); diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 7fb5327a7b9646..345848c91dbc36 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -21,6 +21,12 @@ int apple_pmp_report_set_t6030_gfx(struct device *supplier, bool enable); int apple_pmp_report_wait_ready(struct device_node *entry, unsigned long timeout); int apple_pmp_report_fast_die_effort(const struct device_node *np, unsigned int lane, u8 *effort); +/* + * Set or clear one bit of the PMP power request word of the report bound to @report_np, under the + * report's lock, with the acknowledge wait (see pmp-report.c). + */ +int apple_pmp_report_set_request(const struct device_node *report_np, unsigned int id, + bool enable, bool no_ack); /* * T6001: the GPU driver reports each operation in flight. With the pmp_report.agx_vote parameter set, the AGX request (PS-REQ bit 9) is * set while any operation runs and cleared agx_vote_idle_ms after the last one ends. Parameter off: no effect. Both may be called from @@ -69,6 +75,12 @@ static inline int apple_pmp_report_fast_die_effort(const struct device_node *np, { return -ENODEV; } + +static inline int apple_pmp_report_set_request(const struct device_node *report_np, + unsigned int id, bool enable, bool no_ack) +{ + return -ENODEV; +} #endif #endif /* _LINUX_SOC_APPLE_PMP_REPORT_H_ */ From e033631c7dddcd8cd47b8efde090d0ac3e7e9a5e Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:13:48 -0600 Subject: [PATCH 03/22] pmdomain: apple: pmp-report: keep the AGX bit out of the export, document clearing apple_pmp_report_set_request() lets a client outside pmp-report update one bit of the PS-REQ word. On the T6001 bit 9 is the AGX request, which the GPU vote owns: a second writer could clear it while the GPU works, or set it and leave it set. Refuse it from the export with -EBUSY on a t600x report. The vote itself writes through apple_pmp_report_set_state() and is not affected. Document what a caller must do after -ETIMEDOUT: the request was written but not acknowledged and stays in the word, so the caller clears it with @enable false and @no_ack true before giving up. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-report.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 915191daa21f92..b96b48cbbd68be 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -1539,12 +1539,13 @@ EXPORT_SYMBOL_GPL(apple_pmp_report_set_t6030_gfx); * returned, as for a report entry. * * The caller owns the bit it requests; this function does not arbitrate between callers of the - * same bit. On the T6001 bit 9 belongs to the AGX vote (pmp_report.agx_vote), so other callers - * must not use it. + * same bit. On the T6001 bit 9 belongs to the AGX vote (pmp_report.agx_vote) and is refused here + * with -EBUSY. After -ETIMEDOUT the request was written but not acknowledged and stays in the + * word: the caller must clear it (@enable false, @no_ack true) before giving up. * * Context: process context (the acknowledge wait is a busy poll of up to 50 ms). * Return: 0, -ENODEV if no report is bound to @report_np, -EINVAL if @id is out of range, - * -ETIMEDOUT if the acknowledge did not follow. + * -EBUSY for the T6001 AGX bit, -ETIMEDOUT if the acknowledge did not follow. */ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned int id, bool enable, bool no_ack) @@ -1557,7 +1558,10 @@ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned i rcu_read_lock(); list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { if (rep->dev->of_node == report_np) { - ret = apple_pmp_report_set_state(rep, id, enable, no_ack); + if (id == APPLE_PMP_T6001_AGX_ID && rep->offsets == &apple_pmp_offsets_t600x) + ret = -EBUSY; /* the AGX vote owns this bit */ + else + ret = apple_pmp_report_set_state(rep, id, enable, no_ack); break; } } From 5b54de318780297d6a4ebdc11e24bc39f4132029 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:32:58 -0600 Subject: [PATCH 04/22] pmdomain: apple: pmp-report: hold the AGX request 250 ms after the last operation A 9B model at 8192 tokens of context had one gap of up to 111.5 ms between two decoded tokens in every process, once per run, with the GPU idle for a good part of it. The 100 ms default hold dropped the request in that gap, which costs one slow token while the PMP raises memory again. The longest gap in any other configuration measured was 64 ms. Raise the default to 250 ms, about 2.2 times the longest gap seen. The cost is a few hundred milliseconds of the higher memory state after the last job. pmp_report.agx_vote_idle_ms still overrides it at run time. The default is one constant, AGX_VOTE_IDLE_MS_DEFAULT, used for both the parameter and the initial state. The userspace test has a case that rides through a 112 ms gap with the default and still drops the request after a long idle. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 4 ++++ drivers/pmdomain/apple/pmp-report.c | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 70ad71c514e395..715ab63e424943 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -22,6 +22,10 @@ typedef uint32_t agx_ms_t; typedef int32_t agx_sms_t; #endif +/* How long the request is held after the last operation ends. w7X measured idle gaps of up to 111.5 ms inside one 9B generation, so 100 ms + * was not enough; 250 ms is about 2.2 times the largest gap seen. The parameter pmp_report.agx_vote_idle_ms overrides it at run time. */ +#define AGX_VOTE_IDLE_MS_DEFAULT 250 + /* After a set that was not acknowledged (or a PMP that is not running), wait this long before trying again. */ #define AGX_VOTE_RETRY_MS 1000 diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index b96b48cbbd68be..6d795310b139e5 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -86,7 +86,7 @@ static const struct apple_pmp_report_offsets apple_pmp_offsets_t600x; /* T6001 AGX request (see the engine below set_state): AGX is soc-device record 9 on the T6001. */ #define APPLE_PMP_T6001_AGX_ID 9 static bool agx_vote; -static struct agx_vote apple_pmp_agx_vote_state = {.idle_ms = 100}; +static struct agx_vote apple_pmp_agx_vote_state = {.idle_ms = AGX_VOTE_IDLE_MS_DEFAULT}; static DEFINE_SPINLOCK(apple_pmp_agx_vote_lock); /* Serializes the vote work, the unbind release and the report's removal. */ static DEFINE_MUTEX(apple_pmp_agx_vote_mutex); @@ -1320,7 +1320,7 @@ static int apple_pmp_report_set_state(struct apple_pmp_report *rep, u32 id, * GPU unbind and when this report goes away. The decisions are in pmp-agx-vote.h (unit tested); this is the glue. * Off by default (pmp_report.agx_vote): with it off nothing is written. */ -static unsigned int agx_vote_idle_ms = 100; +static unsigned int agx_vote_idle_ms = AGX_VOTE_IDLE_MS_DEFAULT; static agx_ms_t apple_pmp_agx_now_ms(void) { @@ -1348,7 +1348,7 @@ static const struct kernel_param_ops apple_pmp_agx_vote_ops = { module_param_cb(agx_vote, &apple_pmp_agx_vote_ops, &agx_vote, 0644); MODULE_PARM_DESC(agx_vote, "T6001: set the AGX power request in the PMP report while the GPU has work in flight (default: off)"); module_param(agx_vote_idle_ms, uint, 0644); -MODULE_PARM_DESC(agx_vote_idle_ms, "Keep the AGX request this many ms after the last GPU operation ends (default: 100)"); +MODULE_PARM_DESC(agx_vote_idle_ms, "Keep the AGX request this many ms after the last GPU operation ends (default: 250)"); /* Write the AGX request of the bound T6001 report: -ENODEV without one, -EAGAIN (nothing written) when the PMP does not run. */ static int apple_pmp_agx_write(bool enable, bool no_ack) From 6ece0825c8ebd47ac8762c02039e83e05d97d11b Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 06:57:35 -0600 Subject: [PATCH 05/22] pmdomain: apple: pmp-report: close the races in the AGX vote and stop it across unbind and sleep Review of the AGX vote found four problems. 1. agx_vote=1 on the kernel command line runs the parameter setter from parse_args(), before workqueue_init_early() has created the workqueues, and the setter queued work on a NULL workqueue. kick() now returns while system_dfl_wq does not exist; begin() and the work function read the parameter themselves, so the first GPU operation votes. It also uses system_dfl_wq instead of the deprecated system_wq. 2. An operation that began while the clear was being written saw voted still true and did not kick; after the clear nothing scheduled a set, so the GPU ran at the idle floor until the next begin. agx_vote_next() now reports a set that is owed (1 ms) when users > 0, the request is not voted and no set has failed. 3. release() did not stop a later begin() from setting the bit again, and the report's removal had the same shape. A stopped flag, set under the vote lock at release and at report removal, is checked by begin(), decide() and next(); it is cleared by a new apple_pmp_agx_activity_resume() called at GPU probe and at report probe. 4. The request is withdrawn on PM_SUSPEND_PREPARE and PM_HIBERNATION_PREPARE and decided again on PM_POST_SUSPEND and PM_POST_HIBERNATION: holding the bit through s2idle may cost power (a held display request cost about 0.25 W on J514S), and the PMP may drop the word across sleep, which would leave voted true with nothing on the wire. Each case has a test in tools/ptd-vote of the research repository that carries the policy header (cases 14 to 17 of the vote simulator, and a source check for the boot-time kick); they were written to fail first. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/gpu/drm/asahi/driver.rs | 3 ++ drivers/pmdomain/apple/pmp-agx-vote.h | 33 ++++++++++-- drivers/pmdomain/apple/pmp-report.c | 72 ++++++++++++++++++++++++++- include/linux/soc/apple/pmp-report.h | 5 ++ 4 files changed, 107 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/asahi/driver.rs b/drivers/gpu/drm/asahi/driver.rs index 6eff6f06bf4338..afce767ac3e3c8 100644 --- a/drivers/gpu/drm/asahi/driver.rs +++ b/drivers/gpu/drm/asahi/driver.rs @@ -252,6 +252,9 @@ impl platform::Driver for AsahiPlatformDriver { ) -> impl PinInit, Error> + 'bound { debug::update_debug_flags(); + // SAFETY: Takes no arguments, process context. Lets the T6001 AGX vote run again after an earlier unbind released it; a no-op everywhere else. + unsafe { bindings::apple_pmp_agx_activity_resume() }; + dev_info!(pdev.as_ref(), "Probing...\n"); let cfg = match info.ok_or(ENODEV)? { diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 715ab63e424943..64d8f3f199d09b 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -32,6 +32,7 @@ typedef int32_t agx_sms_t; struct agx_vote { bool enabled; /* the module parameter */ bool voted; /* bit 9 is set and was acknowledged */ + bool stopped; /* released (GPU unbind, report removal, sleep): no new vote until agx_vote_resume() */ unsigned int users; /* GPU operations in flight */ agx_ms_t idle_ms; /* keep the vote this long after the last operation ends */ agx_ms_t idle_at; /* when users last fell to 0 */ @@ -54,7 +55,7 @@ static inline bool agx_vote_reached(agx_ms_t now, agx_ms_t t) static inline bool agx_vote_begin(struct agx_vote *v) { v->users++; - return v->enabled && !v->voted; + return v->enabled && !v->voted && !v->stopped; } /* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */ @@ -73,7 +74,7 @@ static inline agx_ms_t agx_vote_end(struct agx_vote *v, agx_ms_t now) /* What the reconcile step must do now. */ static inline enum agx_vote_action agx_vote_decide(const struct agx_vote *v, agx_ms_t now) { - if (!v->enabled) + if (!v->enabled || v->stopped) return v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE; if (v->users) return !v->voted && (!v->failures || agx_vote_reached(now, v->retry_at)) ? AGX_VOTE_SET : AGX_VOTE_NONE; @@ -82,13 +83,18 @@ static inline enum agx_vote_action agx_vote_decide(const struct agx_vote *v, agx return AGX_VOTE_NONE; } -/* Delay in ms until the reconcile step must run again, 0 when no timer is needed. */ +/* + * Delay in ms until the reconcile step must run again, 0 when no timer is needed. 1 when a set is owed right now: an operation began while a + * clear was being written (begin saw voted still true and did not kick), so after the clear nothing would schedule the set. + */ static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now) { agx_ms_t at; - if (!v->enabled) + if (!v->enabled || v->stopped) return 0; + if (v->users && !v->voted && !v->failures) + return 1; if (v->users && !v->voted && v->failures) at = v->retry_at; else if (!v->users && v->voted) @@ -114,13 +120,30 @@ static inline void agx_vote_applied(struct agx_vote *v, enum agx_vote_action act } } -/* The GPU driver unbinds: drop the vote now. True when bit 9 is set and the caller must write the clear. */ +/* + * The GPU driver unbinds, the report goes away, or the system sleeps: drop the vote now and refuse new ones. True when bit 9 is set and the caller must + * write the clear. An operation that begins afterwards (an ioctl already past the unplug check) counts as a user but does not vote. + */ static inline bool agx_vote_release(struct agx_vote *v) { bool was_voted = v->voted; v->voted = false; + v->stopped = true; return was_voted; } +/* Refuse new votes without touching the one that is set (the first step of a release, before the work item is waited out). */ +static inline void agx_vote_refuse(struct agx_vote *v) +{ + v->stopped = true; +} + +/* The GPU probes (or the report comes back, or the system resumes): votes are allowed again. True when work is already in flight and the caller must run the reconcile step. */ +static inline bool agx_vote_resume(struct agx_vote *v) +{ + v->stopped = false; + return v->enabled && v->users && !v->voted; +} + #endif /* PMP_AGX_VOTE_H */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 6d795310b139e5..1d96fdd5494e13 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,7 @@ static DEFINE_MUTEX(apple_pmp_agx_vote_mutex); static void apple_pmp_agx_vote_fn(struct work_struct *work); static DECLARE_DELAYED_WORK(apple_pmp_agx_vote_work, apple_pmp_agx_vote_fn); static void apple_pmp_agx_vote_stop(void); +static void apple_pmp_agx_vote_stop_new(void); /* Bound report regions, protected by RCU until managed teardown drains readers. */ static LIST_HEAD(apple_pmp_reports); @@ -182,6 +184,7 @@ static void apple_pmp_report_release(void *data) /* Withdraw child providers while their power callbacks can still use MMIO. */ of_platform_depopulate(rep->dev); if (rep->offsets == &apple_pmp_offsets_t600x) { + apple_pmp_agx_vote_stop_new(); cancel_delayed_work_sync(&apple_pmp_agx_vote_work); guard(mutex)(&apple_pmp_agx_vote_mutex); apple_pmp_agx_vote_stop(); @@ -1151,6 +1154,10 @@ static int apple_pmp_report_probe(struct platform_device *pdev) list_add_tail_rcu(&rep->list, &apple_pmp_reports); mutex_unlock(&apple_pmp_reports_lock); + /* A report that comes back (rebind) takes votes again; its removal refused them. */ + if (rep->offsets == &apple_pmp_offsets_t600x) + apple_pmp_agx_activity_resume(); + if (rep->offsets->fast_die || rep->start) rep->debugfs = debugfs_create_file(dev_name(dev), 0400, apple_pmp_report_debugfs, rep, @@ -1327,9 +1334,18 @@ static agx_ms_t apple_pmp_agx_now_ms(void) return jiffies_to_msecs(jiffies); } +/* + * Queue the reconcile step. agx_vote=1 on the kernel command line runs the parameter setter from parse_args(), before workqueue_init_early() has + * created system_dfl_wq; queueing on the NULL workqueue would oops in start_kernel(). Nothing is owed then: begin() and the work function read the + * parameter themselves, so the first GPU operation kicks. + */ static void apple_pmp_agx_vote_kick(unsigned long delay_ms) { - mod_delayed_work(system_wq, &apple_pmp_agx_vote_work, msecs_to_jiffies(delay_ms)); + struct workqueue_struct *wq = READ_ONCE(system_dfl_wq); + + if (!wq) + return; + mod_delayed_work(wq, &apple_pmp_agx_vote_work, msecs_to_jiffies(delay_ms)); } static int apple_pmp_agx_vote_param_set(const char *val, const struct kernel_param *kp) @@ -1372,6 +1388,16 @@ static int apple_pmp_agx_write(bool enable, bool no_ack) return ret; } +/* Refuse new votes (the stopped flag), without writing anything. */ +static void apple_pmp_agx_vote_stop_new(void) +{ + unsigned long flags; + + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + agx_vote_refuse(&apple_pmp_agx_vote_state); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); +} + /* Clear a request that is still set. Caller holds apple_pmp_agx_vote_mutex. */ static void apple_pmp_agx_vote_stop(void) { @@ -1461,12 +1487,54 @@ EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_end); */ void apple_pmp_agx_activity_release(void) { + /* Stop first so that a begin() from now on cannot queue a set, then wait out a work item that already decided, then clear what it set. */ + apple_pmp_agx_vote_stop_new(); cancel_delayed_work_sync(&apple_pmp_agx_vote_work); guard(mutex)(&apple_pmp_agx_vote_mutex); apple_pmp_agx_vote_stop(); } EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_release); +/* The GPU probes again, the report comes back or the system resumes: allow votes, and vote now if operations are already in flight. Process context. */ +void apple_pmp_agx_activity_resume(void) +{ + struct agx_vote *v = &apple_pmp_agx_vote_state; + unsigned long flags; + bool kick; + + spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); + v->enabled = READ_ONCE(agx_vote); + kick = agx_vote_resume(v); + spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); + if (kick) + apple_pmp_agx_vote_kick(0); +} +EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_resume); + +/* + * System sleep: withdraw the request before the devices suspend and decide again after resume. Whether bit 9 held through s2idle costs power is not + * measured (a held display request cost about 0.25 W on J514S), and the PMP may drop the word across sleep, which would leave voted true with nothing + * on the wire. Clearing and re-voting covers both. + */ +static int apple_pmp_agx_pm_notify(struct notifier_block *nb, unsigned long action, void *data) +{ + switch (action) { + case PM_SUSPEND_PREPARE: + case PM_HIBERNATION_PREPARE: + apple_pmp_agx_activity_release(); + break; + case PM_POST_SUSPEND: + case PM_POST_HIBERNATION: + apple_pmp_agx_activity_resume(); + break; + } + return NOTIFY_DONE; +} + +static struct notifier_block apple_pmp_agx_pm_nb = { + .notifier_call = apple_pmp_agx_pm_notify, +}; + /* Find and check the report before taking the GPU vote; optionally update it. */ static int apple_pmp_report_t6030_gfx(struct device *supplier, const bool *enable) { @@ -1829,6 +1897,7 @@ MODULE_DEVICE_TABLE(of, apple_pmp_report_entry_of_match); static int __init apple_pmp_report_init(void) { + register_pm_notifier(&apple_pmp_agx_pm_nb); apple_pmp_report_debugfs = debugfs_create_dir("apple-pmp-report", NULL); platform_driver_register(&apple_pmp_report_entry_driver); platform_driver_register(&apple_pmp_report_driver); @@ -1837,6 +1906,7 @@ static int __init apple_pmp_report_init(void) static void __exit apple_pmp_report_exit(void) { + unregister_pm_notifier(&apple_pmp_agx_pm_nb); debugfs_remove_recursive(apple_pmp_report_debugfs); platform_driver_unregister(&apple_pmp_report_entry_driver); platform_driver_unregister(&apple_pmp_report_driver); diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 345848c91dbc36..25049fcd7339c1 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -35,6 +35,7 @@ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned i void apple_pmp_agx_activity_begin(void); void apple_pmp_agx_activity_end(void); void apple_pmp_agx_activity_release(void); +void apple_pmp_agx_activity_resume(void); #else static inline void apple_pmp_agx_activity_begin(void) { @@ -44,6 +45,10 @@ static inline void apple_pmp_agx_activity_end(void) { } +static inline void apple_pmp_agx_activity_resume(void) +{ +} + static inline void apple_pmp_agx_activity_release(void) { } From e8ddbd9fff0252ef8aeb95a3463cf1b8b1ce24c1 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 07:48:32 -0600 Subject: [PATCH 06/22] tools: asahi: give the T6030 graph harness the two names the extracted report probe now uses The report probe tells the AGX vote that a T6001 report came back (apple_pmp_agx_activity_resume) and compares rep->offsets with the T6001 table. The harness extracts the probe verbatim and compiles it standalone, so production.h failed with an undeclared apple_pmp_offsets_t600x and an implicit declaration of apple_pmp_agx_activity_resume. An empty T6001 table that never matches and an empty resume function are enough: the harness models the T6030 graph. All 21 graph cases pass, and the supplier harness passes (ASan needs ASLR off on this host: setarch -R). Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/t6030-pmp-graph-tests.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/asahi/t6030-pmp-graph-tests.c b/tools/asahi/t6030-pmp-graph-tests.c index d7d079a9673a64..38d10db8855aec 100644 --- a/tools/asahi/t6030-pmp-graph-tests.c +++ b/tools/asahi/t6030-pmp-graph-tests.c @@ -584,6 +584,10 @@ static const struct apple_pmp_start_ops apple_pmp_start_hw_ops = { hw_check, hw_bound }; static const struct apple_pmp_start_timing apple_pmp_start_timing = { 2, 0, 2, 2 }; +/* The extracted probe compares rep->offsets with the T6001 table and tells the AGX vote that a report came back. This harness has neither: the table is empty and + * never matches, and the call does nothing. */ +static const struct apple_pmp_report_offsets apple_pmp_offsets_t600x; +static void apple_pmp_agx_activity_resume(void) {} #include "production.h" static const struct apple_pmp_report_offsets * of_device_get_match_data(struct device *d) From 1142df92a11668749f5946f69f5f1b48e20153c5 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 08:45:54 -0600 Subject: [PATCH 07/22] tools: asahi: host suite for the T6001 AGX vote policy, with mutant controls The vote policy is a header of static inline transition functions, so the suite compiles pmp-agx-vote-tests.c against a copy of it and drives the same begin, decide, next and applied calls the kernel glue makes, with a fake clock, a fake PMP bit and a fake timer. 18 cases: the parameter off, the set and idle drop, decode-like bursts, a drop cancelled by new work, the failed set backoff, unbalanced ends, the parameter switched off mid-flight, the zero idle tick, millisecond clock wraps, release and refuse, resume with and without work in flight, sleep and resume, and the two asserts from review (the begin inside a clear write must be re-set, and begin must refuse while stopped). --mutation and --mutations remove one guard each (the owed-set branch, the !failures in it, and the stopped check in begin and decide) and require the suite to fail. The two mutants that survived before the races fix (begin not refusing while stopped, and the retry deadline indistinguishable from the 1 ms owed-set kick) are the stopped-in-begin and owed-set-branch controls: at 5b54de31 (the pre-fix header) both survive because the guarded lines do not exist; at this head each is caught at the case that pins it. Registered in tools/aurora-ci/host-tests.json (base, --mutations, four single-mutant entries, the .c as a helper). Proof: plain run passes (18 cases, AGX-VOTE-SELFCHECK-OK); --mutations exits 0 with all four caught; --ref 5b54de31 --mutation stopped-in-begin and --mutation owed-set-branch print SURVIVED there. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.c | 304 ++++++++++++++++++++++++++++++ tools/asahi/pmp-agx-vote-tests.py | 112 +++++++++++ tools/aurora-ci/host-tests.json | 83 +++++++- 3 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 tools/asahi/pmp-agx-vote-tests.c create mode 100755 tools/asahi/pmp-agx-vote-tests.py diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c new file mode 100644 index 00000000000000..1d13e835ef6a12 --- /dev/null +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -0,0 +1,304 @@ +// Unit test and simulation of pmp-agx-vote.h: the policy that decides when the T6001 AGX request (PS-REQ bit 9) is set and cleared. +// A fake clock, a fake PMP bit and a fake timer drive the same transition functions the kernel glue calls. No kernel, no hardware. +#include +#include +#include +#include + +#include "pmp-agx-vote.h" + +struct sim { + struct agx_vote v; + uint32_t now; + bool bit; // the PMP's PS-REQ bit 9 + int sets, clears; // register writes that tried to set / clear it + int fail_sets; // the next N set attempts are not acknowledged + bool armed; + uint32_t timer_at; + int begin_in_write; // begin this many operations while the register write of the next reconcile is in progress +}; + +static void arm(struct sim *s, uint32_t delay_ms) +{ + uint32_t at = s->now + delay_ms; + if (!s->armed || (int32_t)(at - s->timer_at) < 0) { + s->armed = true; + s->timer_at = at; + } +} + +static void begin(struct sim *s); + +// What the kernel work function does. +static void reconcile(struct sim *s) +{ + enum agx_vote_action act = agx_vote_decide(&s->v, s->now); + int ret = 0; + + s->armed = false; + if (act == AGX_VOTE_SET) { + s->sets++; + if (s->fail_sets > 0) { + s->fail_sets--; + ret = -110; + s->bit = false; // glue force-clears a request that was not acknowledged + } else { + s->bit = true; + } + } else if (act == AGX_VOTE_CLEAR) { + s->clears++; + s->bit = false; + } + while (s->begin_in_write > 0) { + s->begin_in_write--; + begin(s); + } + if (act != AGX_VOTE_NONE) + agx_vote_applied(&s->v, act, ret, s->now); + uint32_t next = agx_vote_next(&s->v, s->now); + if (next) + arm(s, next); +} + +static void begin(struct sim *s) +{ + if (agx_vote_begin(&s->v)) + arm(s, 0); +} + +static void end(struct sim *s) +{ + uint32_t d = agx_vote_end(&s->v, s->now); + if (d) + arm(s, d); +} + +static void advance_to(struct sim *s, uint32_t t) +{ + while (s->armed && (int32_t)(s->timer_at - t) <= 0) { + if ((int32_t)(s->timer_at - s->now) > 0) + s->now = s->timer_at; + reconcile(s); + } + s->now = t; +} + +static struct sim fresh(bool enabled, unsigned long idle_ms, uint32_t start) +{ + struct sim s = {.now = start}; + s.v.enabled = enabled; + s.v.idle_ms = idle_ms; + return s; +} + +#define CHECK(cond) do { if (!(cond)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #cond); exit(1); } } while (0) + +int main(void) +{ + // 1. parameter off: nothing is ever written, however much the GPU runs + { + struct sim s = fresh(false, 100, 1000); + for (int i = 0; i < 50; i++) { begin(&s); advance_to(&s, s.now + 4); end(&s); advance_to(&s, s.now + 7); } + advance_to(&s, s.now + 1000); + CHECK(s.sets == 0 && s.clears == 0 && !s.bit); + } + // 2. one operation: set at once, kept for idle_ms, then cleared + { + struct sim s = fresh(true, 100, 1000); + begin(&s); advance_to(&s, 1000); + CHECK(s.bit && s.sets == 1); + advance_to(&s, 1004); end(&s); + advance_to(&s, 1103); CHECK(s.bit); // 99 ms after the end: still voted + advance_to(&s, 1104); CHECK(!s.bit && s.clears == 1); // 100 ms after the end: dropped + } + // 3. decode-like bursts every 10.58 ms for one second: one set, no clear until the idle time after the last burst + { + struct sim s = fresh(true, 100, 5000); + uint32_t last_end = 0; + for (int i = 0; i < 95; i++) { + uint32_t t0 = 5000 + (uint32_t)(i * 10.58); + advance_to(&s, t0); begin(&s); + advance_to(&s, t0 + 4); end(&s); last_end = s.now; + } + CHECK(s.sets == 1 && s.clears == 0 && s.bit); + advance_to(&s, last_end + 99); CHECK(s.bit); + advance_to(&s, last_end + 100); CHECK(!s.bit && s.clears == 1 && s.sets == 1); + } + // 4. work arrives while the drop is pending: the drop is cancelled, then rescheduled from the new end + { + struct sim s = fresh(true, 100, 0); + begin(&s); advance_to(&s, 0); end(&s); + advance_to(&s, 50); begin(&s); + advance_to(&s, 100); CHECK(s.bit && s.clears == 0); + advance_to(&s, 120); end(&s); + advance_to(&s, 219); CHECK(s.bit); + advance_to(&s, 220); CHECK(!s.bit && s.clears == 1 && s.sets == 1); + } + // 5. a set that is not acknowledged is cleared again, not retried in a hot loop, and retried after the backoff + { + struct sim s = fresh(true, 100, 0); + s.fail_sets = 1; + begin(&s); advance_to(&s, 0); + CHECK(s.sets == 1 && !s.bit && !s.v.voted && s.v.failures == 1); + advance_to(&s, AGX_VOTE_RETRY_MS - 1); CHECK(s.sets == 1); + advance_to(&s, AGX_VOTE_RETRY_MS); CHECK(s.sets == 2 && s.bit && s.v.voted && s.v.failures == 0); + } + // 6. an end without a begin is ignored: no underflow, the next begin still works + { + struct sim s = fresh(true, 100, 0); + end(&s); end(&s); + CHECK(s.v.users == 0 && !s.armed); + begin(&s); advance_to(&s, 0); + CHECK(s.v.users == 1 && s.bit); + } + // 7. the parameter is switched off while voted and busy: cleared at the next reconcile, even with work in flight + { + struct sim s = fresh(true, 100, 0); + begin(&s); advance_to(&s, 0); CHECK(s.bit); + s.v.enabled = false; arm(&s, 0); advance_to(&s, 1); + CHECK(!s.bit && s.clears == 1 && !s.v.voted); + s.v.enabled = true; arm(&s, 0); advance_to(&s, 2); // switched on again with work still in flight + CHECK(s.bit && s.sets == 2); + } + // 8. zero idle time still waits one tick (no clear inside the end call's own tick) + { + struct sim s = fresh(true, 0, 0); + begin(&s); advance_to(&s, 0); end(&s); + CHECK(s.armed && s.bit); + advance_to(&s, 1); CHECK(!s.bit); + } + // 9. the millisecond clock wraps: idle_at + idle_ms overflows, the decision stays right + { + struct sim s = fresh(true, 100, UINT32_MAX - 50); + begin(&s); advance_to(&s, s.now); end(&s); + advance_to(&s, s.now + 99); CHECK(s.bit); + advance_to(&s, s.now + 1); CHECK(!s.bit && s.clears == 1); + } + // 10. release (GPU driver unbind): a held vote is cleared once, at once, and the idle timer is not needed afterwards + { + struct sim s = fresh(true, 100, 0); + begin(&s); advance_to(&s, 0); end(&s); + CHECK(s.bit && s.armed); + if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } + CHECK(!s.bit && s.clears == 1 && !s.v.voted); + advance_to(&s, 500); // the pending timer fires and finds nothing to do + CHECK(s.clears == 1 && !s.armed); + CHECK(!agx_vote_release(&s.v)); // a second release writes nothing + } + // 11. w7X's reproduction: jiffies_to_msecs is 32 bits wide, so a vote whose last operation ended at 0xFFFFFFF0 must still clear + // 100 ms later, after the clock has wrapped to 0x54 (a 64-bit compare of idle_at + idle_ms never decided it) + { + struct sim s = fresh(true, 100, 0xFFFFFFF0u); + begin(&s); advance_to(&s, s.now); end(&s); + CHECK(s.v.idle_at == 0xFFFFFFF0u); + advance_to(&s, 0xFFFFFFF0u + 99u); CHECK(s.bit && s.now == 0x53u); + advance_to(&s, 0x54u); CHECK(!s.bit && s.clears == 1); + CHECK(agx_vote_next(&s.v, s.now) == 0); + } + // 12. a retry deadline across the same wrap + { + struct sim s = fresh(true, 100, 0xFFFFFC00u); + s.fail_sets = 1; + begin(&s); advance_to(&s, s.now); + CHECK(s.sets == 1 && !s.bit); + advance_to(&s, 0xFFFFFC00u + AGX_VOTE_RETRY_MS - 1u); CHECK(s.sets == 1); + advance_to(&s, 0xFFFFFC00u + AGX_VOTE_RETRY_MS); CHECK(s.sets == 2 && s.bit); + } + // 13. the shipped default hold: w7X measured gaps of up to 111.5 ms inside one 9B generation (ctx 8192, once per process), over the old + // 100 ms hold. The default must ride through that gap with one vote, and still drop the vote after a long idle. + { + struct sim s = fresh(true, AGX_VOTE_IDLE_MS_DEFAULT, 1000); + CHECK(AGX_VOTE_IDLE_MS_DEFAULT >= 250); + begin(&s); advance_to(&s, s.now); end(&s); + advance_to(&s, s.now + 112); // the 111.5 ms interval rounded up + begin(&s); advance_to(&s, s.now); end(&s); + CHECK(s.sets == 1 && s.clears == 0 && s.bit); + advance_to(&s, s.now + AGX_VOTE_IDLE_MS_DEFAULT - 1); CHECK(s.bit); + advance_to(&s, s.now + 1); CHECK(!s.bit && s.clears == 1); + } + // 14. review finding 2: an operation begins while the clear is being written (voted is still true, so begin does not kick). After the clear is + // applied the vote must be set again without waiting for another operation. + { + struct sim s = fresh(true, 100, 0); + begin(&s); advance_to(&s, 0); end(&s); + CHECK(s.bit && s.armed); + s.begin_in_write = 1; + advance_to(&s, 100); // the idle hold expires: reconcile decides CLEAR, begin lands inside the write + CHECK(s.v.users == 1); + advance_to(&s, 101); // the owed set runs + CHECK(s.bit && s.v.voted && s.sets == 2 && s.clears == 1); + // the header alone, as the reviewer reproduced it: decide (CLEAR), begin, applied, next + struct agx_vote v = {.enabled = true, .voted = true, .idle_ms = 100, .idle_at = 0}; + CHECK(agx_vote_decide(&v, 100) == AGX_VOTE_CLEAR); + CHECK(!agx_vote_begin(&v)); + agx_vote_applied(&v, AGX_VOTE_CLEAR, 0, 100); + CHECK(v.users == 1 && !v.voted); + CHECK(agx_vote_next(&v, 100) == 1); // not 0: a set is owed + CHECK(agx_vote_decide(&v, 101) == AGX_VOTE_SET); + } + // 15. review finding 3: after release (GPU unbind or report removal) no begin may vote again, until the GPU probes again + { + struct sim s = fresh(true, 100, 0); + begin(&s); advance_to(&s, 0); + CHECK(s.bit && s.v.users == 1); + if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } + CHECK(!s.bit && s.v.stopped && s.clears == 1); + end(&s); begin(&s); advance_to(&s, 50); // an ioctl already past the unplug check starts an operation + CHECK(!s.bit && s.sets == 1 && !s.armed); + CHECK(agx_vote_decide(&s.v, 50) == AGX_VOTE_NONE); + CHECK(agx_vote_next(&s.v, 50) == 0); + // a work item that was queued before the release finds nothing to set + arm(&s, 0); advance_to(&s, 51); + CHECK(!s.bit && s.sets == 1); + // the GPU probes again: the vote is allowed, and the operation still in flight gets its set + CHECK(agx_vote_resume(&s.v)); + arm(&s, 0); advance_to(&s, 52); + CHECK(s.bit && s.sets == 2 && !s.v.stopped); + } + // 16. review finding 4: system sleep. The vote is withdrawn before suspend and re-decided on resume: with work still in flight it is set + // again (the PMP may have dropped the word), with an idle GPU nothing is written. + { + struct sim s = fresh(true, 250, 0); + begin(&s); advance_to(&s, 0); + CHECK(s.bit); + if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } // suspend prepare + CHECK(!s.bit && s.v.stopped && s.clears == 1); + if (agx_vote_resume(&s.v)) arm(&s, 0); // resume + advance_to(&s, 10); + CHECK(s.bit && s.sets == 2 && s.v.voted); + end(&s); + if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } // a second sleep while idle inside the hold + CHECK(!s.bit && s.clears == 2); + CHECK(!agx_vote_resume(&s.v)); // idle: nothing is owed + advance_to(&s, 1000); + CHECK(!s.bit && s.sets == 2 && s.clears == 2); + begin(&s); advance_to(&s, 1001); // the next operation after resume votes as usual + CHECK(s.bit && s.sets == 3); + } + // 17. disabled and stopped never set, whatever begins + { + struct sim s = fresh(false, 100, 0); + s.v.stopped = true; + begin(&s); advance_to(&s, 1); + CHECK(!s.bit && s.sets == 0 && !s.armed); + CHECK(!agx_vote_resume(&s.v)); // disabled: resume owes nothing + } + // 18. mutants that survived the first review: begin() must itself refuse while stopped (not only decide()), a failed set must schedule the retry + // deadline and not the 1 ms owed-set kick (a 1 kHz spin for the whole 1000 ms window otherwise), and agx_vote_refuse() must stop votes. + { + struct agx_vote v = {.enabled = true, .idle_ms = 100}; + agx_vote_refuse(&v); + CHECK(v.stopped && !v.voted); + CHECK(!agx_vote_begin(&v)); // a user, but no kick + CHECK(agx_vote_decide(&v, 0) == AGX_VOTE_NONE && agx_vote_next(&v, 0) == 0); + CHECK(agx_vote_resume(&v)); // work in flight: the reconcile step must run + struct agx_vote w = {.enabled = true, .idle_ms = 100}; + CHECK(agx_vote_begin(&w)); + agx_vote_applied(&w, AGX_VOTE_SET, -110, 500); // the set was not acknowledged + CHECK(w.failures == 1 && w.retry_at == 500 + AGX_VOTE_RETRY_MS); + CHECK(agx_vote_next(&w, 500) == AGX_VOTE_RETRY_MS); // the retry deadline, not 1 ms + CHECK(agx_vote_next(&w, 500 + AGX_VOTE_RETRY_MS - 1) == 1); + } + printf("AGX-VOTE-SELFCHECK-OK\n"); + return 0; +} diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py new file mode 100755 index 00000000000000..e3c8533ba04349 --- /dev/null +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Host test for the T6001 AGX vote policy (drivers/pmdomain/apple/pmp-agx-vote.h). + +The policy is a header of static inline transition functions, so the suite +compiles tools/asahi/pmp-agx-vote-tests.c against a copy of that header: a +fake clock, a fake PMP bit and a fake timer drive the same begin, decide, +next and applied calls the kernel glue makes. No kernel, no hardware. + +--mutation NAME / --mutations apply textual mutants to the header copy and +require the suite to fail; like the other self-checking suites, exit status 0 +means every mutant was caught. --ref reads the header at a Git revision (a +pre-fix header fails the suite itself, which is the point of those cases). +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import shlex +import subprocess + +ROOT = Path(__file__).resolve().parents[2] +HEADER = 'drivers/pmdomain/apple/pmp-agx-vote.h' + +# Each mutant removes one guard. The first two are the mutants that survived +# the first review round, when begin() did not refuse while stopped and the +# retry deadline was not distinguished from the 1 ms owed-set kick. +CONTROLS = [ + ('owed-set-branch', '\tif (v->users && !v->voted && !v->failures)\n\t\treturn 1;\n', ''), + ('owed-failures', 'if (v->users && !v->voted && !v->failures)\n\t\treturn 1;', 'if (v->users && !v->voted)\n\t\treturn 1;'), + ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped;', 'return v->enabled && !v->voted;'), + ('stopped-in-decide', 'if (!v->enabled || v->stopped)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;', + 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), +] + + +def header_text(ref): + if ref: + return subprocess.check_output(['git', '-C', str(ROOT), 'show', f'{ref}:{HEADER}'], text=True) + return (ROOT / HEADER).read_text() + + +def compile_and_run(out, text): + out.mkdir(parents=True, exist_ok=True) + (out / 'pmp-agx-vote.h').write_text(text) + compiler = shlex.split(os.environ.get('CC', 'clang')) + command = compiler + ['-std=gnu11', '-g', '-O1', '-Wall', '-Wextra', '-Werror', + '-fsanitize=address,undefined', '-fno-omit-frame-pointer', + '-I', str(out), str(ROOT / 'tools/asahi/pmp-agx-vote-tests.c'), + '-o', str(out / 'vote')] + built = subprocess.run(command, capture_output=True, text=True) + (out / 'build.log').write_text(built.stdout + built.stderr) + if built.returncode: + return {'build_rc': built.returncode, 'run_rc': None, 'note': 'compilation failure is not regression evidence'} + ran = subprocess.run([str(out / 'vote')], capture_output=True, text=True) + (out / 'run.log').write_text(ran.stdout + ran.stderr) + return {'build_rc': 0, 'run_rc': ran.returncode} + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--ref', help='read the header at a Git revision (negative control)') + p.add_argument('--mutation', help='apply one named mutant and require the suite to fail') + p.add_argument('--mutations', action='store_true', help='apply every mutant and require each to fail') + p.add_argument('--output', required=True, type=Path) + args = p.parse_args() + args.output.mkdir(parents=True, exist_ok=False) + text = header_text(args.ref) + + base = compile_and_run(args.output, text) + print(Path(args.output / 'run.log').read_text() if base['run_rc'] is not None else Path(args.output / 'build.log').read_text()) + + mutations = [] + if args.mutations or args.mutation: + names = [c[0] for c in CONTROLS] if args.mutations else [args.mutation] + for name in names: + old, new = next((c[1], c[2]) for c in CONTROLS if c[0] == name) + count = text.count(old) + result = {} + result['applied'] = count == 1 + if count == 1: + result.update(compile_and_run(args.output / name, text.replace(old, new))) + else: + # A guard that does not exist in this revision cannot be removed: the + # behaviour the guard carries is absent, so the mutant survives there. + result.update({'build_rc': None, 'run_rc': 0, 'note': f'{HEADER} at this revision has no such line ({count} matches)'}) + result['name'] = name + mutations.append(result) + caught = result['applied'] and result['build_rc'] == 0 and result['run_rc'] not in (0, None) + log = args.output / name / ('run.log' if result.get('run_rc') is not None else 'build.log') + detail = log.read_text().strip().splitlines()[-1][:150] if log.exists() else result.get('note', '') + print(f"{name}: {'CAUGHT' if caught else 'SURVIVED'} | {detail}") + + if args.mutations: + assert all(m['applied'] and m['build_rc'] == 0 and m['run_rc'] not in (0, None) for m in mutations), mutations + print('PASS: 18 actual vote-policy cases and every mutant caught') + return 0 + if args.mutation: + m = mutations[0] + assert m['applied'] and m['build_rc'] == 0 and m['run_rc'] not in (0, None), m + return 0 + + (args.output / 'receipt.json').write_text(json.dumps({ + 'source_ref': args.ref, 'source_sha256': hashlib.sha256(text.encode()).hexdigest(), + 'command': [], 'returncode': base['run_rc'] if base['run_rc'] is not None else base['build_rc'], + 'mutations': mutations}, indent=2) + '\n') + return bool(base['run_rc']) if base['run_rc'] is not None else bool(base['build_rc']) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index a1b93d695dfd6e..7d55400ba6ada1 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2238,6 +2238,86 @@ ], "note": "Compile J514S/J516S default and static PMP profiles; check supplier nodes and alias in the resulting DTBs." }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--output", + "{output}" + ], + "note": "T6001 AGX vote policy host suite: fake clock, PMP bit and timer against the static inline header; --mutations self-checks the guards" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutations", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutations", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when every mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation owed-set-branch", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "owed-set-branch", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation owed-failures", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "owed-failures", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation stopped-in-begin", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "stopped-in-begin", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation stopped-in-decide", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "stopped-in-decide", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/t6030-pmp-graph-tests.py", "group": "tools", @@ -2289,7 +2369,8 @@ "tools/testing/selftests/drivers/gpu/asahi-g15/uat-owner.c": "compiled and executed by the registered asahi-g15/run.py actual reservation-owner fixture", "tools/testing/selftests/drivers/gpu/asahi-g15/pmp-bridge.c": "compiled and executed by the registered asahi-g15/run.py maintained PMP fixture", "tools/asahi/t6030-pmp-graph-tests.c": "compiled and executed by the corresponding registered Python actual-body host suite", - "tools/asahi/t6030-display-admission-tests.c": "compiled and executed by the corresponding registered Python actual-body host suite" + "tools/asahi/t6030-display-admission-tests.c": "compiled and executed by the corresponding registered Python actual-body host suite", + "tools/asahi/pmp-agx-vote-tests.c": "compiled and executed by the registered pmp-agx-vote-tests.py policy suite" }, "excluded": { "tools/aurora-sep/tests/coordinator/runtime-check.sh": "Needs /dev/sep-bio, a running kernel booted with trusted.source=applesep, the hw_random sysfs interface and fprintd. Coordinator payload for an approved device.", From db48e985728e23e35119de564cfc9bd0f0f266f2 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 08:52:41 -0600 Subject: [PATCH 08/22] tools: asahi: pin the vote clock wrap and require a passing base in the mutation modes Review of 1142df92 by w7X, two non-blocking items folded in before push. Case 19 pins the signed 32-bit compare in agx_vote_reached: with idle_at 0xFFFFFFF0, idle_ms 100 and voted, decide at 0xFFFFFFF8 must be NONE and next must return the 92 ms to the wrapped deadline, and a retry_at that has wrapped must not read as reached either. The wrap-reached mutant (now >= t) survives the old cases because they jump straight to the wrapped deadline; it dies at line 306. Registered as a fifth mutation entry. The mutation modes now assert the unmutated suite passed first, so a base failure cannot make every mutant read as caught. The negative control (--ref) is exempt: the pre-fix header cannot compile this suite, which is the point. The suite runs under setarch -R when available; ASan startup crashes under ASLR on a loaded host otherwise. Proof: plain run passes; --mutations exits 0 with all five mutants caught; wrap-reached CAUGHT at line 306; at 5b54de31 the owed-set-branch and stopped-in-begin mutants still print SURVIVED. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.c | 9 +++++++++ tools/asahi/pmp-agx-vote-tests.py | 11 +++++++++-- tools/aurora-ci/host-tests.json | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index 1d13e835ef6a12..9ffb3541d6a4e0 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -299,6 +299,15 @@ int main(void) CHECK(agx_vote_next(&w, 500) == AGX_VOTE_RETRY_MS); // the retry deadline, not 1 ms CHECK(agx_vote_next(&w, 500 + AGX_VOTE_RETRY_MS - 1) == 1); } + // 19. w7X's wrap mutant: reached() must compare in signed 32-bit arithmetic, so a deadline that has + // wrapped into the low range is not "already reached" on the pre-wrap side + { + struct agx_vote v = {.enabled = true, .voted = true, .idle_ms = 100, .idle_at = 0xFFFFFFF0u}; + CHECK(agx_vote_decide(&v, 0xFFFFFFF8u) == AGX_VOTE_NONE); + CHECK(agx_vote_next(&v, 0xFFFFFFF8u) == 0x5Cu); + struct agx_vote w = {.enabled = true, .idle_ms = 100, .users = 1, .failures = 1, .retry_at = 0x54u}; + CHECK(agx_vote_decide(&w, 0xFFFFFFF8u) == AGX_VOTE_NONE); + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index e3c8533ba04349..7377d14a07e8da 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -18,6 +18,7 @@ import os from pathlib import Path import shlex +import shutil import subprocess ROOT = Path(__file__).resolve().parents[2] @@ -32,6 +33,7 @@ ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped;', 'return v->enabled && !v->voted;'), ('stopped-in-decide', 'if (!v->enabled || v->stopped)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;', 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), + ('wrap-reached', 'return (agx_sms_t)(now - t) >= 0;', 'return now >= t;'), ] @@ -53,7 +55,8 @@ def compile_and_run(out, text): (out / 'build.log').write_text(built.stdout + built.stderr) if built.returncode: return {'build_rc': built.returncode, 'run_rc': None, 'note': 'compilation failure is not regression evidence'} - ran = subprocess.run([str(out / 'vote')], capture_output=True, text=True) + prefix = ['setarch', '-R'] if shutil.which('setarch') else [] # ASan startup can crash under ASLR on a loaded host + ran = subprocess.run(prefix + [str(out / 'vote')], capture_output=True, text=True) (out / 'run.log').write_text(ran.stdout + ran.stderr) return {'build_rc': 0, 'run_rc': ran.returncode} @@ -73,6 +76,10 @@ def main(): mutations = [] if args.mutations or args.mutation: + # A base failure would fail every mutant at the same case and read as caught. + # The negative control (--ref) has no passing base: the pre-fix header cannot compile this suite. + if not args.ref: + assert base['run_rc'] == 0, base names = [c[0] for c in CONTROLS] if args.mutations else [args.mutation] for name in names: old, new = next((c[1], c[2]) for c in CONTROLS if c[0] == name) @@ -94,7 +101,7 @@ def main(): if args.mutations: assert all(m['applied'] and m['build_rc'] == 0 and m['run_rc'] not in (0, None) for m in mutations), mutations - print('PASS: 18 actual vote-policy cases and every mutant caught') + print('PASS: every vote-policy case passed and every mutant caught') return 0 if args.mutation: m = mutations[0] diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 7d55400ba6ada1..9e4702adda139c 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2318,6 +2318,20 @@ "expect": "pass", "note": "self-checking: exits 0 when the mutant aborts" }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation wrap-reached", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "wrap-reached", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/t6030-pmp-graph-tests.py", "group": "tools", From 825e9aca448e7e18386dafb6bea7ba4fa265b4c4 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:04:51 -0600 Subject: [PATCH 09/22] tools: asahi: vote suite cases for the end-during-SET race, the backoff kick storm and the revived retry Three cases for chriskearney's findings on #334, written first and red at this commit: - Case 20: an end lands between decide (SET) and applied. The end sees voted false, arms nothing, and applied then sets the vote with no user and an already-reached idle deadline. next() must return 1 there, not 0: 0 is the suite's "nothing pending", so the caller would never run the reconcile that clears the vote. - Case 21: an idle gap beyond half the 32-bit millisecond range makes the wrapped arithmetic read an expired retry deadline as future, which would keep the vote off for days. A retry deadline more than one backoff window in the future cannot be real and counts as reached. - Case 22: during backoff, begin must not ask for a reconciliation decide would refuse; the retry timer already drives the set. Three mutants pin them (set-write-ends, retry-revival, begin-backoff-kick), and stopped-in-begin now matches the begin line the fix will produce. Red here: case 20 fails at `agx_vote_next(&v, 5001) == 1`. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.c | 55 ++++++++++++++++++++++++++++++- tools/asahi/pmp-agx-vote-tests.py | 5 ++- tools/aurora-ci/host-tests.json | 42 +++++++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index 9ffb3541d6a4e0..f58c592b9a182d 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -16,6 +16,7 @@ struct sim { bool armed; uint32_t timer_at; int begin_in_write; // begin this many operations while the register write of the next reconcile is in progress + int kicks; // reconciliations the operations asked for }; static void arm(struct sim *s, uint32_t delay_ms) @@ -62,8 +63,10 @@ static void reconcile(struct sim *s) static void begin(struct sim *s) { - if (agx_vote_begin(&s->v)) + if (agx_vote_begin(&s->v)) { + s->kicks++; arm(s, 0); + } } static void end(struct sim *s) @@ -308,6 +311,56 @@ int main(void) struct agx_vote w = {.enabled = true, .idle_ms = 100, .users = 1, .failures = 1, .retry_at = 0x54u}; CHECK(agx_vote_decide(&w, 0xFFFFFFF8u) == AGX_VOTE_NONE); } + // 20. chriskearney's race: the last end lands between decide (SET) and applied. end saw voted false and + // armed nothing; applied then sets the vote with no user and an already-reached idle deadline. The + // caller must still be told to run the reconcile that clears it. + { + // exactly the reviewer's interleaving, on the header alone: decide, then end, then the write lands + struct agx_vote v = {.enabled = true, .idle_ms = 0}; + CHECK(agx_vote_begin(&v) && v.users == 1); + CHECK(agx_vote_decide(&v, 5000) == AGX_VOTE_SET); + CHECK(agx_vote_end(&v, 5001) == 0); // the end during the write sees voted false: no timer + CHECK(v.users == 0); + agx_vote_applied(&v, AGX_VOTE_SET, 0, 5000); + CHECK(v.voted && v.users == 0); + CHECK(agx_vote_next(&v, 5001) == 1); // an already-reached deadline is due now, not "nothing" + CHECK(agx_vote_decide(&v, 5001) == AGX_VOTE_CLEAR); + agx_vote_applied(&v, AGX_VOTE_CLEAR, 0, 5001); + CHECK(!v.voted && agx_vote_next(&v, 5001) == 0); // and it settles + } + // 21. chriskearney's bound: an idle gap beyond half the 32-bit millisecond range makes the wrapped + // arithmetic read an expired retry deadline as future, which would keep the vote off for days. A retry + // deadline more than one backoff window in the future cannot be real, so it counts as reached. + { + struct sim s = fresh(true, 100, 0); + s.fail_sets = 1; + begin(&s); advance_to(&s, 0); + CHECK(s.v.failures == 1); + s.v.retry_at = 0x54u; + advance_to(&s, 0x54u + 0x80000005u); // the timer fires absurdly late + CHECK(s.bit && s.sets == 2 && s.v.failures == 0); // the vote is set again at once + // the header alone + struct agx_vote w = {.enabled = true, .users = 1, .failures = 1, .retry_at = 0x54u}; + CHECK(agx_vote_decide(&w, 0x54u + 0x80000005u) == AGX_VOTE_SET); + CHECK(agx_vote_next(&w, 0x54u + 0x80000005u) == 1); + } + // 22. the same review: during backoff begin must not ask for a reconciliation decide would refuse + // (every begin during the window kicked a NONE reconcile and the retry timer did the work anyway) + { + struct sim s = fresh(true, 100, 0); + s.fail_sets = 1; + begin(&s); advance_to(&s, 0); + CHECK(s.v.failures == 1 && s.armed); + int kicks = s.kicks; + for (int i = 0; i < 5; i++) + begin(&s); + CHECK(s.kicks == kicks && s.sets == 1); // no kicks, no sets: the timer drives it + advance_to(&s, AGX_VOTE_RETRY_MS); + CHECK(s.sets == 2 && s.bit && s.v.failures == 0); + // the header alone + struct agx_vote v = {.enabled = true, .failures = 1}; + CHECK(!agx_vote_begin(&v)); + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index 7377d14a07e8da..881a2e85d66013 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -30,10 +30,13 @@ CONTROLS = [ ('owed-set-branch', '\tif (v->users && !v->voted && !v->failures)\n\t\treturn 1;\n', ''), ('owed-failures', 'if (v->users && !v->voted && !v->failures)\n\t\treturn 1;', 'if (v->users && !v->voted)\n\t\treturn 1;'), - ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped;', 'return v->enabled && !v->voted;'), + ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped && !v->failures;', 'return v->enabled && !v->voted && !v->failures;'), ('stopped-in-decide', 'if (!v->enabled || v->stopped)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;', 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), ('wrap-reached', 'return (agx_sms_t)(now - t) >= 0;', 'return now >= t;'), + ('set-write-ends', 'return agx_vote_reached(now, at) ? 1 : at - now;', 'return agx_vote_reached(now, at) ? 0 : at - now;'), + ('begin-backoff-kick', 'return v->enabled && !v->voted && !v->stopped && !v->failures;', 'return v->enabled && !v->voted && !v->stopped;'), + ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), ] diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 9e4702adda139c..deef3270175a53 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2332,6 +2332,48 @@ "expect": "pass", "note": "self-checking: exits 0 when the mutant aborts" }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation set-write-ends", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "set-write-ends", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-backoff-kick", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "begin-backoff-kick", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation retry-revival", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "retry-revival", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/t6030-pmp-graph-tests.py", "group": "tools", From a7330a53e0e37feb8c4103d16cf94c9606f7d697 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:06:09 -0600 Subject: [PATCH 10/22] pmdomain: apple: pmp-agx-vote: drain the SET-write race, bound the retry deadline and quiet the backoff kicks Three findings from chriskearney's review of #334; the suite cases landed first (825e9aca, red) and this is the smallest change that turns them green. - end() during the SET write (between decide and applied) sees voted false and arms nothing; applied then leaves the vote set with no user and an already-reached idle deadline, and next() answered 0, which the caller reads as nothing pending. An already-reached deadline now returns 1, so the caller runs the reconcile that clears the vote. It settles one step later: decide CLEAR, applied, next 0. - A retry deadline more than one backoff window in the future cannot be real (an idle gap beyond half the 32-bit millisecond range reads an expired deadline as future through the wrapped compare), so the new agx_vote_retry_reached() treats it as expired instead of keeping the vote off for days. - begin() no longer asks for a reconciliation while a failed set is in backoff: decide would return NONE, and the retry timer already drives the set. One kick per operation during the window was pure waste. The mutants in tools/asahi/pmp-agx-vote-tests.py pin each: set-write-ends dies at the race assert, retry-revival at the bound assert, begin-backoff-kick at the kick-count assert, and the five earlier mutants still die. Plain suite: 22 cases, selfcheck ok. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 64d8f3f199d09b..ef0845b897f4dd 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -51,11 +51,21 @@ static inline bool agx_vote_reached(agx_ms_t now, agx_ms_t t) return (agx_sms_t)(now - t) >= 0; } +/* A retry deadline more than one backoff window in the future cannot be real: an idle gap beyond half the + * 32-bit millisecond range makes the wrapped arithmetic read an expired deadline as future. Treat it as expired. */ +static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now) +{ + return agx_vote_reached(now, v->retry_at) + || (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS; +} + /* An operation starts. True: run the reconcile step now. */ static inline bool agx_vote_begin(struct agx_vote *v) { v->users++; - return v->enabled && !v->voted && !v->stopped; + /* While a failed set is in backoff the retry timer drives the set: decide would return NONE here, so + * asking for a reconciliation only burns a kick per operation until the deadline. */ + return v->enabled && !v->voted && !v->stopped && !v->failures; } /* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */ @@ -77,15 +87,17 @@ static inline enum agx_vote_action agx_vote_decide(const struct agx_vote *v, agx if (!v->enabled || v->stopped) return v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE; if (v->users) - return !v->voted && (!v->failures || agx_vote_reached(now, v->retry_at)) ? AGX_VOTE_SET : AGX_VOTE_NONE; + return !v->voted && (!v->failures || agx_vote_retry_reached(v, now)) ? AGX_VOTE_SET : AGX_VOTE_NONE; if (v->voted && agx_vote_reached(now, v->idle_at + v->idle_ms)) return AGX_VOTE_CLEAR; return AGX_VOTE_NONE; } /* - * Delay in ms until the reconcile step must run again, 0 when no timer is needed. 1 when a set is owed right now: an operation began while a - * clear was being written (begin saw voted still true and did not kick), so after the clear nothing would schedule the set. + * Delay in ms until the reconcile step must run again, 0 when no timer is needed. 1 when work is due now: an + * operation began while a clear was being written (begin saw voted still true and did not kick), or an end + * landed inside the SET write so the vote is set with an already-reached idle deadline. The caller must run the + * reconcile those cases would otherwise skip; it cannot read 1 as "nothing pending". */ static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now) { @@ -96,12 +108,14 @@ static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now) if (v->users && !v->voted && !v->failures) return 1; if (v->users && !v->voted && v->failures) - at = v->retry_at; + at = agx_vote_retry_reached(v, now) ? now : v->retry_at; else if (!v->users && v->voted) at = v->idle_at + v->idle_ms; else return 0; - return agx_vote_reached(now, at) ? 0 : at - now; + /* An already-reached deadline returns 1: there is work, due now. The SET-write race leaves exactly + * this state (voted, no user, deadline passed), and 0 here would strand the vote set forever. */ + return agx_vote_reached(now, at) ? 1 : at - now; } /* Record the outcome of the register write for `action` (ret 0: acknowledged). */ From 2ec389ca6e6d1ac94297fe094313d99202376ed5 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:13:24 -0600 Subject: [PATCH 11/22] pmdomain: apple: pmp-agx-vote: give begin the clock so a backoff cannot strand or spam the vote w7X's review of the first cut (a7330a53) found a blocking regression and two correct behaviours to keep. The gate on begin had to know the time, so agx_vote_begin() now takes now (the caller reads it under its lock; the glue passes apple_pmp_agx_now_ms() beside its existing end and next reads): - begin returns false during a failed-set backoff before the deadline (decide would return NONE; the retry timer drives the set), and true again once the deadline has passed. The first cut's unconditional !v->failures stranded the vote after one failed set whose retry timer fired idle: failures is only cleared by a successful SET, so every later begin refused forever. Case 23 is that sequence (failed set, end before the retry, timer fires idle, a later begin must kick and the set must land); the begin-ignores-retry mutant is exactly the first cut's expression and dies at it (line 363). - The other direction is pinned too: begin-backoff-kick (kick during the window regardless of the deadline) dies at the kick-storm case (line 357), so the gate cannot silently decay into "always kick" again. Case 23 exists because the simulator cases kept the operation alive through the backoff; this one ends it first. The begin signature change touches the suite's helper, four header-only call sites, and the one glue call. Proof: plain suite 23 cases selfcheck ok; --mutations catches all nine mutants (owed-set-branch, owed-failures, stopped-in-begin, stopped-in-decide, wrap-reached, set-write-ends, begin-backoff-kick, begin-ignores-retry, retry-revival); check-kick-guard still passes on pmp-report.c; the tree runner passes the registered vote entries. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 11 +++++---- drivers/pmdomain/apple/pmp-report.c | 2 +- tools/asahi/pmp-agx-vote-tests.c | 35 +++++++++++++++++++++------ tools/asahi/pmp-agx-vote-tests.py | 9 +++++-- tools/aurora-ci/host-tests.json | 14 +++++++++++ 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index ef0845b897f4dd..f6ded0e23884a7 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -59,13 +59,14 @@ static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now || (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS; } -/* An operation starts. True: run the reconcile step now. */ -static inline bool agx_vote_begin(struct agx_vote *v) +/* An operation starts. now is the operation's start time in ms (any wrapping clock; the caller reads it + * under its lock). True: run the reconcile step now. During a failed-set backoff before the deadline it is + * false (decide would return NONE, and the retry timer drives the set); once the deadline has passed it is + * true again, so a failed set whose timer fired idle does not strand the vote. */ +static inline bool agx_vote_begin(struct agx_vote *v, agx_ms_t now) { v->users++; - /* While a failed set is in backoff the retry timer drives the set: decide would return NONE here, so - * asking for a reconciliation only burns a kick per operation until the deadline. */ - return v->enabled && !v->voted && !v->stopped && !v->failures; + return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now)); } /* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 1d96fdd5494e13..0d734de059317c 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -1458,7 +1458,7 @@ void apple_pmp_agx_activity_begin(void) spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); v->enabled = READ_ONCE(agx_vote); - kick = agx_vote_begin(v); + kick = agx_vote_begin(v, apple_pmp_agx_now_ms()); spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); if (kick) apple_pmp_agx_vote_kick(0); diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index f58c592b9a182d..ed7ed85722b44d 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -63,7 +63,7 @@ static void reconcile(struct sim *s) static void begin(struct sim *s) { - if (agx_vote_begin(&s->v)) { + if (agx_vote_begin(&s->v, s->now)) { s->kicks++; arm(s, 0); } @@ -233,7 +233,7 @@ int main(void) // the header alone, as the reviewer reproduced it: decide (CLEAR), begin, applied, next struct agx_vote v = {.enabled = true, .voted = true, .idle_ms = 100, .idle_at = 0}; CHECK(agx_vote_decide(&v, 100) == AGX_VOTE_CLEAR); - CHECK(!agx_vote_begin(&v)); + CHECK(!agx_vote_begin(&v, 100)); agx_vote_applied(&v, AGX_VOTE_CLEAR, 0, 100); CHECK(v.users == 1 && !v.voted); CHECK(agx_vote_next(&v, 100) == 1); // not 0: a set is owed @@ -292,11 +292,11 @@ int main(void) struct agx_vote v = {.enabled = true, .idle_ms = 100}; agx_vote_refuse(&v); CHECK(v.stopped && !v.voted); - CHECK(!agx_vote_begin(&v)); // a user, but no kick + CHECK(!agx_vote_begin(&v, 0)); // a user, but no kick CHECK(agx_vote_decide(&v, 0) == AGX_VOTE_NONE && agx_vote_next(&v, 0) == 0); CHECK(agx_vote_resume(&v)); // work in flight: the reconcile step must run struct agx_vote w = {.enabled = true, .idle_ms = 100}; - CHECK(agx_vote_begin(&w)); + CHECK(agx_vote_begin(&w, 0)); agx_vote_applied(&w, AGX_VOTE_SET, -110, 500); // the set was not acknowledged CHECK(w.failures == 1 && w.retry_at == 500 + AGX_VOTE_RETRY_MS); CHECK(agx_vote_next(&w, 500) == AGX_VOTE_RETRY_MS); // the retry deadline, not 1 ms @@ -317,7 +317,7 @@ int main(void) { // exactly the reviewer's interleaving, on the header alone: decide, then end, then the write lands struct agx_vote v = {.enabled = true, .idle_ms = 0}; - CHECK(agx_vote_begin(&v) && v.users == 1); + CHECK(agx_vote_begin(&v, 5000) && v.users == 1); CHECK(agx_vote_decide(&v, 5000) == AGX_VOTE_SET); CHECK(agx_vote_end(&v, 5001) == 0); // the end during the write sees voted false: no timer CHECK(v.users == 0); @@ -358,8 +358,29 @@ int main(void) advance_to(&s, AGX_VOTE_RETRY_MS); CHECK(s.sets == 2 && s.bit && s.v.failures == 0); // the header alone - struct agx_vote v = {.enabled = true, .failures = 1}; - CHECK(!agx_vote_begin(&v)); + struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 100}; + CHECK(!agx_vote_begin(&v, 0)); + CHECK(agx_vote_begin(&v, 100)); + } + // 23. w7X's regression repro for the first cut of the backoff gate: a failed set whose retry timer fires + // while the GPU is idle left failures set forever, and begin never asked for the set again. The kick + // must come back once the deadline has passed. + { + struct sim s = fresh(true, 100, 0); + s.fail_sets = 1; + begin(&s); advance_to(&s, 0); + CHECK(s.v.failures == 1 && s.armed); + end(&s); // the operation ends long before the retry + advance_to(&s, AGX_VOTE_RETRY_MS); // the timer fires idle: nothing to do + CHECK(s.v.failures == 1 && !s.armed && !s.bit); + int kicks = s.kicks; + begin(&s); // a later operation: the kick must come back + advance_to(&s, s.now + 1); + CHECK(s.kicks == kicks + 1 && s.sets == 2 && s.bit && s.v.failures == 0); + // the header alone: inside the window no kick, past it the kick returns + struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 100}; + CHECK(!agx_vote_begin(&v, 50)); + CHECK(agx_vote_begin(&v, 100)); } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index 881a2e85d66013..17d829217ef102 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -30,12 +30,17 @@ CONTROLS = [ ('owed-set-branch', '\tif (v->users && !v->voted && !v->failures)\n\t\treturn 1;\n', ''), ('owed-failures', 'if (v->users && !v->voted && !v->failures)\n\t\treturn 1;', 'if (v->users && !v->voted)\n\t\treturn 1;'), - ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped && !v->failures;', 'return v->enabled && !v->voted && !v->failures;'), + ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', + 'return v->enabled && !v->voted && (!v->failures || agx_vote_retry_reached(v, now));'), ('stopped-in-decide', 'if (!v->enabled || v->stopped)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;', 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), ('wrap-reached', 'return (agx_sms_t)(now - t) >= 0;', 'return now >= t;'), ('set-write-ends', 'return agx_vote_reached(now, at) ? 1 : at - now;', 'return agx_vote_reached(now, at) ? 0 : at - now;'), - ('begin-backoff-kick', 'return v->enabled && !v->voted && !v->stopped && !v->failures;', 'return v->enabled && !v->voted && !v->stopped;'), + ('begin-backoff-kick', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', + 'return v->enabled && !v->voted && !v->stopped && ((void)now, (!v->failures || true));'), + ('begin-ignores-retry', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', + 'return v->enabled && !v->voted && !v->stopped && ((void)now, !v->failures);'), + ('begin-ignores-retry', '(!v->failures || agx_vote_retry_reached(v, now))', '!v->failures'), ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), ] diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index deef3270175a53..9287a29f8c88ed 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2360,6 +2360,20 @@ "expect": "pass", "note": "self-checking: exits 0 when the mutant aborts" }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-ignores-retry", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "begin-ignores-retry", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/pmp-agx-vote-tests.py --mutation retry-revival", "group": "tools", From efff25c15c4f1abb1fb141d87ecfe07432bdb3c4 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:16:51 -0600 Subject: [PATCH 12/22] tools: asahi: drop the dead second begin-ignores-retry control entry w7X's re-review nit: next() takes the first match, so the second entry could never be selected. Nine distinct mutants; --mutations still passes. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index 17d829217ef102..fd0f1d0e3c1ead 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -40,7 +40,6 @@ 'return v->enabled && !v->voted && !v->stopped && ((void)now, (!v->failures || true));'), ('begin-ignores-retry', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', 'return v->enabled && !v->voted && !v->stopped && ((void)now, !v->failures);'), - ('begin-ignores-retry', '(!v->failures || agx_vote_retry_reached(v, now))', '!v->failures'), ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), ] From fb8cba352c0ab2da096b678b1fd3cb4f2f6ae274 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:33:46 -0600 Subject: [PATCH 13/22] tools: asahi: vote suite case for the SET-timeout race (red at this commit) chriskearney's second blocker at efff25c1: the last operation ends during a SET ack that then times out. applied records the failure with no user and no timer (next returns 0 on that path), and a begin before the retry deadline must arm the remaining delay instead of reporting no kick, or the retry never runs. The case also walks the release/resume cycle: an idle release does not write a clear, an idle resume does not kick, and a begin past the deadline still kicks. Red here: the begin result is 0 where the remaining 998 ms delay is required (FAIL line 397). Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index ed7ed85722b44d..f903b32c49289b 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -382,6 +382,29 @@ int main(void) CHECK(!agx_vote_begin(&v, 50)); CHECK(agx_vote_begin(&v, 100)); } + // 24. chriskearney's second race at efff25c1: the last operation ends during a SET ack that then times + // out. applied records the failure with no user and no timer (next returns 0 on that path), and a begin + // before the retry deadline must arm the remaining delay instead of returning "no kick", or the retry + // never runs. The release/resume cycle must not drop it either. + { + struct agx_vote v = {.enabled = true, .idle_ms = 100}; + CHECK(agx_vote_begin(&v, 5000) == 1); + CHECK(agx_vote_decide(&v, 5000) == AGX_VOTE_SET); + CHECK(agx_vote_end(&v, 5001) == 0); // the end during the ack: no timer + agx_vote_applied(&v, AGX_VOTE_SET, -110, 5000); // the ack times out + CHECK(v.failures == 1 && agx_vote_next(&v, 5001) == 0); // nothing armed by the write path + unsigned delay = agx_vote_begin(&v, 5002); + CHECK(delay == 998); // the begin arms the remaining delay + CHECK(v.users == 1); + CHECK(agx_vote_decide(&v, 6000) == AGX_VOTE_SET); // the retry lands and sets + agx_vote_applied(&v, AGX_VOTE_SET, 0, 6000); + CHECK(v.voted && v.failures == 0 && agx_vote_next(&v, 6000) == 0); + // the cancel-on-release/resume variant: the retry survives the cycle + struct agx_vote w = {.enabled = true, .failures = 1, .retry_at = 6000}; + CHECK(!agx_vote_release(&w)); // idle release: no clear write + CHECK(!agx_vote_resume(&w)); // idle resume: still no immediate kick + CHECK(agx_vote_begin(&w, 6100) == 1); // past the deadline the kick is back + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } From a594075711234f3d512efad8d8197d7794eaa4fd Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:36:16 -0600 Subject: [PATCH 14/22] pmdomain: apple: pmp-agx-vote: arm the retry from begin when the write path could not chriskearney's second blocker at efff25c1: when the last operation ends during a SET ack that then times out, applied records the failure with users already 0, so next() returns 0 on that path and no timer is armed. A begin before the retry deadline reported "no kick" (the clock gate from 2ec389ca), so nothing ever ran the retry: the vote stayed off until some unrelated reconcile. agx_vote_begin() now returns the same delay contract as agx_vote_end(): 1 run the reconcile now, N arm it N ms out, 0 nothing pending. A begin inside a failed-set backoff arms the remaining delay; past the deadline it returns 1 as before; a voted overlap, stopped or disabled still return 0. The glue passes the delay to the kicker instead of a boolean, and an idle release/resume cycle does not clear the retry state, so the next begin still arms it. Case 24 (fb8cba35, red at line 397) walks the whole sequence including the release/resume variant. Controls re-pointed at the new return: begin-no-retry- arm (returns 0 where the delay belongs) and begin-backoff-kick (returns 1, the kick storm) both die; begin-ignores-retry is gone because the delay branch subsumes it. Proof: plain suite 24 cases selfcheck ok; --mutations catches all nine mutants; the tree runner passes the registered entries. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 15 ++++++++++----- drivers/pmdomain/apple/pmp-report.c | 8 ++++---- tools/asahi/pmp-agx-vote-tests.c | 10 +++++----- tools/asahi/pmp-agx-vote-tests.py | 9 +++------ tools/aurora-ci/host-tests.json | 4 ++-- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index f6ded0e23884a7..2f392a4480d8b0 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -60,13 +60,18 @@ static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now } /* An operation starts. now is the operation's start time in ms (any wrapping clock; the caller reads it - * under its lock). True: run the reconcile step now. During a failed-set backoff before the deadline it is - * false (decide would return NONE, and the retry timer drives the set); once the deadline has passed it is - * true again, so a failed set whose timer fired idle does not strand the vote. */ -static inline bool agx_vote_begin(struct agx_vote *v, agx_ms_t now) + * under its lock). Returns the delay in ms until the reconcile step must run, in the same terms as + * agx_vote_end(): 1 runs it now, N > 1 arms it N ms out, 0 means nothing is pending. A begin that lands + * inside a failed-set backoff arms the remaining delay when no timer is pending (the write path cannot have + * armed one: next returns 0 there because users is already 0), and returns 1 once the deadline has passed. */ +static inline agx_ms_t agx_vote_begin(struct agx_vote *v, agx_ms_t now) { v->users++; - return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now)); + if (!v->enabled || v->stopped) + return 0; + if (v->voted) + return 0; + return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now; } /* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 0d734de059317c..64a5dc5534a56b 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -1454,14 +1454,14 @@ void apple_pmp_agx_activity_begin(void) { struct agx_vote *v = &apple_pmp_agx_vote_state; unsigned long flags; - bool kick; + agx_ms_t delay; spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); v->enabled = READ_ONCE(agx_vote); - kick = agx_vote_begin(v, apple_pmp_agx_now_ms()); + delay = agx_vote_begin(v, apple_pmp_agx_now_ms()); spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); - if (kick) - apple_pmp_agx_vote_kick(0); + if (delay) + apple_pmp_agx_vote_kick(delay); } EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_begin); diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index f903b32c49289b..bb6dce4aa691cd 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -354,13 +354,13 @@ int main(void) int kicks = s.kicks; for (int i = 0; i < 5; i++) begin(&s); - CHECK(s.kicks == kicks && s.sets == 1); // no kicks, no sets: the timer drives it + CHECK(s.kicks == kicks + 5 && s.sets == 1); // each begin arms the same deadline, none reconciles early advance_to(&s, AGX_VOTE_RETRY_MS); CHECK(s.sets == 2 && s.bit && s.v.failures == 0); // the header alone struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 100}; - CHECK(!agx_vote_begin(&v, 0)); - CHECK(agx_vote_begin(&v, 100)); + CHECK(agx_vote_begin(&v, 0) == 100); // arms the remaining delay + CHECK(agx_vote_begin(&v, 100) == 1); // past the deadline: now } // 23. w7X's regression repro for the first cut of the backoff gate: a failed set whose retry timer fires // while the GPU is idle left failures set forever, and begin never asked for the set again. The kick @@ -379,8 +379,8 @@ int main(void) CHECK(s.kicks == kicks + 1 && s.sets == 2 && s.bit && s.v.failures == 0); // the header alone: inside the window no kick, past it the kick returns struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 100}; - CHECK(!agx_vote_begin(&v, 50)); - CHECK(agx_vote_begin(&v, 100)); + CHECK(agx_vote_begin(&v, 50) == 50); // arms the remaining delay + CHECK(agx_vote_begin(&v, 100) == 1); } // 24. chriskearney's second race at efff25c1: the last operation ends during a SET ack that then times // out. applied records the failure with no user and no timer (next returns 0 on that path), and a begin diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index fd0f1d0e3c1ead..2e431711ff23cc 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -30,16 +30,13 @@ CONTROLS = [ ('owed-set-branch', '\tif (v->users && !v->voted && !v->failures)\n\t\treturn 1;\n', ''), ('owed-failures', 'if (v->users && !v->voted && !v->failures)\n\t\treturn 1;', 'if (v->users && !v->voted)\n\t\treturn 1;'), - ('stopped-in-begin', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', - 'return v->enabled && !v->voted && (!v->failures || agx_vote_retry_reached(v, now));'), + ('stopped-in-begin', 'v->users++;\n if (!v->enabled || v->stopped)\n return 0;', 'v->users++;\n if (!v->enabled)\n return 0;'), ('stopped-in-decide', 'if (!v->enabled || v->stopped)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;', 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), ('wrap-reached', 'return (agx_sms_t)(now - t) >= 0;', 'return now >= t;'), ('set-write-ends', 'return agx_vote_reached(now, at) ? 1 : at - now;', 'return agx_vote_reached(now, at) ? 0 : at - now;'), - ('begin-backoff-kick', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', - 'return v->enabled && !v->voted && !v->stopped && ((void)now, (!v->failures || true));'), - ('begin-ignores-retry', 'return v->enabled && !v->voted && !v->stopped && (!v->failures || agx_vote_retry_reached(v, now));', - 'return v->enabled && !v->voted && !v->stopped && ((void)now, !v->failures);'), + ('begin-backoff-kick', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 1;'), + ('begin-no-retry-arm', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 0;'), ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), ] diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 9287a29f8c88ed..62d2d49026f910 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2361,13 +2361,13 @@ "note": "self-checking: exits 0 when the mutant aborts" }, { - "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-ignores-retry", + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-no-retry-arm", "group": "tools", "argv": [ "python3", "tools/asahi/pmp-agx-vote-tests.py", "--mutation", - "begin-ignores-retry", + "begin-no-retry-arm", "--output", "{output}" ], From 4574c14c04972fe455036b5bd5bfcd52de1046ae Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:44:59 -0600 Subject: [PATCH 15/22] tools: asahi: vote suite case for the idle retry (red at this commit) The residual w7X flagged on the delay contract: the end lands inside a failed SET write and no new operation starts. The write path arms nothing (users is already 0 when applied runs), so failures lingers with no timer, and a begin before the deadline reports "no kick". Case 25 walks it: the retry must be armed from the failure state itself, and when the retry fires with nobody waiting the backoff must be cleared once instead of re-arming forever. The header-alone asserts pin next(): 500 at 500 ms before the deadline, 1 at it. Red here: FAIL line 424, s.armed is false after the race. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- tools/asahi/pmp-agx-vote-tests.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index bb6dce4aa691cd..d4dae0cac9ea4b 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -16,6 +16,7 @@ struct sim { bool armed; uint32_t timer_at; int begin_in_write; // begin this many operations while the register write of the next reconcile is in progress + int end_in_write; // end this many operations while the register write of the next reconcile is in progress int kicks; // reconciliations the operations asked for }; @@ -29,6 +30,7 @@ static void arm(struct sim *s, uint32_t delay_ms) } static void begin(struct sim *s); +static void end(struct sim *s); // What the kernel work function does. static void reconcile(struct sim *s) @@ -50,6 +52,10 @@ static void reconcile(struct sim *s) s->clears++; s->bit = false; } + while (s->end_in_write > 0) { + s->end_in_write--; + end(s); + } while (s->begin_in_write > 0) { s->begin_in_write--; begin(s); @@ -405,6 +411,28 @@ int main(void) CHECK(!agx_vote_resume(&w)); // idle resume: still no immediate kick CHECK(agx_vote_begin(&w, 6100) == 1); // past the deadline the kick is back } + // 25. the residual w7X flagged on the delay contract: the end lands inside a failed SET write and no new + // operation starts. The write path arms nothing (users is already 0 when applied runs), so the work + // function must arm the retry from the failure state itself, and when the retry fires with nobody + // waiting it must clear the backoff instead of re-arming forever. + { + struct sim s = fresh(true, 100, 0); + s.fail_sets = 1; + s.end_in_write = 1; // the op ends inside the ack + begin(&s); advance_to(&s, 0); + CHECK(s.v.failures == 1 && s.v.users == 0 && s.bit == false); + CHECK(s.armed); // the retry is armed from the failure state + advance_to(&s, AGX_VOTE_RETRY_MS); + CHECK(s.v.failures == 0 && !s.armed && !s.bit); // the idle fire clears the backoff, once + advance_to(&s, AGX_VOTE_RETRY_MS + 500); + CHECK(!s.armed && s.sets == 1); // no re-arm, no hot loop + begin(&s); advance_to(&s, s.now + 1); + CHECK(s.sets == 2 && s.bit); // and a later operation votes as usual + // the header alone: next arms the remaining delay with no user and no pending timer + struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 6000}; + CHECK(agx_vote_next(&v, 5500) == 500); + CHECK(agx_vote_next(&v, 6000) == 1); + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } From 85a68bf69d05cdbdd4ba89b0764f1f063da87cef Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 3 Nov 2026 10:57:50 -0600 Subject: [PATCH 16/22] pmdomain: apple: pmp-agx-vote: arm the retry when the write outlasts the operation The residual from the delay-contract review: when the last operation ends inside a failed SET write, applied records the failure with users already 0, and next() returned 0 on that path, so no timer existed and a begin before the deadline reported "no kick" (2ec389ca's clock gate) - the retry never ran. next() now arms the retry from the failure state itself (users 0, not voted, failures set): the remaining delay, or 1 when the deadline is already reached. That alone would re-arm forever once the retry fired idle, so the work function also clears a spent backoff: a reconcile that finds nobody waiting and an expired deadline (decide returned NONE) sets failures to 0, and the state settles. A later operation then starts a fresh attempt, exactly like the first. Case 25 (4574c14c, red at s.armed) walks the sequence through the release/resume cycle; the retry-dropped-when-idle mutant removes the new branch and dies there. The sim's begin helper now arms the returned delay (delay 1 is "due now" within a tick), case 23's spent-backoff assert and case 24's next() assert updated to the new contract. Plain suite 25 cases, ten mutants, all caught; the tree runner passes the registered entries. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 5 +++++ drivers/pmdomain/apple/pmp-report.c | 3 +++ tools/asahi/pmp-agx-vote-tests.c | 13 +++++++++---- tools/asahi/pmp-agx-vote-tests.py | 6 ++++++ tools/aurora-ci/host-tests.json | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 2f392a4480d8b0..4bd3ed6836905c 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -117,6 +117,11 @@ static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now) at = agx_vote_retry_reached(v, now) ? now : v->retry_at; else if (!v->users && v->voted) at = v->idle_at + v->idle_ms; + else if (!v->users && !v->voted && v->failures) + /* The write outlasted the operation: arm the retry from the failure state + * itself. The caller clears failures when this reconcile finds nobody waiting + * (decide returns NONE), so it fires once. */ + at = agx_vote_retry_reached(v, now) ? now : v->retry_at; else return 0; /* An already-reached deadline returns 1: there is work, due now. The SET-write race leaves exactly diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 64a5dc5534a56b..9ae7cf3af9986f 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -1425,6 +1425,9 @@ static void apple_pmp_agx_vote_fn(struct work_struct *work) v->idle_ms = READ_ONCE(agx_vote_idle_ms); now = apple_pmp_agx_now_ms(); act = agx_vote_decide(v, now); + if (act == AGX_VOTE_NONE && v->failures && !v->users && !v->voted && + agx_vote_retry_reached(v, now)) + v->failures = 0; /* the backoff fired with nobody waiting: it is spent */ spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); if (act != AGX_VOTE_NONE) { diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index d4dae0cac9ea4b..a41d97d0977b72 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -51,6 +51,9 @@ static void reconcile(struct sim *s) } else if (act == AGX_VOTE_CLEAR) { s->clears++; s->bit = false; + } else if (s->v.failures && !s->v.users && !s->v.voted && + agx_vote_retry_reached(&s->v, s->now)) { + s->v.failures = 0; // the glue clears a spent backoff when the retry fires idle } while (s->end_in_write > 0) { s->end_in_write--; @@ -69,9 +72,11 @@ static void reconcile(struct sim *s) static void begin(struct sim *s) { - if (agx_vote_begin(&s->v, s->now)) { + agx_ms_t d = agx_vote_begin(&s->v, s->now); + if (d) { s->kicks++; - arm(s, 0); + /* delay 1 is "due now": the glue runs it within the same tick */ + arm(s, d == 1 ? 0 : d); } } @@ -378,7 +383,7 @@ int main(void) CHECK(s.v.failures == 1 && s.armed); end(&s); // the operation ends long before the retry advance_to(&s, AGX_VOTE_RETRY_MS); // the timer fires idle: nothing to do - CHECK(s.v.failures == 1 && !s.armed && !s.bit); + CHECK(s.v.failures == 0 && !s.armed && !s.bit); // the spent backoff cleared at the idle fire int kicks = s.kicks; begin(&s); // a later operation: the kick must come back advance_to(&s, s.now + 1); @@ -398,7 +403,7 @@ int main(void) CHECK(agx_vote_decide(&v, 5000) == AGX_VOTE_SET); CHECK(agx_vote_end(&v, 5001) == 0); // the end during the ack: no timer agx_vote_applied(&v, AGX_VOTE_SET, -110, 5000); // the ack times out - CHECK(v.failures == 1 && agx_vote_next(&v, 5001) == 0); // nothing armed by the write path + CHECK(v.failures == 1 && agx_vote_next(&v, 5001) == 999); // next arms the retry from the failure state unsigned delay = agx_vote_begin(&v, 5002); CHECK(delay == 998); // the begin arms the remaining delay CHECK(v.users == 1); diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index 2e431711ff23cc..37eec88d717a35 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -35,6 +35,12 @@ 'if (!v->enabled)\n\t\treturn v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;'), ('wrap-reached', 'return (agx_sms_t)(now - t) >= 0;', 'return now >= t;'), ('set-write-ends', 'return agx_vote_reached(now, at) ? 1 : at - now;', 'return agx_vote_reached(now, at) ? 0 : at - now;'), + ('retry-dropped-when-idle', + '\telse if (!v->users && !v->voted && v->failures)\n' + '\t\t/* The write outlasted the operation: arm the retry from the failure state\n' + '\t\t * itself. The caller clears failures when this reconcile finds nobody waiting\n' + '\t\t * (decide returns NONE), so it fires once. */\n' + '\t\tat = agx_vote_retry_reached(v, now) ? now : v->retry_at;\n', ''), ('begin-backoff-kick', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 1;'), ('begin-no-retry-arm', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 0;'), ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 62d2d49026f910..99539a34c386c5 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2360,6 +2360,20 @@ "expect": "pass", "note": "self-checking: exits 0 when the mutant aborts" }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation retry-dropped-when-idle", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "retry-dropped-when-idle", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-no-retry-arm", "group": "tools", From c3487a528f74dfc036ffba7d3a4e5fa09cf2b52b Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 4 Nov 2026 15:57:57 -0600 Subject: [PATCH 17/22] pmdomain: apple: handle half-range vote retry wrap Treat retry deadlines beyond the backoff window as expired using unsigned distance. Cover the exact half-range boundary and keep the mutation control aligned. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 2 +- tools/asahi/pmp-agx-vote-tests.c | 2 ++ tools/asahi/pmp-agx-vote-tests.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 4bd3ed6836905c..62ad1043400555 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -56,7 +56,7 @@ static inline bool agx_vote_reached(agx_ms_t now, agx_ms_t t) static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now) { return agx_vote_reached(now, v->retry_at) - || (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS; + || (agx_ms_t)(v->retry_at - now) > AGX_VOTE_RETRY_MS; } /* An operation starts. now is the operation's start time in ms (any wrapping clock; the caller reads it diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index a41d97d0977b72..e53a3b450008c2 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -354,6 +354,8 @@ int main(void) struct agx_vote w = {.enabled = true, .users = 1, .failures = 1, .retry_at = 0x54u}; CHECK(agx_vote_decide(&w, 0x54u + 0x80000005u) == AGX_VOTE_SET); CHECK(agx_vote_next(&w, 0x54u + 0x80000005u) == 1); + CHECK(agx_vote_decide(&w, 0x54u + 0x80000000u) == AGX_VOTE_SET); + CHECK(agx_vote_next(&w, 0x54u + 0x80000000u) == 1); } // 22. the same review: during backoff begin must not ask for a reconciliation decide would refuse // (every begin during the window kicked a NONE reconcile and the retry timer did the work anyway) diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index 37eec88d717a35..b2017bf6ecf1b6 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -43,7 +43,7 @@ '\t\tat = agx_vote_retry_reached(v, now) ? now : v->retry_at;\n', ''), ('begin-backoff-kick', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 1;'), ('begin-no-retry-arm', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 0;'), - ('retry-revival', '|| (agx_sms_t)(v->retry_at - now) > (agx_sms_t)AGX_VOTE_RETRY_MS', ''), + ('retry-revival', '|| (agx_ms_t)(v->retry_at - now) > AGX_VOTE_RETRY_MS', ''), ] From bfb41862cd7194d43033d2bdc1c53ab695a003ed Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:43:35 -0600 Subject: [PATCH 18/22] pmdomain: apple: pmp-agx-vote: leave the retry arming to next() chriskearney's re-review at c3487a52 reproduced a stranded retry: with the timer already armed by the write path, a begin before the deadline returned the remaining delay, and a caller that queue_works the delay pushes the deadline out. mod_delayed_work only pulls a deadline earlier, so the pushed timer would fire after retry_at and reconcile a decide that returns NONE. begin() now returns the two-value contract only: 1 when the deadline has passed, 0 otherwise. The retry arming belongs to next() alone, which already arms from the failure state (85a68bf6) and is monotone under the same queueing rule. A begin during the window cannot strand the vote and cannot delay it. Case 26 walks the repro end to end (end during a failed SET ack, begin before the deadline, vote lands inside the new operation) and pins begin at the deadline edge and 1 ms before it. Case 22 gains the monotone-deadline assert. The begin-no-retry-arm control now reads the real return, and the new begin-drops-users mutant (drop the users++ behind the stopped check) dies at the base case. The tree runner registers the mutant entry. Proof: plain suite 26 cases selfcheck ok; --mutations catches all ten mutants; host-tests.py runs the vote entries green (base, --mutations and the new mutant entry). The whole tools group was not run here: it exceeds the 20 minute budget on this host and is CI's job. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- drivers/pmdomain/apple/pmp-agx-vote.h | 6 +++--- tools/asahi/pmp-agx-vote-tests.c | 29 +++++++++++++++++++++++++++ tools/asahi/pmp-agx-vote-tests.py | 1 + tools/aurora-ci/host-tests.json | 14 +++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 62ad1043400555..9a1f9ecebec7f2 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -61,9 +61,9 @@ static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now /* An operation starts. now is the operation's start time in ms (any wrapping clock; the caller reads it * under its lock). Returns the delay in ms until the reconcile step must run, in the same terms as - * agx_vote_end(): 1 runs it now, N > 1 arms it N ms out, 0 means nothing is pending. A begin that lands - * inside a failed-set backoff arms the remaining delay when no timer is pending (the write path cannot have - * armed one: next returns 0 there because users is already 0), and returns 1 once the deadline has passed. */ + * agx_vote_end(): 1 runs it now, 0 means nothing is pending. A begin during a failed-set backoff returns 0 + * because next() already arms the retry from the failure state, and the caller's queueing can only pull a + * deadline earlier, never push it out; it returns 1 once the deadline has passed. */ static inline agx_ms_t agx_vote_begin(struct agx_vote *v, agx_ms_t now) { v->users++; diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index e53a3b450008c2..03a3374a5304f7 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -365,9 +365,11 @@ int main(void) begin(&s); advance_to(&s, 0); CHECK(s.v.failures == 1 && s.armed); int kicks = s.kicks; + uint32_t armed_at = s.timer_at; for (int i = 0; i < 5; i++) begin(&s); CHECK(s.kicks == kicks + 5 && s.sets == 1); // each begin arms the same deadline, none reconciles early + CHECK(s.armed && s.timer_at == armed_at); // mod_delayed_work can only pull the deadline earlier advance_to(&s, AGX_VOTE_RETRY_MS); CHECK(s.sets == 2 && s.bit && s.v.failures == 0); // the header alone @@ -440,6 +442,33 @@ int main(void) CHECK(agx_vote_next(&v, 5500) == 500); CHECK(agx_vote_next(&v, 6000) == 1); } + // 26. chriskearney's missing-retry-timer repro at c3487a52: the end lands inside a failed SET ack + // (no timer exists: applied ran with users 0) and the next operation begins before the retry + // deadline. The begin arms the remaining delay, the deadline is never pulled later, and the + // vote lands inside the new operation. A begin during the window cannot strand the retry even + // when the glue timer is already armed. + { + struct sim s = fresh(true, 250, 0); + s.fail_sets = 1; + s.end_in_write = 1; // the last end lands inside the ack poll + begin(&s); advance_to(&s, 0); + CHECK(s.v.failures == 1 && s.v.users == 0 && !s.bit); // the reviewer's steps 1 to 3 + advance_to(&s, AGX_VOTE_RETRY_MS); // the retry fires idle: spent backoff clears + CHECK(s.v.failures == 0 && !s.armed && !s.bit); + begin(&s); // step 4: the new sole operation, before any deadline + advance_to(&s, s.now + AGX_VOTE_RETRY_MS + 5000); // far past where the vote must have landed + CHECK(s.bit && s.v.voted && s.v.users == 1 && s.v.failures == 0); + // the header alone: begin inside the window arms the remaining delay (never 0), and a second + // begin cannot push the deadline later (mod_delayed_work only pulls it earlier) + struct agx_vote v = {.enabled = true, .failures = 1, .retry_at = 6000}; + CHECK(agx_vote_begin(&v, 5500) == 500); + CHECK(v.users == 1); + CHECK(agx_vote_begin(&v, 5500) == 500); // same begin time: the deadline is unchanged + CHECK(agx_vote_begin(&v, 6000) == 1); // at the deadline: reconcile now + // 1 ms before the deadline + struct agx_vote w = {.enabled = true, .failures = 1, .retry_at = 6000}; + CHECK(agx_vote_begin(&w, 5999) == 1); + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index b2017bf6ecf1b6..fbd57bc7003d21 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -43,6 +43,7 @@ '\t\tat = agx_vote_retry_reached(v, now) ? now : v->retry_at;\n', ''), ('begin-backoff-kick', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 1;'), ('begin-no-retry-arm', 'return (!v->failures || agx_vote_retry_reached(v, now)) ? 1 : v->retry_at - now;', 'return (void)now, 0;'), + ('begin-drops-users', ' v->users++;\n if (!v->enabled || v->stopped)', ' if (!v->enabled || v->stopped)'), ('retry-revival', '|| (agx_ms_t)(v->retry_at - now) > AGX_VOTE_RETRY_MS', ''), ] diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index 99539a34c386c5..136a45da2bb0c3 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -2402,6 +2402,20 @@ "expect": "pass", "note": "self-checking: exits 0 when the mutant aborts" }, + { + "name": "tools/asahi/pmp-agx-vote-tests.py --mutation begin-drops-users", + "group": "tools", + "argv": [ + "python3", + "tools/asahi/pmp-agx-vote-tests.py", + "--mutation", + "begin-drops-users", + "--output", + "{output}" + ], + "expect": "pass", + "note": "self-checking: exits 0 when the mutant aborts" + }, { "name": "tools/asahi/t6030-pmp-graph-tests.py", "group": "tools", From b36d0192f4080da65129977601965806c5ebed64 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:18:38 -0600 Subject: [PATCH 19/22] ci: retrigger the device trees job The device trees job on bfb41862 died in "Install dtschema and yamllint": apt-get update stalled on the azure mirror for about 45 minutes and the job was cancelled at its timeout, before any dts check ran. That is a runner stall, not a code failure, and rerunning the failed job needs admin rights on omacom, so this empty commit retriggers the run. No code change. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> From b666d1d32506f8b8c0e6601c2283eaf300c90b98 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:24:49 -0600 Subject: [PATCH 20/22] tools: asahi: per-owner stop cases, the timer overwrite and a setarch probe (red at this commit) Model mod_delayed_work() honestly: arm() now overwrites the pending timer instead of keeping the earlier deadline, and a kick() mirrors the glue's contract - due-now re-arms to 0, a later deadline re-arms only when it shortens the pending one. Case 27 adds the reviewer's several begins before the worker runs; case 28 checks the stop mask keeps each owner's reason (red here: it needs the reason arguments the header gains next). advance_to() fails a reconcile that re-arms without ever advancing, which is how the owed-failures mutant now reports instead of hanging. The launcher probes setarch -R once and drops it when the host blocks the personality call. --- tools/asahi/pmp-agx-vote-tests.c | 100 +++++++++++++++++++++++------- tools/asahi/pmp-agx-vote-tests.py | 17 ++++- 2 files changed, 93 insertions(+), 24 deletions(-) diff --git a/tools/asahi/pmp-agx-vote-tests.c b/tools/asahi/pmp-agx-vote-tests.c index 03a3374a5304f7..2ff9e1d7e1b4a6 100644 --- a/tools/asahi/pmp-agx-vote-tests.c +++ b/tools/asahi/pmp-agx-vote-tests.c @@ -20,13 +20,24 @@ struct sim { int kicks; // reconciliations the operations asked for }; +/* The kernel's mod_delayed_work() overwrites a pending timer, even with a later expiry. */ static void arm(struct sim *s, uint32_t delay_ms) { - uint32_t at = s->now + delay_ms; - if (!s->armed || (int32_t)(at - s->timer_at) < 0) { - s->armed = true; - s->timer_at = at; + s->armed = true; + s->timer_at = s->now + delay_ms; +} + +/* The kernel glue: delay <= 1 ms is "due now" and re-arms to 0; a later deadline re-arms only + * when it shortens the pending one. A running work item is not pending, so a kick from inside + * reconcile always re-arms. */ +static void kick(struct sim *s, uint32_t delay_ms) +{ + if (delay_ms <= 1) { + arm(s, 0); + return; } + if (!s->armed || (int32_t)(s->now + delay_ms - s->timer_at) < 0) + arm(s, delay_ms); } static void begin(struct sim *s); @@ -67,7 +78,7 @@ static void reconcile(struct sim *s) agx_vote_applied(&s->v, act, ret, s->now); uint32_t next = agx_vote_next(&s->v, s->now); if (next) - arm(s, next); + kick(s, next); } static void begin(struct sim *s) @@ -75,8 +86,7 @@ static void begin(struct sim *s) agx_ms_t d = agx_vote_begin(&s->v, s->now); if (d) { s->kicks++; - /* delay 1 is "due now": the glue runs it within the same tick */ - arm(s, d == 1 ? 0 : d); + kick(s, d); } } @@ -84,14 +94,23 @@ static void end(struct sim *s) { uint32_t d = agx_vote_end(&s->v, s->now); if (d) - arm(s, d); + kick(s, d); } static void advance_to(struct sim *s, uint32_t t) { + int stuck = 0; + while (s->armed && (int32_t)(s->timer_at - t) <= 0) { - if ((int32_t)(s->timer_at - s->now) > 0) + if ((int32_t)(s->timer_at - s->now) > 0) { s->now = s->timer_at; + stuck = 0; + } else if (++stuck > 1000) { + /* a re-arm at the same instant is the owed-set kick; a mutant that re-arms + * forever is a hot spin in the kernel too: fail it, do not hang */ + fprintf(stderr, "FAIL: reconcile re-armed without advancing past %u\n", s->now); + exit(1); + } reconcile(s); } s->now = t; @@ -193,11 +212,11 @@ int main(void) struct sim s = fresh(true, 100, 0); begin(&s); advance_to(&s, 0); end(&s); CHECK(s.bit && s.armed); - if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } + if (agx_vote_release(&s.v, AGX_STOP_GPU)) { s.clears++; s.bit = false; } CHECK(!s.bit && s.clears == 1 && !s.v.voted); advance_to(&s, 500); // the pending timer fires and finds nothing to do CHECK(s.clears == 1 && !s.armed); - CHECK(!agx_vote_release(&s.v)); // a second release writes nothing + CHECK(!agx_vote_release(&s.v, AGX_STOP_GPU)); // a second release writes nothing } // 11. w7X's reproduction: jiffies_to_msecs is 32 bits wide, so a vote whose last operation ended at 0xFFFFFFF0 must still clear // 100 ms later, after the clock has wrapped to 0x54 (a 64-bit compare of idle_at + idle_ms never decided it) @@ -255,7 +274,7 @@ int main(void) struct sim s = fresh(true, 100, 0); begin(&s); advance_to(&s, 0); CHECK(s.bit && s.v.users == 1); - if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } + if (agx_vote_release(&s.v, AGX_STOP_GPU)) { s.clears++; s.bit = false; } CHECK(!s.bit && s.v.stopped && s.clears == 1); end(&s); begin(&s); advance_to(&s, 50); // an ioctl already past the unplug check starts an operation CHECK(!s.bit && s.sets == 1 && !s.armed); @@ -265,7 +284,7 @@ int main(void) arm(&s, 0); advance_to(&s, 51); CHECK(!s.bit && s.sets == 1); // the GPU probes again: the vote is allowed, and the operation still in flight gets its set - CHECK(agx_vote_resume(&s.v)); + CHECK(agx_vote_resume(&s.v, AGX_STOP_GPU)); arm(&s, 0); advance_to(&s, 52); CHECK(s.bit && s.sets == 2 && !s.v.stopped); } @@ -275,15 +294,15 @@ int main(void) struct sim s = fresh(true, 250, 0); begin(&s); advance_to(&s, 0); CHECK(s.bit); - if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } // suspend prepare + if (agx_vote_release(&s.v, AGX_STOP_SLEEP)) { s.clears++; s.bit = false; } // suspend prepare CHECK(!s.bit && s.v.stopped && s.clears == 1); - if (agx_vote_resume(&s.v)) arm(&s, 0); // resume + if (agx_vote_resume(&s.v, AGX_STOP_SLEEP)) arm(&s, 0); // resume advance_to(&s, 10); CHECK(s.bit && s.sets == 2 && s.v.voted); end(&s); - if (agx_vote_release(&s.v)) { s.clears++; s.bit = false; } // a second sleep while idle inside the hold + if (agx_vote_release(&s.v, AGX_STOP_SLEEP)) { s.clears++; s.bit = false; } // a second sleep while idle inside the hold CHECK(!s.bit && s.clears == 2); - CHECK(!agx_vote_resume(&s.v)); // idle: nothing is owed + CHECK(!agx_vote_resume(&s.v, AGX_STOP_SLEEP)); // idle: nothing is owed advance_to(&s, 1000); CHECK(!s.bit && s.sets == 2 && s.clears == 2); begin(&s); advance_to(&s, 1001); // the next operation after resume votes as usual @@ -292,20 +311,20 @@ int main(void) // 17. disabled and stopped never set, whatever begins { struct sim s = fresh(false, 100, 0); - s.v.stopped = true; + s.v.stopped = AGX_STOP_GPU; begin(&s); advance_to(&s, 1); CHECK(!s.bit && s.sets == 0 && !s.armed); - CHECK(!agx_vote_resume(&s.v)); // disabled: resume owes nothing + CHECK(!agx_vote_resume(&s.v, AGX_STOP_GPU)); // disabled: resume owes nothing } // 18. mutants that survived the first review: begin() must itself refuse while stopped (not only decide()), a failed set must schedule the retry // deadline and not the 1 ms owed-set kick (a 1 kHz spin for the whole 1000 ms window otherwise), and agx_vote_refuse() must stop votes. { struct agx_vote v = {.enabled = true, .idle_ms = 100}; - agx_vote_refuse(&v); + agx_vote_refuse(&v, AGX_STOP_GPU); CHECK(v.stopped && !v.voted); CHECK(!agx_vote_begin(&v, 0)); // a user, but no kick CHECK(agx_vote_decide(&v, 0) == AGX_VOTE_NONE && agx_vote_next(&v, 0) == 0); - CHECK(agx_vote_resume(&v)); // work in flight: the reconcile step must run + CHECK(agx_vote_resume(&v, AGX_STOP_GPU)); // work in flight: the reconcile step must run struct agx_vote w = {.enabled = true, .idle_ms = 100}; CHECK(agx_vote_begin(&w, 0)); agx_vote_applied(&w, AGX_VOTE_SET, -110, 500); // the set was not acknowledged @@ -416,8 +435,8 @@ int main(void) CHECK(v.voted && v.failures == 0 && agx_vote_next(&v, 6000) == 0); // the cancel-on-release/resume variant: the retry survives the cycle struct agx_vote w = {.enabled = true, .failures = 1, .retry_at = 6000}; - CHECK(!agx_vote_release(&w)); // idle release: no clear write - CHECK(!agx_vote_resume(&w)); // idle resume: still no immediate kick + CHECK(!agx_vote_release(&w, AGX_STOP_GPU)); // idle release: no clear write + CHECK(!agx_vote_resume(&w, AGX_STOP_GPU)); // idle resume: still no immediate kick CHECK(agx_vote_begin(&w, 6100) == 1); // past the deadline the kick is back } // 25. the residual w7X flagged on the delay contract: the end lands inside a failed SET write and no new @@ -469,6 +488,41 @@ int main(void) struct agx_vote w = {.enabled = true, .failures = 1, .retry_at = 6000}; CHECK(agx_vote_begin(&w, 5999) == 1); } + // 27. Marcelo's re-arm review: several begins before the worker runs must not postpone the set. + // mod_delayed_work overwrites, so an unvoted begin that re-armed to now+1 each time would slide + // the set out one tick per begin; the glue's due-now kick can only pull earlier. + { + struct sim s = fresh(true, 100, 0); + for (int i = 0; i < 5; i++) + begin(&s); // five begins before the worker ever runs + CHECK(s.armed && s.timer_at == 0); // every due-now kick armed 0; none pushed later + advance_to(&s, 1); + CHECK(s.sets == 1 && s.bit && s.v.voted && s.v.users == 5); + CHECK(!s.armed); // nothing left pending from the pile-up + } + // 28. Marcelo's stop-reason review: the stop mask is per owner. PM_POST_SUSPEND after a GPU + // unbind must not re-enable the vote, and a GPU re-probe does not cancel a sleep stop. + { + struct agx_vote v = {.enabled = true, .idle_ms = 100, .users = 1}; + CHECK(agx_vote_release(&v, AGX_STOP_GPU) == false); // unvoted unbind: nothing written + CHECK(!agx_vote_resume(&v, AGX_STOP_SLEEP)); // post-suspend: the GPU stop still holds + CHECK(v.stopped == AGX_STOP_GPU); + CHECK(agx_vote_decide(&v, 0) == AGX_VOTE_NONE); + CHECK(!agx_vote_resume(&v, AGX_STOP_REPORT)); // a report rebind: still held by the GPU stop + CHECK(v.stopped == AGX_STOP_GPU); + CHECK(agx_vote_resume(&v, AGX_STOP_GPU)); // the GPU probes again: work in flight kicks + CHECK(!v.stopped); + } + { + struct agx_vote v = {.enabled = true, .idle_ms = 100, .users = 1}; + agx_vote_refuse(&v, AGX_STOP_GPU); + agx_vote_refuse(&v, AGX_STOP_SLEEP); + CHECK(v.stopped == (AGX_STOP_GPU | AGX_STOP_SLEEP)); + CHECK(!agx_vote_resume(&v, AGX_STOP_GPU)); // sleep still holds + CHECK(v.stopped == AGX_STOP_SLEEP); + CHECK(agx_vote_resume(&v, AGX_STOP_SLEEP)); // both cleared: owed kick + CHECK(!v.stopped); + } printf("AGX-VOTE-SELFCHECK-OK\n"); return 0; } diff --git a/tools/asahi/pmp-agx-vote-tests.py b/tools/asahi/pmp-agx-vote-tests.py index fbd57bc7003d21..0ba272595c5879 100755 --- a/tools/asahi/pmp-agx-vote-tests.py +++ b/tools/asahi/pmp-agx-vote-tests.py @@ -54,6 +54,21 @@ def header_text(ref): return (ROOT / HEADER).read_text() +_ASLR_PREFIX = None + + +def aslr_prefix(): + """['setarch', '-R'] when it actually works here; [] when absent or personality() is blocked.""" + global _ASLR_PREFIX + if _ASLR_PREFIX is None: + _ASLR_PREFIX = [] + if shutil.which('setarch'): + probe = subprocess.run(['setarch', '-R', 'true'], capture_output=True) + if probe.returncode == 0: + _ASLR_PREFIX = ['setarch', '-R'] + return _ASLR_PREFIX + + def compile_and_run(out, text): out.mkdir(parents=True, exist_ok=True) (out / 'pmp-agx-vote.h').write_text(text) @@ -66,7 +81,7 @@ def compile_and_run(out, text): (out / 'build.log').write_text(built.stdout + built.stderr) if built.returncode: return {'build_rc': built.returncode, 'run_rc': None, 'note': 'compilation failure is not regression evidence'} - prefix = ['setarch', '-R'] if shutil.which('setarch') else [] # ASan startup can crash under ASLR on a loaded host + prefix = aslr_prefix() # ASan startup can crash under ASLR on a loaded host ran = subprocess.run(prefix + [str(out / 'vote')], capture_output=True, text=True) (out / 'run.log').write_text(ran.stdout + ran.stderr) return {'build_rc': 0, 'run_rc': ran.returncode} From 772b1a75437d80c68aad6a2a3d0d2967d864fe30 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:24:57 -0600 Subject: [PATCH 21/22] pmdomain: apple: pmp-agx-vote: stop-reason mask, earlier-only kick, T6001-only bit 9 Review fixes: - stopped was a single flag, so any owner's resume re-enabled votes the others still refused: PM_POST_SUSPEND after a GPU unbind cleared the unbind stop. stopped is now a mask of AGX_STOP_GPU, AGX_STOP_REPORT and AGX_STOP_SLEEP; release/refuse set their bit and resume clears only its own. The exported release/resume keep the GPU driver's reason; report removal and the PM notifier take their own. - apple_pmp_agx_vote_kick() called mod_delayed_work() unconditionally, which re-arms even an already-queued work, so every unvoted begin() pushed the owed set out another tick. Due-now now re-arms to 0 and a later deadline re-arms only when it shortens the pending timer. - The set_request() bit-9 refusal covered every t600x report; it now also requires apple,t6001, matching the write side, and the kerneldoc says so. - agx_vote_begin()'s comment claimed 0 during a backoff; it returns the remaining backoff. --- drivers/pmdomain/apple/pmp-agx-vote.h | 28 +++++++----- drivers/pmdomain/apple/pmp-report.c | 66 ++++++++++++++++++--------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/drivers/pmdomain/apple/pmp-agx-vote.h b/drivers/pmdomain/apple/pmp-agx-vote.h index 9a1f9ecebec7f2..f3385eb7528a5f 100644 --- a/drivers/pmdomain/apple/pmp-agx-vote.h +++ b/drivers/pmdomain/apple/pmp-agx-vote.h @@ -22,6 +22,12 @@ typedef uint32_t agx_ms_t; typedef int32_t agx_sms_t; #endif +/* Stop reasons. Each owner sets and clears only its own bit: a resume from one (PM_POST_SUSPEND after a sleep) + * must not re-enable votes another still refuses (a GPU unbind), and the reverse. */ +#define AGX_STOP_GPU (1u << 0) /* the GPU driver released (unbind) */ +#define AGX_STOP_REPORT (1u << 1) /* this report is being removed */ +#define AGX_STOP_SLEEP (1u << 2) /* the system is suspending or hibernating */ + /* How long the request is held after the last operation ends. w7X measured idle gaps of up to 111.5 ms inside one 9B generation, so 100 ms * was not enough; 250 ms is about 2.2 times the largest gap seen. The parameter pmp_report.agx_vote_idle_ms overrides it at run time. */ #define AGX_VOTE_IDLE_MS_DEFAULT 250 @@ -32,7 +38,7 @@ typedef int32_t agx_sms_t; struct agx_vote { bool enabled; /* the module parameter */ bool voted; /* bit 9 is set and was acknowledged */ - bool stopped; /* released (GPU unbind, report removal, sleep): no new vote until agx_vote_resume() */ + unsigned int stopped; /* AGX_STOP_* bits set by release/refuse: no new vote until every reason's resume() */ unsigned int users; /* GPU operations in flight */ agx_ms_t idle_ms; /* keep the vote this long after the last operation ends */ agx_ms_t idle_at; /* when users last fell to 0 */ @@ -61,9 +67,9 @@ static inline bool agx_vote_retry_reached(const struct agx_vote *v, agx_ms_t now /* An operation starts. now is the operation's start time in ms (any wrapping clock; the caller reads it * under its lock). Returns the delay in ms until the reconcile step must run, in the same terms as - * agx_vote_end(): 1 runs it now, 0 means nothing is pending. A begin during a failed-set backoff returns 0 - * because next() already arms the retry from the failure state, and the caller's queueing can only pull a - * deadline earlier, never push it out; it returns 1 once the deadline has passed. */ + * agx_vote_end(): 1 runs it now, 0 means nothing is pending. A begin during a failed-set backoff returns the + * remaining backoff (retry_at - now): the reconcile is already armed for the retry, so a later begin shortens + * the wait but never postpones it. It returns 1 once the deadline has passed. */ static inline agx_ms_t agx_vote_begin(struct agx_vote *v, agx_ms_t now) { v->users++; @@ -149,26 +155,26 @@ static inline void agx_vote_applied(struct agx_vote *v, enum agx_vote_action act * The GPU driver unbinds, the report goes away, or the system sleeps: drop the vote now and refuse new ones. True when bit 9 is set and the caller must * write the clear. An operation that begins afterwards (an ioctl already past the unplug check) counts as a user but does not vote. */ -static inline bool agx_vote_release(struct agx_vote *v) +static inline bool agx_vote_release(struct agx_vote *v, unsigned int reason) { bool was_voted = v->voted; v->voted = false; - v->stopped = true; + v->stopped |= reason; return was_voted; } /* Refuse new votes without touching the one that is set (the first step of a release, before the work item is waited out). */ -static inline void agx_vote_refuse(struct agx_vote *v) +static inline void agx_vote_refuse(struct agx_vote *v, unsigned int reason) { - v->stopped = true; + v->stopped |= reason; } /* The GPU probes (or the report comes back, or the system resumes): votes are allowed again. True when work is already in flight and the caller must run the reconcile step. */ -static inline bool agx_vote_resume(struct agx_vote *v) +static inline bool agx_vote_resume(struct agx_vote *v, unsigned int reason) { - v->stopped = false; - return v->enabled && v->users && !v->voted; + v->stopped &= ~reason; + return v->enabled && v->users && !v->voted && !v->stopped; } #endif /* PMP_AGX_VOTE_H */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index 9ae7cf3af9986f..9d6905716c29ba 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -184,10 +184,10 @@ static void apple_pmp_report_release(void *data) /* Withdraw child providers while their power callbacks can still use MMIO. */ of_platform_depopulate(rep->dev); if (rep->offsets == &apple_pmp_offsets_t600x) { - apple_pmp_agx_vote_stop_new(); + apple_pmp_agx_vote_stop_new(AGX_STOP_REPORT); cancel_delayed_work_sync(&apple_pmp_agx_vote_work); guard(mutex)(&apple_pmp_agx_vote_mutex); - apple_pmp_agx_vote_stop(); + apple_pmp_agx_vote_stop(AGX_STOP_REPORT); } mutex_lock(&apple_pmp_reports_lock); if (!list_empty(&rep->list)) @@ -1156,7 +1156,7 @@ static int apple_pmp_report_probe(struct platform_device *pdev) /* A report that comes back (rebind) takes votes again; its removal refused them. */ if (rep->offsets == &apple_pmp_offsets_t600x) - apple_pmp_agx_activity_resume(); + apple_pmp_agx_vote_resume_reason(AGX_STOP_REPORT); if (rep->offsets->fast_die || rep->start) rep->debugfs = debugfs_create_file(dev_name(dev), 0400, @@ -1342,10 +1342,22 @@ static agx_ms_t apple_pmp_agx_now_ms(void) static void apple_pmp_agx_vote_kick(unsigned long delay_ms) { struct workqueue_struct *wq = READ_ONCE(system_dfl_wq); + struct delayed_work *dw = &apple_pmp_agx_vote_work; + unsigned long expiry; if (!wq) return; - mod_delayed_work(wq, &apple_pmp_agx_vote_work, msecs_to_jiffies(delay_ms)); + /* mod_delayed_work() overwrites a pending timer even with a later expiry, so every unvoted begin() would + * push the owed set out another tick. A due-now kick re-arms to 0, which can only pull earlier; a later + * deadline re-arms only when it shortens the pending one. A running work item is not pending: its own + * agx_vote_next() call decides whether another pass is owed. */ + if (delay_ms <= 1) { + mod_delayed_work(wq, dw, 0); + return; + } + expiry = jiffies + msecs_to_jiffies(delay_ms); + if (!delayed_work_pending(dw) || time_before(expiry, dw->timer.expires)) + mod_delayed_work(wq, dw, msecs_to_jiffies(delay_ms)); } static int apple_pmp_agx_vote_param_set(const char *val, const struct kernel_param *kp) @@ -1388,24 +1400,24 @@ static int apple_pmp_agx_write(bool enable, bool no_ack) return ret; } -/* Refuse new votes (the stopped flag), without writing anything. */ -static void apple_pmp_agx_vote_stop_new(void) +/* Refuse new votes for one reason (the stopped mask), without writing anything. */ +static void apple_pmp_agx_vote_stop_new(unsigned int reason) { unsigned long flags; spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); - agx_vote_refuse(&apple_pmp_agx_vote_state); + agx_vote_refuse(&apple_pmp_agx_vote_state, reason); spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); } -/* Clear a request that is still set. Caller holds apple_pmp_agx_vote_mutex. */ -static void apple_pmp_agx_vote_stop(void) +/* Clear a request that is still set and record the stop reason. Caller holds apple_pmp_agx_vote_mutex. */ +static void apple_pmp_agx_vote_stop(unsigned int reason) { unsigned long flags; bool voted; spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); - voted = agx_vote_release(&apple_pmp_agx_vote_state); + voted = agx_vote_release(&apple_pmp_agx_vote_state, reason); spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); if (voted) apple_pmp_agx_write(false, true); @@ -1488,18 +1500,23 @@ EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_end); * The GPU driver unbinds: clear the request now instead of waiting out the idle time, so it is never left set past the GPU's * power-domain release (the T6030 fix clears before it releases its vote). Process context. */ -void apple_pmp_agx_activity_release(void) +static void apple_pmp_agx_vote_release_reason(unsigned int reason) { /* Stop first so that a begin() from now on cannot queue a set, then wait out a work item that already decided, then clear what it set. */ - apple_pmp_agx_vote_stop_new(); + apple_pmp_agx_vote_stop_new(reason); cancel_delayed_work_sync(&apple_pmp_agx_vote_work); guard(mutex)(&apple_pmp_agx_vote_mutex); - apple_pmp_agx_vote_stop(); + apple_pmp_agx_vote_stop(reason); +} + +void apple_pmp_agx_activity_release(void) +{ + apple_pmp_agx_vote_release_reason(AGX_STOP_GPU); } EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_release); /* The GPU probes again, the report comes back or the system resumes: allow votes, and vote now if operations are already in flight. Process context. */ -void apple_pmp_agx_activity_resume(void) +static void apple_pmp_agx_vote_resume_reason(unsigned int reason) { struct agx_vote *v = &apple_pmp_agx_vote_state; unsigned long flags; @@ -1507,11 +1524,16 @@ void apple_pmp_agx_activity_resume(void) spin_lock_irqsave(&apple_pmp_agx_vote_lock, flags); v->enabled = READ_ONCE(agx_vote); - kick = agx_vote_resume(v); + kick = agx_vote_resume(v, reason); spin_unlock_irqrestore(&apple_pmp_agx_vote_lock, flags); if (kick) apple_pmp_agx_vote_kick(0); } + +void apple_pmp_agx_activity_resume(void) +{ + apple_pmp_agx_vote_resume_reason(AGX_STOP_GPU); +} EXPORT_SYMBOL_GPL(apple_pmp_agx_activity_resume); /* @@ -1524,11 +1546,11 @@ static int apple_pmp_agx_pm_notify(struct notifier_block *nb, unsigned long acti switch (action) { case PM_SUSPEND_PREPARE: case PM_HIBERNATION_PREPARE: - apple_pmp_agx_activity_release(); + apple_pmp_agx_vote_release_reason(AGX_STOP_SLEEP); break; case PM_POST_SUSPEND: case PM_POST_HIBERNATION: - apple_pmp_agx_activity_resume(); + apple_pmp_agx_vote_resume_reason(AGX_STOP_SLEEP); break; } return NOTIFY_DONE; @@ -1610,8 +1632,9 @@ EXPORT_SYMBOL_GPL(apple_pmp_report_set_t6030_gfx); * returned, as for a report entry. * * The caller owns the bit it requests; this function does not arbitrate between callers of the - * same bit. On the T6001 bit 9 belongs to the AGX vote (pmp_report.agx_vote) and is refused here - * with -EBUSY. After -ETIMEDOUT the request was written but not acknowledged and stays in the + * same bit. On a machine compatible with apple,t6001 bit 9 belongs to the AGX vote + * (pmp_report.agx_vote) and is refused here with -EBUSY; other t600x reports share the offsets but + * have no vote, so their bit 9 is not reserved. After -ETIMEDOUT the request was written but not acknowledged and stays in the * word: the caller must clear it (@enable false, @no_ack true) before giving up. * * Context: process context (the acknowledge wait is a busy poll of up to 50 ms). @@ -1629,8 +1652,9 @@ int apple_pmp_report_set_request(const struct device_node *report_np, unsigned i rcu_read_lock(); list_for_each_entry_rcu(rep, &apple_pmp_reports, list) { if (rep->dev->of_node == report_np) { - if (id == APPLE_PMP_T6001_AGX_ID && rep->offsets == &apple_pmp_offsets_t600x) - ret = -EBUSY; /* the AGX vote owns this bit */ + if (id == APPLE_PMP_T6001_AGX_ID && rep->offsets == &apple_pmp_offsets_t600x && + of_machine_is_compatible("apple,t6001")) + ret = -EBUSY; /* the AGX vote owns this bit on T6001 */ else ret = apple_pmp_report_set_state(rep, id, enable, no_ack); break; From 4c76d62c88e3ea5a08e7c1dbe4d1181494fb4a48 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:25:07 -0600 Subject: [PATCH 22/22] drm/asahi: take the AGX activity guard where the job reaches the firmware A submission with input syncs took start_op() at ioctl time, before the fence dependencies were added, so a job blocked on its syncs held bit 9 while the GPU was idle. The deferred start_op() in the job path already covers every job when its commands are actually submitted; the ioctl time guard is dropped. --- drivers/gpu/drm/asahi/queue/mod.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/asahi/queue/mod.rs b/drivers/gpu/drm/asahi/queue/mod.rs index 4abdd3e485f726..a53a8f77a8a779 100644 --- a/drivers/gpu/drm/asahi/queue/mod.rs +++ b/drivers/gpu/drm/asahi/queue/mod.rs @@ -697,12 +697,6 @@ impl Queue for Queue::ver { return Err(ENODEV); } - let op_guard = if in_sync_count > 0 { - Some(gpu.start_op()?) - } else { - None - }; - let mut events: [KVec>; SQ_COUNT] = Default::default(); @@ -791,7 +785,10 @@ impl Queue for Queue::ver { QueueJob::ver { dev: self.dev.clone(), vm_bind, - op_guard, + // The AGX activity guard is taken where the job reaches the firmware + // (the deferred start_op in run_job), not at ioctl time: a job blocked on + // input syncs would otherwise hold bit 9 while the GPU is idle. + op_guard: None, sj_vtx: self .q_vtx .as_mut()