Skip to content
Draft
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
27 changes: 27 additions & 0 deletions drivers/mfd/macsmc-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,32 @@ static void macsmc_port_reasons_test(struct kunit *test)
KUNIT_EXPECT_FALSE(test, apple_smc_port_reasons_only(&ctx->smc, BIT_ULL(43)));
}

static void macsmc_atomic_after_fault_test(struct kunit *test)
{
struct macsmc_test_context *ctx = test->priv;

ctx->result = 1;

/* Without a fault: notifications are turned off, then atomic mode. */
KUNIT_EXPECT_EQ(test, apple_smc_enter_atomic(&ctx->smc), 0);
KUNIT_EXPECT_TRUE(test, ctx->smc.atomic_mode);
KUNIT_ASSERT_EQ(test, ctx->seen_count, 1U);
KUNIT_EXPECT_EQ(test, ctx->seen[0], (u32)0);

/* A protocol fault refuses commands, but power-off still gets atomic mode. */
ctx->smc.atomic_mode = false;
ctx->seen_count = 0;
ctx->smc.saved_enabled = true;
ctx->smc.saved_fault = -EPROTO;
KUNIT_EXPECT_EQ(test, apple_smc_enter_atomic(&ctx->smc), 0);
KUNIT_EXPECT_TRUE(test, ctx->smc.atomic_mode);
KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U);
/* Ordinary commands stay refused. */
KUNIT_EXPECT_EQ(test, apple_smc_write_u32(&ctx->smc, SMC_KEY(MBSE), SMC_KEY(offw)),
-EPROTO);
KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U);
}

static struct kunit_case macsmc_parser_cases[] = {
KUNIT_CASE(macsmc_key_info_lock_test),
KUNIT_CASE(macsmc_key_index_test),
Expand All @@ -412,6 +438,7 @@ static struct kunit_case macsmc_parser_cases[] = {
KUNIT_CASE(macsmc_lid_wake_test),
KUNIT_CASE(macsmc_wake_reason_test),
KUNIT_CASE(macsmc_port_reasons_test),
KUNIT_CASE(macsmc_atomic_after_fault_test),
{}
};

Expand Down
33 changes: 28 additions & 5 deletions drivers/mfd/macsmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,21 @@ int apple_smc_enter_atomic(struct apple_smc *smc)
guard(mutex)(&smc->mutex);
int ret = apple_smc_saved_guard(smc);

if (ret)
return ret;
/*
* Only the power-off and restart handlers enter atomic mode, and the
* final MBSE command is all that follows. When the saved-state guard
* refuses commands (a protocol fault, say), refusing this one too
* would leave the machine running with no way to turn off or restart.
* Skip the notification write and still enter atomic mode, so that
* apple_smc_write_atomic() can send the final command.
*/
if (ret) {
dev_warn(smc->dev,
"atomic: commands are refused (%d); only the final power command is sent\n",
ret);
smc->atomic_mode = true;
return 0;
}

/*
* Disable notifications since this is called before shutdown and no
Expand Down Expand Up @@ -401,16 +414,26 @@ int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf,
ktime_t t0;
s64 us;

ret = apple_smc_saved_guard(smc);
if (ret)
return ret;
if (size > SMC_MAX_SIZE || size == 0)
return -EINVAL;

if (smc->boot_stage != APPLE_SMC_INITIALIZED)
return -EIO;
if (!smc->atomic_mode)
return -EIO;
/*
* Atomic mode only carries the final power-off or restart command (see
* apple_smc_enter_atomic()), so the saved-state guard does not stop it.
* A saved-state command still marked pending will not be answered in
* a way anything waits for now: stop routing replies to it, so that
* this command's reply is taken as this command's.
*/
ret = apple_smc_saved_guard(smc);
if (ret) {
dev_warn(smc->dev, "atomic: sending key %p4ch although commands are refused (%d)\n",
&key, ret);
smp_store_release(&smc->saved_pending, false); /* pairs with the reply path */
}
/*
* A poll timeout or a mismatched reply does not cancel the command or
* release its SRAM. Its late reply does: take it if it has arrived.
Expand Down
28 changes: 21 additions & 7 deletions drivers/power/reset/macsmc-reboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,28 @@ static int nvmem_cell_set_u8(struct nvmem_cell *cell, u8 val)
static int macsmc_prepare_atomic(struct sys_off_data *data)
{
struct macsmc_reboot *reboot = data->cb_data;
int ret;

dev_info(reboot->dev, "Preparing SMC for atomic mode\n");

apple_smc_enter_atomic(reboot->smc);
ret = apple_smc_enter_atomic(reboot->smc);
if (ret)
dev_err(reboot->dev, "Failed to enter atomic mode: %d\n", ret);
return NOTIFY_OK;
}

static int macsmc_power_off(struct sys_off_data *data)
{
struct macsmc_reboot *reboot = data->cb_data;
int ret;

dev_info(reboot->dev, "Issuing power off (off1)\n");

if (apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)) < 0) {
dev_err(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n");
ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1));
if (ret < 0) {
dev_err(reboot->dev,
"Failed to issue MBSE = off1 (power_off): %d; trying the next power-off handler\n",
ret);
} else {
mdelay(100);
WARN_ONCE(1, "Unable to power off system\n");
Expand All @@ -143,11 +150,15 @@ static int macsmc_power_off(struct sys_off_data *data)
static int macsmc_restart(struct sys_off_data *data)
{
struct macsmc_reboot *reboot = data->cb_data;
int ret;

dev_info(reboot->dev, "Issuing restart (phra)\n");

if (apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(phra)) < 0) {
dev_err(reboot->dev, "Failed to issue MBSE = phra (restart)\n");
ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(phra));
if (ret < 0) {
dev_err(reboot->dev,
"Failed to issue MBSE = phra (restart): %d; trying the next restart handler\n",
ret);
} else {
mdelay(100);
WARN_ONCE(1, "Unable to restart system\n");
Expand All @@ -161,6 +172,7 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio
struct macsmc_reboot *reboot = container_of(this, struct macsmc_reboot, reboot_notify);
u8 shutdown_flag;
u32 val;
int ret;

switch (action) {
case SYS_RESTART:
Expand All @@ -178,8 +190,10 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio
dev_info(reboot->dev, "Preparing for reboot (%p4ch)\n", &val);

/* On the Mac Mini, this will turn off the LED for power off */
if (apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val) < 0)
dev_err(reboot->dev, "Failed to issue MBSE = %p4ch (reboot_prepare)\n", &val);
ret = apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val);
if (ret < 0)
dev_err(reboot->dev, "Failed to issue MBSE = %p4ch (reboot_prepare): %d\n",
&val, ret);

/* Set the boot_stage to 0, which means we're doing a clean shutdown/reboot. */
if (reboot->nvm.boot_stage &&
Expand Down
4 changes: 3 additions & 1 deletion include/linux/mfd/macsmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, s
* This function switches the SMC backend to atomic mode which allows the
* use of apple_smc_write_atomic while disabling *all* other functions.
* This is only used for shutdown/reboot which requires writing to a SMC
* key from atomic context.
* key from atomic context. Atomic mode is entered even when ordinary commands
* are refused after a protocol fault, so that the final power command can
* still be sent.
*
* Return: Zero on success, negative errno on error
*/
Expand Down