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: 4 additions & 2 deletions drivers/mfd/macsmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,8 @@ static void apple_smc_sleep_wake(struct apple_smc *smc)
}
smc->sleep_warned = false;
apple_smc_lid_wake(smc);
if (apple_smc_port_reasons_only(smc, smc->wake_record))
apple_sleep_port_wake(apple_sleep_lid_closed_at_wake() > 0);
/* With a sleep policy (the J714s), the record is only named and the lid read. */
if (smc->sleep_policy)
return;
Expand Down Expand Up @@ -1479,8 +1481,8 @@ static int apple_smc_prepare(struct device *dev)
int ret = 0;

if (smc->saved_enabled) {
if (!mutex_trylock(&smc->mutex))
return -EBUSY;
/* A sleep right after a dark wake finds the wake's commands in flight */
mutex_lock(&smc->mutex);
ret = apple_smc_saved_guard(smc);
mutex_unlock(&smc->mutex);
}
Expand Down
52 changes: 41 additions & 11 deletions drivers/soc/apple/rtkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,25 @@ 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 @@ -1373,11 +1392,12 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
return;
}

/* Preserve the tested Neo/M5 and synchronous-log no-ACK policy. */
if (type == APPLE_RTKIT_OSLOG_LOG &&
(rtk->sync_syslog_ack || of_machine_is_compatible("apple,t8140") ||
of_machine_is_compatible("apple,t8142"))) {
dev_dbg(rtk->dev, "RTKit: oslog notification: %016llx\n", msg);
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(rtk->dev, "RTKit: invalid oslog notification: %llx\n", msg);
return;
}

Expand All @@ -1392,13 +1412,9 @@ static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
* 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.
* channel zero needs no buffer mapping to acknowledge its counter
* (apple_rtkit_oslog_echoed()).
*/
if ((!rtk->oslog_buffer.size && !rtk->oslog_inherited) ||
(msg & APPLE_RTKIT_OSLOG_LOG_RESERVED)) {
dev_warn(rtk->dev, "RTKit: invalid oslog notification: %llx\n", msg);
break;
}
err = apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_OSLOG,
msg, NULL, false);
if (err)
Expand Down Expand Up @@ -1800,6 +1816,16 @@ bool apple_rtkit_m5_saved_unresolved(struct apple_rtkit *rtk)
}
EXPORT_SYMBOL_GPL(apple_rtkit_m5_saved_unresolved);

/* A log word the ordinary handler answers with its echo and nothing else. */
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);
}

int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk)
{
struct apple_mbox_msg msg;
Expand All @@ -1824,6 +1850,10 @@ int apple_rtkit_m5_saved_poll(struct apple_rtkit *rtk)
rtk->ops->recv_message_early &&
rtk->ops->recv_message_early(rtk->cookie, msg.msg1, msg.msg0))
return 1;
/* Echoed here, its record unread: the firmware takes no AP idle notice with one owed. */
if (apple_rtkit_log_word(rtk, msg.msg1, msg.msg0) &&
!apple_mbox_send_nowait(rtk->mbox, msg))
return 1;
/* SMC notifications remain ordered for the ordinary notifier worker.
* Every other packet is retained, and refuses this attempt. In
* particular no crash/report message is silently eaten by polling.
Expand Down
26 changes: 26 additions & 0 deletions drivers/soc/apple/sleep-t6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -3601,6 +3601,32 @@ static void t6050_wake_source_report(struct apple_sleep *s)
dev_info(s->dev, "wake status set: GFX %u\n", b);
}

/* Only the SMC's lines are latched: its own wake, whatever else was armed. */
static bool t6050_smc_woke_alone(struct apple_sleep *s)
{
struct apple_sleep_region_state *r = s->region;
u32 smc[T6050_WAKE_WORDS] = {};
unsigned int i;
bool any = false;

if (!r || !r->wake || !r->wake_gfx || r->wake_status[T6050_WAKE_WORDS])
return false;
for (i = 0; i < ARRAY_SIZE(t6050_wake_names); i++) {
u32 id = t6050_wake_names[i].id;

if (strstarts(t6050_wake_names[i].name, "SMC.") &&
id / 32 < T6050_WAKE_WORDS)
smc[id / 32] |= BIT(id % 32);
}
for (i = 0; i < T6050_WAKE_WORDS; i++) {
if (r->wake_status[i] & ~smc[i])
return false;
any |= !!r->wake_status[i];
}

return any;
}

