[PATCH 9/9] platform/x86: asus-wmi: add setting dGPU TGP

From: Luke D. Jones
Date: Mon May 27 2024 - 21:39:15 EST


The dGPU TGP can be set on a few ROG laptops. This gnerally needs some
extra userspace inference to find the base and limit:

- Find max-tgp from nvidia-smi
- Find max-boost from nv_dynamic_boost_max (asus_wmi)
- (max-tgp - max-boost - initial TGP) = base TGP
- (max-tgp - max-boost - base TGP) = max additional TGP

On some laptops the dgpu_base_tgp may return 0, in which case the base
TGP must be derived as above.

Signed-off-by: Luke D. Jones <luke@xxxxxxxxxx>
---
.../ABI/testing/sysfs-platform-asus-wmi | 19 +++++++++++++++++++
drivers/platform/x86/asus-wmi.c | 9 +++++++++
include/linux/platform_data/x86/asus-wmi.h | 3 +++
3 files changed, 31 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-platform-asus-wmi b/Documentation/ABI/testing/sysfs-platform-asus-wmi
index d221a3bc1a81..46df3452c9da 100644
--- a/Documentation/ABI/testing/sysfs-platform-asus-wmi
+++ b/Documentation/ABI/testing/sysfs-platform-asus-wmi
@@ -253,3 +253,22 @@ Contact: "Luke Jones" <luke@xxxxxxxxxx>
Description:
Set the maximum available system memory for the APU.
* Min=0, Max=8
+
+What: /sys/devices/platform/<platform>/dgpu_tgp
+Date: Jun 2024
+KernelVersion: 6.11
+Contact: "Luke Jones" <luke@xxxxxxxxxx>
+Description:
+ Read and set the extra TGP applied to the dGPU. This is applied on
+ top of the dgpu_base_tgp.
+
+ If the dGPU maximum power including boost is 175 then we can calculate:
+ 175 - 25 (dynamic boost) - 70 (initial dgpu_tgp) = 80 (dgpu_base_tgp).
+ For NVIDIA dGPU the max power can be found with nvidia-smi.
+
+What: /sys/devices/platform/<platform>/dgpu_base_tgp
+Date: Jun 2024
+KernelVersion: 6.11
+Contact: "Luke Jones" <luke@xxxxxxxxxx>
+Description:
+ Read the absolute base TGP of the dGPU.
diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
index 4b5fbae8c563..4d291429e7a1 100644
--- a/drivers/platform/x86/asus-wmi.c
+++ b/drivers/platform/x86/asus-wmi.c
@@ -123,6 +123,7 @@ module_param(fnlock_default, bool, 0444);
#define NVIDIA_BOOST_MAX 25
#define NVIDIA_TEMP_MIN 75
#define NVIDIA_TEMP_MAX 87
+#define NVIDIA_GPU_POWER_MAX 70

#define ASUS_SCREENPAD_BRIGHT_MIN 20
#define ASUS_SCREENPAD_BRIGHT_MAX 255
@@ -797,6 +798,8 @@ WMI_ATTR_SIMPLE_RO(egpu_connected, ASUS_WMI_DEVID_EGPU_CONNECTED);
WMI_ATTR_SIMPLE_RW(panel_od, 0, 1, ASUS_WMI_DEVID_PANEL_OD);
WMI_ATTR_SIMPLE_RW(boot_sound, 0, 1, ASUS_WMI_DEVID_BOOT_SOUND);
WMI_ATTR_SIMPLE_RO(charge_mode, ASUS_WMI_DEVID_CHARGE_MODE);
+WMI_ATTR_SIMPLE_RO(dgpu_base_tgp, ASUS_WMI_DEVID_DGPU_BASE_TGP);
+WMI_ATTR_SIMPLE_RW(dgpu_tgp, 0, NVIDIA_GPU_POWER_MAX, ASUS_WMI_DEVID_DGPU_SET_TGP);

static ssize_t panel_fhd_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
@@ -4204,6 +4207,8 @@ static struct attribute *platform_attributes[] = {
&dev_attr_boot_sound.attr,
&dev_attr_panel_od.attr,
&dev_attr_panel_fhd.attr,
+ &dev_attr_dgpu_base_tgp.attr,
+ &dev_attr_dgpu_tgp.attr,
&dev_attr_cores_enabled.attr,
&dev_attr_cores_max.attr,
&dev_attr_apu_mem.attr,
@@ -4280,6 +4285,10 @@ static umode_t asus_sysfs_is_visible(struct kobject *kobj,
devid = ASUS_WMI_DEVID_PANEL_OD;
else if (attr == &dev_attr_panel_fhd.attr)
devid = ASUS_WMI_DEVID_PANEL_FHD;
+ else if (attr == &dev_attr_dgpu_base_tgp.attr)
+ devid = ASUS_WMI_DEVID_DGPU_BASE_TGP;
+ else if (attr == &dev_attr_dgpu_tgp.attr)
+ ok = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU_SET_TGP);
else if (attr == &dev_attr_cores_enabled.attr
|| attr == &dev_attr_cores_max.attr)
ok = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CORES_SET);
diff --git a/include/linux/platform_data/x86/asus-wmi.h b/include/linux/platform_data/x86/asus-wmi.h
index efe608861e55..71833bd60f27 100644
--- a/include/linux/platform_data/x86/asus-wmi.h
+++ b/include/linux/platform_data/x86/asus-wmi.h
@@ -135,6 +135,9 @@
/* dgpu on/off */
#define ASUS_WMI_DEVID_DGPU 0x00090020

+#define ASUS_WMI_DEVID_DGPU_BASE_TGP 0x00120099
+#define ASUS_WMI_DEVID_DGPU_SET_TGP 0x00120098
+
/* gpu mux switch, 0 = dGPU, 1 = Optimus */
#define ASUS_WMI_DEVID_GPU_MUX 0x00090016
#define ASUS_WMI_DEVID_GPU_MUX_VIVO 0x00090026
--
2.45.1