Skip to content
Closed
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
70 changes: 66 additions & 4 deletions drivers/mfd/macsmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ static int apple_smc_probe(struct platform_device *pdev)
of_device_is_compatible(dev->of_node, "apple,t6050-smc");
if (smc->saved_enabled)
smc->wake_reasons = apple_smc_j714s_wake_reasons;
/* J813's late notice needs the bounded, IRQ-independent transport too. */
if (of_machine_is_compatible("apple,j813"))
smc->saved_enabled = true;
smc->sram_base = devm_platform_get_and_ioremap_resource(pdev, 1, &smc->sram);
if (IS_ERR(smc->sram_base))
return dev_err_probe(dev, PTR_ERR(smc->sram_base), "Failed to map SRAM region");
Expand Down Expand Up @@ -1130,7 +1133,8 @@ static int apple_smc_sleep_saved_begin(void *data)
struct apple_smc *smc = data;
int ret;

if (!smc->saved_enabled || !smc->sleep_policy)
if (!smc->saved_enabled ||
(!smc->sleep_policy && !apple_sleep_smc_defer_notice()))
return -EOPNOTSUPP;
if (!mutex_trylock(&smc->mutex))
return -EBUSY;
Expand Down Expand Up @@ -1182,7 +1186,7 @@ static int apple_smc_sleep_saved_begin(void *data)
return ret;
}

