Skip to content
Open
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
46 changes: 46 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,51 @@ 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 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 +206,7 @@ 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),
{},
};

Expand Down
29 changes: 24 additions & 5 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 @@ -249,7 +250,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 @@ -681,6 +683,17 @@ static int apple_pmp_add_cluster(unsigned int index, unsigned int cpu,
return 0;
}

/*
* The J604 (MacBook Pro 14-inch) has a fan, whose speed the SMC appears to
* set from the heat the SoC makes. Its ADT gives the J713's die sensors and
* limits (a 110 C target, 125 C maximum), so the die guard below applies
* unchanged; the Airs' energy budget does not.
*/
static bool apple_pmp_unbudgeted(void)
{
return of_machine_is_compatible("apple,j604");
}

static int apple_pmp_thermal_setup(void)
{
struct thermal_zone_device *zone;
Expand All @@ -694,7 +707,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 @@ -743,7 +759,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
62 changes: 46 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,29 @@ 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;
}

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 +141,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 +151,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 +206,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