From 728573afa7eda049a57626cc37ec68733035fdef Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Sun, 29 Aug 2021 13:21:23 +1200 Subject: [PATCH v8 1/1] asus-wmi: Add support for custom fan curves Add support for custom fan curves found on some ASUS ROG laptops. These laptops have the ability to set a custom curve for the CPU and GPU fans via an ACPI method call. This patch enables this, additionally enabling custom fan curves per-profile, where profile here means each of the 3 levels of "throttle_thermal_policy". This patch adds two blocks of attributes to the hwmon sysfs, 1 block each for CPU and GPU fans. When the user switches profiles the associated curve data for that profile is then show/store enabled to allow users to rotate through the profiles and set a fan curve for each profile which then activates on profile switch if enabled. Signed-off-by: Luke D. Jones --- drivers/platform/x86/asus-wmi.c | 494 ++++++++++++++++++++- include/linux/platform_data/x86/asus-wmi.h | 2 + 2 files changed, 492 insertions(+), 4 deletions(-) diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c index cc5811844012..f83f153d60ba 100644 --- a/drivers/platform/x86/asus-wmi.c +++ b/drivers/platform/x86/asus-wmi.c @@ -106,8 +106,16 @@ module_param(fnlock_default, bool, 0444); #define WMI_EVENT_MASK 0xFFFF +/* The number of ASUS_THROTTLE_THERMAL_POLICY_* available */ +#define FAN_CURVE_PROFILE_NUM (ASUS_THROTTLE_THERMAL_POLICY_SILENT + 1) +#define FAN_CURVE_POINTS 8 +#define FAN_CURVE_DEV_CPU 0x00 +#define FAN_CURVE_DEV_GPU 0x01 + static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL }; +static int throttle_thermal_policy_write(struct asus_wmi *); + static bool ashs_present(void) { int i = 0; @@ -122,7 +130,8 @@ struct bios_args { u32 arg0; u32 arg1; u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */ - u32 arg4; + u32 arg3; + u32 arg4; /* Some ROG laptops require a full 5 input args */ u32 arg5; } __packed; @@ -173,6 +182,17 @@ enum fan_type { FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */ }; +/* + * The related ACPI method for testing availability also returns the factory + * default fan curves. We save them here so that a user can reset custom + * settings if required. + */ +struct fan_curve_data { + u8 enabled; + u8 temps[FAN_CURVE_POINTS]; + u8 percents[FAN_CURVE_POINTS]; +}; + struct asus_wmi { int dsts_id; int spec; @@ -220,6 +240,11 @@ struct asus_wmi { bool throttle_thermal_policy_available; u8 throttle_thermal_policy_mode; + bool cpu_fan_curve_available; + bool gpu_fan_curve_available; + /* [throttle modes][fan count] */ + struct fan_curve_data throttle_fan_curves[FAN_CURVE_PROFILE_NUM][2]; + struct platform_profile_handler platform_profile_handler; bool platform_profile_support; @@ -285,6 +310,105 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval) } EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method); +static int asus_wmi_evaluate_method5(u32 method_id, + u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval) +{ + struct bios_args args = { + .arg0 = arg0, + .arg1 = arg1, + .arg2 = arg2, + .arg3 = arg3, + .arg4 = arg4, + }; + struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; + acpi_status status; + union acpi_object *obj; + u32 tmp = 0; + + status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id, + &input, &output); + + if (ACPI_FAILURE(status)) + return -EIO; + + obj = (union acpi_object *)output.pointer; + if (obj && obj->type == ACPI_TYPE_INTEGER) + tmp = (u32) obj->integer.value; + + if (retval) + *retval = tmp; + + kfree(obj); + + if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) + return -ENODEV; + + return 0; +} + +/* + * Returns as an error if the method output is not a buffer. Typically this + * means that the method called is unsupported. + */ +static int asus_wmi_evaluate_method_buf(u32 method_id, + u32 arg0, u32 arg1, u8 *ret_buffer, size_t size) +{ + struct bios_args args = { + .arg0 = arg0, + .arg1 = arg1, + .arg2 = 0, + }; + struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; + struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; + acpi_status status; + union acpi_object *obj; + u32 int_tmp = 0; + int err = 0; + + status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id, + &input, &output); + + if (ACPI_FAILURE(status)) + return -EIO; + + obj = (union acpi_object *)output.pointer; + + if (obj && obj->type == ACPI_TYPE_BUFFER) { + if (obj->buffer.length > size) + err = -ENOSPC; + if (obj->buffer.length == 0) + err = -ENODATA; + if (err) { + kfree(obj); + return err; + } + memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length); + } + + if (obj && obj->type == ACPI_TYPE_INTEGER) { + int_tmp = (u32)obj->integer.value; + + kfree(obj); + if (int_tmp == ASUS_WMI_UNSUPPORTED_METHOD) + return -ENODEV; + /* + * At least one method returns a 0 with no buffer if no arg + * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE + */ + if (int_tmp == 0) + return -ENODATA; + return int_tmp; + } + + kfree(obj); + + if (obj == NULL) + return -ENODATA; + + return 0; +} + static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args) { struct acpi_buffer input; @@ -2043,6 +2167,357 @@ static ssize_t fan_boost_mode_store(struct device *dev, // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent static DEVICE_ATTR_RW(fan_boost_mode); +/* Custom fan curves per-profile **********************************************/ + +static void init_fan_curve(struct fan_curve_data *data, u8 *buf) +{ + int i; + + for (i = 0; i < FAN_CURVE_POINTS; i++) + data->temps[i] = buf[i]; + + for (i = 0; i < FAN_CURVE_POINTS; i++) + data->percents[i] = buf[i + 8]; +} + +/* + * Check if the ability to set fan curves on either fan exists, and populate + * with system defaults to provide users with a starting point. + * + * "dev" is either CPU_FAN_CURVE or GPU_FAN_CURVE. + */ +static int custom_fan_check_present(struct asus_wmi *asus, + bool *available, u32 dev) +{ + struct fan_curve_data *curves; + u8 buf[FAN_CURVE_POINTS * 2]; + int fan_idx = 0; + int err; + + *available = false; + if (dev == ASUS_WMI_DEVID_GPU_FAN_CURVE) + fan_idx = 1; + + /* Balanced default */ + curves = &asus->throttle_fan_curves + [ASUS_THROTTLE_THERMAL_POLICY_DEFAULT][fan_idx]; + err = asus_wmi_evaluate_method_buf(asus->dsts_id, dev, 0, buf, + FAN_CURVE_POINTS * 2); + if (err) { + if (err == -ENODEV) + return 0; + return err; + } + init_fan_curve(curves, buf); + + /* + * Quiet default. The index num for ACPI method does not match the + * throttle_thermal number, same for Performance. + */ + curves = &asus->throttle_fan_curves + [ASUS_THROTTLE_THERMAL_POLICY_SILENT][fan_idx]; + err = asus_wmi_evaluate_method_buf(asus->dsts_id, dev, 1, buf, + FAN_CURVE_POINTS * 2); + if (err) { + if (err == -ENODEV) + return 0; + return err; + } + init_fan_curve(curves, buf); + + /* Performance default */ + curves = &asus->throttle_fan_curves + [ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST][fan_idx]; + err = asus_wmi_evaluate_method_buf(asus->dsts_id, dev, 2, buf, + FAN_CURVE_POINTS * 2); + if (err) { + if (err == -ENODEV) + return 0; + return err; + } + init_fan_curve(curves, buf); + + *available = true; + return 0; +} + +/* + * "dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE. + */ +static int fan_curve_write_arg(struct asus_wmi *asus, u32 dev, + struct fan_curve_data *data) +{ + u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0; + int ret, i, shift = 0; + + for (i = 0; i < FAN_CURVE_POINTS / 2; i++) { + arg1 += data->temps[i] << shift; + arg2 += data->temps[i + 4] << shift; + arg3 += data->percents[0] << shift; + arg4 += data->percents[i + 4] << shift; + shift += 8; + } + + return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS, dev, + arg1, arg2, arg3, arg4, &ret); +} + +/* + * Called only by throttle_thermal_policy_write() + */ +static int fan_curve_write_data(struct asus_wmi *asus) +{ + struct fan_curve_data *cpu; + struct fan_curve_data *gpu; + int err, mode; + + mode = asus->throttle_thermal_policy_mode; + cpu = &asus->throttle_fan_curves[mode][FAN_CURVE_DEV_CPU]; + gpu = &asus->throttle_fan_curves[mode][FAN_CURVE_DEV_GPU]; + + if (cpu->enabled) { + err = fan_curve_write_arg(asus, ASUS_WMI_DEVID_CPU_FAN_CURVE, cpu); + if (err) + return err; + } + + if (gpu->enabled) { + err = fan_curve_write_arg(asus, ASUS_WMI_DEVID_GPU_FAN_CURVE, gpu); + if (err) + return err; + } + + return 0; +} + +static int fan_curve_verify(struct device *dev, + enum hwmon_sensor_types type, u32 fan_dev) +{ + struct asus_wmi *asus = dev_get_drvdata(dev); + u8 mode = asus->throttle_thermal_policy_mode; + struct fan_curve_data *data; + u8 tmp = 0, prev_tmp = 0; + int i; + + switch (type) { + case hwmon_temp: + data = &asus->throttle_fan_curves[mode][fan_dev]; + break; + case hwmon_fan: + data = &asus->throttle_fan_curves[mode][fan_dev]; + break; + default: + return -EOPNOTSUPP; + } + + for (i = 0; i < FAN_CURVE_POINTS; i++) { + tmp = data->temps[i]; + if (tmp < prev_tmp) + return -EINVAL; + prev_tmp = tmp; + } + + tmp = prev_tmp = 0; + for (i = 0; i < FAN_CURVE_POINTS; i++) { + tmp = data->percents[i]; + if (tmp < prev_tmp) + return -EINVAL; + prev_tmp = tmp; + } + + return 0; +} + +/* ret is a pointer to the requested profile->fan->point */ +static int fan_curve_point_select(struct device *dev, + enum hwmon_sensor_types type, int point, u32 fan_dev, u8 **ret) +{ + struct asus_wmi *asus = dev_get_drvdata(dev); + u8 mode = asus->throttle_thermal_policy_mode; + + switch (type) { + case hwmon_temp: + *ret = &asus->throttle_fan_curves[mode][fan_dev].temps[point]; + break; + case hwmon_fan: + *ret = &asus->throttle_fan_curves[mode][fan_dev].percents[point]; + break; + case hwmon_in: + *ret = &asus->throttle_fan_curves[mode][fan_dev].enabled; + break; + default: + return -EOPNOTSUPP; + } + return 0; +} + +static int asus_fan_curve_read(struct device *dev, enum hwmon_sensor_types type, + int point, long *val, u32 fan_dev) +{ + int err; + u8 *ret; + + err = fan_curve_point_select(dev, type, point, fan_dev, &ret); + if (err) + return err; + *val = *ret; + + return 0; +} + +static int asus_cpu_fan_curve_read(struct device *dev, + enum hwmon_sensor_types type, u32 attr, int point, long *val) +{ + return asus_fan_curve_read(dev, type, point, val, FAN_CURVE_DEV_CPU); +} + +static int asus_gpu_fan_curve_read(struct device *dev, + enum hwmon_sensor_types type, u32 attr, int point, long *val) +{ + return asus_fan_curve_read(dev, type, point, val, FAN_CURVE_DEV_GPU); +} + + +static int asus_fan_curve_write(struct device *dev, + enum hwmon_sensor_types type, + u32 attr, int point, long val, u32 fan_dev) +{ + u8 value, old_value, *ret; + int err; + + err = fan_curve_point_select(dev, type, point, fan_dev, &ret); + if (err) + return err; + + value = *ret; + old_value = value; + if (type == hwmon_temp) + value = 100 * value / 255; + + /* The check here forces writing a curve graph in reverse for safety */ + err = fan_curve_verify(dev, type, fan_dev); + if (err) { + switch (type) { + case hwmon_temp: + dev_err(dev, "a fan curve temperature was higher than the next in sequence\n"); + *ret = old_value; + break; + case hwmon_fan: + dev_err(dev, "a fan curve percentage was higher than the next in sequence\n"); + *ret = old_value; + break; + default: + break; + } + return err; + } + + return 0; +} + +static int asus_cpu_fan_curve_write(struct device *dev, + enum hwmon_sensor_types type, u32 attr, int point, long val) +{ + return asus_fan_curve_write(dev, type, attr, point, val, FAN_CURVE_DEV_CPU); +} + +static int asus_gpu_fan_curve_write(struct device *dev, + enum hwmon_sensor_types type, u32 attr, int point, long val) +{ + return asus_fan_curve_write(dev, type, attr, point, val, FAN_CURVE_DEV_GPU); +} + +static umode_t asus_cpu_fan_curve_is_visible(const void *data, + enum hwmon_sensor_types type, u32 attr, int point) +{ + const struct asus_wmi *asus = data; + + if (asus->cpu_fan_curve_available) + return 0644; + return 0; +} + +static umode_t asus_gpu_fan_curve_is_visible(const void *data, + enum hwmon_sensor_types type, u32 attr, int point) +{ + const struct asus_wmi *asus = data; + + if (asus->gpu_fan_curve_available) + return 0644; + return 0; +} + +static const struct hwmon_ops asus_cpu_fan_curve_ops = { + .is_visible = asus_cpu_fan_curve_is_visible, + .read = asus_cpu_fan_curve_read, + .write = asus_cpu_fan_curve_write, +}; + +static const struct hwmon_ops asus_gpu_fan_curve_ops = { + .is_visible = asus_gpu_fan_curve_is_visible, + .read = asus_gpu_fan_curve_read, + .write = asus_gpu_fan_curve_write, +}; + +/* Channel number determines CPU or GPU, 0-1 = CPU */ +static const struct hwmon_channel_info *asus_fan_curve_info[] = { + HWMON_CHANNEL_INFO(temp, + HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, + HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT), + HWMON_CHANNEL_INFO(fan, + HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT, + HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT), + HWMON_CHANNEL_INFO(in, HWMON_I_ENABLE), + NULL +}; + +static const struct hwmon_chip_info asus_cpu_fan_curve_chip_info = { + .ops = &asus_cpu_fan_curve_ops, + .info = asus_fan_curve_info, +}; + +static const struct hwmon_chip_info asus_gpu_fan_curve_chip_info = { + .ops = &asus_gpu_fan_curve_ops, + .info = asus_fan_curve_info, +}; + +static int asus_wmi_fan_curve_init(struct asus_wmi *asus) +{ + struct device *dev = &asus->platform_device->dev; + struct device *hwmon; + int err; + + err = custom_fan_check_present(asus, + &asus->cpu_fan_curve_available, ASUS_WMI_DEVID_CPU_FAN_CURVE); + if (err) + return err; + + err = custom_fan_check_present(asus, + &asus->gpu_fan_curve_available, ASUS_WMI_DEVID_GPU_FAN_CURVE); + if (err) + return err; + + hwmon = hwmon_device_register_with_info(dev, + "asus_cpu_fan_custom_curve", asus, + &asus_cpu_fan_curve_chip_info, NULL); + + if (IS_ERR(hwmon)) { + dev_err(dev, "Could not register asus_cpu_fan_curve device\n"); + return PTR_ERR(hwmon); + } + + hwmon = hwmon_device_register_with_info(dev, + "asus_gpu_fan_custom_curve", asus, + &asus_gpu_fan_curve_chip_info, NULL); + + if (IS_ERR(hwmon)) { + dev_err(dev, "Could not register asus_cpu_fan_curve device\n"); + return PTR_ERR(hwmon); + } + + return 0; +} + /* Throttle thermal policy ****************************************************/ static int throttle_thermal_policy_check_present(struct asus_wmi *asus) @@ -2053,8 +2528,8 @@ static int throttle_thermal_policy_check_present(struct asus_wmi *asus) asus->throttle_thermal_policy_available = false; err = asus_wmi_get_devstate(asus, - ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY, - &result); + ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY, + &result); if (err) { if (err == -ENODEV) return 0; @@ -2092,6 +2567,12 @@ static int throttle_thermal_policy_write(struct asus_wmi *asus) return -EIO; } + if (asus->cpu_fan_curve_available || asus->gpu_fan_curve_available) { + err = fan_curve_write_data(asus); + if (err) + return err; + } + return 0; } @@ -2904,7 +3385,7 @@ static int show_call(struct seq_file *m, void *data) if (ACPI_FAILURE(status)) return -EIO; - obj = (union acpi_object *)output.pointer; + obj = output.pointer; if (obj && obj->type == ACPI_TYPE_INTEGER) seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id, asus->debug.dev_id, asus->debug.ctrl_param, @@ -3038,6 +3519,10 @@ static int asus_wmi_add(struct platform_device *pdev) if (err) goto fail_hwmon; + err = asus_wmi_fan_curve_init(asus); + if (err) + goto fail_custom_fan_curve; + err = asus_wmi_led_init(asus); if (err) goto fail_leds; @@ -3109,6 +3594,7 @@ static int asus_wmi_add(struct platform_device *pdev) asus_wmi_sysfs_exit(asus->platform_device); fail_sysfs: fail_throttle_thermal_policy: +fail_custom_fan_curve: fail_platform_profile_setup: if (asus->platform_profile_support) platform_profile_remove(); diff --git a/include/linux/platform_data/x86/asus-wmi.h b/include/linux/platform_data/x86/asus-wmi.h index 17dc5cb6f3f2..a571b47ff362 100644 --- a/include/linux/platform_data/x86/asus-wmi.h +++ b/include/linux/platform_data/x86/asus-wmi.h @@ -77,6 +77,8 @@ #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011 #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012 /* deprecated */ #define ASUS_WMI_DEVID_CPU_FAN_CTRL 0x00110013 +#define ASUS_WMI_DEVID_CPU_FAN_CURVE 0x00110024 +#define ASUS_WMI_DEVID_GPU_FAN_CURVE 0x00110025 /* Power */ #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012 -- 2.31.1