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
91 changes: 91 additions & 0 deletions drivers/thermal/apple-pmp-policy-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,95 @@ static void apple_pmp_test_gpu_attachment(struct kunit *test)
KUNIT_EXPECT_EQ(test, state.level[2], 0U);
}

static void apple_pmp_test_unbudgeted(struct kunit *test)
{
struct apple_pmp_power_policy state;
u32 rails[3] = {20000, 4000, 12000};
unsigned long long now = 0;
unsigned int i;

apple_pmp_power_init_unbudgeted(&state);
apple_pmp_power_set_active(&state, 7);
/* A cool start grants no burst credit; restricted until confirmed. */
for (i = 1; i <= 3; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 50000, 0);
KUNIT_EXPECT_FALSE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.credit_mj, 0LL);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 23U);
/* Then one step per second, however hot and loaded. */
for (i = 0; i < 20; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 105000, 0);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 13U);
/* Sustained heavy load on a hot die never restricts again. */
for (i = 0; i < 1200; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 105000, 0);
KUNIT_EXPECT_EQ(test, state.total_mw, 36000);
KUNIT_EXPECT_EQ(test, state.level[0], 0U);
KUNIT_EXPECT_EQ(test, state.level[1], 0U);
KUNIT_EXPECT_EQ(test, state.level[2], 0U);
KUNIT_EXPECT_EQ(test, state.credit_mj, 0LL);
/* A rail fault still selects full protection. */
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 105000, -EIO);
KUNIT_EXPECT_TRUE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[0], 8U);
KUNIT_EXPECT_EQ(test, state.level[2], 8U);
/* Recovery: three good samples, then one step per second. */
for (i = 0; i < 22; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 105000, 0);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 14U);
for (i = 0; i < 38; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, rails, 105000, 0);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 0U);
/* A telemetry gap also selects full protection. */
apple_pmp_power_sample(&state, now += 2500000000ULL, rails, 105000, 0);
KUNIT_EXPECT_TRUE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[1], 8U);
}

static void apple_pmp_test_unbudgeted_dark(struct kunit *test)
{
struct apple_pmp_power_policy state, budgeted;
u32 rails[3] = {400, 200, 100};
u32 load[3] = {15000, 3000, 0};
unsigned long long now = 0;
unsigned int i;

apple_pmp_power_init_unbudgeted(&state);
apple_pmp_power_set_active(&state, 7);
for (i = 0; i < 60; i++)
apple_pmp_power_sample(&state, now += 500000000ULL, load, 60000, 0);
KUNIT_EXPECT_FALSE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 0U);
/* A 10 s dark-sensor span keeps rail telemetry fresh without clamping. */
for (i = 0; i < 20; i++)
apple_pmp_power_dark(&state, now += 500000000ULL, rails);
KUNIT_EXPECT_FALSE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.last_ns, now);
KUNIT_EXPECT_EQ(test, state.total_mw, 700);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 0U);
apple_pmp_power_sample(&state, now += 500000000ULL, load, 65000, 0);
KUNIT_EXPECT_FALSE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 0U);
/* A genuine >2 s gap while dark still fails closed. */
apple_pmp_power_dark(&state, now += 2500000000ULL, rails);
KUNIT_EXPECT_TRUE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[0], 8U);
KUNIT_EXPECT_EQ(test, state.level[1], 8U);
KUNIT_EXPECT_EQ(test, state.level[2], 8U);
/* Dark samples never clear a latched fault or release levels. */
for (i = 0; i < 10; i++)
apple_pmp_power_dark(&state, now += 500000000ULL, rails);
KUNIT_EXPECT_TRUE(test, state.fault);
KUNIT_EXPECT_EQ(test, state.level[0] + state.level[1] + state.level[2], 24U);
/* On a budgeted (Air) policy, apple_pmp_power_dark() is a no-op. */
apple_pmp_power_init(&budgeted);
apple_pmp_power_set_active(&budgeted, 3);
apple_pmp_power_sample(&budgeted, 500000000ULL, load, 60000, 0);
apple_pmp_power_dark(&budgeted, 1000000000ULL, rails);
KUNIT_EXPECT_EQ(test, budgeted.last_ns, 500000000ULL);
KUNIT_EXPECT_EQ(test, budgeted.total_mw, 18000);
}

