Skip to content
Merged
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
64 changes: 64 additions & 0 deletions drivers/soc/apple/rtkit-helper-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ static void rtkh_inherit_one_sram(struct kunit *test)
KUNIT_EXPECT_NULL(test, b[0].shmem.buffer);
KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[0].shmem.iomem,
ctx->sram_mem + 0x1000);
KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers);
}

static void rtkh_inherit_max(struct kunit *test)
Expand Down Expand Up @@ -459,6 +460,7 @@ static void rtkh_inherit_max(struct kunit *test)
KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[i].shmem.iomem,
ctx->sram_mem + (b[i].shmem.iova - s));
}
KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers);
}

static void rtkh_inherit_bad_count(struct kunit *test)
Expand Down Expand Up @@ -569,6 +571,67 @@ static void rtkh_inherit_dma_mask_edge(struct kunit *test)
KUNIT_EXPECT_EQ(test, rtkh_inherit(ctx, b), -EINVAL);
}

#define RTKH_TEST_DRAM_START 0x9f0000000ULL
#define RTKH_TEST_DRAM_SIZE 0x10000

static void __iomem *rtkh_stub_map_dram(struct apple_rtkit_helper *helper,
const struct resource *res)
{
struct kunit *test = kunit_get_current_test();
struct rtkh_test *ctx = test->priv;

if (res->start >= RTKH_TEST_DRAM_START &&
res->end < RTKH_TEST_DRAM_START + RTKH_TEST_DRAM_SIZE)
return (__force void __iomem *)(ctx->sram_mem + (res->start - RTKH_TEST_DRAM_START));
return ERR_PTR(-EINVAL);
}

static void rtkh_inherit_dram_buffers(struct kunit *test)
{
struct rtkh_test *ctx = test->priv;
struct apple_rtkit_inherited_buffer b[APPLE_RTKIT_HELPER_MAX_BUFFERS];
const u64 s = RTKH_TEST_SRAM_START;
const u64 d = RTKH_TEST_DRAM_START;
const u32 dram_only[] = { 2, RTKH_HI(d + 0x1000), RTKH_LO(d + 0x1000), 0x2000, 0 };
const u32 mixed[] = {
1, RTKH_HI(s), RTKH_LO(s), 0x1000, 0,
2, RTKH_HI(d + 0x2000), RTKH_LO(d + 0x2000), 0x1000, 0,
};
const u32 dram_then_bad[] = {
2, RTKH_HI(d), RTKH_LO(d), 0x1000, 0,
4, RTKH_HI(s), RTKH_LO(s), 0x1000, 3,
};
const u32 sram_only[] = { 1, RTKH_HI(s), RTKH_LO(s), 0x1000, 0 };

kunit_activate_static_stub(test, apple_rtkit_helper_map_dram, rtkh_stub_map_dram);

/* A DRAM buffer followed by a bad tuple fails without setting dram_buffers. */
rtkh_node(ctx, "apple,t8112-mtp");
rtkh_inventory(ctx, dram_then_bad, ARRAY_SIZE(dram_then_bad));
KUNIT_EXPECT_EQ(test, rtkh_inherit(ctx, b), -EINVAL);
KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers);

/* A valid reserved DRAM buffer records dram_buffers. */
rtkh_node(ctx, "apple,t8112-mtp");
rtkh_inventory(ctx, dram_only, ARRAY_SIZE(dram_only));
KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 1);
KUNIT_EXPECT_TRUE(test, ctx->helper.dram_buffers);
KUNIT_EXPECT_PTR_EQ(test, (__force void *)b[0].shmem.iomem,
ctx->sram_mem + 0x1000);

/* Mixed SRAM and DRAM buffers also record dram_buffers. */
rtkh_node(ctx, "apple,t8112-mtp");
rtkh_inventory(ctx, mixed, ARRAY_SIZE(mixed));
KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 2);
KUNIT_EXPECT_TRUE(test, ctx->helper.dram_buffers);

/* A subsequent SRAM-only inventory clears dram_buffers. */
rtkh_node(ctx, "apple,t8112-mtp");
rtkh_inventory(ctx, sram_only, ARRAY_SIZE(sram_only));
KUNIT_ASSERT_EQ(test, rtkh_inherit(ctx, b), 1);
KUNIT_EXPECT_FALSE(test, ctx->helper.dram_buffers);
}

