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
19 changes: 15 additions & 4 deletions arch/arm64/boot/dts/apple/t6030.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -2185,10 +2185,21 @@
#include "t6030-misc-interfaces.dtsi"

&smc_hwmon {
/* Not yet checked on hardware; absent firmware keys are skipped. */
apple,soc-die-keys = "Tf04", "Tf09", "Tf0A", "Tf0B", "Tf0D",
"Tf0E", "Tf14", "Tf18", "Tf19", "Tf1A",
"Tf24", "Tf25", "Tf2A", "Tf2B";
/*
* Hardware-verified on a 12-core J514S (MacBook Pro 14", M3 Pro):
* - E-cluster (6 Sawtooth cores): Te05, Te0H, Te0P (+8-9 C under load).
* - P-cores 0..5 (Everest, middle key of each SMC triplet, tracking
* the PMP hotspot ~7 C above the first key as on M3/M4):
* Tp0V (cpu_p00), Tp0b (cpu_p01), Tp1J (cpu_p02), Tp1R (cpu_p03),
* Tp0h (cpu_p04), Tp0n (cpu_p05, +24-28 C under single-core burn).
* - Verified present GPU/fabric Tf keys (excluding Tf0x/Tf2A/Tf2B which
* are absent on J514S, and Tf1D/Tf2D which spike >135 C transiently).
* Absent keys on binned 11-core SKUs are skipped at probe.
*/
apple,soc-die-keys = "Te05", "Te0H", "Te0P",
"Tp0V", "Tp0b", "Tp1J", "Tp1R", "Tp0h", "Tp0n",
"Tf14", "Tf18", "Tf19", "Tf1A",
"Tf24", "Tf25", "Tf28", "Tf29", "Tf2E";
/*
* SoC die limits from the ADT's /arm-io/pmp/iop-pmp-nub, the same in
* the J514s and J516s device trees: the PMP's fast die control holds
Expand Down
90 changes: 90 additions & 0 deletions drivers/hwmon/macsmc-hwmon-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ static void macsmc_hwmon_test_find_temp(struct kunit *test)
#define DIE_TEST_F40 0x42200000 /* 40.0f */
#define DIE_TEST_F45_5 0x42360000 /* 45.5f */
#define DIE_TEST_F50 0x42480000 /* 50.0f */
#define DIE_TEST_FNEG127 0xc2fe0000 /* -127.0f, below lower die bound */
#define DIE_TEST_F151 0x43170000 /* 151.0f, above upper die bound */
#define DIE_TEST_FNAN 0x7fc00000 /* NaN, unavailable reading */

static u32 die_test_gpu;
static u32 die_test_opt_val;
static int die_test_opt_err;

static int die_test_read(struct apple_smc *smc, smc_key key, void *buf, size_t size)
{
Expand All @@ -118,6 +123,11 @@ static int die_test_read(struct apple_smc *smc, smc_key key, void *buf, size_t s
case __SMC_KEY('T', 'p', '0', '1'):
value = DIE_TEST_F45_5;
break;
case __SMC_KEY('T', 'p', '0', 'n'):
if (die_test_opt_err)
return die_test_opt_err;
value = die_test_opt_val;
break;
default:
return -EINVAL;
}
Expand All @@ -144,6 +154,7 @@ static void macsmc_hwmon_test_die_skips_no_data(struct kunit *test)
};
int temp = 0;

mutex_init(&hwmon.die_lock);
die_test_sensor(&sensors[0], SMC_KEY(Tg0f));
sensors[0].has_no_data_below = true;
sensors[0].no_data_below = 10000;
Expand All @@ -166,6 +177,84 @@ static void macsmc_hwmon_test_die_skips_no_data(struct kunit *test)
die_test_gpu = DIE_TEST_F4_607;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&hwmon, &temp), -ENODATA);
}