static void t6050_sleep_wake_regs(struct apple_sleep *s)
{
struct apple_sleep_region_state *r = s->region;
Expand Down
20 changes: 20 additions & 0 deletions drivers/soc/apple/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -3698,6 +3698,21 @@ void apple_sleep_lid_closed_wake(void)
}
EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_wake);

/*
* macOS leaves a wake for a charger or a cable alone a dark one. With the lid closed
* nothing there is for the user: sleep again.
*/
void apple_sleep_port_wake(bool lid_closed)
{
struct apple_sleep *s = apple_sleep;

if (!s || !s->smc_woke_alone || !lid_closed)
return;
dev_info(s->dev, "woken for a port with the lid closed: dark wake\n");
pm_set_dark_wake();
}
EXPORT_SYMBOL_GPL(apple_sleep_port_wake);

/* Read in the SMC's noirq resume. Forgotten as each sleep begins. */
static int sleep_lid_closed = -ENODATA;

Expand Down Expand Up @@ -6685,13 +6700,18 @@ static int apple_sleep_prepare(void)
}

/* Only the SMC's sources woke the SoC. */
static bool t6050_smc_woke_alone(struct apple_sleep *s);

static bool apple_sleep_smc_woke_alone(struct apple_sleep *s)
{
const struct apple_sleep_wake_name *names = s->soc->wake_names;
u32 smc[WAKE_WORDS] = {};
bool any = false;
int i;

if (s->soc->regions)
return t6050_smc_woke_alone(s);

/* SMC.CPUWakeupAP and SMC.OutboxNotEmpty, by their names. */
for (i = 0; i < s->soc->nwake_names; i++) {
u32 id = names[i].id;
Expand Down
5 changes: 5 additions & 0 deletions include/linux/soc/apple/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ bool apple_sleep_debug_link_up(void);
* device has resumed, as macOS does from a dark wake.
*/
void apple_sleep_lid_closed_wake(void);
void apple_sleep_port_wake(bool lid_closed);

/*
* From the SMC's driver, in its noirq resume: the lid as it reads now, after every
Expand Down Expand Up @@ -377,6 +378,10 @@ static inline void apple_sleep_lid_closed_wake(void)
{
}

static inline void apple_sleep_port_wake(bool lid_closed)
{
}

static inline void apple_sleep_lid_at_wake(bool closed)
{
}
Expand Down
6 changes: 5 additions & 1 deletion tools/asahi/rtkit-oslog-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def function(source, name):
match = re.search(r'^(?:static )?(?:void|int) ' + name + r'\(', source, re.M)
match = re.search(r'^(?:static )?(?:void|int|bool) ' + name + r'\(', source, re.M)
assert match, name
opening = source.index('{', match.end())
depth, end = 1, opening + 1
Expand Down Expand Up @@ -173,12 +173,16 @@ 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')
elif args.mutate == 'counter':
Expand Down
7 changes: 6 additions & 1 deletion tools/testing/selftests/drivers/gpu/apple-dcp/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def oslog(root):
source = (root / 'drivers/soc/apple/rtkit.c').read_text()
constants = '\n'.join(line for line in source.splitlines()
if line.startswith('#define APPLE_RTKIT_OSLOG_'))
# The echo's predicate, where the receiver is built on one.
helpers = ''.join(block(source, marker) + '\n'
for marker in ('static bool apple_rtkit_oslog_unanswered(',
'static bool apple_rtkit_oslog_echoed(')
if marker in source)
return r'''
#include <assert.h>
#include <stdbool.h>
Expand Down Expand Up @@ -37,7 +42,7 @@ def oslog(root):
{ (void)rtk; assert(ep==8 && !completion && !atomic); acks++; observed=msg; return 0; }
static void apple_rtkit_common_rx_get_buffer(struct apple_rtkit *rtk, struct buffer *b, u8 ep, u64 msg)
{ assert(b==&rtk->oslog_buffer && ep==8); requests++; observed=msg; }
''' + constants + '\n' + block(source, 'static void apple_rtkit_oslog_rx(') + r'''
''' + constants + '\n' + helpers + block(source, 'static void apple_rtkit_oslog_rx(') + r'''
static void reset(void) { warnings=notifications=requests=acks=errors=0; observed=0; }
int main(void)
{
Expand Down
Loading