From f925befaa2679e41db2ca422d0aef91c6c8be011 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 10:11:30 +1100 Subject: [PATCH 1/2] thunderbolt: test: cover the control channel kept through s2idle A host router that stays awake through suspend-to-idle keeps its control channel when every connection is another host, so that the peer's XDomain discovery is still answered. Nothing tests which suspends retain the channel or what gets through it while the domain is suspended. Add four cases to the thunderbolt suite: - tb_xdp_noirq_request() admits the UUID, properties and link state status requests and no other packet type; - tb_domain_retain_noirq_control() retains only for a suspend-to-idle of a host router with QUIRK_NO_SYSTEM_SLEEP that has an XDomain and no router behind any port; - tb_domain_suspend_noirq() and tb_domain_resume_noirq() leave a retained control channel alone, retain nothing when the connection manager refuses the suspend, and tb_domain_event_cb() drops every event but an XDomain request until the domain has resumed; - tb_xdomain_handle_request() consumes service protocol packets, responses and state-changing discovery requests while retained, without calling a handler or queueing work. The domain in these cases has no control channel, so a path that stops or starts a retained one faults. tb_domain_event_cb(), tb_domain_retain_noirq_control() and tb_xdp_noirq_request() become VISIBLE_IF_KUNIT for this; they stay static without CONFIG_KUNIT. Signed-off-by: Ryan Murray --- drivers/thunderbolt/domain.c | 7 +- drivers/thunderbolt/tb.h | 4 + drivers/thunderbolt/test.c | 335 ++++++++++++++++++++++++++++++++++ drivers/thunderbolt/xdomain.c | 3 +- 4 files changed, 345 insertions(+), 4 deletions(-) diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c index e77c0eab18cc4..b9e66861fbc75 100644 --- a/drivers/thunderbolt/domain.c +++ b/drivers/thunderbolt/domain.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "tb.h" @@ -334,8 +335,8 @@ const struct device_type tb_domain_type = { .release = tb_domain_release, }; -static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, - const void *buf, size_t size) +VISIBLE_IF_KUNIT bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, + const void *buf, size_t size) { struct tb *tb = data; @@ -533,7 +534,7 @@ void tb_domain_remove(struct tb *tb) } EXPORT_SYMBOL_FOR_MODULES(tb_domain_remove, "thunderbolt_apple"); -static bool tb_domain_retain_noirq_control(struct tb *tb) +VISIBLE_IF_KUNIT bool tb_domain_retain_noirq_control(struct tb *tb) { struct tb_port *port; bool xdomain = false; diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 839468ed8def0..70caebcf2b38b 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -773,6 +773,10 @@ int tb_consumed_dp_bandwidth(struct tb *tb, struct tb_port *src_port, int *consumed_up, int *consumed_down); u32 tb_dp_host_rate_limit(struct tb *tb); void tb_dp_host_tunnel_gone(struct tb *tb, unsigned int before); +bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, + const void *buf, size_t size); +bool tb_domain_retain_noirq_control(struct tb *tb); +bool tb_xdp_noirq_request(enum tb_xdp_type type); #endif #if IS_ENABLED(CONFIG_USB4_KUNIT_TEST) size_t tb_test_cm_size(void); diff --git a/drivers/thunderbolt/test.c b/drivers/thunderbolt/test.c index f8418132064af..fbb318f17812d 100644 --- a/drivers/thunderbolt/test.c +++ b/drivers/thunderbolt/test.c @@ -4316,6 +4316,337 @@ static void tb_test_dp_shared_rate_debounce_deferred(struct kunit *test) tb_test_dp_shared_rate_arrival(test, false, true); } +/* + * A domain as tb_domain_suspend_noirq() sees it on a host that keeps its + * router awake through suspend-to-idle (QUIRK_NO_SYSTEM_SLEEP): a host + * router and a connection manager that counts its calls. There is no + * control channel: the paths that keep it running must not touch it. + */ +struct tb_test_noirq { + struct tb_nhi nhi; + struct tb_cm_ops ops; + struct tb *tb; + struct tb_switch *host; + unsigned int saved_flags; + unsigned int suspends, resumes, events; + enum tb_cfg_pkg_type last_event; + int suspend_ret; + unsigned int handled; +}; + +static int tb_test_noirq_cm_suspend(struct tb *tb) +{ + struct tb_test_noirq *ctx = kunit_get_current_test()->priv; + + ctx->suspends++; + return ctx->suspend_ret; +} + +static int tb_test_noirq_cm_resume(struct tb *tb) +{ + struct tb_test_noirq *ctx = kunit_get_current_test()->priv; + + ctx->resumes++; + return 0; +} + +static void tb_test_noirq_cm_event(struct tb *tb, enum tb_cfg_pkg_type type, + const void *buf, size_t size) +{ + struct tb_test_noirq *ctx = kunit_get_current_test()->priv; + + ctx->events++; + ctx->last_event = type; +} + +/* The suspend in progress: to idle (the platform is not involved) or deeper. */ +#ifdef CONFIG_SUSPEND +static unsigned int tb_test_noirq_pm_flags(void) +{ + return pm_suspend_global_flags; +} + +static void tb_test_noirq_set_pm_flags(unsigned int flags) +{ + pm_suspend_global_flags = flags; +} + +static void tb_test_noirq_to_idle(bool to_idle) +{ + pm_suspend_clear_flags(); + if (to_idle) + pm_set_suspend_no_platform(); +} +#else +static unsigned int tb_test_noirq_pm_flags(void) +{ + return 0; +} + +static void tb_test_noirq_set_pm_flags(unsigned int flags) +{ +} + +static void tb_test_noirq_to_idle(bool to_idle) +{ +} +#endif + +static void tb_test_noirq_restore(void *data) +{ + struct tb_test_noirq *ctx = data; + + tb_test_noirq_set_pm_flags(ctx->saved_flags); + put_device(&ctx->tb->dev); +} + +/* The domain in a suspend-to-idle, with a host directly on port 1. */ +static struct tb_test_noirq *tb_test_noirq_alloc(struct kunit *test) +{ + struct tb_test_noirq *ctx; + struct tb_xdomain *xd; + struct tb *tb; + + if (!IS_ENABLED(CONFIG_SUSPEND)) + kunit_skip(test, "needs CONFIG_SUSPEND"); + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + tb = kunit_kzalloc(test, sizeof(*tb), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, tb); + xd = kunit_kzalloc(test, sizeof(*xd), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, xd); + ctx->host = alloc_host(test); + KUNIT_ASSERT_NOT_NULL(test, ctx->host); + + ctx->ops.suspend_noirq = tb_test_noirq_cm_suspend; + ctx->ops.resume_noirq = tb_test_noirq_cm_resume; + ctx->ops.handle_event = tb_test_noirq_cm_event; + ctx->nhi.quirks = QUIRK_NO_SYSTEM_SLEEP; + ctx->tb = tb; + tb->nhi = &ctx->nhi; + tb->cm_ops = &ctx->ops; + tb->root_switch = ctx->host; + ctx->host->tb = tb; + ctx->host->ports[1].xdomain = xd; + device_initialize(&tb->dev); + tb->dev.release = tb_test_dprx_release; + mutex_init(&tb->lock); + + ctx->saved_flags = tb_test_noirq_pm_flags(); + tb_test_noirq_to_idle(true); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, tb_test_noirq_restore, ctx), 0); + + test->priv = ctx; + return ctx; +} + +static bool tb_test_noirq_retain(struct tb_test_noirq *ctx) +{ + bool retain; + + mutex_lock(&ctx->tb->lock); + retain = tb_domain_retain_noirq_control(ctx->tb); + mutex_unlock(&ctx->tb->lock); + return retain; +} + +/* Only requests that read state are answered while the domain is suspended. */ +static void tb_test_noirq_xdp_request_types(struct kunit *test) +{ + unsigned int type; + + for (type = 0; type < 64; type++) { + bool discovery = type == UUID_REQUEST_OLD || type == UUID_REQUEST || + type == PROPERTIES_REQUEST || + type == LINK_STATE_STATUS_REQUEST; + + KUNIT_EXPECT_EQ_MSG(test, tb_xdp_noirq_request(type), discovery, + "XDomain packet type %u", type); + } +} + +/* + * The control channel stays up only for a suspend-to-idle of an awake host + * router whose every connection is another host. + */ +static void tb_test_noirq_retain_decision(struct kunit *test) +{ + struct tb_test_noirq *ctx = tb_test_noirq_alloc(test); + struct tb_port *ports = ctx->host->ports; + struct tb_xdomain *xd = ports[1].xdomain; + struct tb_port *remote; + + remote = kunit_kzalloc(test, sizeof(*remote), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, remote); + + KUNIT_EXPECT_TRUE(test, tb_test_noirq_retain(ctx)); + ports[3].xdomain = xd; + KUNIT_EXPECT_TRUE(test, tb_test_noirq_retain(ctx)); + + /* A router behind any port needs the full suspend of its tunnels. */ + ports[3].xdomain = NULL; + ports[3].remote = remote; + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); + ports[1].xdomain = NULL; + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); + + /* Nothing connected: nobody to answer. */ + ports[3].remote = NULL; + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); + ports[1].xdomain = xd; + KUNIT_EXPECT_TRUE(test, tb_test_noirq_retain(ctx)); + + /* A host router that sleeps with the system loses its rings. */ + ctx->nhi.quirks = 0; + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); + ctx->nhi.quirks = QUIRK_NO_SYSTEM_SLEEP; + + /* The platform may power the router down in a deeper sleep. */ + tb_test_noirq_to_idle(false); + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); + tb_test_noirq_to_idle(true); + + ctx->tb->root_switch = NULL; + KUNIT_EXPECT_FALSE(test, tb_test_noirq_retain(ctx)); +} + +/* + * A retained control channel is neither stopped nor started (the domain has + * none here), and its events are dropped until the domain has resumed. + */ +static void tb_test_noirq_suspend_resume_retained(struct kunit *test) +{ + struct tb_test_noirq *ctx = tb_test_noirq_alloc(test); + static const enum tb_cfg_pkg_type dropped[] = { + TB_CFG_PKG_ERROR, TB_CFG_PKG_NOTIFY_ACK, TB_CFG_PKG_EVENT, + TB_CFG_PKG_ICM_EVENT, + }; + struct tb *tb = ctx->tb; + u32 buf[4] = {}; + int i; + + KUNIT_EXPECT_TRUE(test, tb_domain_event_cb(tb, TB_CFG_PKG_EVENT, buf, sizeof(buf))); + KUNIT_EXPECT_EQ(test, ctx->events, 1U); + KUNIT_EXPECT_EQ(test, ctx->last_event, TB_CFG_PKG_EVENT); + + /* The connection manager refused: nothing is retained. */ + ctx->suspend_ret = -EBUSY; + KUNIT_EXPECT_EQ(test, tb_domain_suspend_noirq(tb), -EBUSY); + KUNIT_EXPECT_FALSE(test, tb->noirq_ctl_retained); + ctx->suspend_ret = 0; + + KUNIT_ASSERT_EQ(test, tb_domain_suspend_noirq(tb), 0); + KUNIT_EXPECT_EQ(test, ctx->suspends, 2U); + KUNIT_ASSERT_TRUE(test, tb->noirq_ctl_retained); + + for (i = 0; i < ARRAY_SIZE(dropped); i++) + KUNIT_EXPECT_TRUE(test, tb_domain_event_cb(tb, dropped[i], buf, sizeof(buf))); + KUNIT_EXPECT_EQ(test, ctx->events, 1U); + + KUNIT_ASSERT_EQ(test, tb_domain_resume_noirq(tb), 0); + KUNIT_EXPECT_EQ(test, ctx->resumes, 1U); + KUNIT_EXPECT_FALSE(test, tb->noirq_ctl_retained); + + KUNIT_EXPECT_TRUE(test, tb_domain_event_cb(tb, TB_CFG_PKG_ERROR, buf, sizeof(buf))); + KUNIT_EXPECT_EQ(test, ctx->events, 2U); + KUNIT_EXPECT_EQ(test, ctx->last_event, TB_CFG_PKG_ERROR); +} + +/* The XDomain discovery protocol and a service protocol of the test's own. */ +static const uuid_t tb_test_xdp_uuid = + UUID_INIT(0xb638d70e, 0x42ff, 0x40bb, + 0x97, 0xc2, 0x90, 0xe2, 0xc0, 0xb2, 0xff, 0x07); +static const uuid_t tb_test_service_uuid = + UUID_INIT(0x4b756e69, 0x7420, 0x7465, + 0x73, 0x74, 0x20, 0x6e, 0x6f, 0x69, 0x72, 0x71); + +static int tb_test_noirq_service(const void *buf, size_t size, void *data) +{ + struct tb_test_noirq *ctx = data; + + ctx->handled++; + return 1; +} + +static void tb_test_noirq_unregister(void *data) +{ + tb_unregister_protocol_handler(data); +} + +static bool tb_test_noirq_packet(struct tb_test_noirq *ctx, const uuid_t *uuid, + enum tb_cfg_pkg_type pkg, u32 type) +{ + struct tb_xdp_header hdr = { + .xd_hdr.length_sn = (sizeof(hdr) - sizeof(hdr.xd_hdr)) / 4, + .uuid = *uuid, + .type = type, + }; + + return tb_xdomain_handle_request(ctx->tb, pkg, &hdr, sizeof(hdr)); +} + +/* + * While the control channel is retained nothing but a discovery request + * gets past the XDomain dispatch: no service protocol, no response, no + * discovery request that changes state. + */ +static void tb_test_noirq_xdomain_filter(struct kunit *test) +{ + static const u32 refused[] = { + 0, UUID_RESPONSE, PROPERTIES_RESPONSE, PROPERTIES_CHANGED_REQUEST, + PROPERTIES_CHANGED_RESPONSE, ERROR_RESPONSE, + LINK_STATE_STATUS_RESPONSE, LINK_STATE_CHANGE_REQUEST, + LINK_STATE_CHANGE_RESPONSE, 31, + }; + struct tb_test_noirq *ctx = tb_test_noirq_alloc(test); + struct tb_protocol_handler *handler; + struct tb *tb = ctx->tb; + int i; + + handler = kunit_kzalloc(test, sizeof(*handler), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, handler); + handler->uuid = &tb_test_service_uuid; + handler->callback = tb_test_noirq_service; + handler->data = ctx; + KUNIT_ASSERT_EQ(test, tb_register_protocol_handler(handler), 0); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, tb_test_noirq_unregister, + handler), 0); + + /* Awake: the service sees its packets, a discovery response is not consumed. */ + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_service_uuid, + TB_CFG_PKG_XDOMAIN_REQ, UUID_REQUEST)); + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_service_uuid, + TB_CFG_PKG_XDOMAIN_RESP, UUID_RESPONSE)); + KUNIT_EXPECT_EQ(test, ctx->handled, 2U); + KUNIT_EXPECT_FALSE(test, tb_test_noirq_packet(ctx, &tb_test_xdp_uuid, + TB_CFG_PKG_XDOMAIN_RESP, UUID_RESPONSE)); + + WRITE_ONCE(tb->noirq_ctl_retained, true); + + /* A service packet is dropped, whatever sits where discovery has its type. */ + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_service_uuid, + TB_CFG_PKG_XDOMAIN_REQ, UUID_REQUEST)); + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_service_uuid, + TB_CFG_PKG_XDOMAIN_RESP, UUID_RESPONSE)); + KUNIT_EXPECT_EQ(test, ctx->handled, 2U); + + /* A discovery response is consumed: no request of ours waits for one. */ + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_xdp_uuid, + TB_CFG_PKG_XDOMAIN_RESP, UUID_RESPONSE)); + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_xdp_uuid, + TB_CFG_PKG_XDOMAIN_RESP, UUID_REQUEST)); + + /* Queueing a request takes a reference to the domain: none is queued. */ + for (i = 0; i < ARRAY_SIZE(refused); i++) { + KUNIT_EXPECT_TRUE(test, tb_test_noirq_packet(ctx, &tb_test_xdp_uuid, + TB_CFG_PKG_XDOMAIN_REQ, + refused[i])); + KUNIT_EXPECT_EQ_MSG(test, kref_read(&tb->dev.kobj.kref), 1U, + "XDomain request %u was queued", refused[i]); + } +} + static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_apple_right_dpin_wiring), KUNIT_CASE(tb_test_dp_reduce_bandwidth_shared), @@ -4339,6 +4670,10 @@ static struct kunit_case tb_test_cases[] = { KUNIT_CASE(tb_test_property_parse_u32_wrap), KUNIT_CASE(tb_test_property_parse_recursion), KUNIT_CASE(tb_test_property_parse_dir_len_underflow), + KUNIT_CASE(tb_test_noirq_xdp_request_types), + KUNIT_CASE(tb_test_noirq_retain_decision), + KUNIT_CASE(tb_test_noirq_suspend_resume_retained), + KUNIT_CASE(tb_test_noirq_xdomain_filter), KUNIT_CASE(tb_test_pci_host_teardown), KUNIT_CASE(tb_test_pci_host_daisy_chain), KUNIT_CASE(tb_test_tunnel_dp_host_credits), diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index d174e7129191c..7858eaafb9967 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "tb.h" @@ -763,7 +764,7 @@ static void stop_handshake(struct tb_xdomain *xd) * The XDomain discovery requests answered while the control channel is kept * through s2idle (tb->noirq_ctl_retained): they only read state. */ -static bool tb_xdp_noirq_request(enum tb_xdp_type type) +VISIBLE_IF_KUNIT bool tb_xdp_noirq_request(enum tb_xdp_type type) { switch (type) { case UUID_REQUEST_OLD: From 005b8bbfd59868ec13d3804f92187553c05535c0 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 10:11:30 +1100 Subject: [PATCH 2/2] pmdomain: apple: pmp-report: test: cover the fast die-temperature effort apple_pmp_report_fast_die_effort() and the T8142 report layout have no test. A wrong lane shift, a missed "PMP is running" check or a 32-bit read of the T8142's 16-byte entry would hand a CPU cluster the wrong thermal effort, or none. Add the apple-pmp-fast-die suite, on a report in memory published the way probe publishes it: - each lane is one byte of the low word, read 32 or 64 bits wide, and a lane past the report's count is -EINVAL with nothing stored; - a stopped PMP gives -EAGAIN, and status bits other than "ready" count as running only where the SoC's table says so; - the report is found by its node (-EPROBE_DEFER when none is bound) and one without the entry gives -EOPNOTSUPP; - the T8142 table matches its PMP's entry numbers: status 1, fast die 15 (two lanes, wide), device requests 39, acknowledgments 47, DVFS 55..70. Signed-off-by: Ryan Murray --- drivers/pmdomain/apple/Kconfig | 4 +- .../pmdomain/apple/pmp-report-start-test.c | 197 +++++++++++++++++- 2 files changed, 199 insertions(+), 2 deletions(-) diff --git a/drivers/pmdomain/apple/Kconfig b/drivers/pmdomain/apple/Kconfig index 252cceba3c561..0d67b542b97fa 100644 --- a/drivers/pmdomain/apple/Kconfig +++ b/drivers/pmdomain/apple/Kconfig @@ -129,7 +129,9 @@ config APPLE_PMP_REPORT_START_KUNIT_TEST failed checks, a PMP driver that does not bind, a PMP that does not report ready or acknowledge its startup requests, consumers that time out, and the teardown of the report and its entries while a - start or a consumer waits. The report region is simulated in + start or a consumer waits. Also reads the fast die-temperature + effort of a report: its lanes, reports that have none or do not + run, and the T8142 layout. The report region is simulated in memory. Only useful for kernel developers. endif diff --git a/drivers/pmdomain/apple/pmp-report-start-test.c b/drivers/pmdomain/apple/pmp-report-start-test.c index 36cf2c87eb128..b6a8daa78ec74 100644 --- a/drivers/pmdomain/apple/pmp-report-start-test.c +++ b/drivers/pmdomain/apple/pmp-report-start-test.c @@ -6,6 +6,9 @@ * bind, no ready status, no acknowledgment), a consumer's own timeout and * the teardown of the provider can be driven without Apple hardware. * + * The fast die-temperature effort is read from a report in memory, too: its + * lanes, the reports that have none or do not run, and the T8142 layout. + * * Included by pmp-report.c. */ @@ -407,4 +410,196 @@ static struct kunit_suite pmp_start_suite = { .test_cases = pmp_start_cases, }; -kunit_test_suite(pmp_start_suite); +/* + * A report in memory with the PMP's status in its first word and the fast + * die-temperature efforts in an entry of its own: 16 bytes, of which the + * low 32 bits hold the lanes. + */ +#define PFD_STATUS 0x08 +#define PFD_EFFORT 0x10 +#define PFD_LANES 0xa4c30f5aU +#define PFD_OTHER 0x1122334400000000ULL + +struct pfd { + struct apple_pmp_report rep; + struct apple_pmp_report_offsets offsets; + struct device dev; + struct device_node node; + u64 regs[4]; +}; + +static void pfd_remove(void *data) +{ + struct pfd *t = data; + + mutex_lock(&apple_pmp_reports_lock); + list_del_rcu(&t->rep.list); + mutex_unlock(&apple_pmp_reports_lock); + synchronize_rcu(); +} + +/* A running report with @lanes fast-die lanes, published as probe does. */ +static struct pfd *pfd_new(struct kunit *test, u32 lanes) +{ + struct pfd *t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, t); + t->offsets.status = PFD_STATUS; + t->offsets.fast_die = lanes ? PFD_EFFORT : 0; + t->offsets.fast_die_lanes = lanes; + t->dev.of_node = &t->node; + t->rep.dev = &t->dev; + t->rep.offsets = &t->offsets; + t->rep.base = (__force void __iomem *)t->regs; + spin_lock_init(&t->rep.lock); + + t->regs[PFD_STATUS / sizeof(u64)] = PMP_REPORT_READY; + t->regs[PFD_EFFORT / sizeof(u64)] = PFD_OTHER | PFD_LANES; + t->regs[PFD_EFFORT / sizeof(u64) + 1] = ~0ULL; + + mutex_lock(&apple_pmp_reports_lock); + list_add_tail_rcu(&t->rep.list, &apple_pmp_reports); + mutex_unlock(&apple_pmp_reports_lock); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, pfd_remove, t), 0); + return t; +} + +static int pfd_effort(struct pfd *t, unsigned int lane, u8 *effort) +{ + *effort = 0xee; + return apple_pmp_report_fast_die_effort(&t->node, lane, effort); +} + +/* Each lane is one byte of the low word, read 32 or 64 bits wide. */ +static void pmp_fast_die_lanes_test(struct kunit *test) +{ + struct pfd *t = pfd_new(test, 3); + u8 effort; + int wide; + + for (wide = 0; wide < 2; wide++) { + t->offsets.fast_die_wide = wide; + t->offsets.fast_die_lanes = 3; + KUNIT_EXPECT_EQ(test, pfd_effort(t, 0, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x5a); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 1, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x0f); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 2, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0xc3); + + /* Past the report's lanes: refused, nothing is stored. */ + KUNIT_EXPECT_EQ(test, pfd_effort(t, 3, &effort), -EINVAL); + KUNIT_EXPECT_EQ(test, effort, 0xee); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 4, &effort), -EINVAL); + KUNIT_EXPECT_EQ(test, pfd_effort(t, UINT_MAX, &effort), -EINVAL); + + t->offsets.fast_die_lanes = 4; + KUNIT_EXPECT_EQ(test, pfd_effort(t, 3, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0xa4); + } +} + +/* A stopped PMP reads 0, which would ask for no limit: no effort then. */ +static void pmp_fast_die_running_test(struct kunit *test) +{ + struct pfd *t = pfd_new(test, 2); + u64 *status = &t->regs[PFD_STATUS / sizeof(u64)]; + u8 effort; + + WRITE_ONCE(*status, 0); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 0, &effort), -EAGAIN); + KUNIT_EXPECT_EQ(test, effort, 0xee); + t->offsets.running_any = true; + KUNIT_EXPECT_EQ(test, pfd_effort(t, 0, &effort), -EAGAIN); + KUNIT_EXPECT_EQ(test, effort, 0xee); + + /* Status bits other than "ready" only count where the SoC says so. */ + WRITE_ONCE(*status, BIT_ULL(36)); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 1, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x0f); + t->offsets.running_any = false; + KUNIT_EXPECT_EQ(test, pfd_effort(t, 1, &effort), -EAGAIN); + + WRITE_ONCE(*status, BIT_ULL(36) | PMP_REPORT_READY); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 1, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x0f); + + /* A lane the report does not have is the caller's error, running or not. */ + WRITE_ONCE(*status, 0); + KUNIT_EXPECT_EQ(test, pfd_effort(t, 2, &effort), -EINVAL); +} + +/* The report is found by its node; one without the entry says so. */ +static void pmp_fast_die_lookup_test(struct kunit *test) +{ + struct pfd *none = pfd_new(test, 0); + struct pfd *a = pfd_new(test, 2); + struct pfd *b = pfd_new(test, 2); + struct device_node *unbound; + u8 effort = 0xee; + + unbound = kunit_kzalloc(test, sizeof(*unbound), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, unbound); + KUNIT_EXPECT_EQ(test, apple_pmp_report_fast_die_effort(unbound, 0, &effort), + -EPROBE_DEFER); + KUNIT_EXPECT_EQ(test, effort, 0xee); + + b->regs[PFD_EFFORT / sizeof(u64)] = 0x7781; + KUNIT_EXPECT_EQ(test, pfd_effort(a, 0, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x5a); + KUNIT_EXPECT_EQ(test, pfd_effort(b, 0, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x81); + KUNIT_EXPECT_EQ(test, pfd_effort(b, 1, &effort), 0); + KUNIT_EXPECT_EQ(test, effort, 0x77); + + /* No entry: unsupported whatever the lane and the PMP's state. */ + KUNIT_EXPECT_EQ(test, pfd_effort(none, 0, &effort), -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, effort, 0xee); + none->regs[PFD_STATUS / sizeof(u64)] = 0; + KUNIT_EXPECT_EQ(test, pfd_effort(none, 0, &effort), -EOPNOTSUPP); +} + +/* + * The T8142 report as its PMP's table of PTD entries gives it: PMP-STATUS 1, + * FAST-DIE-CE 15 (E, P), SOC-DEV-PS-REQ 39, SOC-DEV-PS-ACK 47 and + * SOC-DEV-DVFS 55 to 70. An entry is read in 16 bytes from the base and + * written in 8 bytes from the update aperture. + */ +static void pmp_fast_die_t8142_test(struct kunit *test) +{ + const struct apple_pmp_report_offsets *offs = NULL; + const struct of_device_id *id; + + for (id = apple_pmp_report_of_match; id->compatible[0]; id++) + if (!strcmp(id->compatible, "apple,t8142-pmp-v2-report")) + offs = id->data; + KUNIT_ASSERT_NOT_NULL(test, offs); + + KUNIT_EXPECT_EQ(test, offs->status, 1 * 16); + KUNIT_EXPECT_EQ(test, offs->fast_die, 15 * 16); + KUNIT_EXPECT_EQ(test, offs->fast_die_lanes, 2); + KUNIT_EXPECT_TRUE(test, offs->fast_die_wide); + KUNIT_EXPECT_EQ(test, offs->tgt_read, 39 * 16); + KUNIT_EXPECT_EQ(test, offs->tgt_write, offs->update + 39 * 8); + KUNIT_EXPECT_EQ(test, offs->actual, 47 * 16); + KUNIT_EXPECT_EQ(test, offs->dvfs_first, 55); + KUNIT_EXPECT_EQ(test, offs->dvfs_first + offs->dvfs_count - 1, 70); + KUNIT_EXPECT_TRUE(test, offs->running_any); + KUNIT_EXPECT_TRUE(test, offs->defer_requests); + KUNIT_EXPECT_FALSE(test, offs->starts_pmp); +} + +static struct kunit_case pmp_fast_die_cases[] = { + KUNIT_CASE(pmp_fast_die_lanes_test), + KUNIT_CASE(pmp_fast_die_running_test), + KUNIT_CASE(pmp_fast_die_lookup_test), + KUNIT_CASE(pmp_fast_die_t8142_test), + {} +}; + +static struct kunit_suite pmp_fast_die_suite = { + .name = "apple-pmp-fast-die", + .test_cases = pmp_fast_die_cases, +}; + +kunit_test_suites(&pmp_start_suite, &pmp_fast_die_suite);