static struct kunit_case apple_pmp_policy_cases[] = {
KUNIT_CASE(apple_pmp_test_fault_recovery),
KUNIT_CASE(apple_pmp_test_poll_cadence),
Expand All @@ -161,6 +250,8 @@ static struct kunit_case apple_pmp_policy_cases[] = {
KUNIT_CASE(apple_pmp_test_gpu_budget_freshness),
KUNIT_CASE(apple_pmp_test_shared_power_arbitration),
KUNIT_CASE(apple_pmp_test_gpu_attachment),
KUNIT_CASE(apple_pmp_test_unbudgeted),
KUNIT_CASE(apple_pmp_test_unbudgeted_dark),
{},
};

Expand Down
43 changes: 37 additions & 6 deletions drivers/thermal/apple-pmp-thermal.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ static int apple_pmp_power_status(char *buffer, const struct kernel_param *kp)

guard(mutex)(&pmp_thermal.lock);
return scnprintf(buffer, PAGE_SIZE,
"ns=%llu p_mw=%u e_mw=%u gpu_mw=%u total_mw=%d sustainable_mw=%d allowed_mw=%d credit_mj=%lld filtered_mc=%d p_level=%u e_level=%u gpu_level=%u fault=%d error=%d\n",
"ns=%llu p_mw=%u e_mw=%u gpu_mw=%u total_mw=%d sustainable_mw=%d allowed_mw=%d credit_mj=%lld filtered_mc=%d p_level=%u e_level=%u gpu_level=%u fault=%d error=%d budget=%d\n",
p->last_ns, pmp_thermal.rail_mw[0], pmp_thermal.rail_mw[1],
pmp_thermal.rail_mw[2], p->total_mw, p->sustainable_mw,
p->allowed_mw, p->credit_mj, p->filtered_mc,
p->level[0], p->level[1], p->level[2], p->fault, pmp_thermal.power_error);
p->level[0], p->level[1], p->level[2], p->fault, pmp_thermal.power_error,
!p->unbudgeted);
}
static const struct kernel_param_ops apple_pmp_power_status_ops = {
.get = apple_pmp_power_status,
Expand Down Expand Up @@ -226,8 +227,13 @@ static void apple_pmp_power_work(struct work_struct *work)
/*
* Nothing else restricts the GPU's budget while the sensors are
* dark: a zero publication selects full protection, and the next
* real budget sample republishes its level at once.
* real budget sample republishes its level at once. On fan-cooled
* boards there is no temperature-driven energy budget, so keep
* rail freshness and hwmon totals current while dark instead of
* turning a >2 s dark span into a fault clamp on wake.
*/
if (p->unbudgeted)
apple_pmp_power_dark(p, now, mw);
apple_pmp_gpu_publish(pmp_thermal.sensors, epoch, 8, 0);
} else {
apple_pmp_power_sample(p, now, mw, pmp_thermal.cached_mc, ret);
Expand Down Expand Up @@ -273,7 +279,8 @@ static int apple_pmp_power_label(struct device *dev, enum hwmon_sensor_types typ
};
if (type != hwmon_power || channel < 0 || channel > 3)
return -EINVAL;
*label = labels[channel];
*label = channel == 3 && pmp_thermal.power.unbudgeted ?
"CPU/GPU rail sum" : labels[channel];
return 0;
}
static const struct hwmon_ops apple_pmp_power_ops = {
Expand Down Expand Up @@ -705,6 +712,24 @@ static int apple_pmp_add_cluster(unsigned int index, unsigned int cpu,
return 0;
}

/*
* A board whose available SMC hwmon node describes an available fan (a child
* with apple,fan-maximum) is fan cooled: the SMC appears to set the fan speed
* from the heat the SoC makes, so the fanless Airs' energy budget would
* starve the fan. The die guard below applies unchanged.
*/
static bool apple_pmp_unbudgeted(void)
{
for_each_compatible_node_scoped(hwmon, NULL, "apple,smc-hwmon") {
if (!of_device_is_available(hwmon))
continue;
for_each_available_child_of_node_scoped(hwmon, child)
if (of_property_present(child, "apple,fan-maximum"))
return true;
}
return false;
}

static int apple_pmp_thermal_setup(void)
{
struct thermal_zone_device *zone;
Expand All @@ -718,7 +743,10 @@ static int apple_pmp_thermal_setup(void)
p_members = BIT(0) | GENMASK(5, 3);
e_members = GENMASK(2, 1) | GENMASK(9, 6);
}
apple_pmp_power_init(&pmp_thermal.power);
if (apple_pmp_unbudgeted())
apple_pmp_power_init_unbudgeted(&pmp_thermal.power);
else
apple_pmp_power_init(&pmp_thermal.power);
apple_pmp_cooling_init(&pmp_thermal.cooling);
pmp_thermal.guard_level = 8;
ret = apple_pmp_add_cluster(0, 0, p_members);
Expand Down Expand Up @@ -767,7 +795,10 @@ static int apple_pmp_thermal_setup(void)
if (ret)
goto fail;
}
pr_info("apple-pmp-thermal: T8132 provisional policy: CPU/GPU cooling from 106 C, target 110 C, full protection 115 C, three >=120 C samples for shutdown, 5 ms sampling from 95 C or while cooling (100 ms below), 100 ms governor, shared measured-rail power budget\n");
pr_info("apple-pmp-thermal: T8132 provisional policy: CPU/GPU cooling from 106 C, target 110 C, full protection 115 C, three >=120 C samples for shutdown, 5 ms sampling from 95 C or while cooling (100 ms below), 100 ms governor, %s\n",
pmp_thermal.power.unbudgeted ?
"no power budget (fan cooled; rail faults still restrict)" :
"shared measured-rail power budget");
pmp_thermal.ready = true;
return 0;
fail:
Expand Down
92 changes: 76 additions & 16 deletions include/linux/soc/apple/pmp-power-policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct apple_pmp_power_policy {
int sustainable_mw, allowed_mw, total_mw, filtered_mc;
unsigned int level[3], next_release, good;
unsigned int active;
bool initialized, fault;
bool initialized, fault, unbudgeted;
};

