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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,7 @@ impl Coprocessor {
) -> Result<rtkit::RtKit<RoleOps>> {
for attempt in 0..RECEIVER_ATTEMPTS {
match rtkit::RtKit::<RoleOps>::new(dev.as_ref().as_ref(), None, index, state.clone()) {
Ok(receiver) => {
// Its OSLOG counters stay unanswered until a soak shows echoing them is safe.
receiver.set_oslog_echo(false);
return Ok(receiver);
}
Ok(receiver) => return Ok(receiver),
Err(error) if error == EINVAL || error == ETIMEDOUT => {
if attempt + 1 < RECEIVER_ATTEMPTS {
fsleep(Delta::from_millis(RECEIVER_RETRY_MS));
Expand Down
6 changes: 4 additions & 2 deletions drivers/soc/apple/rtkit-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ struct apple_rtkit {
* hand out kdebug buffers, and poll the mailbox while waiting.
*/
bool sync_syslog_ack;
/* Echo each OSLOG channel-0 counter, as macOS does. */
/*
* Echo each OSLOG channel-0 counter, as macOS does. On unless the node has
* apple,sync-syslog-ack or the client turned it off; off drops the counters.
*/
bool oslog_echo;
/* A counter queued for the receive work and not yet echoed. */
u64 oslog_owed;
Expand Down Expand Up @@ -158,7 +161,6 @@ struct apple_rtkit {
bool crashlog_inherited;
/* The crash log buffer is the firmware's own, mapped here when it crashed. */
bool crashlog_placed;
bool oslog_inherited;

/* A crash reported before the crashlog buffer was mapped: the log is
* read once the co-processor has written it. */
Expand Down
67 changes: 16 additions & 51 deletions drivers/soc/apple/rtkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ enum {
#define APPLE_RTKIT_OSLOG_TYPE GENMASK_ULL(63, 56)
#define APPLE_RTKIT_OSLOG_BUFFER_REQUEST 1
#define APPLE_RTKIT_OSLOG_LOG 2
#define APPLE_RTKIT_OSLOG_LOG_RESERVED GENMASK_ULL(55, 32)
#define APPLE_RTKIT_OSLOG_SIZE GENMASK_ULL(55, 36)
#define APPLE_RTKIT_OSLOG_IOVA GENMASK_ULL(35, 0)

Expand Down Expand Up @@ -1410,25 +1409,6 @@ static void apple_rtkit_syslog_rx(struct apple_rtkit *rtk, u64 msg)
}
}

/* Where a counter has only ever run unanswered: nothing is echoed. */
static bool apple_rtkit_oslog_unanswered(struct apple_rtkit *rtk)
{
return !rtk->oslog_echo &&
(rtk->sync_syslog_ack || of_machine_is_compatible("apple,t8140") ||
of_machine_is_compatible("apple,t8142"));
}

/* An OSLOG counter that is answered with its echo, on any lane. */
static bool apple_rtkit_oslog_echoed(struct apple_rtkit *rtk, u64 msg)
{
if (rtk->oslog_echo)
return true;
if (apple_rtkit_oslog_unanswered(rtk))
return false;
return (rtk->oslog_buffer.size || rtk->oslog_inherited) &&
!(msg & APPLE_RTKIT_OSLOG_LOG_RESERVED);
}

static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
{
u8 type = FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg);
Expand All @@ -1438,6 +1418,8 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
* A firmware counts its log writes and holds the next counter until the last is
* echoed, and with one outstanding takes no AP power notice: a DCP after a warm
* restart, the MTP and the SMC after about eight sleeps. macOS echoes every one.
* The counter is cumulative, so echoing the newest one acknowledges every write
* before it.
*/
if (type == APPLE_RTKIT_OSLOG_LOG && rtk->oslog_echo) {
/* A polled idle that came first echoed it already. */
Expand All @@ -1452,13 +1434,9 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
return;
}

if (type == APPLE_RTKIT_OSLOG_LOG && !apple_rtkit_oslog_echoed(rtk, msg)) {
/* Preserve the tested Neo/M5 and synchronous-log no-ACK policy. */
if (apple_rtkit_oslog_unanswered(rtk))
dev_dbg(rtk->dev, "RTKit: oslog notification: %016llx\n", msg);
else
dev_warn_ratelimited(rtk->dev, "RTKit: invalid oslog notification: %llx\n",
msg);
/* Where the echo is off (apple_rtkit_set_oslog_echo()), the counter is dropped. */
if (type == APPLE_RTKIT_OSLOG_LOG) {
dev_dbg(rtk->dev, "RTKit: oslog notification: %016llx\n", msg);
return;
}

Expand All @@ -1467,20 +1445,6 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
apple_rtkit_common_rx_get_buffer(rtk, &rtk->oslog_buffer,
APPLE_RTKIT_EP_OSLOG, msg);
break;
case APPLE_RTKIT_OSLOG_LOG:
/* Channel zero reports a cumulative 32-bit write counter. Even
* though we discard these logs, acknowledge that exact counter:
* AppleDCP waits for its log transport to drain before AP sleep.
* This is not a buffer offset and may wrap. Other channels do not
* have a buffer admitted by this endpoint implementation. An inherited
* channel zero needs no buffer mapping to acknowledge its counter
* (apple_rtkit_oslog_echoed()).
*/
err = apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_OSLOG,
msg, NULL, false);
if (err)
dev_err(rtk->dev, "RTKit: oslog acknowledgment failed: %d\n", err);
break;
case 3 ... 5:
/*
* Undocumented notifications sent during normal operation. They
Expand Down Expand Up @@ -1904,8 +1868,7 @@ static bool apple_rtkit_log_word(struct apple_rtkit *rtk, u8 ep, u64 msg)
if (ep == APPLE_RTKIT_EP_SYSLOG)
return apple_rtkit_notification_msg(ep, msg);
return ep == APPLE_RTKIT_EP_OSLOG &&
FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg) == APPLE_RTKIT_OSLOG_LOG &&
apple_rtkit_oslog_echoed(rtk, msg);
FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg) == APPLE_RTKIT_OSLOG_LOG && rtk->oslog_echo;
}

int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk)
Expand Down Expand Up @@ -2384,7 +2347,6 @@ static void apple_rtkit_mark_running(struct apple_rtkit *rtk)
rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_ON;
rtk->syslog_inherited = true;
rtk->crashlog_inherited = true;
rtk->oslog_inherited = true;
}

static int apple_rtkit_claim_rx(struct apple_rtkit *rtk)
Expand Down Expand Up @@ -2576,13 +2538,17 @@ static struct apple_rtkit *__apple_rtkit_init(struct device *dev, void *cookie,
rtk->sync_syslog_ack =
of_property_read_bool(dev->of_node, "apple,sync-syslog-ack");
/*
* As macOS does; a driver whose firmware shouldn't get them turns it off. T8142 too:
* its MTP left a counter unechoed after the first sleep's resume, then took no AP
* idle notice for the next one (J815, -ETIME at prepare) and asserted on the one
* after ("Invalid AP power").
* Echo every OSLOG channel-0 counter, as macOS does. This is the OSLOG protocol of
* every RTKit firmware that sends counters: it holds the next one until the last is
* echoed and takes no AP power notice while one is outstanding (T8140, and the J815
* MTP, whose second sleep failed at prepare with -ETIME without it). The IOP sends
* no version or capability for this; a firmware that does not use the protocol
* sends no counters, so the echo never goes out. apple,sync-syslog-ack turns it
* off: the dcpext that carries it (T8122) has only run with its counters
* unanswered; a DCP driver that restarts its DCP across a sleep turns the echo
* back on.
*/
rtk->oslog_echo = of_machine_is_compatible("apple,t8140") ||
of_machine_is_compatible("apple,t8142");
rtk->oslog_echo = !rtk->sync_syslog_ack;
rtk->m5_saved.tracked = track_saved;
atomic_set(&rtk->m5_saved.jobs, 0);
atomic_set(&rtk->v27_jobs, 0);
Expand Down Expand Up @@ -2910,7 +2876,6 @@ static void apple_rtkit_reset_session(struct apple_rtkit *rtk, bool keep_buffers
rtk->syslog_msg_size = 0;
rtk->syslog_inherited = false;
rtk->crashlog_inherited = false;
rtk->oslog_inherited = false;
}

bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS);
Expand Down
5 changes: 3 additions & 2 deletions include/linux/soc/apple/rtkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,9 @@ int apple_rtkit_power_cycled_return(struct apple_rtkit *rtk, void __iomem *cpu_c
int apple_rtkit_sleep_return_start(struct apple_rtkit *rtk, void __iomem *cpu_control);

/*
* Echo each OSLOG channel-0 counter the firmware sends, as macOS does. On by default on
* T8140, where a firmware with one outstanding takes no AP power notice.
* Echo each OSLOG channel-0 counter the firmware sends, as macOS does, or drop them. On by
* default (off on a node with apple,sync-syslog-ack): a firmware with one outstanding
* takes no AP power notice.
*/
void apple_rtkit_set_oslog_echo(struct apple_rtkit *rtk, bool echo);

Expand Down
85 changes: 42 additions & 43 deletions tools/asahi/rtkit-oslog-tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only OR MIT
"""Check OSLog counter acknowledgements and inherited-session reset."""
"""Check the OSLog counter echo policy and the session reset."""
import argparse
import hashlib
import json
Expand Down Expand Up @@ -61,7 +61,7 @@ def function(source, name):
int sync_ack_fifo;
int ioreport_lock, ioreport_state_lock;
enum apple_rtkit_ioreport_state ioreport_state;
bool sync_syslog_ack, oslog_inherited, syslog_inherited, crashlog_inherited, crashed, shutting_down;
bool sync_syslog_ack, syslog_inherited, crashlog_inherited, crashed, shutting_down;
bool oslog_echo;
u64 oslog_owed;
/* Read only by the J714s firmware 27 sleep; a failed echo sets it. */
Expand All @@ -77,9 +77,6 @@ def function(source, name):
static unsigned sent, warnings, errors, requests, detachments, flushes, freed, checks;
static int send_error, setup_error, start_error;
static u64 sent_message;
static bool neo, m5;
static bool of_machine_is_compatible(const char *s)
{ return (!strcmp(s, "apple,t8140") && neo) || (!strcmp(s, "apple,t8142") && m5); }
#define kfifo_reset(f) (*(f)=0)
#define dev_dbg(...) ((void)0)
#define dev_warn(...) (++warnings)
Expand Down Expand Up @@ -108,63 +105,69 @@ def function(source, name):

MAIN = r'''
static void reset(void) { sent=warnings=errors=requests=0; send_error=setup_error=0; }
static void event(struct apple_rtkit *r, u64 msg, bool ack)
/* A counter as the receive work sees it: owed is what the IRQ path recorded. */
static void event(struct apple_rtkit *r, u64 msg, u64 owed, bool echo)
{
reset(); apple_rtkit_oslog_rx(r,msg);
assert(sent==(unsigned)ack && requests==0 && errors==0);
if (ack) { assert(sent_message==msg && warnings==0); }
else { assert(warnings==1); }
reset(); r->oslog_owed=owed; apple_rtkit_oslog_rx(r,msg);
assert(sent==(unsigned)echo && requests==0 && errors==0 && warnings==0);
if (echo) assert(sent_message==msg && r->oslog_owed==0);
else assert(r->oslog_owed==owed);
++checks;
}
int main(void)
{
struct mailbox m={0}; struct apple_rtkit r={.mbox=&m};
const u64 type=(u64)APPLE_RTKIT_OSLOG_LOG<<56;
const uint32_t counters[]={0,1,4095,4096,UINT32_MAX,0,0x80000000};
for (unsigned owner=0; owner<3; owner++) {
r.oslog_buffer.size=owner==1?4096:0; r.oslog_inherited=false;
if (owner==2) { apple_rtkit_mark_running(&r); assert(r.oslog_inherited); }
for (unsigned i=0;i<ARRAY_SIZE(counters);i++) event(&r,type|counters[i],owner!=0);
for (unsigned bit=32;bit<56;bit++) event(&r,type|(1ULL<<bit)|23,false);
for (unsigned kind=0;kind<256;kind++) {
if (kind==APPLE_RTKIT_OSLOG_BUFFER_REQUEST || kind==APPLE_RTKIT_OSLOG_LOG || (kind>=3 && kind<=5)) continue;
event(&r,(u64)kind<<56,false);
for (unsigned echo=0; echo<2; echo++) {
for (unsigned owner=0; owner<3; owner++) {
memset(&r,0,sizeof(r)); r.mbox=&m; r.oslog_echo=echo;
r.oslog_buffer.size=owner==1?4096:0;
if (owner==2) apple_rtkit_mark_running(&r);
// Echoed exactly when the echo is on, with or without a buffer, on any channel.
for (unsigned i=0;i<ARRAY_SIZE(counters);i++) event(&r,type|counters[i],type|counters[i],echo);
for (unsigned bit=32;bit<56;bit++) event(&r,type|(1ULL<<bit)|23,type|(1ULL<<bit)|23,echo);
// Already echoed by a polled idle, or superseded by a newer cumulative counter.
event(&r,type|5,0,false);
event(&r,type|5,type|6,false);
for (unsigned kind=0;kind<256;kind++) {
if (kind==APPLE_RTKIT_OSLOG_BUFFER_REQUEST || kind==APPLE_RTKIT_OSLOG_LOG || (kind>=3 && kind<=5)) continue;
reset(); apple_rtkit_oslog_rx(&r,(u64)kind<<56);
assert(warnings==1 && sent==0); ++checks;
}
}
}
for (unsigned fail=0;fail<2;fail++) {
memset(&r,0,sizeof(r));r.mbox=&m; reset();setup_error=fail?-5:0;
memset(&r,0,sizeof(r));r.mbox=&m;r.oslog_echo=true; reset();setup_error=fail?-5:0;
const u64 req=((u64)APPLE_RTKIT_OSLOG_BUFFER_REQUEST<<56)|(4096ULL<<36);
apple_rtkit_oslog_rx(&r,req); assert(requests==1 && sent==0);
event(&r,type|UINT32_MAX,!fail);++checks;
event(&r,type|UINT32_MAX,type|UINT32_MAX,true);
}
r.oslog_buffer.size=4096;
for (unsigned failure=0;failure<2;failure++) {
reset();send_error=failure?-5:0;apple_rtkit_oslog_rx(&r,type|UINT32_MAX);
assert(sent==1 && sent_message==(type|UINT32_MAX));assert(errors==failure && warnings==0);++checks;
memset(&r,0,sizeof(r));r.mbox=&m;r.oslog_echo=true;
reset();send_error=failure?-5:0;r.oslog_owed=type|UINT32_MAX;apple_rtkit_oslog_rx(&r,type|UINT32_MAX);
assert(sent==1 && sent_message==(type|UINT32_MAX));assert(errors==failure && warnings==0);
assert(r.v27_rx_failed==failure);++checks;
}
for (unsigned fail=0;fail<2;fail++) {
memset(&r,0,sizeof(r));r.mbox=&m;apple_rtkit_mark_running(&r);
assert(r.oslog_inherited && (r.endpoints[0]&(1UL<<APPLE_RTKIT_EP_OSLOG)));
memset(&r,0,sizeof(r));r.mbox=&m;r.oslog_echo=true;apple_rtkit_mark_running(&r);
assert(r.syslog_inherited && (r.endpoints[0]&(1UL<<APPLE_RTKIT_EP_OSLOG)));
struct apple_rtkit_shmem *buffers[]={&r.oslog_buffer,&r.syslog_buffer,&r.ioreport_buffer,
&r.crashlog_buffer,&r.kdebug_buffer0,&r.kdebug_buffer1,&r.coreanalytics_buffer,&r.pmp_aux_buffer};
for (unsigned i=0;i<ARRAY_SIZE(buffers);i++) buffers[i]->size=4096;
r.crashed=true;r.syslog_msg_size=128;
detachments=flushes=freed=0;start_error=fail?-5:0;
assert(apple_rtkit_reinit(&r)==start_error);
assert(!r.oslog_inherited && !r.syslog_inherited && !r.crashlog_inherited);
assert(!r.syslog_inherited && !r.crashlog_inherited);
assert(!r.crashed && !r.syslog_msg_size && freed==ARRAY_SIZE(buffers));
// Every buffer of the old firmware session goes, the OSLog one included.
for (unsigned i=0;i<ARRAY_SIZE(buffers);i++) assert(!buffers[i]->size);
assert(r.endpoints[0]==(1UL<<APPLE_RTKIT_EP_MGMT));
assert(detachments==(fail?2U:1U) && flushes==(fail?2U:1U));
event(&r,type|1,false);++checks;
// The echo policy survives the reset: the restarted firmware's counters are echoed.
event(&r,type|1,type|1,true);
}
for (unsigned policy=0;policy<3;policy++) {
neo=policy==0; m5=policy==1; r.sync_syslog_ack=policy==2;
reset(); apple_rtkit_oslog_rx(&r,type|23);
assert(sent==0 && warnings==0 && errors==0); ++checks;
}
printf("%u OSLog counter/channel/session controls passed\n",checks);
printf("%u OSLog counter/echo/session controls passed\n",checks);
return 0;
}
'''
Expand All @@ -174,7 +177,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ref', help='Git source revision to exercise')
parser.add_argument('--out', type=Path)
parser.add_argument('--mutate', choices=['admission', 'channel', 'counter', 'reset', 'buffer'])
parser.add_argument('--mutate', choices=['owed', 'policy', 'counter', 'reset', 'buffer'])
args = parser.parse_args()
source = subprocess.check_output(['git', 'show', args.ref + ':' + DRIVER], cwd=ROOT, text=True) if args.ref else (ROOT / DRIVER).read_text()
constants = '\n'.join(re.findall(r'^#define APPLE_RTKIT_OSLOG_.*$', source, re.M))
Expand All @@ -184,22 +187,18 @@ def main():
constants += '\n#define APPLE_RTKIT_OSLOG_LOG 2\n'
# The session reset reinit() and a cold restart share, where the driver has one.
names = ['apple_rtkit_oslog_rx', 'apple_rtkit_mark_running']
# The echo's predicate, where the receiver is built on one.
if 'static bool apple_rtkit_oslog_echoed(' in source:
names[:0] = ['apple_rtkit_oslog_unanswered', 'apple_rtkit_oslog_echoed']
if 'static void apple_rtkit_reset_session(' in source:
names.append('apple_rtkit_reset_session')
names.append('apple_rtkit_reinit')
bodies = '\n'.join(function(source, name) for name in names)
if args.mutate == 'admission':
bodies = bodies.replace('(!rtk->oslog_buffer.size && !rtk->oslog_inherited)', 'false')
bodies = bodies.replace('(rtk->oslog_buffer.size || rtk->oslog_inherited)', 'true')
elif args.mutate == 'channel':
bodies = bodies.replace('(msg & APPLE_RTKIT_OSLOG_LOG_RESERVED)', 'false')
if args.mutate == 'owed':
bodies = bodies.replace('cmpxchg(&rtk->oslog_owed, msg, 0) != msg', 'false')
elif args.mutate == 'policy':
bodies = bodies.replace('type == APPLE_RTKIT_OSLOG_LOG && rtk->oslog_echo', 'type == APPLE_RTKIT_OSLOG_LOG')
elif args.mutate == 'counter':
bodies = bodies.replace('msg, NULL, false);', '(msg & ~0xffffffffULL) | ((u32)msg % 4096), NULL, false);')
elif args.mutate == 'reset':
bodies = bodies.replace('rtk->oslog_inherited = false;', 'rtk->oslog_inherited = true;')
bodies = bodies.replace('rtk->syslog_inherited = false;', 'rtk->syslog_inherited = true;')
elif args.mutate == 'buffer':
bodies = bodies.replace('apple_rtkit_free_buffer(rtk, &rtk->oslog_buffer);', '')
# A mutation that no longer matches the driver would test nothing.
Expand Down
2 changes: 1 addition & 1 deletion tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def mutations(flag, names, base, expect=KILL):
'rtkit-crashlog-map-tests.py': [(out(), PASS),
(out() + ['--old-ref', 'cacf35543b4d534fccef3033bd2ddbb415a325b2'], PASS)],
'rtkit-oslog-tests.py': [(out(), PASS)]
+ mutations('--mutate', ('admission', 'channel', 'counter', 'reset', 'buffer'), out()),
+ mutations('--mutate', ('owed', 'policy', 'counter', 'reset', 'buffer'), out()),
'rtkit-adopted-session-tests.py': [(out(), PASS)]
+ mutations('--mutation', ('no-management', 'any-system', 'any-version',
'ignore-ops-version', 'any-buffer-endpoint'), out()),
Expand Down
Loading
Loading