static int apple_smc_sleep_saved_end(void *data)
static int apple_smc_sleep_saved_release(void *data, bool wake_ap)
{
struct apple_smc *smc = data;
int ret, restore = 0;
Expand All @@ -1195,7 +1199,7 @@ static int apple_smc_sleep_saved_end(void *data)
/* Bounded reconciliation; never release pending ownership on timeout. */
if (!ret && smc->saved_pending)
ret = apple_smc_saved_wait_reply(smc);
if (smc->ap_notified) {
if (wake_ap && smc->ap_notified) {
restore = apple_rtkit_m5_saved_ap_power(smc->rtk, true, NULL, NULL);
if (!restore) {
smc->ap_notified = false;
Expand All @@ -1217,6 +1221,23 @@ static int apple_smc_sleep_saved_end(void *data)
return ret;
}

static int apple_smc_sleep_saved_end(void *data)
{
return apple_smc_sleep_saved_release(data, true);
}

/*
* J813's ordinary wake notice belongs in macsmc's resume_noirq, after the
* device dependencies have resumed. Platform wake precedes every noirq
* device callback; an AP ON there can wake the radio before its DMA path.
* Release the bounded transport here, retaining ap_notified for the usual
* noirq wake callback. A refused entry never posted AP IDLE.
*/
static int apple_smc_j813_saved_end(void *data)
{
return apple_smc_sleep_saved_release(data, false);
}

/* Readiness is a returning operation BEFORE SEP/ANS/PMP stop. A drained
* notification remains retained and can cancel only on this side of the boundary.
*/
Expand Down Expand Up @@ -1336,6 +1357,42 @@ static const struct apple_sleep_smc_saved_ops apple_smc_saved_ops = {
.pmvc = apple_smc_sleep_saved_pmvc,
};

/*
* J813 retains its existing MBSE/AP sequence and delay floors. The transport
* is the saved lane: no scheduler, jiffies, IRQ worker or SRAM reuse is needed
* after the peer stops. Unrelated notifications are retained until resume.
*/
static int apple_smc_sleep_after_peers(void *data, u64 *sent, u64 *ack)
{
struct apple_smc *smc = data;
int ret;

lockdep_assert_held(&smc->mutex);
*sent = *ack = 0;
if (!irqs_disabled() || !smc->saved_scope || !smc->sleep_warned ||
smc->ap_notified)
return -EINVAL;

smc->saved_committed = true;
ret = apple_smc_saved_write_u32(smc, SMC_KEY(MBSE), SMC_KEY(slpa));
if (ret)
goto out;
mdelay(SMC_SLEEP_ARM_MS);
smc->ap_notified = true;
ret = apple_rtkit_m5_saved_ap_power(smc->rtk, false, sent, ack);
if (!ret)
mdelay(SMC_SLEEP_IDLE_MS);
out:
smc->saved_notice_error = ret;
return ret;
}

static const struct apple_sleep_smc_saved_ops apple_smc_j813_saved_ops = {
.begin = apple_smc_sleep_saved_begin,
.late_notice = apple_smc_sleep_after_peers,
.end = apple_smc_j813_saved_end,
};

static int apple_smc_prepare_policy(struct apple_smc *smc)
{
const struct apple_sleep_smc_policy *p = smc->sleep_policy;
Expand Down Expand Up @@ -1401,6 +1458,8 @@ static int apple_smc_sleep_prepare(struct apple_smc *smc)
else
apple_smc_sleep_warn(smc);

if (apple_sleep_smc_defer_notice())
return apple_sleep_smc_register_saved(smc->dev, &apple_smc_j813_saved_ops, smc);
return 0;
}

Expand Down Expand Up @@ -1514,6 +1573,9 @@ static int apple_smc_sleep_enter(struct apple_smc *smc)
apple_smc_outbox_poll(smc, "late sleep warning", SMC_SLEEP_WARN_MS, 0);
}

/* Warning and debug-link handling stay in noirq; only the notice moves. */
if (apple_sleep_smc_defer_notice())
return 0;
return apple_smc_sleep_commit(smc);
}

Expand Down Expand Up @@ -1541,7 +1603,7 @@ static int apple_smc_resume_noirq(struct device *dev)
if (smc->saved_enabled && (smc->saved_recovery || smc->saved_pending ||
smc->saved_fault))
return smc->saved_restore_error ?: smc->saved_fault ?: -EBUSY;
if (smc->saved_enabled && smc->sleep_policy) {
if (smc->saved_enabled && (smc->sleep_policy || smc->saved_wake)) {
if (smc->saved_wake) {
apple_smc_set_polled(smc, true);
apple_smc_sleep_wake(smc);
Expand Down
46 changes: 41 additions & 5 deletions drivers/soc/apple/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,18 @@ const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void)
}
EXPORT_SYMBOL_GPL(apple_sleep_smc_policy);

static bool sleep_j813_smc_late = true;
module_param_named(j813_smc_late, sleep_j813_smc_late, bool, 0444);
MODULE_PARM_DESC(j813_smc_late,
"TEST ONLY: J813 final SMC sleep notice after peer stops");

bool apple_sleep_smc_defer_notice(void)
{
return apple_sleep && READ_ONCE(sleep_j813_smc_late) &&
of_machine_is_compatible("apple,j813") && pm_suspend_via_firmware();
}
EXPORT_SYMBOL_GPL(apple_sleep_smc_defer_notice);

int apple_sleep_smc_register(struct device *dev,
int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data)
{
Expand All @@ -1781,8 +1793,8 @@ int apple_sleep_smc_register_saved(struct device *dev,
struct apple_sleep *s = apple_sleep;
guard(mutex)(&apple_sleep_smc_lock);

if (!s || !s->soc->early_context || !s->soc->smc_policy ||
!pm_suspend_via_firmware())
if (!s || ((!s->soc->early_context || !s->soc->smc_policy) &&
!apple_sleep_smc_defer_notice()) || !pm_suspend_via_firmware())
return -EOPNOTSUPP;
if (!ops || !ops->begin || !ops->late_notice || !ops->end || s->smc.dev)
return -EBUSY;
Expand Down Expand Up @@ -5743,11 +5755,12 @@ static void t8132_wake(struct apple_sleep *s)

static int apple_sleep_smc_late_notice(struct apple_sleep *s)
{
if (!s->soc->smc_policy)
if (!s->soc->smc_policy && !apple_sleep_smc_defer_notice())
return 0;
if (!s->smc.dev || !s->smc.late_notice)
return -ENODEV;
if (s->soc->early_context && (!s->smc.saved_ops || !s->smc.held))
if ((s->soc->early_context || apple_sleep_smc_defer_notice()) &&
(!s->smc.saved_ops || !s->smc.held))
return -EBUSY;
return s->smc.late_notice(s->smc.data, &s->smc_idle_sent, &s->smc_idle_ack);
}
Expand Down Expand Up @@ -5936,6 +5949,14 @@ static int apple_sleep_enter(suspend_state_t state)
if (s->soc->early_context)
return apple_sleep_enter_early(s);

/* Refuse while all peer stops are still reversible. */
if (apple_sleep_smc_defer_notice()) {
if (!s->smc.dev || !s->smc.late_notice)
return -ENODEV;
if (!s->smc.saved_ops || !s->smc.held)
return -EBUSY;
}

sleep_note(s, 0x10);
ret = apple_sleep_refuse(s);
if (ret) {
Expand Down Expand Up @@ -6254,6 +6275,18 @@ static int apple_sleep_enter(suspend_state_t state)
if (s->soc->ack_cluster_dcs && (readl(s->cluster_dcs) & CLUSTER_UP_DONE))
writel(readl(s->cluster_dcs), s->cluster_dcs);
apple_sleep_coprocs_stop(s, false);
/*
* J813 native trace: ANS idle completes before SMC slpa/AP IDLE.
* In the battery failure SMC faulted at ANS + 0xc110 after ANS's
* idle ACK was missed. Keep its final notice behind the peer stops
* and ahead of the domain power-down in the last mile.
*/
if (apple_sleep_smc_defer_notice()) {
ret = apple_sleep_smc_late_notice(s);
dev_info(s->dev, "TEST: SMC notice after peer stops: %d\n", ret);
if (ret)
panic("J813 SMC notice failed after peer stops: %d", ret);
}
/* Done by now unless no SMC driver is bound. */
apple_sleep_usb_down(s);
s->deep_once = READ_ONCE(sleep_deep_requested);
Expand Down Expand Up @@ -6529,7 +6562,8 @@ static int apple_sleep_smc_scope_begin(void)
struct apple_sleep *s = apple_sleep;
int ret = 0;

if (!s->soc->early_context || !s->soc->smc_policy)
if ((!s->soc->early_context || !s->soc->smc_policy) &&
!apple_sleep_smc_defer_notice())
return 0;
mutex_lock(&apple_sleep_smc_lock);
if (!s->smc.saved_ops || !s->smc.dev) {
Expand Down Expand Up @@ -6749,6 +6783,8 @@ static const struct platform_suspend_ops apple_sleep_ops = {
.valid = apple_sleep_valid,
.begin = apple_sleep_begin,
.prepare = apple_sleep_prepare,
.prepare_late = apple_sleep_smc_scope_begin,
.finish = apple_sleep_smc_scope_end,
.enter = apple_sleep_enter,
.wake = apple_sleep_report,
.end = apple_sleep_end,
Expand Down
3 changes: 3 additions & 0 deletions include/linux/soc/apple/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct apple_sleep_smc_saved_ops {

#if IS_ENABLED(CONFIG_APPLE_SOC_SLEEP)
const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void);
/* J813 experiment: send the final SMC notice after the peer idle handshakes. */
bool apple_sleep_smc_defer_notice(void);
int apple_sleep_smc_register(struct device *dev,
int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data);
int apple_sleep_smc_register_saved(struct device *dev,
Expand Down Expand Up @@ -241,6 +243,7 @@ void apple_sleep_restore_pins(void);
int apple_sleep_engine_reset(struct device *dev);
#else
static inline const struct apple_sleep_smc_policy *apple_sleep_smc_policy(void) { return NULL; }
static inline bool apple_sleep_smc_defer_notice(void) { return false; }
static inline int apple_sleep_smc_register(struct device *dev,
int (*late_notice)(void *data, u64 *sent, u64 *ack), void *data) { return -EOPNOTSUPP; }
static inline int apple_sleep_smc_register_saved(struct device *dev,
Expand Down
Loading