From 3d23ad05be446716fe5244ac297cc0b475bcd9ab Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:28:04 +1100 Subject: [PATCH 1/2] hwmon: macsmc: Select sensor keys by key metadata, not by SoC macsmc_hwmon_is_m3() matched apple,t6030/t6031/t6032/t6034/t8122 and gated two things that do not depend on the SoC: 1. The key check in macsmc_hwmon_create_sensor(). A sensor node was admitted only if the SMC reports its key readable and as a 4-byte "flt " or 8-byte "ioft" value, but only on those five SoCs. The gate was added to keep the earlier admission policy on other machines, not because of a firmware difference. Every SMC reports a key's type, size and flags (apple_smc_get_key_info()), and macsmc_hwmon_read_key() decodes only those two formats on every SoC, so a key that fails the check can only fail every read. The die sensor, thermal pressure and temperature discovery paths already apply the same check without a SoC gate. Apply it to every sensor node. On a node that is a thermal sensor provider (#thermal-sensor-cells, used today by the T600x MacBook Pro, T604x, T8112, T8122 and T8140 trees) a rejected temperature node keeps its hidden channel, so zone indices do not move; elsewhere the unusable channel is no longer registered. 2. The debugfs "keys" inventory, enabled on the same SoCs and T6050. It only walks the SMC key index and key info and reads readable 4-byte float T* and P* keys on demand, which every SMC supports. It is debugfs, root only, and reads nothing at probe. Create it on every machine. No SMC write and no probe-time key read is added on any SoC. The key check and the inventory were written by Chris Kearney for T6030. Fixes: c91c5f4c1f68 ("hwmon: macsmc: preserve the legacy sensor export policy") Fixes: 2604bb0c739b ("hwmon: macsmc: expose read-only SMC key inventory on T6050") Signed-off-by: Ryan Murray --- drivers/hwmon/macsmc-hwmon.c | 45 +++++++++++++++--------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c index c519c70755c00..87ad71b3984d3 100644 --- a/drivers/hwmon/macsmc-hwmon.c +++ b/drivers/hwmon/macsmc-hwmon.c @@ -58,15 +58,6 @@ module_param_unsafe(fan_control, bool, 0644); MODULE_PARM_DESC(fan_control, "Allow manual fan speeds on every fan with a mode key, also where the device tree does not (default: the device tree decides)"); -static bool macsmc_hwmon_is_m3(void) -{ - return of_machine_is_compatible("apple,t6030") || - of_machine_is_compatible("apple,t6031") || - of_machine_is_compatible("apple,t6032") || - of_machine_is_compatible("apple,t6034") || - of_machine_is_compatible("apple,t8122"); -} - struct macsmc_hwmon_sensor { struct apple_smc_key_info info; smc_key macsmc_key; @@ -863,22 +854,25 @@ static int macsmc_hwmon_create_sensor(struct device *dev, struct apple_smc *smc, if (ret) return ret; - if (macsmc_hwmon_is_m3()) { - if (!(sensor->info.flags & APPLE_SMC_READABLE)) - return -EACCES; + /* + * Admit a key only if its own metadata says it can be read in a + * format macsmc_hwmon_read_key() decodes. Any other key could only + * fail every read, whatever the SoC. + */ + if (!(sensor->info.flags & APPLE_SMC_READABLE)) + return -EACCES; - switch (sensor->info.type_code) { - case __SMC_KEY('f', 'l', 't', ' '): - if (sensor->info.size != sizeof(u32)) - return -EINVAL; - break; - case __SMC_KEY('i', 'o', 'f', 't'): - if (sensor->info.size != sizeof(u64)) - return -EINVAL; - break; - default: - return -EOPNOTSUPP; - } + switch (sensor->info.type_code) { + case __SMC_KEY('f', 'l', 't', ' '): + if (sensor->info.size != sizeof(u32)) + return -EINVAL; + break; + case __SMC_KEY('i', 'o', 'f', 't'): + if (sensor->info.size != sizeof(u64)) + return -EINVAL; + break; + default: + return -EOPNOTSUPP; } ret = of_property_read_string(sensor_node, "label", &label); @@ -1680,8 +1674,7 @@ static int macsmc_hwmon_probe(struct platform_device *pdev) return dev_err_probe(hwmon->dev, PTR_ERR(hwmon->hwmon_dev), "Probing SMC hwmon device failed\n"); - if (macsmc_hwmon_is_m3() || of_machine_is_compatible("apple,t6050")) - macsmc_hwmon_debugfs_init(hwmon); + macsmc_hwmon_debugfs_init(hwmon); ret = macsmc_hwmon_die_register(hwmon); if (ret) From ff5baa71bc0a825367f44f45544c2c6d9740b010 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:29:44 +1100 Subject: [PATCH 2/2] hwmon: macsmc: Drop the fan_control module parameter fan_control (module_param_unsafe, default off) made fanX_target writable on every fan with mode, target, minimum and maximum keys, whatever the device tree says. Since commit 65f0c0fddad9 ("hwmon: macsmc: let the device tree allow manual fan speeds") the hardware-described path is the apple,fan-manual-control property on a fan node, set in a machine's device tree where its SMC firmware is known to accept a manual speed and to take the fan back. The parameter was kept then as the mainline interface. It is now the only way left to force manual speeds on a fan whose firmware behaviour is not described, and nothing in this tree's kits, scripts or device trees sets it. Remove it. fanX_target is writable exactly on the fans whose node has apple,fan-manual-control. A leftover macsmc_hwmon.fan_control=1 on the command line or in modprobe options is reported as an unknown parameter and ignored; the driver still loads. Kernel thermal requests through macsmc_hwmon_fans_request() never depended on the parameter and are unchanged, as is the unlock and restore sequence. The fan KUnit suite now sets the per-fan device tree flag instead of the parameter, and a new case checks that a fan without it refuses a target without writing to the SMC. The parameter came with the original driver by James Calligeros. Fixes: 65f0c0fddad9 ("hwmon: macsmc: let the device tree allow manual fan speeds") Signed-off-by: Ryan Murray --- Documentation/hwmon/macsmc-hwmon.rst | 9 +++++---- drivers/hwmon/macsmc-hwmon-test.c | 21 ++++++++++++++------- drivers/hwmon/macsmc-hwmon.c | 17 +++++------------ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Documentation/hwmon/macsmc-hwmon.rst b/Documentation/hwmon/macsmc-hwmon.rst index 46a6d504b7f5c..c4df2eef90fbd 100644 --- a/Documentation/hwmon/macsmc-hwmon.rst +++ b/Documentation/hwmon/macsmc-hwmon.rst @@ -26,10 +26,11 @@ Manual fan speed is available on the fans whose Devicetree node has apple,fan-manual-control, which a machine's Devicetree sets once its SMC firmware is known to accept a manual speed and to take the fan back. On those fans fanX_target is writable; writing 0 hands the fan back to the -firmware. The fan_control module parameter, disabled by default and marked -as unsafe, allows manual speeds on every fan with a mode key whatever the -Devicetree says. It cannot be proven that the system will fail safe if it -overheats under a manual fan speed. +firmware. On other fans fanX_target is read-only; there is no module +parameter to override the Devicetree. The SMC does not check a manual speed +against its own thermal policy, so a machine's Devicetree should set +apple,fan-manual-control only where the firmware is known to take the fan +back. The driver leaves fan policy to the SMC until userspace writes a nonzero ``fanX_target``. Thermal zones do not take fans into manual mode. Suspend, diff --git a/drivers/hwmon/macsmc-hwmon-test.c b/drivers/hwmon/macsmc-hwmon-test.c index 7557360659bd8..d2f1333f0571e 100644 --- a/drivers/hwmon/macsmc-hwmon-test.c +++ b/drivers/hwmon/macsmc-hwmon-test.c @@ -320,6 +320,7 @@ static int macsmc_fan_test_init(struct kunit *test) fan_test_key(&t->fans[i].max, keys[i][2], flt); fan_test_key(&t->fans[i].set, keys[i][3], flt); fan_test_key(&t->fans[i].mode, keys[i][4], ui8); + t->fans[i].manual_control = true; t->mode[i] = MACSMC_FAN_MODE_SYSTEM; } t->hwmon.smc = &t->smc; @@ -328,17 +329,11 @@ static int macsmc_fan_test_init(struct kunit *test) t->hwmon.fan.unlock_key = SMC_KEY(Ftst); mutex_init(&t->hwmon.fan.lock); test->priv = t; - fan_control = true; kunit_activate_static_stub(test, apple_smc_read, fan_test_read); kunit_activate_static_stub(test, apple_smc_write, fan_test_write); return 0; } -static void macsmc_fan_test_exit(struct kunit *test) -{ - fan_control = false; -} - static void fan_test_released(struct kunit *test, struct macsmc_fan_test *t) { KUNIT_EXPECT_EQ(test, t->ftst, 0); @@ -422,6 +417,18 @@ static void macsmc_fan_test_no_unlock_key(struct kunit *test) KUNIT_EXPECT_EQ(test, t->hwmon.fan.manual, 0); } +/* A fan without apple,fan-manual-control refuses a target, with no SMC write. */ +static void macsmc_fan_test_not_manual(struct kunit *test) +{ + struct macsmc_fan_test *t = test->priv; + + t->fans[0].manual_control = false; + KUNIT_EXPECT_EQ(test, macsmc_hwmon_set_fan(&t->hwmon, 0, 2500), -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, t->writes, 0); + KUNIT_EXPECT_EQ(test, t->mode[0], MACSMC_FAN_MODE_SYSTEM); + KUNIT_EXPECT_EQ(test, t->hwmon.fan.manual, 0); +} + /* Firmware that takes the mode directly keeps the mode 0 restore. */ static void macsmc_fan_test_legacy(struct kunit *test) { @@ -559,6 +566,7 @@ static struct kunit_case macsmc_fan_test_cases[] = { KUNIT_CASE(macsmc_fan_test_target_write_fails), KUNIT_CASE(macsmc_fan_test_two_fans), KUNIT_CASE(macsmc_fan_test_no_unlock_key), + KUNIT_CASE(macsmc_fan_test_not_manual), KUNIT_CASE(macsmc_fan_test_legacy), KUNIT_CASE(macsmc_fan_test_legacy_write_only), KUNIT_CASE(macsmc_fan_test_untouched), @@ -570,7 +578,6 @@ static struct kunit_case macsmc_fan_test_cases[] = { static struct kunit_suite macsmc_fan_test_suite = { .name = "macsmc-hwmon-fan", .init = macsmc_fan_test_init, - .exit = macsmc_fan_test_exit, .test_cases = macsmc_fan_test_cases, }; kunit_test_suites(&macsmc_hwmon_test_suite, &macsmc_fan_test_suite); diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c index 87ad71b3984d3..7e5eb19f6ff7f 100644 --- a/drivers/hwmon/macsmc-hwmon.c +++ b/drivers/hwmon/macsmc-hwmon.c @@ -48,16 +48,6 @@ #define FLT_MANT_MASK GENMASK(22, 0) #define FLT_SIGN_MASK BIT(31) -/* - * Fans whose device tree node has apple,fan-manual-control take a target speed - * from userspace. This parameter, kept for compatibility, allows it for every - * fan with a mode key, whatever the device tree says. - */ -static bool fan_control; -module_param_unsafe(fan_control, bool, 0644); -MODULE_PARM_DESC(fan_control, - "Allow manual fan speeds on every fan with a mode key, also where the device tree does not (default: the device tree decides)"); - struct macsmc_hwmon_sensor { struct apple_smc_key_info info; smc_key macsmc_key; @@ -539,10 +529,13 @@ static int macsmc_hwmon_fan_manual(struct macsmc_hwmon *hwmon, return ret; } -/* Whether userspace may set this fan's speed. */ +/* + * Whether userspace may set this fan's speed: only fans whose device tree + * node has apple,fan-manual-control. On other fans fanX_target is read-only. + */ static bool macsmc_hwmon_fan_user_control(const struct macsmc_hwmon_fan *fan) { - return fan->mode.macsmc_key && (fan->manual_control || fan_control); + return fan->mode.macsmc_key && fan->manual_control; } static int macsmc_hwmon_set_fan(struct macsmc_hwmon *hwmon, int channel, long val)