/* SMC little-endian IEEE binary32 Watts -> mW. Reject NaN/Inf, negative,
Expand Down Expand Up @@ -53,6 +53,20 @@ static inline void apple_pmp_power_init(struct apple_pmp_power_policy *s)
};
}

/* A Mac whose fan the SMC appears to run on the heat the SoC makes: the
* energy budget above is the fanless Airs' and takes that heat away, so the
* fan stops and power stays near the floor. Such a board keeps the die guard as its
* limit; the rails are still read, and a fault or a gap in them still selects
* full protection and recovers one step per second, as on the Airs.
*/
static inline void apple_pmp_power_init_unbudgeted(struct apple_pmp_power_policy *s)
{
apple_pmp_power_init(s);
s->sustainable_mw = 0;
s->allowed_mw = 0;
s->unbudgeted = true;
}

/* A newly attached consumer starts restricted. An absent consumer cannot
* absorb releases or be selected as a way to reduce measured consumption.
*/
Expand All @@ -76,6 +90,59 @@ static inline void apple_pmp_power_fail(struct apple_pmp_power_policy *s)
s->good = 0;
}

/* Release the largest weighted restriction, round-robin on ties. */
static inline void apple_pmp_power_release(struct apple_pmp_power_policy *s,
unsigned long long now,
const unsigned int weight[3])
{
unsigned int i, chosen = 3;

for (i = 0; i < 3; i++) {
unsigned int candidate = (s->next_release + i) % 3;

if ((s->active & (1U << candidate)) && s->level[candidate] &&
(chosen == 3 ||
s->level[candidate] * weight[candidate] >
s->level[chosen] * weight[chosen]))
chosen = candidate;
}
if (chosen < 3) {
s->level[chosen]--;
s->next_release = (chosen + 1) % 3;
}
s->change_ns = now;
}

/* On an unbudgeted board die temperature does not drive an energy bucket.
* While every die sensor is dark (-ENODATA) and the SMC rails were read, keep
* the rail sum and sample timestamp current so a >2 s idle dark span does not
* trip the gap fault on the next valid die reading. A genuine >2 s telemetry
* gap still fails closed, and any prior fault stays latched until valid die
* samples confirm recovery.
*/
static inline void apple_pmp_power_dark(struct apple_pmp_power_policy *s,
unsigned long long now,
const unsigned int mw[3])
{
unsigned long long dt = s->last_ns ? now - s->last_ns : 0;
unsigned int i;

if (!s->unbudgeted)
return;
if (dt > 2000000000ULL) {
apple_pmp_power_fail(s);
s->last_ns = now;
return;
}
if (s->last_ns && dt < 400000000ULL)
return;
s->last_ns = now;
s->total_mw = 0;
for (i = 0; i < 3; i++)
if (s->active & (1U << i))
s->total_mw += mw[i];
}

static inline void apple_pmp_power_sample(struct apple_pmp_power_policy *s,
unsigned long long now, const unsigned int mw[3], int mc, int error)
{
Expand Down Expand Up @@ -104,7 +171,7 @@ static inline void apple_pmp_power_sample(struct apple_pmp_power_policy *s,
s->total_mw += mw[i];
if (!s->initialized) {
s->initialized = true;
s->credit_mj = mc < 75000 ? APPLE_PMP_BURST_MJ : 0;
s->credit_mj = mc < 75000 && !s->unbudgeted ? APPLE_PMP_BURST_MJ : 0;
s->filtered_mc = mc;
s->tune_ns = now;
}
Expand All @@ -114,6 +181,12 @@ static inline void apple_pmp_power_sample(struct apple_pmp_power_policy *s,
return;
s->fault = false;
s->filtered_mc += (mc - s->filtered_mc) * (long long)ms / (long long)(5000 + ms);
if (s->unbudgeted) {
/* Release one step per second; nothing restricts again. */
if (now - s->change_ns >= 1000000000ULL)
apple_pmp_power_release(s, now, weight);
return;
}
if (s->total_mw > s->sustainable_mw)
s->credit_mj -= (s->total_mw - s->sustainable_mw) * (long long)ms / 1000;
else if (mc < 75000)
Expand Down Expand Up @@ -163,20 +236,7 @@ static inline void apple_pmp_power_sample(struct apple_pmp_power_policy *s,
* This restores graphics headroom before spending it on CPU stress.
* Fault recovery still restores only one step per elapsed interval.
*/
for (i = 0; i < 3; i++) {
unsigned int candidate = (s->next_release + i) % 3;

if ((s->active & (1U << candidate)) && s->level[candidate] &&
(chosen == 3 ||
s->level[candidate] * weight[candidate] >
s->level[chosen] * weight[chosen]))
chosen = candidate;
}
if (chosen < 3) {
s->level[chosen]--;
s->next_release = (chosen + 1) % 3;
}
s->change_ns = now;
apple_pmp_power_release(s, now, weight);
}
}

Expand Down
Loading