[PATCH v5 3/3] platform/x86: huawei-wmi: expose native firmware power profiles
From: Fuwei Xie
Date: Tue Dec 01 2026 - 09:42:43 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.
Handle 0x2a0, 0x2a1, 0x2a5 and 0x2a6 in the event driver. Pin the
method state across the read and notification so unbind can wait for an
in-flight event. Run the resume callback on the bound method device.
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 | 174 ++++++++++++++++++++++++++++++
2 files changed, 175 insertions(+)
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 adba3b5c4..1094dbb8a 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -17,6 +17,8 @@
#include <linux/mutex.h>
#include <linux/notifier.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>
@@ -40,6 +42,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 {
@@ -71,6 +75,8 @@ struct huawei_wmi {
struct device *dev;
struct mutex wmi_lock;
+ struct device *profile_dev;
+ enum platform_profile_option reported_profile;
};
/* The existing platform device owns the system-wide userspace paths. */
@@ -78,6 +84,10 @@ static struct platform_device *huawei_pdev;
static BLOCKING_NOTIFIER_HEAD(huawei_wmi_keycode_chain);
+/* Pins the method driver's profile state while a hotkey is being handled. */
+static DEFINE_MUTEX(huawei_profile_event_lock);
+static struct huawei_wmi *huawei_profile_owner;
+
static const struct key_entry huawei_wmi_keymap[] = {
{ KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } },
{ KE_KEY, 0x282, { KEY_BRIGHTNESSUP } },
@@ -198,6 +208,144 @@ static int huawei_wmi_cmd(struct huawei_wmi *huawei, u64 arg, u8 *buf, size_t bu
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 detaching the method device from the hotkey handler. */
+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,
enum led_brightness brightness)
{
@@ -813,6 +961,14 @@ static void huawei_wmi_input_notify(struct wmi_device *wdev, const struct wmi_bu
code = keycode.code;
}
+ /* A delayed mode event may describe an earlier switch. */
+ if (code == 0x2a0 || code == 0x2a1 || code == 0x2a5 || code == 0x2a6) {
+ guard(mutex)(&huawei_profile_event_lock);
+
+ if (huawei_profile_owner && huawei_wmi_profile_sync(huawei_profile_owner))
+ return;
+ }
+
key = sparse_keymap_entry_from_scancode(idev, code);
if (!key) {
dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code);
@@ -887,6 +1043,7 @@ static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
huawei->wdev = wdev;
huawei->dev = dev;
mutex_init(&huawei->wmi_lock);
+ huawei->reported_profile = PLATFORM_PROFILE_LAST;
dev_set_drvdata(&wdev->dev, huawei);
platform_set_drvdata(huawei_pdev, huawei);
@@ -894,6 +1051,9 @@ static int huawei_wmi_method_probe(struct wmi_device *wdev, const void *context)
huawei_wmi_fn_lock_setup(dev);
huawei_wmi_battery_setup(dev);
huawei_wmi_debugfs_setup(dev);
+ scoped_guard(mutex, &huawei_profile_event_lock)
+ huawei_profile_owner = huawei;
+ huawei_wmi_profile_setup(huawei);
return 0;
}
@@ -901,12 +1061,24 @@ static void huawei_wmi_method_remove(struct wmi_device *wdev)
{
struct huawei_wmi *huawei = dev_get_drvdata(&wdev->dev);
+ scoped_guard(mutex, &huawei_profile_event_lock)
+ huawei_profile_owner = NULL;
+ huawei_wmi_profile_exit(huawei);
huawei_wmi_debugfs_exit(huawei->dev);
huawei_wmi_battery_exit(huawei->dev);
huawei_wmi_fn_lock_exit(huawei->dev);
platform_set_drvdata(huawei_pdev, NULL);
}
+/* 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 const struct wmi_device_id huawei_wmi_method_ids[] = {
{ HWMI_METHOD_GUID },
{}
@@ -915,6 +1087,7 @@ MODULE_DEVICE_TABLE(wmi, huawei_wmi_method_ids);
static struct wmi_driver huawei_wmi_method_driver = {
.driver.name = "huawei-wmi-method",
+ .driver.pm = pm_sleep_ptr(&huawei_wmi_pm),
.id_table = huawei_wmi_method_ids,
.probe = huawei_wmi_method_probe,
.remove = huawei_wmi_method_remove,
@@ -935,6 +1108,7 @@ static int huawei_wmi_probe(struct platform_device *pdev)
if (err)
goto err_unregister_keyblock;
+ /* Receive profile changes from the time the method driver takes its first sample. */
err = wmi_driver_register(&huawei_wmi_method_driver);
if (err)
goto err_unregister_events;
--
2.55.0