/*
* A die sensor that has never returned a valid reading (e.g. a fused-off core
* on a binned SKU, or a brief boot-time glitch) is skipped while other sensors
* are live, joins as soon as it returns a valid reading, and fails the zone
* strictly if it breaks or goes out of range afterwards.
*/
static void macsmc_hwmon_test_die_never_valid_and_recovery(struct kunit *test)
{
struct {
struct apple_smc smc;
struct device dev;
struct macsmc_hwmon_sensor sensors[3];
struct macsmc_hwmon hwmon;
} *ctx;
int temp = 0;

ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ctx);
ctx->hwmon.dev = &ctx->dev;
ctx->hwmon.smc = &ctx->smc;
ctx->hwmon.temp.sensors = ctx->sensors;
ctx->hwmon.temp.count = 3;

mutex_init(&ctx->hwmon.die_lock);
die_test_sensor(&ctx->sensors[0], SMC_KEY(Te05));
die_test_sensor(&ctx->sensors[1], SMC_KEY(Tp01));
die_test_sensor(&ctx->sensors[2], SMC_KEY(Tp0n));
kunit_activate_static_stub(test, apple_smc_read, die_test_read);

/* Never-valid sensor returning -EIO, -ETIMEDOUT, NaN, or -127 C does not break the zone. */
die_test_opt_err = -EIO;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 45500);
KUNIT_EXPECT_FALSE(test, ctx->sensors[2].die_seen);

die_test_opt_err = -ETIMEDOUT;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 45500);

die_test_opt_err = 0;
die_test_opt_val = DIE_TEST_FNAN;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 45500);

die_test_opt_val = DIE_TEST_FNEG127;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 45500);
KUNIT_EXPECT_FALSE(test, ctx->sensors[2].die_seen);

/* Once it produces a valid reading, it joins the aggregate and latches live. */
die_test_opt_val = DIE_TEST_F50;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 50000);
KUNIT_EXPECT_TRUE(test, ctx->sensors[2].die_seen);

/* After latching live, any error or out-of-range value fails the zone strictly. */
die_test_opt_val = DIE_TEST_F151;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), -ERANGE);
die_test_opt_val = DIE_TEST_FNAN;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), -ENODATA);
die_test_opt_err = -EIO;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), -EIO);

/* Single-key board: never hides failure when no sensor is valid, and recovers cleanly. */
memset(&ctx->sensors[0], 0, sizeof(ctx->sensors[0]));
die_test_sensor(&ctx->sensors[0], SMC_KEY(Tp0n));
ctx->hwmon.temp.count = 1;
die_test_opt_err = -ETIMEDOUT;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), -ETIMEDOUT);
die_test_opt_err = 0;
die_test_opt_val = DIE_TEST_FNAN;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), -ENODATA);
die_test_opt_val = DIE_TEST_F50;
KUNIT_EXPECT_EQ(test, macsmc_hwmon_die_hottest(&ctx->hwmon, &temp), 0);
KUNIT_EXPECT_EQ(test, temp, 50000);
KUNIT_EXPECT_TRUE(test, ctx->sensors[0].die_seen);
}
#endif