/* Sleep: the AP power notices */

static void rtkh_expect_calls(struct kunit *test, const bool *on, unsigned int n)
Expand Down Expand Up @@ -891,6 +954,7 @@ static struct kunit_case apple_rtkit_helper_test_cases[] = {
KUNIT_CASE(rtkh_inherit_bad_tuple),
KUNIT_CASE(rtkh_inherit_bad_second),
KUNIT_CASE(rtkh_inherit_dma_mask_edge),
KUNIT_CASE(rtkh_inherit_dram_buffers),
KUNIT_CASE(rtkh_pm_fw_sleep_cycle),
KUNIT_CASE(rtkh_pm_idle_late),
KUNIT_CASE(rtkh_pm_no_fw_no_notice),
Expand Down
44 changes: 37 additions & 7 deletions drivers/soc/apple/rtkit-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/pm_wakeup.h>
#include <linux/soc/apple/rtkit.h>
#include <linux/soc/apple/rtkit-helper.h>
#include <linux/soc/apple/sleep.h>
#include <linux/soc/apple/t8132.h>
#include <linux/string.h>
#include <linux/suspend.h>
Expand Down Expand Up @@ -44,6 +45,8 @@ struct apple_rtkit_helper {
* it does not list must not replace storage the firmware still owns.
*/
bool strict_buffers;
/* Adopted buffers include reserved DRAM regions outside on-chip SRAM. */
bool dram_buffers;
/* The AP idle notice waits for .suspend_late, after the HID host's packets */
bool idle_late;
/* The firmware was told the AP is going to sleep. */
Expand Down Expand Up @@ -130,6 +133,11 @@ static void apple_rtkit_helper_release(void *rtk)
apple_rtkit_free_retaining_buffers(rtk);
}

static void apple_rtkit_helper_release_slp_ddr(void *unused)
{
apple_sleep_coproc_slp_ddr(false);
}

/*
* MTP firmware cannot always be restarted once it has been taken to QUIESCED
* or SLEEP, so a boot stage that used it hands the session over running. It
Expand Down Expand Up @@ -203,6 +211,18 @@ static bool apple_rtkit_helper_retained_mtp(struct device_node *np)
of_device_is_compatible(np, "apple,t6041-mtp");
}

static void __iomem *apple_rtkit_helper_map_dram(struct apple_rtkit_helper *helper,
const struct resource *res)
{
#if IS_ENABLED(CONFIG_APPLE_RTKIT_HELPER_KUNIT_TEST)
KUNIT_STATIC_STUB_REDIRECT(apple_rtkit_helper_map_dram, helper, res);
#endif
/* DVA == PA is part of this retained-bypass handoff contract. */
if (!apple_rtkit_helper_reserved(helper->dev->of_node, res))
return ERR_PTR(-EINVAL);
return devm_ioremap(helper->dev, res->start, resource_size(res)) ?: ERR_PTR(-ENOMEM);
}

static int apple_rtkit_helper_inherit_buffers(
struct apple_rtkit_helper *helper,
struct apple_rtkit_inherited_buffer *buffers)
Expand All @@ -211,6 +231,7 @@ static int apple_rtkit_helper_inherit_buffers(
struct property *inventory;
u32 cells[APPLE_RTKIT_HELPER_MAX_BUFFERS * APPLE_RTKIT_HELPER_BUFFER_CELLS];
int count, i, length, ret;
bool has_dram = false;

/* T8140 producers predating the buffer inventory remain compatible. */
inventory = of_find_property(np, "apple,rtkit-buffers", &length);
Expand All @@ -219,8 +240,10 @@ static int apple_rtkit_helper_inherit_buffers(
return -EINVAL;
return 0;
}
if (!length)
if (!length) {
helper->dram_buffers = false;
return 0;
}
count = of_property_count_u32_elems(np, "apple,rtkit-buffers");
if (count < 0 || count > ARRAY_SIZE(cells) ||
count % APPLE_RTKIT_HELPER_BUFFER_CELLS)
Expand Down Expand Up @@ -253,14 +276,13 @@ static int apple_rtkit_helper_inherit_buffers(
if (helper->sram && resource_contains(helper->sram, &res)) {
b->iomem = helper->sram_base + res.start - helper->sram->start;
} else {
/* DVA == PA is part of this retained-bypass handoff contract. */
if (!apple_rtkit_helper_reserved(np, &res))
return -EINVAL;
b->iomem = devm_ioremap(helper->dev, res.start, resource_size(&res));
if (!b->iomem)
return -ENOMEM;
b->iomem = apple_rtkit_helper_map_dram(helper, &res);
if (IS_ERR(b->iomem))
return PTR_ERR(b->iomem);
has_dram = true;
}
}
helper->dram_buffers = has_dram;
return count;
}

