diff --git a/drivers/thermal/apple-pmp-hold.h b/drivers/thermal/apple-pmp-hold.h index ecb837f060b40..379880d7b41f5 100644 --- a/drivers/thermal/apple-pmp-hold.h +++ b/drivers/thermal/apple-pmp-hold.h @@ -240,6 +240,17 @@ static inline bool apple_pmp_fault_disable(const void *failed_reader, error && error != -EAGAIN; } +/* + * A light load runs its cluster for only part of each slow period, and a load + * with a matching period can keep every slow sample in the gated part until the + * hold expires. Look for a real reading at the fast cadence while the sensors + * are dark (@ret is the raw sample's) and the CPUs have work. + */ +static inline bool apple_pmp_dark_fast(int ret, bool idle) +{ + return ret == -ENODATA && !idle; +} + enum apple_pmp_zone_action { APPLE_PMP_ZONE_NONE, APPLE_PMP_ZONE_UPDATE, diff --git a/drivers/thermal/apple-pmp-policy-test.c b/drivers/thermal/apple-pmp-policy-test.c index 7c020f96cf62e..96bbbdabd886c 100644 --- a/drivers/thermal/apple-pmp-policy-test.c +++ b/drivers/thermal/apple-pmp-policy-test.c @@ -367,6 +367,55 @@ static void apple_pmp_test_gated_hold(struct kunit *test) KUNIT_EXPECT_EQ(test, hold.real_valid_ns, 0ULL); } +/* + * A load that powers its cluster for 30 ms of every 100 ms, with the slow samples + * in the gated part. Slow sampling alone loses the sensors to the 2 s hold; fast + * sampling while dark and busy reads the cluster before that. + */ +static void apple_pmp_test_dark_fast(struct kunit *test) +{ + const u64 ms = 1000000ULL; + unsigned int pass; + + KUNIT_EXPECT_TRUE(test, apple_pmp_dark_fast(-ENODATA, false)); + KUNIT_EXPECT_FALSE(test, apple_pmp_dark_fast(-ENODATA, true)); + KUNIT_EXPECT_FALSE(test, apple_pmp_dark_fast(0, false)); + KUNIT_EXPECT_FALSE(test, apple_pmp_dark_fast(-EIO, false)); + + for (pass = 0; pass < 2; pass++) { + struct apple_pmp_gated_hold hold; + unsigned int faults = 0, reads = 0; + u64 t = 1000 * ms, worst = 0; + long mc = 70000; + + apple_pmp_gated_hold_reset(&hold); + KUNIT_EXPECT_EQ(test, apple_pmp_gated_hold_sample(&hold, t, &mc, 0, false), 0); + t += 50 * ms; + while (t < 11000 * ms) { + u64 phase; + int raw; + + div64_u64_rem(t, 100 * ms, &phase); + raw = phase < 30 * ms ? 0 : -ENODATA; + mc = 70000; + reads += !raw; + if (apple_pmp_gated_hold_sample(&hold, t, &mc, raw, false)) + faults++; + else + worst = max(worst, t - hold.real_valid_ns); + t += pass && apple_pmp_dark_fast(raw, false) ? 5 * ms : 100 * ms; + } + if (pass) { + KUNIT_EXPECT_EQ(test, faults, 0U); + KUNIT_EXPECT_GT(test, reads, 0U); + KUNIT_EXPECT_LE(test, worst, 150 * ms); + } else { + KUNIT_EXPECT_GT(test, faults, 0U); + KUNIT_EXPECT_EQ(test, reads, 0U); + } + } +} + static void apple_pmp_test_cpu_idle_tracker(struct kunit *test) { struct apple_pmp_idle_tracker tracker; @@ -676,6 +725,7 @@ static struct kunit_case apple_pmp_policy_cases[] = { KUNIT_CASE(apple_pmp_test_shared_power_arbitration), KUNIT_CASE(apple_pmp_test_gpu_attachment), KUNIT_CASE(apple_pmp_test_gated_hold), + KUNIT_CASE(apple_pmp_test_dark_fast), KUNIT_CASE(apple_pmp_test_cpu_idle_tracker), KUNIT_CASE(apple_pmp_test_zone_notify), KUNIT_CASE(apple_pmp_test_fault_disable_origin), diff --git a/drivers/thermal/apple-pmp-thermal.c b/drivers/thermal/apple-pmp-thermal.c index aec2d3b2c2fc5..3f06a88b9c17e 100644 --- a/drivers/thermal/apple-pmp-thermal.c +++ b/drivers/thermal/apple-pmp-thermal.c @@ -463,7 +463,7 @@ static int apple_pmp_sample(bool cpus_idle) struct apple_pmp_snapshot snapshot; struct apple_pmp_cooling *cooling = &pmp_thermal.cooling; long value = 0; - bool was_confirmed; + bool was_confirmed, dark_fast; unsigned int old_level, level, i; int old_reason, channel = -1, ret, guard_ret; const char *reason; @@ -472,6 +472,7 @@ static int apple_pmp_sample(bool cpus_idle) old_level = pmp_thermal.guard_level; old_reason = cooling->reason; ret = apple_pmp_sample_all(&snapshot, &value); + dark_fast = apple_pmp_dark_fast(ret, cpus_idle); ret = apple_pmp_gated_hold_sample(&pmp_thermal.hold, snapshot.finished_ns, &value, ret, cpus_idle); @@ -492,7 +493,8 @@ static int apple_pmp_sample(bool cpus_idle) DIV_ROUND_UP(state * 8, cdev->max_state)); } level = apple_pmp_cooling_level(cooling); - WRITE_ONCE(pmp_thermal.fast_poll, apple_pmp_cooling_fast(cooling, value, ret)); + WRITE_ONCE(pmp_thermal.fast_poll, + apple_pmp_cooling_fast(cooling, value, ret) || dark_fast); /* Never release startup/fault protection with disabled or unbound cooling. */ if (!pmp_thermal.enabled) level = 8;