From 4b5650c9f5115afcd748f12a419c560823747d97 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:18:03 +1100 Subject: [PATCH 1/2] soc: apple: rtkit: let ordinary firmware traffic through the committed v27 display stop The firmware-27 late display stop checks each display coprocessor once before the sleep's commit and again right before its stop, after the commit, where any failure ends in panic("Apple committed display stop failed"). Two steps failed on ordinary firmware traffic: - apple_rtkit_v27_check() consumed any pending mailbox message into the scope FIFO and returned -EAGAIN, also when called after the commit. The stop that follows reads the mailbox itself. - apple_rtkit_v27_idle_atomic() returned -EPROTO for any message that was neither one of the three power-state answers nor an owed OSLOG counter, e.g. a syslog line or an application endpoint's message. The check op now learns whether the sleep is committed (the sleep core passes entry_committed). Before the commit a pending message still refuses the sleep and is kept for the disarm; after it, the check leaves the message in the mailbox for the stop. The atomic idle keeps messages from other endpoints in the scope FIFO, which the disarm already replays after the restart, and goes on waiting for the power-state answers within the same 100 ms budget. A management message off the handshake, a crashlog message, a full FIFO and the timeout still fail the stop, as before. The panic itself stays: a coprocessor that did not stop still runs with its DARTs unstopped, and the DART stop requires the initiator to be stopped first. tools/asahi/rtkit-v27-late-tests.py compiles both functions from the source against a fake mailbox and firmware; its --consume-committed and --fail-foreign mutations restore the old behaviour and fail. Reported-by: erikwb <169195+erikwb@users.noreply.github.com> Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/dcp-v27-sleep.inc | 4 +- drivers/soc/apple/rtkit-v27-late.inc | 19 +- drivers/soc/apple/sleep-v27-late.inc | 2 +- include/linux/soc/apple/dcp-v27-sleep.h | 5 +- tools/asahi/rtkit-v27-late-tests.py | 266 ++++++++++++++++++++++++ tools/asahi/run-host-tests.py | 2 + 6 files changed, 291 insertions(+), 7 deletions(-) create mode 100755 tools/asahi/rtkit-v27-late-tests.py diff --git a/drivers/gpu/drm/apple/dcp-v27-sleep.inc b/drivers/gpu/drm/apple/dcp-v27-sleep.inc index 027927a4ec055..37a16ec8f4fec 100644 --- a/drivers/gpu/drm/apple/dcp-v27-sleep.inc +++ b/drivers/gpu/drm/apple/dcp-v27-sleep.inc @@ -1305,7 +1305,7 @@ static int dcp_v27_late_arm(void *data) return 0; } -static int dcp_v27_late_check(void *data) +static int dcp_v27_late_check(void *data, bool committed) { struct dcp_v27_sleep *s = data; struct apple_dcp *dcp = s->dcp; @@ -1322,7 +1322,7 @@ static int dcp_v27_late_check(void *data) if (!ret) ret = apple_dart_v27_check(s->firmware); if (!ret) - ret = apple_rtkit_v27_check(s->rx); + ret = apple_rtkit_v27_check(s->rx, committed); return ret; } diff --git a/drivers/soc/apple/rtkit-v27-late.inc b/drivers/soc/apple/rtkit-v27-late.inc index 6f3bfd84ab0a5..82d39de6f8cc0 100644 --- a/drivers/soc/apple/rtkit-v27-late.inc +++ b/drivers/soc/apple/rtkit-v27-late.inc @@ -96,7 +96,11 @@ undo: } EXPORT_SYMBOL_GPL(apple_rtkit_v27_arm); -int apple_rtkit_v27_check(struct apple_rtkit_v27_scope *s) +/* + * Before the commit, a pending message refuses the sleep (it is kept for the disarm). After it, + * the stop that follows reads the mailbox itself: a message there is left to it. + */ +int apple_rtkit_v27_check(struct apple_rtkit_v27_scope *s, bool committed) { struct apple_mbox_msg msg; int ret; @@ -109,6 +113,8 @@ int apple_rtkit_v27_check(struct apple_rtkit_v27_scope *s) return -EBUSY; if (kfifo_is_full(&s->deferred)) return -ENOBUFS; + if (committed) + return 0; ret = apple_mbox_recv_nowait(s->rtk->mbox, &msg); if (ret > 0) { kfifo_put(&s->deferred, msg); @@ -190,10 +196,19 @@ int apple_rtkit_v27_idle_atomic(struct apple_rtkit_v27_scope *s) } else if (apple_rtkit_oslog_owed_msg(rtk, msg.msg1, msg.msg0)) { echo = msg; echo_pending = true; - } else { + } else if (msg.msg1 == APPLE_RTKIT_EP_MGMT || + msg.msg1 == APPLE_RTKIT_EP_CRASHLOG) { + /* Off the power-state handshake, or a crash: no stop. */ kfifo_put(&s->deferred, msg); ret = -EPROTO; goto poison; + } else { + /* + * Ordinary traffic (a log line, a report, an endpoint's + * message): kept for the disarm's replay, and the wait + * for the power-state answers goes on. + */ + kfifo_put(&s->deferred, msg); } } } diff --git a/drivers/soc/apple/sleep-v27-late.inc b/drivers/soc/apple/sleep-v27-late.inc index a130d3bd94d3a..5adb3a10f261b 100644 --- a/drivers/soc/apple/sleep-v27-late.inc +++ b/drivers/soc/apple/sleep-v27-late.inc @@ -141,7 +141,7 @@ static int sleep_v27_final_check(unsigned int i) if (!sleep_v27_held || i >= ARRAY_SIZE(sleep_v27) || !sleep_v27[i].armed || !READ_ONCE(sleep_v27[i].prepared)) return -EBUSY; - return sleep_v27[i].ops->check(sleep_v27[i].data); + return sleep_v27[i].ops->check(sleep_v27[i].data, apple_sleep->entry_committed); } static bool sleep_v27_gate_prepared(const struct device_node *np) diff --git a/include/linux/soc/apple/dcp-v27-sleep.h b/include/linux/soc/apple/dcp-v27-sleep.h index 5e56c40141903..9237932c95167 100644 --- a/include/linux/soc/apple/dcp-v27-sleep.h +++ b/include/linux/soc/apple/dcp-v27-sleep.h @@ -16,7 +16,8 @@ struct apple_dart_v27_lease; */ struct apple_sleep_v27_ops { int (*arm)(void *data); - int (*check)(void *data); + /* committed: called after the sleep's commit, right before stop. */ + int (*check)(void *data, bool committed); int (*stop)(void *data); int (*disarm)(void *data, bool stopped); }; @@ -37,7 +38,7 @@ static inline void apple_sleep_v27_prepared(struct device *dev, bool prepared) { int apple_rtkit_v27_arm(struct apple_rtkit *, struct apple_rtkit_v27_scope **); bool apple_rtkit_v27_live(struct apple_rtkit *); -int apple_rtkit_v27_check(struct apple_rtkit_v27_scope *); +int apple_rtkit_v27_check(struct apple_rtkit_v27_scope *, bool committed); int apple_rtkit_v27_idle_atomic(struct apple_rtkit_v27_scope *); int apple_rtkit_v27_disarm(struct apple_rtkit_v27_scope *, bool stopped); diff --git a/tools/asahi/rtkit-v27-late-tests.py b/tools/asahi/rtkit-v27-late-tests.py new file mode 100755 index 0000000000000..a40afaee4c3a8 --- /dev/null +++ b/tools/asahi/rtkit-v27-late-tests.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run the firmware-27 late display stop's RTKit check and atomic idle against a fake mailbox. + +The bodies of apple_rtkit_v27_check() and apple_rtkit_v27_idle_atomic() are compiled from +drivers/soc/apple/rtkit-v27-late.inc, the RTKit constants and apple_rtkit_oslog_owed_msg() +from rtkit.c. The fake firmware answers the AP and IOP power-state requests and can put +ordinary traffic (a syslog line, an endpoint message) into the receive queue at any point. + +Cases: before the commit a pending message refuses (kept for the disarm); after the commit +the check leaves it in the mailbox and the stop completes; traffic during the stop's wait is +deferred into the scope FIFO in order and the stop completes; an unexpected management or a +crashlog message still fails the stop; no answer still times out; a full FIFO still fails. + +Mutations: --consume-committed (the committed check consumes and refuses as before), +--fail-foreign (the stop fails on any foreign message as before). +""" +from pathlib import Path +import argparse +import re +import subprocess +import sys +import tempfile + +ap = argparse.ArgumentParser() +ap.add_argument('--out') +ap.add_argument('--consume-committed', action='store_true') +ap.add_argument('--fail-foreign', action='store_true') +args = ap.parse_args() + +root = Path(__file__).resolve().parents[2] +inc = (root / 'drivers/soc/apple/rtkit-v27-late.inc').read_text() +rtkit = (root / 'drivers/soc/apple/rtkit.c').read_text() + + +def function(src, sig): + a = src.index(sig) + a = src.rfind('\n', 0, a) + 1 + b = src.index('\n}', src.index('{', a)) + 2 + return src[a:b] + + +def scope_struct(): + a = inc.index('struct apple_rtkit_v27_scope {') + return inc[a:inc.index('};', a) + 2] + + +consts = [] +for name in ['APPLE_RTKIT_PWR_STATE_IDLE', 'APPLE_RTKIT_PWR_STATE_ON', 'APPLE_RTKIT_EP_MGMT', + 'APPLE_RTKIT_EP_CRASHLOG', 'APPLE_RTKIT_EP_SYSLOG', 'APPLE_RTKIT_EP_OSLOG', + 'APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE', 'APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK', + 'APPLE_RTKIT_MGMT_SET_AP_PWR_STATE', 'APPLE_RTKIT_MGMT_SET_AP_PWR_STATE_ACK', + 'APPLE_RTKIT_MGMT_IDLE_NOTIFY']: + m = re.search(r'\b' + name + r' = (0x[0-9a-fA-F]+|[0-9]+),', rtkit) + consts.append(f'#define {name} {m.group(1)}') +for name in ['APPLE_RTKIT_MGMT_TYPE', 'APPLE_RTKIT_MGMT_PWR_STATE', 'APPLE_RTKIT_SYSLOG_TYPE', + 'APPLE_RTKIT_SYSLOG_LOG', 'APPLE_RTKIT_OSLOG_TYPE', 'APPLE_RTKIT_OSLOG_LOG']: + m = re.search(r'#define ' + name + r' (.+)', rtkit) + consts.append(f'#define {name} {m.group(1)}') + +check = function(inc, 'int apple_rtkit_v27_check(') +idle = function(inc, 'int apple_rtkit_v27_idle_atomic(') +if args.consume_committed: + old = '\tif (committed)\n\t\treturn 0;\n' + assert check.count(old) == 1 + check = check.replace(old, '') +if args.fail_foreign: + old = ('\t\t\t\t\t */\n\t\t\t\t\tkfifo_put(&s->deferred, msg);\n\t\t\t\t}') + assert idle.count(old) == 1 + idle = idle.replace(old, '\t\t\t\t\t */\n\t\t\t\t\tkfifo_put(&s->deferred, msg);\n' + '\t\t\t\t\tret = -EPROTO;\n\t\t\t\t\tgoto poison;\n\t\t\t\t}') + +pre = r''' +#include +#include +#include +#include +#include +#include +typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t u64; +#define GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (63 - (h)))) +#define FIELD_PREP(m, v) ((((u64)(v)) << __builtin_ctzll(m)) & (m)) +#define FIELD_GET(m, v) ((((u64)(v)) & (m)) >> __builtin_ctzll(m)) +#define dma_wmb() ((void)0) +#define dma_rmb() ((void)0) +#define EXPORT_SYMBOL_GPL(x) +struct module; +struct apple_mbox_msg { u64 msg0; u32 msg1; }; +#define FIFO_N 64 +#define DECLARE_KFIFO(n, t, sz) struct { t buf[sz]; unsigned in, out; } n +#define INIT_KFIFO(f) ((f).in = (f).out = 0) +#define kfifo_is_full(f) ((f)->in - (f)->out == FIFO_N) +#define kfifo_len(f) ((f)->in - (f)->out) +#define kfifo_put(f, v) (kfifo_is_full(f) ? 0 : ((f)->buf[(f)->in++ % FIFO_N] = (v), 1)) +#define kfifo_get(f, p) ((f)->in == (f)->out ? 0 : (*(p) = (f)->buf[(f)->out++ % FIFO_N], 1)) +#define QN 256 +struct apple_mbox { + bool removing, active; + struct apple_mbox_msg rx[QN]; unsigned rx_in, rx_out; + struct apple_mbox_msg tx[QN]; unsigned tx_n; + bool recv_enabled; +}; +struct apple_rtkit { + struct apple_mbox *mbox; + bool crashed, shutting_down, strict_idle_poisoned, oslog_echo, silent; + unsigned ap_power_state, iop_power_state; + unsigned ticks, inject_at, injected; + struct apple_mbox_msg inject[8]; unsigned ninject; +}; +''' +consts_c = '\n'.join(consts) + '\n' +fake = r''' +static bool irqs_disabled(void) { return true; } +static struct apple_rtkit *cur; +static void push(struct apple_mbox *b, u32 ep, u64 msg0) +{ + b->rx[b->rx_in++ % QN] = (struct apple_mbox_msg){ .msg0 = msg0, .msg1 = ep }; +} +static void udelay(unsigned us) +{ + (void)us; + if (++cur->ticks == cur->inject_at) + for (unsigned i = 0; i < cur->ninject; i++, cur->injected++) + push(cur->mbox, cur->inject[i].msg1, cur->inject[i].msg0); +} +static bool apple_rtkit_v27_rx_drained(struct apple_rtkit *rtk) { return true; } +static void apple_mbox_set_recv_enabled(struct apple_mbox *b, bool on) { b->recv_enabled = on; } +static int apple_mbox_recv_nowait(struct apple_mbox *b, struct apple_mbox_msg *m) +{ + if (b->removing || !b->active) + return -ENODEV; + if (b->rx_in == b->rx_out) + return 0; + *m = b->rx[b->rx_out++ % QN]; + return 1; +} +/* The firmware: answers each power-state request, IOP idle followed by its notice. */ +static int apple_mbox_send_nowait(struct apple_mbox *b, struct apple_mbox_msg m) +{ + u8 type = FIELD_GET(APPLE_RTKIT_MGMT_TYPE, m.msg0); + u64 st = FIELD_GET(APPLE_RTKIT_MGMT_PWR_STATE, m.msg0); + + b->tx[b->tx_n++] = m; + if (cur->silent || m.msg1 != APPLE_RTKIT_EP_MGMT) + return 0; + if (type == APPLE_RTKIT_MGMT_SET_AP_PWR_STATE) + push(b, 0, FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE_ACK) | st); + if (type == APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE) { + push(b, 0, FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK) | st); + push(b, 0, FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_IDLE_NOTIFY)); + } + return 0; +} +''' +owed = function(rtkit, 'static bool apple_rtkit_oslog_owed_msg(') + +main = r''' +static struct apple_mbox mb; +static struct apple_rtkit rk; +static struct apple_rtkit_v27_scope sc; +static void reset(void) +{ + memset(&mb, 0, sizeof(mb)); memset(&rk, 0, sizeof(rk)); memset(&sc, 0, sizeof(sc)); + mb.active = mb.recv_enabled = true; + rk.mbox = &mb; rk.ap_power_state = rk.iop_power_state = APPLE_RTKIT_PWR_STATE_ON; + sc.rtk = &rk; sc.held = true; INIT_KFIFO(sc.deferred); + cur = &rk; +} +#define SYSLOG_LINE (FIELD_PREP(APPLE_RTKIT_SYSLOG_TYPE, APPLE_RTKIT_SYSLOG_LOG) | 0x12) +#define APP_MSG 0x123456789aULL +static void stopped_ok(void) +{ + assert(sc.stopped && !sc.failed && !rk.strict_idle_poisoned && !mb.recv_enabled); + assert(rk.ap_power_state == APPLE_RTKIT_PWR_STATE_IDLE); + assert(rk.iop_power_state == APPLE_RTKIT_PWR_STATE_IDLE); +} +int main(void) +{ + struct apple_mbox_msg m; + + /* Before the commit: a pending log line refuses the sleep and is kept for the disarm. */ + reset(); push(&mb, APPLE_RTKIT_EP_SYSLOG, SYSLOG_LINE); + assert(apple_rtkit_v27_check(&sc, false) == -EAGAIN); + assert(kfifo_len(&sc.deferred) == 1 && mb.rx_in == mb.rx_out); + reset(); + assert(apple_rtkit_v27_check(&sc, false) == 0 && apple_rtkit_v27_check(&sc, true) == 0); + + /* After the commit: the log line stays in the mailbox and the stop answers it. */ + reset(); push(&mb, APPLE_RTKIT_EP_SYSLOG, SYSLOG_LINE); + assert(apple_rtkit_v27_check(&sc, true) == 0); + assert(kfifo_len(&sc.deferred) == 0 && mb.rx_in != mb.rx_out); + assert(apple_rtkit_v27_idle_atomic(&sc) == 0); + stopped_ok(); + assert(kfifo_get(&sc.deferred, &m) && m.msg1 == APPLE_RTKIT_EP_SYSLOG && m.msg0 == SYSLOG_LINE); + assert(!kfifo_get(&sc.deferred, &m)); + + /* Ordinary traffic in the middle of the wait: deferred in order, the stop completes. */ + reset(); rk.inject_at = 1; rk.ninject = 2; + rk.inject[0] = (struct apple_mbox_msg){ .msg0 = SYSLOG_LINE, .msg1 = APPLE_RTKIT_EP_SYSLOG }; + rk.inject[1] = (struct apple_mbox_msg){ .msg0 = APP_MSG, .msg1 = 0x37 }; + assert(apple_rtkit_v27_idle_atomic(&sc) == 0); + stopped_ok(); assert(rk.injected == 2); + assert(kfifo_get(&sc.deferred, &m) && m.msg1 == APPLE_RTKIT_EP_SYSLOG); + assert(kfifo_get(&sc.deferred, &m) && m.msg1 == 0x37 && m.msg0 == APP_MSG); + assert(!kfifo_get(&sc.deferred, &m)); + + /* An owed OSLOG counter is still echoed, not deferred. */ + reset(); rk.oslog_echo = true; + push(&mb, APPLE_RTKIT_EP_OSLOG, FIELD_PREP(APPLE_RTKIT_OSLOG_TYPE, APPLE_RTKIT_OSLOG_LOG) | 7); + assert(apple_rtkit_v27_idle_atomic(&sc) == 0); + stopped_ok(); assert(kfifo_len(&sc.deferred) == 0); + { + bool echoed = false; + for (unsigned i = 0; i < mb.tx_n; i++) + echoed |= mb.tx[i].msg1 == APPLE_RTKIT_EP_OSLOG; + assert(echoed); + } + + /* A management message off the handshake, or a crashlog: still no stop. */ + reset(); push(&mb, APPLE_RTKIT_EP_MGMT, + FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK) | + APPLE_RTKIT_PWR_STATE_ON); + assert(apple_rtkit_v27_idle_atomic(&sc) == -EPROTO); + assert(sc.failed && !sc.stopped && rk.strict_idle_poisoned && kfifo_len(&sc.deferred) == 1); + reset(); push(&mb, APPLE_RTKIT_EP_CRASHLOG, 0x1ULL << 52); + assert(apple_rtkit_v27_idle_atomic(&sc) == -EPROTO); + assert(sc.failed && !sc.stopped && rk.strict_idle_poisoned); + + /* No answer: the 100 ms budget still ends in a failure. */ + reset(); rk.silent = true; + assert(apple_rtkit_v27_idle_atomic(&sc) == -ETIMEDOUT); + assert(sc.failed && !sc.stopped && rk.strict_idle_poisoned && mb.recv_enabled); + + /* A full FIFO: nothing more is consumed, the stop fails. */ + reset(); + for (unsigned i = 0; i < FIFO_N; i++) + push(&mb, APPLE_RTKIT_EP_SYSLOG, SYSLOG_LINE); + assert(apple_rtkit_v27_idle_atomic(&sc) == -ENOBUFS); + assert(sc.failed && !sc.stopped && kfifo_is_full(&sc.deferred)); + assert(apple_rtkit_v27_check(&sc, true) == -EBUSY); + return 0; +} +''' + +code = pre + consts_c + scope_struct() + '\n' + fake + owed + '\n' + check + '\n' + idle + '\n' + main + + +def build_and_run(out): + out.mkdir(parents=True, exist_ok=True) + c = out / 'rtkit-v27-late.c' + c.write_text(code) + exe = out / 'rtkit-v27-late' + subprocess.run(['clang', '-std=gnu11', '-Wall', '-Werror', '-Wno-unused-function', + '-fsanitize=address,undefined', str(c), '-o', str(exe)], check=True) + return subprocess.run([str(exe)]).returncode + + +if args.out: + rc = build_and_run(Path(args.out)) +else: + with tempfile.TemporaryDirectory() as t: + rc = build_and_run(Path(t)) +if rc: + sys.exit(1) +print('PASS rtkit v27 late stop: committed check leaves a message to the stop, ' + 'foreign traffic deferred in order, owed OSLOG echoed, MGMT/crashlog/timeout/full FIFO still fail') diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 1f0d5963f9290..a0de632a8823e 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -67,6 +67,8 @@ def mutations(flag, names, base, expect=KILL): 'dcpext26-slot-unregister-tests.py': [(out(), PASS)], 'dcpext26-budget-tests.py': [([], PASS)], 'dcpext26-queue-tests.py': [([], PASS), (['--drop-v27-close'], KILL)], + 'rtkit-v27-late-tests.py': [(out(), PASS), (out() + ['--consume-committed'], KILL), + (out() + ['--fail-foreign'], KILL)], 'dcpext26-retrain-tests.py': [([], PASS), (['--negative-control'], KILL)], 'dcpext26-lifecycle-tests.py': [([], PASS)], 'dcpext26-modeset-tests.py': [([], PASS)], From 1b68ad86e50a6bc9f3009b426db00f103e249383 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 17:52:36 +1100 Subject: [PATCH 2/2] soc: apple: rtkit: acknowledge system notifications during late display stop Acknowledge SYSLOG and IOREPORT notifications in the atomic idle loop using the existing bounded echo path. An acknowledged record can be recycled, so leave it out of the deferred FIFO. Preserve the stop refusal for application traffic and replay only other system messages after a successful stop. Signed-off-by: Ryan Murray --- drivers/soc/apple/rtkit-v27-late.inc | 19 +++++------ tools/asahi/rtkit-v27-late-tests.py | 51 ++++++++++++++++++---------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/drivers/soc/apple/rtkit-v27-late.inc b/drivers/soc/apple/rtkit-v27-late.inc index 82d39de6f8cc0..16cb58e0ba3dc 100644 --- a/drivers/soc/apple/rtkit-v27-late.inc +++ b/drivers/soc/apple/rtkit-v27-late.inc @@ -193,21 +193,20 @@ int apple_rtkit_v27_idle_atomic(struct apple_rtkit_v27_scope *s) } else if (msg.msg1 == APPLE_RTKIT_EP_MGMT && iop_sent && !done && type == APPLE_RTKIT_MGMT_IDLE_NOTIFY && !arg) { done = true; - } else if (apple_rtkit_oslog_owed_msg(rtk, msg.msg1, msg.msg0)) { - echo = msg; - echo_pending = true; } else if (msg.msg1 == APPLE_RTKIT_EP_MGMT || - msg.msg1 == APPLE_RTKIT_EP_CRASHLOG) { - /* Off the power-state handshake, or a crash: no stop. */ + msg.msg1 == APPLE_RTKIT_EP_CRASHLOG || + msg.msg1 >= APPLE_RTKIT_APP_ENDPOINT_START) { + /* A crash, unexpected handshake, or live application RPC. */ kfifo_put(&s->deferred, msg); ret = -EPROTO; goto poison; + } else if (apple_rtkit_oslog_owed_msg(rtk, msg.msg1, msg.msg0) || + apple_rtkit_notification_msg(msg.msg1, msg.msg0)) { + /* Echo now. The ring entry can be reused after the echo. */ + echo = msg; + echo_pending = true; } else { - /* - * Ordinary traffic (a log line, a report, an endpoint's - * message): kept for the disarm's replay, and the wait - * for the power-state answers goes on. - */ + /* Other system messages are replayed on disarm. */ kfifo_put(&s->deferred, msg); } } diff --git a/tools/asahi/rtkit-v27-late-tests.py b/tools/asahi/rtkit-v27-late-tests.py index a40afaee4c3a8..5fa65af5fbada 100755 --- a/tools/asahi/rtkit-v27-late-tests.py +++ b/tools/asahi/rtkit-v27-late-tests.py @@ -4,13 +4,13 @@ The bodies of apple_rtkit_v27_check() and apple_rtkit_v27_idle_atomic() are compiled from drivers/soc/apple/rtkit-v27-late.inc, the RTKit constants and apple_rtkit_oslog_owed_msg() -from rtkit.c. The fake firmware answers the AP and IOP power-state requests and can put -ordinary traffic (a syslog line, an endpoint message) into the receive queue at any point. +and notification classifier from rtkit.c. The fake firmware answers the AP and IOP power-state requests and can put +system notifications or application traffic into the receive queue at any point. Cases: before the commit a pending message refuses (kept for the disarm); after the commit the check leaves it in the mailbox and the stop completes; traffic during the stop's wait is -deferred into the scope FIFO in order and the stop completes; an unexpected management or a -crashlog message still fails the stop; no answer still times out; a full FIFO still fails. +either acknowledged inline or deferred in order; unexpected management, crashlog and +application messages fail the stop; no answer still times out; a full FIFO still fails. Mutations: --consume-committed (the committed check consumes and refuses as before), --fail-foreign (the stop fails on any foreign message as before). @@ -45,16 +45,19 @@ def scope_struct(): return inc[a:inc.index('};', a) + 2] -consts = [] +consts = [line for line in (root / "drivers/soc/apple/rtkit-internal.h").read_text().splitlines() + if line.startswith("#define APPLE_RTKIT_APP_ENDPOINT_START ")] for name in ['APPLE_RTKIT_PWR_STATE_IDLE', 'APPLE_RTKIT_PWR_STATE_ON', 'APPLE_RTKIT_EP_MGMT', 'APPLE_RTKIT_EP_CRASHLOG', 'APPLE_RTKIT_EP_SYSLOG', 'APPLE_RTKIT_EP_OSLOG', + 'APPLE_RTKIT_EP_IOREPORT', 'APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE', 'APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK', 'APPLE_RTKIT_MGMT_SET_AP_PWR_STATE', 'APPLE_RTKIT_MGMT_SET_AP_PWR_STATE_ACK', 'APPLE_RTKIT_MGMT_IDLE_NOTIFY']: m = re.search(r'\b' + name + r' = (0x[0-9a-fA-F]+|[0-9]+),', rtkit) consts.append(f'#define {name} {m.group(1)}') for name in ['APPLE_RTKIT_MGMT_TYPE', 'APPLE_RTKIT_MGMT_PWR_STATE', 'APPLE_RTKIT_SYSLOG_TYPE', - 'APPLE_RTKIT_SYSLOG_LOG', 'APPLE_RTKIT_OSLOG_TYPE', 'APPLE_RTKIT_OSLOG_LOG']: + 'APPLE_RTKIT_SYSLOG_LOG', 'APPLE_RTKIT_OSLOG_TYPE', 'APPLE_RTKIT_OSLOG_LOG', + 'APPLE_RTKIT_IOREPORT_UNKNOWN1', 'APPLE_RTKIT_IOREPORT_UNKNOWN2']: m = re.search(r'#define ' + name + r' (.+)', rtkit) consts.append(f'#define {name} {m.group(1)}') @@ -65,10 +68,11 @@ def scope_struct(): assert check.count(old) == 1 check = check.replace(old, '') if args.fail_foreign: - old = ('\t\t\t\t\t */\n\t\t\t\t\tkfifo_put(&s->deferred, msg);\n\t\t\t\t}') + old = ('\t\t\t\t\t/* Other system messages are replayed on disarm. */\n' + '\t\t\t\t\tkfifo_put(&s->deferred, msg);\n') assert idle.count(old) == 1 - idle = idle.replace(old, '\t\t\t\t\t */\n\t\t\t\t\tkfifo_put(&s->deferred, msg);\n' - '\t\t\t\t\tret = -EPROTO;\n\t\t\t\t\tgoto poison;\n\t\t\t\t}') + idle = idle.replace(old, old + '\t\t\t\t\tret = -EPROTO;\n' + '\t\t\t\t\tgoto poison;\n') pre = r''' #include @@ -152,7 +156,8 @@ def scope_struct(): return 0; } ''' -owed = function(rtkit, 'static bool apple_rtkit_oslog_owed_msg(') +owed = function(rtkit, 'static bool apple_rtkit_oslog_owed_msg(') + '\n' + \ + function(rtkit, 'static bool apple_rtkit_notification_msg(') main = r''' static struct apple_mbox mb; @@ -191,19 +196,30 @@ def scope_struct(): assert(kfifo_len(&sc.deferred) == 0 && mb.rx_in != mb.rx_out); assert(apple_rtkit_v27_idle_atomic(&sc) == 0); stopped_ok(); - assert(kfifo_get(&sc.deferred, &m) && m.msg1 == APPLE_RTKIT_EP_SYSLOG && m.msg0 == SYSLOG_LINE); assert(!kfifo_get(&sc.deferred, &m)); + { + unsigned echoes = 0; + for (unsigned i = 0; i < mb.tx_n; i++) + echoes += mb.tx[i].msg1 == APPLE_RTKIT_EP_SYSLOG && + mb.tx[i].msg0 == SYSLOG_LINE; + assert(echoes == 1); + } - /* Ordinary traffic in the middle of the wait: deferred in order, the stop completes. */ + /* Other system messages are retained in FIFO order. */ reset(); rk.inject_at = 1; rk.ninject = 2; - rk.inject[0] = (struct apple_mbox_msg){ .msg0 = SYSLOG_LINE, .msg1 = APPLE_RTKIT_EP_SYSLOG }; - rk.inject[1] = (struct apple_mbox_msg){ .msg0 = APP_MSG, .msg1 = 0x37 }; + rk.inject[0] = (struct apple_mbox_msg){ .msg0 = 0, .msg1 = APPLE_RTKIT_EP_SYSLOG }; + rk.inject[1] = (struct apple_mbox_msg){ .msg0 = 0, .msg1 = APPLE_RTKIT_EP_IOREPORT }; assert(apple_rtkit_v27_idle_atomic(&sc) == 0); stopped_ok(); assert(rk.injected == 2); assert(kfifo_get(&sc.deferred, &m) && m.msg1 == APPLE_RTKIT_EP_SYSLOG); - assert(kfifo_get(&sc.deferred, &m) && m.msg1 == 0x37 && m.msg0 == APP_MSG); + assert(kfifo_get(&sc.deferred, &m) && m.msg1 == APPLE_RTKIT_EP_IOREPORT); assert(!kfifo_get(&sc.deferred, &m)); + /* Application RPCs invalidate the stop. */ + reset(); push(&mb, APPLE_RTKIT_APP_ENDPOINT_START, APP_MSG); + assert(apple_rtkit_v27_idle_atomic(&sc) == -EPROTO); + assert(sc.failed && !sc.stopped && rk.strict_idle_poisoned); + /* An owed OSLOG counter is still echoed, not deferred. */ reset(); rk.oslog_echo = true; push(&mb, APPLE_RTKIT_EP_OSLOG, FIELD_PREP(APPLE_RTKIT_OSLOG_TYPE, APPLE_RTKIT_OSLOG_LOG) | 7); @@ -234,7 +250,7 @@ def scope_struct(): /* A full FIFO: nothing more is consumed, the stop fails. */ reset(); for (unsigned i = 0; i < FIFO_N; i++) - push(&mb, APPLE_RTKIT_EP_SYSLOG, SYSLOG_LINE); + push(&mb, APPLE_RTKIT_EP_SYSLOG, 0); assert(apple_rtkit_v27_idle_atomic(&sc) == -ENOBUFS); assert(sc.failed && !sc.stopped && kfifo_is_full(&sc.deferred)); assert(apple_rtkit_v27_check(&sc, true) == -EBUSY); @@ -263,4 +279,5 @@ def build_and_run(out): if rc: sys.exit(1) print('PASS rtkit v27 late stop: committed check leaves a message to the stop, ' - 'foreign traffic deferred in order, owed OSLOG echoed, MGMT/crashlog/timeout/full FIFO still fail') + 'system traffic deferred in order, notification and OSLOG echoes, ' + 'MGMT/crashlog/application/timeout/full FIFO fail')