Skip to content
Closed
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
9 changes: 5 additions & 4 deletions Documentation/hwmon/macsmc-hwmon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 14 additions & 7 deletions drivers/hwmon/macsmc-hwmon-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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),
Expand All @@ -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);
62 changes: 24 additions & 38 deletions drivers/hwmon/macsmc-hwmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +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)");

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;
Expand Down Expand Up @@ -548,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)
Expand Down Expand Up @@ -863,22 +847,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);
Expand Down Expand Up @@ -1680,8 +1667,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)
Expand Down
Loading