static struct kunit_case macsmc_hwmon_test_cases[] = {
Expand All @@ -174,6 +263,7 @@ static struct kunit_case macsmc_hwmon_test_cases[] = {
KUNIT_CASE(macsmc_hwmon_test_find_temp),
#if IS_ENABLED(CONFIG_THERMAL) && IS_ENABLED(CONFIG_MFD_MACSMC_KUNIT_TEST)
KUNIT_CASE(macsmc_hwmon_test_die_skips_no_data),
KUNIT_CASE(macsmc_hwmon_test_die_never_valid_and_recovery),
#endif
{}
};
Expand Down
56 changes: 45 additions & 11 deletions drivers/hwmon/macsmc-hwmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct macsmc_hwmon_sensor {
u32 attrs;
/* Part of the SoC die temperature zone. */
bool die;
/* Latched once this die sensor has produced a valid in-range reading. */
bool die_seen;
/* One-shot log guard while a never-valid die sensor is skipped. */
bool die_warned;
/* Readings below no_data_below are powered-down placeholders, not temperatures. */
bool has_no_data_below;
s32 no_data_below;
Expand Down Expand Up @@ -130,6 +134,8 @@ struct macsmc_hwmon {
struct macsmc_hwmon_fans fan;
struct macsmc_hwmon_sensor pressure;
bool register_thermal_zones;
/* Serialises SoC die sampling and per-sensor die_seen/die_warned state. */
struct mutex die_lock;
/* SoC die temperature keys listed, and the ones present. */
u32 die_listed;
u32 die_count;
Expand Down Expand Up @@ -1570,10 +1576,14 @@ static void macsmc_hwmon_populate_fan_configs(u32 *configs, const struct macsmc_
* sensors, for in-kernel users that limit SoC power by temperature (they look
* the zone up by name). It is not polled; each read queries every SoC die
* sensor. Its trips come from the DT: the hotspot fan-assistance active trip
* and the SoC die passive and hot limits, each only when configured. A read
* fails if any of the sensors cannot be read or reads outside -40..150 degrees
* Celsius, so users never act on a partial or implausible view. A sensor below
* its no-data threshold is powered down and left out.
* and the SoC die passive and hot limits, each only when configured. Once a
* sensor has produced a valid reading in -40..150 degrees Celsius, any later
* read error or out-of-range value fails the zone read so users never act on a
* partial or implausible view. A sensor that has never returned a valid
* reading since boot (such as a fused-off core key still registered by a
* binned SKU's SMC, or a sensor briefly unavailable at boot) is skipped while
* other die sensors are live and joins automatically as soon as it reads
* validly. A sensor below its no-data threshold is powered down and left out.
*/
#if IS_ENABLED(CONFIG_THERMAL)
#define MACSMC_SOC_DIE_ZONE "macsmc_soc_die"
Expand All @@ -1589,28 +1599,48 @@ static int macsmc_hwmon_die_hottest(struct macsmc_hwmon *hwmon, int *temp)
{
bool found = false;
long hottest = 0, value;
int first_err = 0;
u32 i;
int ret;

guard(mutex)(&hwmon->die_lock);

for (i = 0; i < hwmon->temp.count; i++) {
struct macsmc_hwmon_sensor *sensor = &hwmon->temp.sensors[i];

if (!sensor->die)
continue;
ret = macsmc_hwmon_read_key(hwmon->smc, sensor, 1000, &value);
if (ret)
return ret;
/* A powered-down sensor (a GPU key while the GPU is off) has no say. */
if (sensor->has_no_data_below && value < sensor->no_data_below)
if (!ret) {
/* A powered-down sensor (a GPU key while the GPU is off) has no say. */
if (sensor->has_no_data_below && value < sensor->no_data_below)
continue;
if (value < MACSMC_DIE_MIN_MC || value > MACSMC_DIE_MAX_MC)
ret = -ERANGE;
}
if (ret) {
if (sensor->die_seen)
return ret;
if (!first_err)
first_err = ret;
if (hwmon->dev && !sensor->die_warned) {
sensor->die_warned = true;
dev_info(hwmon->dev,
"SoC die key %c%c%c%c unavailable (%d); skipping until valid\n",
sensor->macsmc_key >> 24,
(sensor->macsmc_key >> 16) & 0xff,
(sensor->macsmc_key >> 8) & 0xff,
sensor->macsmc_key & 0xff, ret);
}
continue;
if (value < MACSMC_DIE_MIN_MC || value > MACSMC_DIE_MAX_MC)
return -ERANGE;
}
sensor->die_seen = true;
if (!found || value > hottest)
hottest = value;
found = true;
}
if (!found)
return -ENODATA;
return first_err ?: -ENODATA;

*temp = hottest;
return 0;
Expand Down Expand Up @@ -2100,6 +2130,10 @@ static int macsmc_hwmon_probe(struct platform_device *pdev)
if (ret)
return ret;

ret = devm_mutex_init(&pdev->dev, &hwmon->die_lock);
if (ret)
return ret;

ret = macsmc_hwmon_populate_sensors(hwmon, hwmon->dev->of_node);
if (ret) {
dev_err(hwmon->dev, "Could not parse sensors\n");
Expand Down
Loading