From 309a55f9c7d147d26852732ffb43b5273c341d9c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 02:15:43 +1100 Subject: [PATCH 1/2] soc: apple: clpc: test the frequency step, ceiling order, vote hold and floor word The low-power ceiling and the rail floors are built from four small helpers that had no test: apple_clpc_step_khz() (the next cpufreq table entry up or down, and the low-power top round-up), apple_clpc_steps_first() (which cluster's ceiling moves next), apple_clpc_vote() (the demand hold) and apple_clpc_floor() (the PMC floor word). Add a KUnit suite, apple-clpc-policy, next to apple-clpc-gpu: - step_khz on an unordered table with an invalid entry: one step either way, off-table values, both ends, and the first entry at or above low_power_khz (exact and in between, and none above the table). - steps_first: down takes the higher ceiling (the faster cluster on a tie), up the lower (the slower), and identical clusters keep the pick. - vote: a demand replaces the state at once, down as well as up; with no demand it holds for APPLE_CLPC_HOLD_MS and then drops, across the jiffies wrap. - floor: each rail's state in the nibble its table rail number names, masked so a wide state cannot spill into another rail. No functional change. A mutant that flips the steps_first tie-break, uses time_after() for the hold, drops the nibble mask and lets a down step return @khz itself fails all four new tests. Signed-off-by: Ryan Murray --- drivers/soc/apple/clpc.c | 137 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 4bd3d6c7c0a95..27c3a15da4fa9 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -2085,6 +2085,143 @@ static struct kunit_suite apple_clpc_suite = { .test_cases = apple_clpc_cases, }; kunit_test_suite(apple_clpc_suite); + +/* A cpufreq table out of order, with an invalid entry, as a driver may give it. */ +static struct cpufreq_policy *apple_clpc_test_policy(struct kunit *test) +{ + static const unsigned int khz[] = { + 600000, CPUFREQ_ENTRY_INVALID, 1200000, 2000000, 1500000, + }; + struct cpufreq_frequency_table *table; + struct cpufreq_policy *policy; + unsigned int i; + + policy = kunit_kzalloc(test, sizeof(*policy), GFP_KERNEL); + table = kunit_kcalloc(test, ARRAY_SIZE(khz) + 1, sizeof(*table), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, policy); + KUNIT_ASSERT_NOT_NULL(test, table); + for (i = 0; i < ARRAY_SIZE(khz); i++) + table[i].frequency = khz[i]; + table[i].frequency = CPUFREQ_TABLE_END; + policy->freq_table = table; + policy->cpuinfo.max_freq = 2000000; + return policy; +} + +static void apple_clpc_step_khz_test(struct kunit *test) +{ + struct cpufreq_policy *policy = apple_clpc_test_policy(test); + + /* One table step either way, whatever the table order. */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1200000, true), 1500000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000, true), 2000000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000, false), 1200000u); + /* The invalid entry is not a step. */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1200000, false), 600000u); + /* Off the table: the nearest entry in that direction. */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1300000, true), 1500000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1300000, false), 1200000u); + /* No step past either end: @khz back. */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 2000000, true), 2000000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 600000, false), 600000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 2100000, true), 2100000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 0, false), 0u); + + /* + * The low-power top, as apple_clpc_profile_limit() takes it: the first + * entry at or above low_power_khz, so a value between two entries + * rounds up and an exact entry is kept. + */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1400000 - 1, true), 1500000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000 - 1, true), 1500000u); + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 600000 - 1, true), 600000u); + /* Above the table nothing is found; the caller falls back to max_freq. */ + KUNIT_EXPECT_LT(test, apple_clpc_step_khz(policy, 2100000 - 1, true), 2100000u); +} + +static void apple_clpc_steps_first_test(struct kunit *test) +{ + /* Down: the higher ceiling first; on a tie, the faster cluster. */ + KUNIT_EXPECT_TRUE(test, apple_clpc_steps_first(2000000, 2000000, 1500000, 3000000, false)); + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(1500000, 3000000, 2000000, 2000000, false)); + KUNIT_EXPECT_TRUE(test, apple_clpc_steps_first(1500000, 3000000, 1500000, 2000000, false)); + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(1500000, 2000000, 1500000, 3000000, false)); + + /* Up: the lower ceiling first; on a tie, the slower cluster. */ + KUNIT_EXPECT_TRUE(test, apple_clpc_steps_first(1500000, 3000000, 2000000, 2000000, true)); + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(2000000, 2000000, 1500000, 3000000, true)); + KUNIT_EXPECT_TRUE(test, apple_clpc_steps_first(1500000, 2000000, 1500000, 3000000, true)); + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(1500000, 3000000, 1500000, 2000000, true)); + + /* Identical clusters: the one already picked keeps the step. */ + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(1500000, 2000000, 1500000, 2000000, true)); + KUNIT_EXPECT_FALSE(test, apple_clpc_steps_first(1500000, 2000000, 1500000, 2000000, false)); +} + +static void apple_clpc_vote_test(struct kunit *test) +{ + unsigned long hold = msecs_to_jiffies(APPLE_CLPC_HOLD_MS); + /* Start close to the jiffies wrap, so the hold spans it. */ + unsigned long t0 = ULONG_MAX - hold / 2; + struct apple_clpc_vote vote = {}; + + KUNIT_ASSERT_GT(test, hold, 1ul); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0), 0u); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 5, t0), 5u); + /* No demand: the state holds for APPLE_CLPC_HOLD_MS, then drops. */ + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0 + hold - 1), 5u); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0 + hold), 0u); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0 + 2 * hold), 0u); + + /* A new demand replaces the state at once, down as well as up. */ + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 7, t0 + 2 * hold), 7u); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 2, t0 + 2 * hold + 1), 2u); + /* and restarts the hold. */ + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0 + 3 * hold), 2u); + KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0 + 3 * hold + 1), 0u); +} + +static void apple_clpc_floor_test(struct kunit *test) +{ + struct apple_clpc *clpc = kunit_kzalloc(test, sizeof(*clpc), GFP_KERNEL); + struct apple_clpc_hw *hw = kunit_kzalloc(test, sizeof(*hw), GFP_KERNEL); + unsigned int state[APPLE_CLPC_RAILS] = {}; + + KUNIT_ASSERT_NOT_NULL(test, clpc); + KUNIT_ASSERT_NOT_NULL(test, hw); + clpc->hw = hw; + + /* Each rail's state in its own nibble of the PMC floor word. */ + hw->rail[APPLE_CLPC_DCS] = 0; + hw->rail[APPLE_CLPC_SOC] = 1; + KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0u); + state[APPLE_CLPC_DCS] = 5; + state[APPLE_CLPC_SOC] = 3; + KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0x35u); + + /* The rail numbers come from the table, not the rail order. */ + hw->rail[APPLE_CLPC_DCS] = 2; + hw->rail[APPLE_CLPC_SOC] = 0; + KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0x503u); + + /* A state wider than a nibble cannot spill into another rail. */ + state[APPLE_CLPC_DCS] = 0x1f; + state[APPLE_CLPC_SOC] = 0x10; + KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0xf00u); +} + +static struct kunit_case apple_clpc_policy_cases[] = { + KUNIT_CASE(apple_clpc_step_khz_test), + KUNIT_CASE(apple_clpc_steps_first_test), + KUNIT_CASE(apple_clpc_vote_test), + KUNIT_CASE(apple_clpc_floor_test), + {} +}; +static struct kunit_suite apple_clpc_policy_suite = { + .name = "apple-clpc-policy", + .test_cases = apple_clpc_policy_cases, +}; +kunit_test_suite(apple_clpc_policy_suite); #endif /* T8152: DCS states 0-7 and fabric states 0-4. */ From fb37343c17f8466efbd278dd256d25b6015b3fa6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 03:49:00 +1100 Subject: [PATCH 2/2] soc: apple: clpc: make the vote test catch a wrap-unsafe hold t0 = ULONG_MAX - hold / 2 puts every held sample past the wrap together with last_demand + hold, so a plain 'now >= last_demand + hold' passes the test. Start at ULONG_MAX - (hold - 1) instead: the last held sample is ULONG_MAX while the deadline has wrapped to 0. Also move the invalid-entry comment to the up steps that can pick it, say the max_freq fallback of apple_clpc_profile_limit() is not covered, and expect the exact value above the table instead of a bound. Signed-off-by: Ryan Murray --- drivers/soc/apple/clpc.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/soc/apple/clpc.c b/drivers/soc/apple/clpc.c index 27c3a15da4fa9..4b3e960a81e18 100644 --- a/drivers/soc/apple/clpc.c +++ b/drivers/soc/apple/clpc.c @@ -2116,27 +2116,30 @@ static void apple_clpc_step_khz_test(struct kunit *test) KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1200000, true), 1500000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000, true), 2000000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000, false), 1200000u); - /* The invalid entry is not a step. */ KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1200000, false), 600000u); /* Off the table: the nearest entry in that direction. */ KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1300000, true), 1500000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1300000, false), 1200000u); - /* No step past either end: @khz back. */ + /* + * No step past either end: @khz back. The invalid entry sits above + * every real one, so these up steps are the ones that would pick it. + */ KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 2000000, true), 2000000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 600000, false), 600000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 2100000, true), 2100000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 0, false), 0u); /* - * The low-power top, as apple_clpc_profile_limit() takes it: the first - * entry at or above low_power_khz, so a value between two entries - * rounds up and an exact entry is kept. + * The lookup apple_clpc_profile_limit() makes for the low-power top: the + * first entry at or above low_power_khz, so a value between two entries + * rounds up and an exact entry is kept. Its max_freq fallback is not + * covered here. */ KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1400000 - 1, true), 1500000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 1500000 - 1, true), 1500000u); KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 600000 - 1, true), 600000u); - /* Above the table nothing is found; the caller falls back to max_freq. */ - KUNIT_EXPECT_LT(test, apple_clpc_step_khz(policy, 2100000 - 1, true), 2100000u); + /* Above the table nothing is found and @khz comes back. */ + KUNIT_EXPECT_EQ(test, apple_clpc_step_khz(policy, 2100000 - 1, true), 2100000u - 1); } static void apple_clpc_steps_first_test(struct kunit *test) @@ -2161,11 +2164,16 @@ static void apple_clpc_steps_first_test(struct kunit *test) static void apple_clpc_vote_test(struct kunit *test) { unsigned long hold = msecs_to_jiffies(APPLE_CLPC_HOLD_MS); - /* Start close to the jiffies wrap, so the hold spans it. */ - unsigned long t0 = ULONG_MAX - hold / 2; + /* + * Start so that the hold deadline wraps to 0 while its last held + * sample is still ULONG_MAX: a compare that is not wrap-safe drops + * the vote early there. + */ + unsigned long t0 = ULONG_MAX - (hold - 1); struct apple_clpc_vote vote = {}; KUNIT_ASSERT_GT(test, hold, 1ul); + KUNIT_ASSERT_EQ(test, t0 + hold - 1, ULONG_MAX); KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 0, t0), 0u); KUNIT_EXPECT_EQ(test, apple_clpc_vote(&vote, 5, t0), 5u); /* No demand: the state holds for APPLE_CLPC_HOLD_MS, then drops. */