[PATCH v4 4/5] platform/x86: huawei-wmi: expose native firmware power profiles

From: Fuwei Xie

Date: Mon Nov 30 2026 - 05:13:37 EST


Expose balanced and performance through platform_profile on machines
with GTUB and STUB. Read the initial mode at registration, serve reads
from the cache, and update it on a successful write.

CREM-WXX9 can report the same transition twice and deliver a stale mode
event after a newer write. Re-read GTUB on a mode notification under
the same mutex as profile writes. Notify outside that mutex only when
the cached mode changes. Also re-read on resume: CREM-WXX9 can clear
Turbo during s2idle without delivering a mode event.

Claude assisted Eren with the test scripts, the event and resume changes
and his report. Eren ran the tests and verified the results on hardware.

Co-developed-by: Eren Özen <erenozen@xxxxxxxxxxxxxx>
Signed-off-by: Eren Özen <erenozen@xxxxxxxxxxxxxx>
Signed-off-by: Fuwei Xie <12313607@xxxxxxxxxxxxxxxxxxx>
---
drivers/platform/x86/Kconfig | 1 +
drivers/platform/x86/huawei-wmi.c | 185 ++++++++++++++++++++++++++++--
2 files changed, 174 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..8eaa21757 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -16,6 +16,8 @@
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
+#include <linux/platform_profile.h>
+#include <linux/pm.h>
#include <linux/power_supply.h>
#include <linux/sysfs.h>
#include <linux/wmi.h>
@@ -39,6 +41,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 +73,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 +165,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 +197,142 @@ 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 };
+ int err;
+
+ if (profile != PLATFORM_PROFILE_BALANCED &&
+ profile != PLATFORM_PROFILE_PERFORMANCE)
+ return -EOPNOTSUPP;
+
+ arg.args[2] = (profile == PLATFORM_PROFILE_PERFORMANCE);
+
+ guard(mutex)(&huawei->wmi_lock);
+ 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,
+};
+
+/*
+ * Sample the firmware and notify only when the reported mode changes.
+ * Return false if no profile has been sampled yet. Otherwise the event
+ * is handled, even if the read fails or the mode stays the same.
+ */
+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,
@@ -760,6 +904,11 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code)
kfree(response.pointer);
}

+ /* A delayed mode event may describe an earlier switch. */
+ if ((code == 0x2a0 || code == 0x2a1 || code == 0x2a6) &&
+ huawei_wmi_profile_sync(huawei_wmi))
+ return;
+
key = sparse_keymap_entry_from_scancode(idev, code);
if (!key) {
dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
@@ -840,6 +989,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 +1010,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 +1031,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);
@@ -888,9 +1039,19 @@ static void huawei_wmi_remove(struct platform_device *pdev)
}
}

+/* CREM-WXX9 can leave Turbo during s2idle without a WMI event. */
+static int huawei_wmi_resume(struct device *dev)
+{
+ huawei_wmi_profile_sync(dev_get_drvdata(dev));
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(huawei_wmi_pm, NULL, huawei_wmi_resume);
+
static struct platform_driver huawei_wmi_driver = {
.driver = {
.name = "huawei-wmi",
+ .pm = pm_sleep_ptr(&huawei_wmi_pm),
},
.probe = huawei_wmi_probe,
.remove = huawei_wmi_remove,
--
2.55.0