[RFC PATCH v3 4/5] platform/x86: huawei-wmi: expose native firmware power profiles
From: Fuwei Xie
Date: Tue Nov 24 2026 - 12:28:20 EST
Expose balanced and performance on systems with GTUB and STUB. Install
the WMI event handlers before sampling the initial mode. Serve profile
reads from the cache and serialize firmware commands and cache updates
with the method lock.
Update the cache after a successful STUB or an explicit WMI mode event.
Read the firmware mode on 0x2a6, observed on a refused request on the
CREM-WXX9. Notify platform_profile outside the lock.
This RFC relies on WMI for unsolicited mode changes. If the EC turns
Turbo off silently on unplug, the cached profile can remain stale.
Signed-off-by: Fuwei Xie <12313607@xxxxxxxxxxxxxxxxxxx>
---
drivers/platform/x86/Kconfig | 1 +
drivers/platform/x86/huawei-wmi.c | 192 ++++++++++++++++++++++++++++--
2 files changed, 181 insertions(+), 12 deletions(-)
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 9ce384e04..05e65a483 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -22,6 +22,7 @@ config HUAWEI_WMI
depends on ACPI_EC
depends on ACPI_WMI
depends on INPUT
+ select ACPI_PLATFORM_PROFILE
select INPUT_SPARSEKMAP
select LEDS_CLASS
select NEW_LEDS
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index 209066260..242926d50 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -16,6 +16,7 @@
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
+#include <linux/platform_profile.h>
#include <linux/power_supply.h>
#include <linux/sysfs.h>
#include <linux/wmi.h>
@@ -39,6 +40,8 @@ enum {
FN_LOCK_GET = 0x00000604, /* \GFRS */
FN_LOCK_SET = 0x00000704, /* \SFRS */
MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
+ POWER_MODE_GET = 0x00000e04, /* \GTUB */
+ POWER_MODE_SET = 0x00000f04, /* \STUB */
};
union hwmi_arg {
@@ -69,6 +72,8 @@ struct huawei_wmi {
struct device *dev;
struct mutex wmi_lock;
+ struct device *profile_dev;
+ enum platform_profile_option reported_profile;
};
static struct huawei_wmi *huawei_wmi;
@@ -159,13 +164,12 @@ struct hwmi_reply {
};
/* Return the number of command data bytes copied, or a negative errno. */
-static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
+static int huawei_wmi_cmd(struct huawei_wmi *huawei, u64 arg, u8 *buf, size_t buflen)
{
- struct huawei_wmi *huawei = huawei_wmi;
struct wmi_buffer in = { .length = sizeof(arg), .data = &arg };
int i;
- guard(mutex)(&huawei->wmi_lock);
+ lockdep_assert_held(&huawei->wmi_lock);
for (i = 0; i < 2; i++) {
struct wmi_buffer out;
size_t len;
@@ -192,6 +196,143 @@ static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen)
return -ENODEV;
}
+/* Native firmware profiles: no RAPL writes and no periodic enforcement. */
+
+static int huawei_wmi_read_profile(struct huawei_wmi *huawei, enum platform_profile_option *profile)
+{
+ u8 state;
+ int len;
+
+ len = huawei_wmi_cmd(huawei, POWER_MODE_GET, &state, sizeof(state));
+ if (len < 0)
+ return len;
+ if (!len || state > 1)
+ return -ENODATA;
+
+ *profile = state ? PLATFORM_PROFILE_PERFORMANCE : PLATFORM_PROFILE_BALANCED;
+ return 0;
+}
+
+static int huawei_wmi_profile_get(struct device *dev, enum platform_profile_option *profile)
+{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
+
+ guard(mutex)(&huawei->wmi_lock);
+ *profile = huawei->reported_profile;
+ return 0;
+}
+
+static int huawei_wmi_profile_set(struct device *dev,
+ enum platform_profile_option profile)
+{
+ struct huawei_wmi *huawei = dev_get_drvdata(dev);
+ union hwmi_arg arg = { .cmd = POWER_MODE_SET };
+
+ if (profile != PLATFORM_PROFILE_BALANCED &&
+ profile != PLATFORM_PROFILE_PERFORMANCE)
+ return -EOPNOTSUPP;
+
+ /*
+ * On NbF-XX (BIOS 3.07), firmware has a small bug :
+ * Fn+P enables Turbo at about 20% displayed charge.
+ * STUB enables Turbo at about 20% / battery health.
+ */
+ arg.args[2] = (profile == PLATFORM_PROFILE_PERFORMANCE);
+
+ guard(mutex)(&huawei->wmi_lock);
+ int err = huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
+
+ if (!err)
+ huawei->reported_profile = profile;
+
+ return err;
+}
+
+static bool huawei_wmi_has_profile_method(char *path)
+{
+ acpi_object_type type;
+ acpi_handle handle;
+
+ return ACPI_SUCCESS(acpi_get_handle(NULL, path, &handle)) &&
+ ACPI_SUCCESS(acpi_get_type(handle, &type)) &&
+ type == ACPI_TYPE_METHOD;
+}
+
+static int huawei_wmi_profile_probe(void *drvdata, unsigned long *choices)
+{
+ struct huawei_wmi *huawei = drvdata;
+
+ set_bit(PLATFORM_PROFILE_BALANCED, choices);
+ set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
+
+ guard(mutex)(&huawei->wmi_lock);
+ return huawei_wmi_read_profile(huawei, &huawei->reported_profile);
+}
+
+static const struct platform_profile_ops huawei_profile_ops = {
+ .probe = huawei_wmi_profile_probe,
+ .profile_get = huawei_wmi_profile_get,
+ .profile_set = huawei_wmi_profile_set,
+};
+
+/* Refresh once when the event does not carry a known mode. */
+static bool huawei_wmi_profile_sync(struct huawei_wmi *huawei)
+{
+ enum platform_profile_option profile;
+ struct device *dev;
+ int err;
+
+ scoped_guard(mutex, &huawei->wmi_lock) {
+ if (huawei->reported_profile == PLATFORM_PROFILE_LAST)
+ return false;
+ err = huawei_wmi_read_profile(huawei, &profile);
+ if (err) {
+ dev_warn_ratelimited(huawei->dev,
+ "Cannot read firmware profile: %d\n", err);
+ return true;
+ }
+ if (profile == huawei->reported_profile)
+ return true;
+ huawei->reported_profile = profile;
+ dev = huawei->profile_dev;
+ }
+ if (dev)
+ platform_profile_notify(dev);
+ return true;
+}
+
+static void huawei_wmi_profile_setup(struct huawei_wmi *huawei)
+{
+ struct device *dev;
+
+ if (!huawei_wmi_has_profile_method("\\GTUB") ||
+ !huawei_wmi_has_profile_method("\\STUB"))
+ return;
+
+ dev = platform_profile_register(huawei->dev, "huawei-wmi", huawei,
+ &huawei_profile_ops);
+ if (IS_ERR(dev)) {
+ scoped_guard(mutex, &huawei->wmi_lock)
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
+ dev_warn(huawei->dev, "Native platform profiles unavailable: %pe\n", dev);
+ return;
+ }
+ scoped_guard(mutex, &huawei->wmi_lock)
+ huawei->profile_dev = dev;
+ /* Publish changes captured between the initial sample and registration. */
+ platform_profile_notify(dev);
+}
+
+/* Call after removing the WMI event handlers. */
+static void huawei_wmi_profile_exit(struct huawei_wmi *huawei)
+{
+ if (!huawei->profile_dev)
+ return;
+ platform_profile_remove(huawei->profile_dev);
+ huawei->profile_dev = NULL;
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
+}
+
/* LEDs */
static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
@@ -238,7 +379,8 @@ static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev,
arg.cmd = MICMUTE_LED_SET;
arg.args[2] = brightness;
- return huawei_wmi_cmd(arg.cmd, NULL, 0);
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
}
@@ -263,7 +405,8 @@ static int huawei_wmi_battery_get(int *start, int *end)
u8 data[0xff];
int len;
- len = huawei_wmi_cmd(BATTERY_THRESH_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei_wmi->wmi_lock)
+ len = huawei_wmi_cmd(huawei_wmi, BATTERY_THRESH_GET, data, sizeof(data));
if (len < 0)
return len;
if (len < 2)
@@ -305,9 +448,8 @@ static int huawei_wmi_battery_set(int start, int end)
msleep(1000);
}
- err = huawei_wmi_cmd(arg.cmd, NULL, 0);
-
- return err;
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
static ssize_t charge_control_start_threshold_show(struct device *dev,
@@ -560,7 +702,8 @@ static int huawei_wmi_fn_lock_get(int *on)
if (acpi_has_method(NULL, "\\GFRS"))
return huawei_acpi_fn_lock_get(on);
- len = huawei_wmi_cmd(FN_LOCK_GET, data, sizeof(data));
+ scoped_guard(mutex, &huawei_wmi->wmi_lock)
+ len = huawei_wmi_cmd(huawei_wmi, FN_LOCK_GET, data, sizeof(data));
if (len < 0)
return len;
if (!len)
@@ -588,7 +731,8 @@ static int huawei_wmi_fn_lock_set(int on)
arg.cmd = FN_LOCK_SET;
arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on.
- return huawei_wmi_cmd(arg.cmd, NULL, 0);
+ guard(mutex)(&huawei_wmi->wmi_lock);
+ return huawei_wmi_cmd(huawei_wmi, arg.cmd, NULL, 0);
}
static ssize_t fn_lock_state_show(struct device *dev,
@@ -738,6 +882,7 @@ static void huawei_wmi_debugfs_exit(struct device *dev)
static void huawei_wmi_process_key(struct input_dev *idev, int code)
{
const struct key_entry *key;
+ struct device *profile_dev;
/*
* WMI0 uses code 0x80 to indicate a hotkey event.
@@ -760,6 +905,27 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)
kfree(response.pointer);
}
+ switch (code) {
+ case 0x2a0: // firmware report turbo closed
+ case 0x2a1: // firmware report turbo enabled
+ {
+ guard(mutex)(&huawei_wmi->wmi_lock);
+
+ if (huawei_wmi->reported_profile == PLATFORM_PROFILE_LAST)
+ break;
+ huawei_wmi->reported_profile = code == 0x2a0 ?
+ PLATFORM_PROFILE_BALANCED : PLATFORM_PROFILE_PERFORMANCE;
+ profile_dev = huawei_wmi->profile_dev;
+ }
+ if (profile_dev)
+ platform_profile_notify(profile_dev);
+ return;
+ case 0x2a6: // firmware reject your profile change request
+ if (huawei_wmi_profile_sync(huawei_wmi))
+ return;
+ break;
+ }
+
key = sparse_keymap_entry_from_scancode(idev, code);
if (!key) {
dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
@@ -840,6 +1006,8 @@ static int huawei_wmi_probe(struct platform_device *pdev)
huawei_wmi->wdev = method ? *method : NULL;
platform_set_drvdata(pdev, huawei_wmi);
huawei_wmi->dev = &pdev->dev;
+ mutex_init(&huawei_wmi->wmi_lock);
+ huawei_wmi->reported_profile = PLATFORM_PROFILE_LAST;
for (guid = huawei_wmi_events_id_table; *guid->guid_string; guid++) {
if (!wmi_has_guid(guid->guid_string))
@@ -859,12 +1027,11 @@ static int huawei_wmi_probe(struct platform_device *pdev)
}
if (huawei_wmi->wdev) {
- mutex_init(&huawei_wmi->wmi_lock);
-
huawei_wmi_leds_setup(&pdev->dev);
huawei_wmi_fn_lock_setup(&pdev->dev);
huawei_wmi_battery_setup(&pdev->dev);
huawei_wmi_debugfs_setup(&pdev->dev);
+ huawei_wmi_profile_setup(huawei_wmi);
}
return 0;
@@ -881,6 +1048,7 @@ static void huawei_wmi_remove(struct platform_device *pdev)
guid++;
}
+ huawei_wmi_profile_exit(huawei_wmi);
if (huawei_wmi->wdev) {
huawei_wmi_debugfs_exit(&pdev->dev);
huawei_wmi_battery_exit(&pdev->dev);
--
2.55.0