Expand Down Expand Up @@ -370,6 +392,14 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev)
ret = apple_rtkit_helper_pm_register(helper);
if (ret)
return ret;
if (helper->dram_buffers) {
apple_sleep_coproc_slp_ddr(true);
ret = devm_add_action_or_reset(dev,
apple_rtkit_helper_release_slp_ddr,
NULL);
if (ret)
return ret;
}

/* There is no safe module-unload boundary for the retained session. */
__module_get(THIS_MODULE);
Expand Down
3 changes: 3 additions & 0 deletions drivers/soc/apple/sleep-t8112.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ static int t8112_sleep_map_regs(struct apple_sleep *s)
}
}

sleep_slp_ddr_bind(s);

{
int ret = apple_sleep_read_wake_sources(s);

Expand Down Expand Up @@ -1222,6 +1224,7 @@ static const struct apple_sleep_soc t8112_sleep_soc = {
.ops = &t8112_sleep_ops,
.untested = true,
.early_context = true,
.coproc_slp_ddr = true,
.early_cpu_wfi = true,
.boot_wfi_delay_ms = 2,
.no_pmu_notes = true,
Expand Down
118 changes: 117 additions & 1 deletion drivers/soc/apple/sleep-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ static void apple_sleep_test_known_profiles(struct kunit *test)
KUNIT_ASSERT_LT(test, n, ARRAY_SIZE(apple_sleep_test_profiles));
KUNIT_EXPECT_STREQ(test, id->compatible, apple_sleep_test_profiles[n]);
if (!strcmp(id->compatible, "apple,t8142-sleep") ||
!strcmp(id->compatible, "apple,t8112-sleep") ||
!strcmp(id->compatible, "apple,t8122-sleep")) {
const struct apple_sleep_soc *soc = id->data;

Expand All @@ -82,6 +81,11 @@ static void apple_sleep_test_known_profiles(struct kunit *test)
/* T8142 has run full deep sleeps and wakes; T8122 has not yet. */
KUNIT_EXPECT_EQ(test, (int)soc->untested,
!strcmp(id->compatible, "apple,t8122-sleep"));
} else if (!strcmp(id->compatible, "apple,t8112-sleep")) {
const struct apple_sleep_soc *soc = id->data;

KUNIT_ASSERT_NOT_NULL(test, soc);
KUNIT_EXPECT_TRUE(test, soc->untested);
}
}
KUNIT_EXPECT_EQ(test, n, ARRAY_SIZE(apple_sleep_test_profiles));
Expand Down Expand Up @@ -607,6 +611,117 @@ static void apple_sleep_test_t6040_wdt_span(struct kunit *test)
KUNIT_EXPECT_FALSE(test, t6040_wdt_resource_ok(&elsewhere));
}

struct apple_sleep_test_slp_ddr_saved {
bool usb;
unsigned int coproc;
bool coproc_en;
void __iomem *word;
bool crashed;
bool deep_once;
};

struct apple_sleep_test_slp_ddr_state {
u32 fake_wake;
struct apple_sleep_soc soc;
struct apple_sleep sleep;
struct apple_sleep_test_slp_ddr_saved saved;
};

static void apple_sleep_test_slp_ddr_restore(void *data)
{
struct apple_sleep_test_slp_ddr_saved *saved = data;

guard(spinlock_irqsave)(&sleep_slp_ddr_lock);
sleep_slp_ddr_usb = saved->usb;
sleep_slp_ddr_coproc = saved->coproc;
sleep_slp_ddr_coproc_en = saved->coproc_en;
sleep_slp_ddr_word = saved->word;
sleep_slp_ddr_crashed = saved->crashed;
sleep_slp_ddr_deep_once = saved->deep_once;
}

static void apple_sleep_test_slp_ddr_voters(struct kunit *test)
{
struct apple_sleep_test_slp_ddr_state *st;

st = kunit_kzalloc(test, sizeof(*st), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, st);

scoped_guard(spinlock_irqsave, &sleep_slp_ddr_lock) {
st->saved.usb = sleep_slp_ddr_usb;
st->saved.coproc = sleep_slp_ddr_coproc;
st->saved.coproc_en = sleep_slp_ddr_coproc_en;
st->saved.word = sleep_slp_ddr_word;
st->saved.crashed = sleep_slp_ddr_crashed;
st->saved.deep_once = sleep_slp_ddr_deep_once;
sleep_slp_ddr_usb = false;
sleep_slp_ddr_coproc = 0;
sleep_slp_ddr_coproc_en = false;
sleep_slp_ddr_word = NULL;
sleep_slp_ddr_crashed = false;
sleep_slp_ddr_deep_once = false;
}
KUNIT_ASSERT_EQ(test,
kunit_add_action_or_reset(test, apple_sleep_test_slp_ddr_restore,
&st->saved), 0);

st->fake_wake = 0xdead0000U;
st->sleep.soc = &st->soc;
st->sleep.wake = (void __iomem __force *)&st->fake_wake;

/* A coprocessor vote taken before sleep probes stays deferred until bind. */
apple_sleep_coproc_slp_ddr(true);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);

/* A SoC without coproc_slp_ddr ignores coprocessor votes (USB still works). */
st->soc.coproc_slp_ddr = false;
sleep_slp_ddr_bind(&st->sleep);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);

apple_sleep_usb_slp_ddr(true);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB);
apple_sleep_usb_slp_ddr(false);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);

/* A SoC with coproc_slp_ddr applies the deferred vote and preserves other bits. */
st->soc.coproc_slp_ddr = true;
sleep_slp_ddr_bind(&st->sleep);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB);

/* Coprocessor votes are refcounted and guard against underflow. */
apple_sleep_coproc_slp_ddr(true);
apple_sleep_coproc_slp_ddr(false);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB);
apple_sleep_coproc_slp_ddr(false);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);
apple_sleep_coproc_slp_ddr(false);
apple_sleep_coproc_slp_ddr(true);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB);

/* deep_once overrides active votes for one sleep and restores on clear. */
sleep_slp_ddr_set_deep_once(true);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);
sleep_slp_ddr_set_deep_once(false);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U | WAKE_SLP_DDR_USB);

/* A crashed unmanaged coprocessor permanently clears bit 0. */
scoped_guard(spinlock_irqsave, &sleep_slp_ddr_lock) {
sleep_slp_ddr_crashed = true;
sleep_slp_ddr_write();
}
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);
apple_sleep_usb_slp_ddr(true);
KUNIT_EXPECT_EQ(test, st->fake_wake, 0xdead0000U);

/* Only T8112 enables coprocessor SLP_DDR votes among matched profiles. */
for (const struct of_device_id *id = apple_sleep_of_match; id->compatible[0]; id++) {
const struct apple_sleep_soc *soc = id->data;
bool want = !strcmp(id->compatible, "apple,t8112-sleep");

KUNIT_EXPECT_EQ_MSG(test, soc->coproc_slp_ddr, want, "%s", id->compatible);
}
}

static struct kunit_case apple_sleep_test_cases[] = {
KUNIT_CASE(apple_sleep_test_layout),
KUNIT_CASE(apple_sleep_test_t6050),
Expand All @@ -623,6 +738,7 @@ static struct kunit_case apple_sleep_test_cases[] = {
KUNIT_CASE(apple_sleep_test_wdt_handoff),
KUNIT_CASE(apple_sleep_test_apcie_slots),
KUNIT_CASE(apple_sleep_test_t6040_wdt_span),
KUNIT_CASE(apple_sleep_test_slp_ddr_voters),
{}
};

Expand Down
Loading
Loading