Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 00/10] drm/xe/hwmon: Add fan control support
@ 2026-11-28  5:59 Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
                   ` (10 more replies)
  0 siblings, 11 replies; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

This series adds Intel Xe fan control through the hwmon sysfs ABI: fan
maximum speed, direct PWM control, mode selection and programmable fan
curves. Supported platforms are BMG in this series.

Each fan has a stock table read from firmware and a user table cached in
the driver and written back through PCODE.

Series overview

 01 Initialize fan-control backend and table cache
    Read the fan enable mask, max RPS, minimum PWM and stock fan tables.
 02 Expose fanN_max
    Maximum fan speed in RPM.
 03 Add ordered write batch helper
    PCODE helper that writes a fan table in order.
 04 Expose pwm[1-3]
    Constant duty cycle in the hwmon 0..255 range.
 05 Expose pwm[1-3]_enable
    0 = full speed, 1 = manual user table, 2 = firmware automatic.
 06 Enable fan curve control support
    pwm[1-3]_auto_point[1-10]_{temp,pwm}.
 07 Restore fan user tables after resume
 08 Refresh fan data after late binding
 09 Expose tjmax_fan_control_enable device sysfs
 10 Add kernel-doc for fan control

The ABI is documented in
Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon.

Usage

Read fan speed and max RPM:
  cat /sys/class/hwmon/hwmonX/fan1_input
  cat /sys/class/hwmon/hwmonX/fan1_max

Manual mode with a fixed PWM:
  echo 1 > /sys/class/hwmon/hwmonX/pwm1_enable
  echo 120 > /sys/class/hwmon/hwmonX/pwm1

Back to firmware automatic control:
  echo 2 > /sys/class/hwmon/hwmonX/pwm1_enable

Program a 3-point fan curve:
  echo 1 > /sys/class/hwmon/hwmonX/pwm1_enable
  echo 30000 > /sys/class/hwmon/hwmonX/pwm1_auto_point1_temp
  echo 50 > /sys/class/hwmon/hwmonX/pwm1_auto_point1_pwm
  echo 60000 > /sys/class/hwmon/hwmonX/pwm1_auto_point2_temp
  echo 120 > /sys/class/hwmon/hwmonX/pwm1_auto_point2_pwm
  echo 85000 > /sys/class/hwmon/hwmonX/pwm1_auto_point3_temp
  echo 200 > /sys/class/hwmon/hwmonX/pwm1_auto_point3_pwm
  echo 0 > /sys/class/hwmon/hwmonX/pwm1_auto_point4_temp
  echo 0 > /sys/class/hwmon/hwmonX/pwm1_auto_point4_pwm

Notes
 - Package temperature (temp2_input) is the fan table temperature
   reference.
 - pwmN and curve point writes need pwmN_enable = 1.
 - A curve has 2 to 10 points. Temperature and PWM must not decrease
   from one point to the next.
 - Curve writes are staged and committed once point K+1 is (0, 0) or
   all 10 points are set.
 - PWM values below the fan's minimum duty cycle are raised to it.

v2:
 - Add kernel-doc for fan control.
 - Save/restore the user fan table across suspend/resume.
 - Refresh fan info after late binding; keep the user's PWM mode.
 - Show stock table points in the curve attributes when pwmN_enable = 2.

v3:
 - Address v2 review comments.
 - Add an ordered PCODE write batch helper.
 - Support a variable number of fan curve points.
 - Add missing failure checks.
 - Use only 2 fan curve points when setting pwmN.

v4:
 - Address v3 https://patchwork.freedesktop.org/series/176241/ review
   comments. (Soham)
 - Clamp oversized fan masks and stock tables; continue when one fan's
   stock table fails to read.
 - Clear cached values on failed pcode reads.
 - Use percent for fan speed and clamp min PWM to 100%.
 - Fail pwmN writes if pcode reports the wrong user point count.
 - Create fan curve attributes at probe time; add the (0, 0) end
   marker; points must not decrease.
 - Document pwmN_enable values and the curve rules.
 - Call the system PM hooks from xe_device_suspend()/xe_device_resume(),
   which also covers FLR; check d3cold only for runtime PM.
 - Fix the late binding firmware ID check; start fans enabled after
   late binding in automatic mode.
 - Rebase on drm-xe-next; update the ABI Date and KernelVersion.

v5:
 - Address review comments on v4 patch
   https://patchwork.freedesktop.org/series/179435/ from sashiko.
 - Add new sysfs tjmax_fan_control_enable for enable/disable of
   tjmax fan control.
 - Move CRI fan control support out of this patch series.
 - Add kgdoc patch at the end of the patch series.

Karthik Poosa (10):
  drm/xe/hwmon: Initialize fan-control backend and table cache
  drm/xe/hwmon: Expose fanN_max
  drm/xe/pcode: Add ordered write batch helper
  drm/xe/hwmon: Expose pwm[1-3]
  drm/xe/hwmon: Expose pwm[1-3]_enable
  drm/xe/hwmon: Enable fan curve control support
  drm/xe/hwmon: Restore fan user tables after resume
  drm/xe/hwmon: Refresh fan data after late binding
  drm/xe: Expose tjmax_fan_control_enable device sysfs
  drm/xe/hwmon: Add kernel-doc for fan control

 .../ABI/testing/sysfs-driver-intel-xe-gpu     |   15 +
 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |   32 +
 drivers/gpu/drm/xe/regs/xe_pmt.h              |    5 +
 drivers/gpu/drm/xe/xe_device.c                |    3 +
 drivers/gpu/drm/xe/xe_device_sysfs.c          |   76 +
 drivers/gpu/drm/xe/xe_hwmon.c                 | 1322 ++++++++++++++++-
 drivers/gpu/drm/xe/xe_hwmon.h                 |    6 +
 drivers/gpu/drm/xe/xe_late_bind_fw.c          |   10 +
 drivers/gpu/drm/xe/xe_pcode.c                 |   39 +
 drivers/gpu/drm/xe/xe_pcode.h                 |   12 +
 drivers/gpu/drm/xe/xe_pcode_api.h             |   10 +
 drivers/gpu/drm/xe/xe_pm.c                    |   11 +-
 12 files changed, 1512 insertions(+), 29 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v5 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 02/10] drm/xe/hwmon: Expose fanN_max Karthik Poosa
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Initialize Xe hwmon fan-control support by detecting enabled fans,
reading stock fan control points and min PWM.

FSC_READ_NUM_FANS returns a bitmask of enabled fans, so use it as a mask
when exposing fanN_input.

v2:
 - Avoid user table initialization during probe. (Badal)
 - Move unused code to appropriate patches.
 - Use xe helpers for dmesg logs.

v3:
 - Rebase.
 - Refine a few debug log messages and comments.
 - Update xe_hwmon_read_fan_control_info() to return on any stock table
   read failures.
 - Avoid returning when num_fans is 0 in
   xe_hwmon_get_preregistration_info(), ensuring thermal mailbox
   initialization is not skipped.
 - Remove XE_PCODE_FAN_CONTROL_TIMEOUT_MS which is unnecessary as default
   timeout itself is now 10ms.
 - Use xe_pcode_read() instead of xe_pcode_read_timeout().
 - Rename fan_control_point_count to point_count in struct fan_table.
 - Resolve kernel-doc warnings.
 - Add valid check for stock table fan point count.
 - Use the fan-enable bitmap reported by FSC_READ_NUM_FANS when enumerating
   fans. This avoids assuming contiguous fan indices and allows masks with
   gaps to be represented correctly in the hwmon interface.

v4:
 - Clamp unsupported fan masks and oversized stock tables instead of
   failing initialization. (Soham)
 - On stock table read failure, mark that fan's table empty and continue
   with the remaining fans; fan_input stays visible. (Soham)
 - Clear fan enable mask and min PWM on pcode read failure. (Soham)
 - Fix FCP_INDEX() type safety and clean up macro usage. (Soham)
 - Use xe_hwmon_pcode_read_fan_control() for fan mailbox reads. (Soham)
 - Fix kernel-doc for fan_table_type and nested fan_table members.
 - Skip min PWM read for fans without a stock table.
 - Make xe_hwmon_get_fan_enable_mask() return void.

v5:
 - Remove min_pwm as it is no longer supported by the hardware.
 - Change xe_hwmon_get_fan_enable_mask() return type from void to int.
 - Copy the stock table to the fan info only if all points are read
   successfully.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_hwmon.c     | 162 +++++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_pcode_api.h |   4 +
 2 files changed, 151 insertions(+), 15 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 717845b7e6a1..503f18625338 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -22,6 +22,7 @@
 #include "xe_pcode_api.h"
 #include "xe_sriov.h"
 #include "xe_pm.h"
+#include "xe_printk.h"
 #include "xe_vsec.h"
 #include "regs/xe_pmt.h"
 
@@ -139,13 +140,51 @@ struct xe_hwmon_energy_info {
 };
 
 /**
- * struct xe_hwmon_fan_info - to cache previous fan reading
+ * enum fan_table_type - fan table types tracked per fan
+ */
+enum fan_table_type {
+	/** @STOCK_FAN_TABLE: firmware-provided fan table */
+	STOCK_FAN_TABLE,
+	/** @USER_FAN_TABLE: user fan table written through sysfs */
+	USER_FAN_TABLE,
+	/** @FAN_TABLE_MAX: number of fan tables tracked per fan */
+	FAN_TABLE_MAX,
+};
+
+/* Check if a fan is enabled based on the fan_enable_mask got from FSC_READ_NUM_FANS. */
+#define IS_FAN_ENABLED(hwmon, fan) ((hwmon)->fan_enable_mask & BIT(fan))
+
+/* Maximum number of fan control points supported by each fan. */
+#define MAX_FAN_CONTROL_POINTS	10
+
+/* Fan control point index: bit31 selects fan table type; bits30:0 control point index. */
+#define FCP_INDEX(table_type, point_num) (((u32)(table_type) << 31) | \
+						((u32)(point_num) & GENMASK(30, 0)))
+
+/**
+ * struct xe_hwmon_fan_info - cached fan telemetry and control state
+ *
+ * Each fan keeps the latest tachometer sampling state along with two fan
+ * tables: the stock table discovered from firmware and the user table managed
+ * by hwmon sysfs writes.
  */
 struct xe_hwmon_fan_info {
 	/** @reg_val_prev: previous fan reg val */
 	u32 reg_val_prev;
 	/** @time_prev: previous timestamp */
 	u64 time_prev;
+	/** @fan_table: fan control tables */
+	struct fan_table {
+		/** @fan_table.point_count: number of fan control points */
+		u8 point_count;
+		/** @fan_table.fcp: array of fan control points */
+		struct fan_control_point {
+			/** @fan_table.fcp.temp: temperature in degree celsius */
+			u8 temp;
+			/** @fan_table.fcp.speed: fan speed in percentage */
+			u8 speed;
+		} fcp[MAX_FAN_CONTROL_POINTS];
+	} fan_table[FAN_TABLE_MAX];
 };
 
 /**
@@ -194,6 +233,8 @@ struct xe_hwmon {
 	int scl_shift_time;
 	/** @ei: Energy info for energyN_input */
 	struct xe_hwmon_energy_info ei[CHANNEL_MAX];
+	/** @fan_enable_mask: bitmask of available fans */
+	u32 fan_enable_mask;
 	/** @fi: Fan info for fanN_input */
 	struct xe_hwmon_fan_info fi[FAN_MAX];
 	/** @read_power_limit_from_pcode: pcode accepted power limits write for the channel */
@@ -1084,17 +1125,106 @@ static int xe_hwmon_pcode_write_i1(const struct xe_hwmon *hwmon, u32 uval)
 			      (uval & POWER_SETUP_I1_DATA_MASK));
 }
 
-static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, u32 subcmd, u32 *uval)
+static int xe_hwmon_pcode_read_fan_control(const struct xe_hwmon *hwmon, u32 subcmd, u8 fan_num,
+					   u32 *uval)
 {
 	struct xe_tile *root_tile = xe_device_get_root_tile(hwmon->xe);
 
+	return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, fan_num),
+				     uval, NULL);
+}
+
+static int xe_hwmon_get_fan_enable_mask(struct xe_hwmon *hwmon)
+{
+	int ret;
+
 	/* Platforms that don't return correct value */
-	if (hwmon->xe->info.platform == XE_DG2 && subcmd == FSC_READ_NUM_FANS) {
-		*uval = 2;
+	if (hwmon->xe->info.platform == XE_DG2) {
+		hwmon->fan_enable_mask = GENMASK(1, 0);
 		return 0;
 	}
 
-	return xe_pcode_read(root_tile, PCODE_MBOX(FAN_SPEED_CONTROL, subcmd, 0), uval, NULL);
+	ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, 0,
+					      &hwmon->fan_enable_mask);
+	if (ret) {
+		hwmon->fan_enable_mask = 0;
+		xe_warn(hwmon->xe, "failed to read number of fans, ret=%d\n", ret);
+		return ret;
+	}
+
+	/* Check if fan mask exceeds FAN_MAX and log a warning if it does */
+	if (hwmon->fan_enable_mask & ~GENMASK(FAN_MAX - 1, 0)) {
+		xe_warn(hwmon->xe, "Fan enable mask exceeds %d supported fans, clamping\n",
+			FAN_MAX);
+		hwmon->fan_enable_mask &= GENMASK(FAN_MAX - 1, 0);
+	}
+
+	xe_info(hwmon->xe, "Number of fans detected: %u\n", hweight32(hwmon->fan_enable_mask));
+
+	return 0;
+}
+
+static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
+{
+	int err = 0;
+	int point;
+	int fan;
+	int ret;
+
+	for (fan = 0; fan < FAN_MAX; fan++) {
+		struct fan_control_point stock_fcp[MAX_FAN_CONTROL_POINTS];
+		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+		u32 stock_fcp_count = 0;
+
+		if (!IS_FAN_ENABLED(hwmon, fan))
+			continue;
+
+		ret = xe_hwmon_pcode_read_fan_control(hwmon,
+						      FSC_READ_STOCK_FAN_CONTROL_POINTS,
+						      fan, &stock_fcp_count);
+		if (ret || !stock_fcp_count) {
+			xe_warn(hwmon->xe,
+				"fan %d stock table unavailable, ret=%d, point count=%u\n",
+				fan, ret, ret ? 0 : stock_fcp_count);
+			err = ret ?: -ENODATA;
+			continue;
+		}
+
+		if (stock_fcp_count > MAX_FAN_CONTROL_POINTS) {
+			xe_warn(hwmon->xe,
+				"fan %d stock table point count %u exceeds maximum %u, clamping\n",
+				fan, stock_fcp_count, MAX_FAN_CONTROL_POINTS);
+			stock_fcp_count = MAX_FAN_CONTROL_POINTS;
+		}
+
+		xe_dbg(hwmon->xe, "fan %d stock table point count is %u\n", fan, stock_fcp_count);
+
+		/* Cache the stock fan table for later use. */
+		for (point = 0; point < stock_fcp_count; point++) {
+			u32 fcp = FCP_INDEX(STOCK_FAN_TABLE, point);
+
+			ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_FAN_TABLE, fan, &fcp);
+			if (ret) {
+				xe_warn(hwmon->xe, "failed to read fan %d stock point %d, ret=%d\n",
+					fan, point, ret);
+				err = ret;
+				break;
+			}
+
+			stock_fcp[point].temp =	REG_FIELD_GET(FAN_CONTROL_POINT_TEMP_MASK, fcp);
+			stock_fcp[point].speed = REG_FIELD_GET(FAN_CONTROL_POINT_SPEED_MASK, fcp);
+			xe_dbg(hwmon->xe, "fan %d stock point %d: temp %u C, speed %u %%\n",
+			       fan, point, stock_fcp[point].temp, stock_fcp[point].speed);
+		}
+
+		if (point < stock_fcp_count)
+			continue;
+
+		memcpy(fi->fan_table[STOCK_FAN_TABLE].fcp, stock_fcp,
+		       stock_fcp_count * sizeof(stock_fcp[0]));
+		fi->fan_table[STOCK_FAN_TABLE].point_count = stock_fcp_count;
+	}
+	return err;
 }
 
 static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, int channel,
@@ -1632,17 +1762,12 @@ xe_hwmon_energy_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 static umode_t
 xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 {
-	u32 uval = 0;
-
 	if (!hwmon->xe->info.has_fan_control)
 		return 0;
 
 	switch (attr) {
 	case hwmon_fan_input:
-		if (xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_NUM_FANS, &uval))
-			return 0;
-
-		return channel < uval ? 0444 : 0;
+		return IS_FAN_ENABLED(hwmon, channel) ? 0444 : 0;
 	default:
 		return 0;
 	}
@@ -1876,11 +2001,18 @@ xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon)
 		if (xe_hwmon_is_visible(hwmon, hwmon_energy, hwmon_energy_input, channel))
 			xe_hwmon_energy_get(hwmon, channel, &energy);
 
-	/* Initialize 'struct xe_hwmon_fan_info' with initial fan register reading. */
-	for (channel = 0; channel < FAN_MAX; channel++)
-		if (xe_hwmon_is_visible(hwmon, hwmon_fan, hwmon_fan_input, channel))
-			xe_hwmon_fan_input_read(hwmon, channel, &fan_speed);
+	if (hwmon->xe->info.has_fan_control) {
+		xe_hwmon_get_fan_enable_mask(hwmon);
+
+		/* Initialize 'struct xe_hwmon_fan_info' with initial fan register reading. */
+		for (channel = 0; channel < FAN_MAX; channel++)
+			if (IS_FAN_ENABLED(hwmon, channel))
+				xe_hwmon_fan_input_read(hwmon, channel, &fan_speed);
 
+		/* Fan control tables initialization */
+		if (xe_hwmon_read_fan_control_info(hwmon))
+			xe_warn(hwmon->xe, "Fan control tables are not available\n");
+	}
 	return xe_hwmon_init_temp_info(hwmon);
 }
 
diff --git a/drivers/gpu/drm/xe/xe_pcode_api.h b/drivers/gpu/drm/xe/xe_pcode_api.h
index f05846931059..387885760205 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -87,6 +87,10 @@
 
 #define   FAN_SPEED_CONTROL			0x7D
 #define     FSC_READ_NUM_FANS			0x4
+#define     FSC_READ_STOCK_FAN_CONTROL_POINTS	0x5
+#define     FSC_READ_FAN_TABLE			0x7
+#define     FAN_CONTROL_POINT_TEMP_MASK		REG_GENMASK(7, 0)
+#define     FAN_CONTROL_POINT_SPEED_MASK	REG_GENMASK(15, 8)
 
 #define PCODE_SCRATCH(x)		XE_REG(0x138320 + ((x) * 4))
 /* PCODE_SCRATCH0 */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 02/10] drm/xe/hwmon: Expose fanN_max
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Expose fan maximum RPM via fanN_max.

The value is derived from the firmware-reported maximum fan RPS and
converted to RPM in the fan hwmon read path.

Update Xe hwmon ABI documentation for fanN_max.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  8 ++++++++
 drivers/gpu/drm/xe/xe_hwmon.c                 | 20 ++++++++++++++++++-
 drivers/gpu/drm/xe/xe_pcode_api.h             |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index 2bb78a503f46..9aa1cfac4f87 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
@@ -266,6 +266,14 @@ Description:	RO. Fan 3 speed in RPM.
 
 		Only supported for particular Intel Xe graphics platforms.
 
+What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/fan[1-3]_max
+Date:		November 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:	RO. Maximum fan speed in RPM for each fan channel.
+
+		Only supported for particular Intel Xe graphics platforms.
+
 What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/power1_cap
 Date:		May 2025
 KernelVersion:	6.15
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 503f18625338..5b7f3d67a70e 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -185,6 +185,8 @@ struct xe_hwmon_fan_info {
 			u8 speed;
 		} fcp[MAX_FAN_CONTROL_POINTS];
 	} fan_table[FAN_TABLE_MAX];
+	/** @max_rps: maximum fan RPS */
+	u32 max_rps;
 };
 
 /**
@@ -919,7 +921,8 @@ static const struct hwmon_channel_info * const hwmon_info[] = {
 	HWMON_CHANNEL_INFO(curr, HWMON_C_LABEL, HWMON_C_CRIT | HWMON_C_LABEL),
 	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL, HWMON_I_INPUT | HWMON_I_LABEL),
 	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL),
-	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_MAX, HWMON_F_INPUT | HWMON_F_MAX,
+			   HWMON_F_INPUT | HWMON_F_MAX),
 	NULL
 };
 
@@ -1171,6 +1174,7 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 	int fan;
 	int ret;
 
+	/* Stock table failures leave fan_input and fan_max visible; they don't need it. */
 	for (fan = 0; fan < FAN_MAX; fan++) {
 		struct fan_control_point stock_fcp[MAX_FAN_CONTROL_POINTS];
 		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
@@ -1179,6 +1183,15 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 		if (!IS_FAN_ENABLED(hwmon, fan))
 			continue;
 
+		ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_MAX_FAN_RPS, fan,
+						      &fi->max_rps);
+		if (ret) {
+			fi->max_rps = 0;
+			xe_warn(hwmon->xe, "failed to read fan %d max RPS, ret=%d\n", fan, ret);
+		} else {
+			xe_dbg(hwmon->xe, "fan %d max RPS %u\n", fan, fi->max_rps);
+		}
+
 		ret = xe_hwmon_pcode_read_fan_control(hwmon,
 						      FSC_READ_STOCK_FAN_CONTROL_POINTS,
 						      fan, &stock_fcp_count);
@@ -1768,6 +1781,8 @@ xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 	switch (attr) {
 	case hwmon_fan_input:
 		return IS_FAN_ENABLED(hwmon, channel) ? 0444 : 0;
+	case hwmon_fan_max:
+		return IS_FAN_ENABLED(hwmon, channel) && hwmon->fi[channel].max_rps ? 0444 : 0;
 	default:
 		return 0;
 	}
@@ -1818,6 +1833,9 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	switch (attr) {
 	case hwmon_fan_input:
 		return xe_hwmon_fan_input_read(hwmon, channel, val);
+	case hwmon_fan_max:
+		*val = mul_u32_u32(hwmon->fi[channel].max_rps, 60);
+		return 0;
 	default:
 		return -EOPNOTSUPP;
 	}
diff --git a/drivers/gpu/drm/xe/xe_pcode_api.h b/drivers/gpu/drm/xe/xe_pcode_api.h
index 387885760205..2308e5464082 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -86,6 +86,7 @@
 #define     MEMORY_CONFIG_SUBCOMMAND_READ_MAX_FREQUENCY 0x1
 
 #define   FAN_SPEED_CONTROL			0x7D
+#define     FSC_READ_MAX_FAN_RPS		0x3
 #define     FSC_READ_NUM_FANS			0x4
 #define     FSC_READ_STOCK_FAN_CONTROL_POINTS	0x5
 #define     FSC_READ_FAN_TABLE			0x7
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 02/10] drm/xe/hwmon: Expose fanN_max Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:04   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 04/10] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Add a pcode helper that issues an ordered sequence of mailbox writes
while holding the pcode lock, so no other mailbox command can be
interleaved.

This will be used to write fan tables atomically with respect to other
pcode users.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_pcode.c | 39 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode.h | 12 +++++++++++
 2 files changed, 51 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_pcode.c b/drivers/gpu/drm/xe/xe_pcode.c
index fa9f45d565cb..409b9ff372e9 100644
--- a/drivers/gpu/drm/xe/xe_pcode.c
+++ b/drivers/gpu/drm/xe/xe_pcode.c
@@ -124,6 +124,45 @@ int xe_pcode_write_timeout(struct xe_tile *tile, u32 mbox, u32 data, int timeout
 	return err;
 }
 
+/**
+ * xe_pcode_write_batch - issue pcode writes in order under the mailbox lock
+ * @tile: tile
+ * @writes: mailbox/data pairs to write
+ * @count: number of entries in @writes
+ *
+ * Holds the pcode lock across all writes so no other mailbox command can be
+ * interleaved; the lock may be held for up to @count * PCODE_DEFAULT_TIMEOUT_MS.
+ * Stops at the first failure; earlier writes are not reverted.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int xe_pcode_write_batch(struct xe_tile *tile, const struct xe_pcode_write *writes,
+			 unsigned int count)
+{
+	unsigned int i;
+	int err = 0;
+
+	if (!count)
+		return -EINVAL;
+
+	mutex_lock(&tile->pcode.lock);
+	for (i = 0; i < count; i++) {
+		u32 data = writes[i].data;
+
+		err = pcode_mailbox_rw(tile, writes[i].mbox, &data, NULL, PCODE_DEFAULT_TIMEOUT_MS,
+				       false, false);
+		if (err) {
+			xe_err(tile_to_xe(tile),
+			       "pcode batch write %u/%u failed: mbox 0x%08x data 0x%08x, ret %pe\n",
+			       i + 1, count, writes[i].mbox, writes[i].data, ERR_PTR(err));
+			break;
+		}
+	}
+	mutex_unlock(&tile->pcode.lock);
+
+	return err;
+}
+
 int xe_pcode_write64_timeout(struct xe_tile *tile, u32 mbox, u32 data0, u32 data1, int timeout)
 {
 	int err;
diff --git a/drivers/gpu/drm/xe/xe_pcode.h b/drivers/gpu/drm/xe/xe_pcode.h
index 7e43792b0037..c601a12baba6 100644
--- a/drivers/gpu/drm/xe/xe_pcode.h
+++ b/drivers/gpu/drm/xe/xe_pcode.h
@@ -18,6 +18,16 @@ struct xe_pcode_version {
 	u32 engg;
 };
 
+/**
+ * struct xe_pcode_write - single pcode mailbox write
+ * @mbox: mailbox command
+ * @data: value written to PCODE_DATA0
+ */
+struct xe_pcode_write {
+	u32 mbox;
+	u32 data;
+};
+
 #define PCODE_DEFAULT_TIMEOUT_MS	10
 
 int xe_pcode_init_early(struct xe_tile *tile);
@@ -28,6 +38,8 @@ int xe_pcode_init_min_freq_table(struct xe_tile *tile, u32 min_gt_freq,
 int xe_pcode_read(struct xe_tile *tile, u32 mbox, u32 *val0, u32 *val1);
 int xe_pcode_write_timeout(struct xe_tile *tile, u32 mbox, u32 val,
 			   int timeout_ms);
+int xe_pcode_write_batch(struct xe_tile *tile, const struct xe_pcode_write *writes,
+			 unsigned int count);
 int xe_pcode_write64_timeout(struct xe_tile *tile, u32 mbox, u32 data0,
 			     u32 data1, int timeout);
 int xe_get_pcode_version(struct xe_device *xe, struct xe_pcode_version *version);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 04/10] drm/xe/hwmon: Expose pwm[1-3]
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (2 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  5:59 ` [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Expose pwm[1-3] hwmon attributes for each available fan. Userspace can
read PWM values in the range 0..255 and set a manual fan speed
independently of the fan curve. Switching back to the stock curve is
added by a later patch.

Read active PWM from PMT on BMG. On other platforms, derive PWM from the
stock curve when no user table is active.

After writing the user table, read back its point count to verify
firmware accepted it. If verification fails, restore stock fan control.

Document pwm[1-3] in the Xe hwmon ABI.

v2:
 - Use xe helpers for dmesg logs.
 - Avoid using hwmon->num_fans to get fan count in
   xe_hwmon_pwm_is_visible().

v3:
 - Set PWM directly through the user table; avoid initializing it from the
   stock table. (Soham)
 - Move the unused xe_hwmon_activate_user_fan_table() to the subsequent
   patch.
 - Fail pwmN reads when no value has been written, rather than activating
   the user table from the stock table.
 - Define MIN_FAN_CONTROL_POINTS as 2.
 - Use MIN_FAN_CONTROL_POINTS for PWM configuration. (Soham)
 - Use batched writes for configuring fan control points when setting pwmN.
 - Add and rephrase couple debug logs.
 - Update the hwmon documentation.

v4:
 - Use tab indentation in ABI doc and MIN_FAN_CONTROL_POINTS. (Soham)
 - Drop unused linux/delay.h include. (Soham)
 - Clamp min PWM to 100% since fan table speed is in percent. (Soham)
 - Log both the requested pwm and the applied percentage. (Soham)
 - Fail the pwm write if pcode reports a user point count other than
   MIN_FAN_CONTROL_POINTS after the write. (Soham)
 - Drop redundant channel range checks in pwm read/write. (Soham)
 - Fix misleading comments and debug log alignment. (Soham)
 - Note that the 0..255 to percent round trip is lossy. (Soham)
 - Update ABI Date and KernelVersion to November 2026 and 7.5.
 - Document that pwm values below the fan minimum are raised to it.
 - Return -ENODATA and log at debug level when pwm has not been set.
 - Invalidate the cached user table when the pwm write or verify fails.
 - Clear point count on pcode read failure.
 - Drop unneeded parentheses in MIN_FAN_CONTROL_POINTS and FIXED_PWM_TEMP.
 - Document pcode user table write ordering.

v5:
 - Expose pwm[1-3] node only if stock table is valid.
 - Read current pwm from PMT for BMG and from stock table for other
   platforms when user table is not active.
 - Read back the user point count after every PWM table write and fall
   back to stock table when the count is not accepted.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |   9 +
 drivers/gpu/drm/xe/regs/xe_pmt.h              |   5 +
 drivers/gpu/drm/xe/xe_hwmon.c                 | 312 ++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode_api.h             |   3 +
 4 files changed, 329 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index 9aa1cfac4f87..24e9eb595746 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
@@ -346,3 +346,12 @@ Description:	RW. Package burst power limit interval (Tau in PL2/Tau) in
 		milliseconds over which sustained power is averaged.
 
 		Only supported for particular Intel Xe graphics platforms.
+
+What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/pwm[1-3]
+Date:		November 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:	RW. Target fan PWM duty value in the range 0..255, where 255
+		is 100% fan speed.
+
+		Only supported for particular Intel Xe graphics platforms.
diff --git a/drivers/gpu/drm/xe/regs/xe_pmt.h b/drivers/gpu/drm/xe/regs/xe_pmt.h
index fb5b5a8648ba..4fec057b2dd6 100644
--- a/drivers/gpu/drm/xe/regs/xe_pmt.h
+++ b/drivers/gpu/drm/xe/regs/xe_pmt.h
@@ -15,6 +15,11 @@
 #define	ENERGY_PKG			REG_GENMASK64(31, 0)
 #define	ENERGY_CARD			REG_GENMASK64(63, 32)
 
+#define BMG_FAN_PWM_PMT_OFFSET		(0x58)
+#define BMG_FAN1_PWM			REG_GENMASK64(15, 0)
+#define BMG_FAN2_PWM			REG_GENMASK64(31, 16)
+#define BMG_FAN3_PWM			REG_GENMASK64(47, 32)
+
 #define PUNIT_VERSION_OFFSET		0xA0
 
 #define XE_PMT_BASE_OFFSET		0xE0000
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 5b7f3d67a70e..1e375613fe18 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -157,6 +157,9 @@ enum fan_table_type {
 /* Maximum number of fan control points supported by each fan. */
 #define MAX_FAN_CONTROL_POINTS	10
 
+/* Minimum number of points required for a user fan table. */
+#define MIN_FAN_CONTROL_POINTS	2
+
 /* Fan control point index: bit31 selects fan table type; bits30:0 control point index. */
 #define FCP_INDEX(table_type, point_num) (((u32)(table_type) << 31) | \
 						((u32)(point_num) & GENMASK(30, 0)))
@@ -923,6 +926,7 @@ static const struct hwmon_channel_info * const hwmon_info[] = {
 	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL),
 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_MAX, HWMON_F_INPUT | HWMON_F_MAX,
 			   HWMON_F_INPUT | HWMON_F_MAX),
+	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT, HWMON_PWM_INPUT, HWMON_PWM_INPUT),
 	NULL
 };
 
@@ -1167,6 +1171,118 @@ static int xe_hwmon_get_fan_enable_mask(struct xe_hwmon *hwmon)
 	return 0;
 }
 
+static int xe_hwmon_get_fan_point_count(struct xe_hwmon *hwmon, u8 fan, u32 *point_count,
+					enum fan_table_type table_type)
+{
+	int ret;
+
+	ret = xe_hwmon_pcode_read_fan_control(hwmon,
+					      (table_type == USER_FAN_TABLE) ?
+					      FSC_READ_USER_FAN_CONTROL_POINTS :
+					      FSC_READ_STOCK_FAN_CONTROL_POINTS,
+					      fan, point_count);
+	if (ret) {
+		*point_count = 0;
+		xe_err(hwmon->xe, "failed to read fan %d %s point count, ret=%d\n", fan,
+		       (table_type == USER_FAN_TABLE) ? "user" : "stock", ret);
+		return ret;
+	}
+
+	if (*point_count > MAX_FAN_CONTROL_POINTS) {
+		xe_err(hwmon->xe, "fan %d %s point count %u exceeds max %u\n", fan,
+		       (table_type == USER_FAN_TABLE) ? "user" : "stock", *point_count,
+		       MAX_FAN_CONTROL_POINTS);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/*
+ * Set the fan to automatic mode by clearing the user fan table.
+ * This will make the firmware run the stock fan table.
+ */
+static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
+{
+	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+	int ret;
+
+	ret = xe_pcode_write(xe_device_get_root_tile(hwmon->xe),
+			     PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, fan),
+			     0);
+	if (ret)
+		return ret;
+
+	fi->fan_table[USER_FAN_TABLE].point_count = 0;
+
+	return 0;
+}
+
+/* Temperature to set when setting the fan PWM with pwm[1-3]. */
+#define FIXED_PWM_TEMP	1
+
+static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
+{
+	struct xe_pcode_write writes[MIN_FAN_CONTROL_POINTS + 1];
+	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+	int ret, write_ret, verify_ret, i;
+	u32 point_count = 0;
+	u8 speed;
+
+	/* Fan table speed are in percent. */
+	speed = DIV_ROUND_CLOSEST(pwm * 100, U8_MAX);
+	speed = min_t(u32, speed, 100);
+
+	/* Fan control needs at least two points; program them all at the same speed. */
+	for (i = 0; i < MIN_FAN_CONTROL_POINTS; i++) {
+		writes[i].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABLE, fan);
+		writes[i].data = FIELD_PREP(FAN_CONTROL_POINT_TEMP_MASK, FIXED_PWM_TEMP + i) |
+				 FIELD_PREP(FAN_CONTROL_POINT_SPEED_MASK, speed);
+		xe_dbg(hwmon->xe, "prepared fan control point %d with temp=%d, speed=%u%%\n", i,
+		       FIXED_PWM_TEMP + i, speed);
+	}
+
+	/* pcode fills the user table from point 0 in write order; the count write commits it. */
+	writes[i].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, fan);
+	writes[i].data = MIN_FAN_CONTROL_POINTS;
+	write_ret = xe_pcode_write_batch(xe_device_get_root_tile(hwmon->xe), writes,
+					 ARRAY_SIZE(writes));
+	if (write_ret)
+		xe_err(hwmon->xe, "failed to write pwm %u (%u%%) for fan %d, ret=%d\n",
+		       pwm, speed, fan, write_ret);
+	else
+		xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan);
+
+	verify_ret = xe_hwmon_get_fan_point_count(hwmon, fan, &point_count, USER_FAN_TABLE);
+	if (write_ret || verify_ret || point_count != MIN_FAN_CONTROL_POINTS) {
+		int failure_ret = write_ret ?: verify_ret ?: -EIO;
+
+		/* Firmware may have accepted only part of the user table. */
+		fi->fan_table[USER_FAN_TABLE].point_count = 0;
+		if (verify_ret || point_count != MIN_FAN_CONTROL_POINTS)
+			xe_err(hwmon->xe,
+			       "fan %d PWM table count verification failed: ret=%d, count=%u\n",
+			       fan, verify_ret, point_count);
+		ret = xe_hwmon_fan_set_auto(hwmon, fan);
+		if (ret)
+			xe_err(hwmon->xe, "failed to restore fan %d to auto mode  ret=%d\n",
+			       fan, ret);
+		return failure_ret;
+	}
+
+	/* Update the user fan table cache. */
+	for (i = 0; i < MIN_FAN_CONTROL_POINTS; i++) {
+		fi->fan_table[USER_FAN_TABLE].fcp[i].temp = FIXED_PWM_TEMP + i;
+		fi->fan_table[USER_FAN_TABLE].fcp[i].speed = speed;
+		xe_dbg(hwmon->xe, "fan %d user table cache point %d with temp=%d, speed=%u%%\n",
+		       fan, i, fi->fan_table[USER_FAN_TABLE].fcp[i].temp,
+		       fi->fan_table[USER_FAN_TABLE].fcp[i].speed);
+	}
+	fi->fan_table[USER_FAN_TABLE].point_count = MIN_FAN_CONTROL_POINTS;
+
+	return 0;
+}
+
 static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 {
 	int err = 0;
@@ -1788,6 +1904,21 @@ xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 	}
 }
 
+static umode_t
+xe_hwmon_pwm_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
+{
+	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) ||
+	    !hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count)
+		return 0;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		return 0644;
+	default:
+		return 0;
+	}
+}
+
 static int
 xe_hwmon_fan_input_read(struct xe_hwmon *hwmon, int channel, long *val)
 {
@@ -1841,6 +1972,180 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	}
 }
 
+/**
+ * xe_hwmon_get_stock_table_pwm - Calculate PWM from the stock fan curve
+ * @hwmon: hwmon instance, with hwmon_lock held and runtime PM active
+ * @channel: fan channel
+ * @val: calculated PWM percentage
+ *
+ * Read package temperature and linearly interpolate the stock table. Temperatures
+ * outside the table range use the speed at the nearest endpoint.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int xe_hwmon_get_stock_table_pwm(struct xe_hwmon *hwmon, int channel, long *val)
+{
+	const struct fan_table *table = &hwmon->fi[channel].fan_table[STOCK_FAN_TABLE];
+	long temperature, pwm;
+	int point, ret;
+
+	lockdep_assert_held(&hwmon->hwmon_lock);
+
+	if (!table->point_count)
+		return -ENODATA;
+
+	ret = xe_hwmon_temp_read(hwmon, hwmon_temp_input, CHANNEL_PKG, &temperature);
+	if (ret)
+		return ret;
+
+	pwm = table->fcp[0].speed;
+	for (point = 1; point < table->point_count; point++) {
+		const struct fan_control_point *lower = &table->fcp[point - 1];
+		const struct fan_control_point *upper = &table->fcp[point];
+		long lower_temp = lower->temp * MILLIDEGREE_PER_DEGREE;
+		long upper_temp = upper->temp * MILLIDEGREE_PER_DEGREE;
+		long pwm_delta = upper->speed - lower->speed;
+
+		/* Find the bracketing points; temperatures outside the curve use endpoints. */
+		if (temperature <= lower_temp)
+			break;
+		if (temperature < upper_temp) {
+			pwm += DIV_ROUND_CLOSEST(pwm_delta * (temperature - lower_temp),
+						upper_temp - lower_temp);
+			break;
+		}
+		pwm = upper->speed;
+	}
+
+	*val = pwm;
+	xe_dbg(hwmon->xe, "fan %d stock table active PWM value: %ld\n", channel, pwm);
+	return 0;
+}
+
+/**
+ * xe_hwmon_get_pwm_from_pmt - Read active fan PWM from PMT telemetry
+ * @hwmon: hwmon instance, with hwmon_lock held and runtime PM active
+ * @channel: fan channel
+ * @val: raw PWM percentage returned by PMT
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int xe_hwmon_get_pwm_from_pmt(struct xe_hwmon *hwmon, int channel, long *val)
+{
+	u64 pmt_val;
+	u32 guid, pwm;
+	int ret;
+
+	lockdep_assert_held(&hwmon->hwmon_lock);
+
+	if (hwmon->xe->info.platform != XE_BATTLEMAGE)
+		return -EOPNOTSUPP;
+
+	ret = xe_vsec_get_guid(hwmon->xe, &guid);
+	if (ret)
+		return ret;
+
+	ret = xe_pmt_telem_read(hwmon->xe->drm.dev, guid, &pmt_val,
+				BMG_FAN_PWM_PMT_OFFSET, sizeof(pmt_val));
+	if (ret != sizeof(pmt_val))
+		return ret < 0 ? ret : -EIO;
+
+	switch (channel) {
+	case FAN_1:
+		pwm = REG_FIELD_GET64(BMG_FAN1_PWM, pmt_val);
+		break;
+	case FAN_2:
+		pwm = REG_FIELD_GET64(BMG_FAN2_PWM, pmt_val);
+		break;
+	case FAN_3:
+		pwm = REG_FIELD_GET64(BMG_FAN3_PWM, pmt_val);
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	xe_dbg(hwmon->xe, "fan %d PMT active PWM value: %u\n", channel, pwm);
+
+	*val = pwm;
+	return 0;
+}
+
+static int
+xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
+{
+	struct xe_hwmon_fan_info *fi;
+	long pwm;
+	int ret = 0;
+
+	mutex_lock(&hwmon->hwmon_lock);
+
+	fi = &hwmon->fi[channel];
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		/* If the user fan table is not populated, get active stock pwm */
+		if (!fi->fan_table[USER_FAN_TABLE].point_count) {
+			xe_dbg(hwmon->xe, "user fan table not active for fan %d\n", channel);
+			switch (hwmon->xe->info.platform) {
+			case XE_BATTLEMAGE:
+				ret = xe_hwmon_get_pwm_from_pmt(hwmon, channel, &pwm);
+				break;
+			default:
+				ret = xe_hwmon_get_stock_table_pwm(hwmon, channel, &pwm);
+				break;
+			}
+			if (ret)
+				goto unlock;
+		} else {
+			/*
+			 * pwmN writes set every user-table point to the same speed,
+			 * so the first point gives the cached manual PWM percentage.
+			 */
+			pwm = fi->fan_table[USER_FAN_TABLE].fcp[0].speed;
+		}
+		if (pwm < 0 || pwm > 100) {
+			xe_err(hwmon->xe, "invalid PWM value %ld read for fan %d\n", pwm, channel);
+			ret = -ERANGE;
+			goto unlock;
+		}
+		/* Speed is cached in percent, so the 0..255 round trip may be off by a step. */
+		*val = DIV_ROUND_CLOSEST(pwm * U8_MAX, 100);
+		xe_dbg(hwmon->xe, "pwm read for fan %d: %ld\n", channel, *val);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+		break;
+	}
+
+unlock:
+	mutex_unlock(&hwmon->hwmon_lock);
+	return ret;
+}
+
+static int
+xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
+{
+	int ret = 0;
+
+	mutex_lock(&hwmon->hwmon_lock);
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val < 0 || val > U8_MAX) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+		break;
+	}
+	mutex_unlock(&hwmon->hwmon_lock);
+
+	return ret;
+}
+
 static umode_t
 xe_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
 		    u32 attr, int channel)
@@ -1867,6 +2172,9 @@ xe_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
 	case hwmon_fan:
 		ret = xe_hwmon_fan_is_visible(hwmon, attr, channel);
 		break;
+	case hwmon_pwm:
+		ret = xe_hwmon_pwm_is_visible(hwmon, attr, channel);
+		break;
 	default:
 		ret = 0;
 		break;
@@ -1896,6 +2204,8 @@ xe_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 		return xe_hwmon_energy_read(hwmon, attr, channel, val);
 	case hwmon_fan:
 		return xe_hwmon_fan_read(hwmon, attr, channel, val);
+	case hwmon_pwm:
+		return xe_hwmon_pwm_read(hwmon, attr, channel, val);
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -1914,6 +2224,8 @@ xe_hwmon_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 		return xe_hwmon_power_write(hwmon, attr, channel, val);
 	case hwmon_curr:
 		return xe_hwmon_curr_write(hwmon, attr, channel, val);
+	case hwmon_pwm:
+		return xe_hwmon_pwm_write(hwmon, attr, channel, val);
 	default:
 		return -EOPNOTSUPP;
 	}
diff --git a/drivers/gpu/drm/xe/xe_pcode_api.h b/drivers/gpu/drm/xe/xe_pcode_api.h
index 2308e5464082..bf321ed19251 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -86,9 +86,12 @@
 #define     MEMORY_CONFIG_SUBCOMMAND_READ_MAX_FREQUENCY 0x1
 
 #define   FAN_SPEED_CONTROL			0x7D
+#define     FSC_WRITE_NUM_FAN_CONTROL_POINTS	0x0
+#define     FSC_WRITE_FAN_TABLE			0x1
 #define     FSC_READ_MAX_FAN_RPS		0x3
 #define     FSC_READ_NUM_FANS			0x4
 #define     FSC_READ_STOCK_FAN_CONTROL_POINTS	0x5
+#define     FSC_READ_USER_FAN_CONTROL_POINTS	0x6
 #define     FSC_READ_FAN_TABLE			0x7
 #define     FAN_CONTROL_POINT_TEMP_MASK		REG_GENMASK(7, 0)
 #define     FAN_CONTROL_POINT_SPEED_MASK	REG_GENMASK(15, 8)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (3 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 04/10] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:13   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Expose pwm[1-3]_enable to control the fan operating mode through
the hwmon interface.

Mode 0 forces the fan to run at full speed.
Mode 1 enables user fan-table control.
Mode 2 restores firmware-controlled fan management.

Document pwm[1-3]_enable in the Xe hwmon ABI.

v2:
 - Use xe helpers for dmesg logs.
 - Update kernel version in Xe hwmon documentation.
 - Correct level of some dmesg logs.

v3:
 - Each pwm_enable mode now sets its own state; drop the redundant
   post-switch pwm_enable_mode/is_full_speed update. (Badal)
 - Fix misindented dead assignment in the stock-table branch.
 - Make error handling consistent across pwm_enable modes. (Badal)
 - Document the pwm[1-3] manual-mode write precondition in the ABI. (Badal)
 - Use xe_pcode_write() instead of the timeout variant api as it is not
   needed.

v4:
 - Rebase on reworked pwm[1-3] patch.
 - Clamp min PWM to 100% when activating the user table, since fan
   table speed is in percent.
 - Drop redundant channel range check in pwm write.
 - Use tab indentation and update Date and KernelVersion to November
   2026 and 7.5 in the pwm[1-3]_enable ABI entry.

v5:
 - Address review comments from sashiko.
 - Avoid returning pwm[1-3]_enable as XE_FAN_PWM_MANUAL_USER_TABLE, when
   setting to full speed mode. (sashiko)
 - Set default pwm[1-3]_enable to XE_FAN_PWM_AUTO_STOCK_TABLE after
   probe. (sashiko)
 - Make pwm[1-3]_enable visible only if stock fan table has valid point
   count.
 - Add flag XE_FAN_PWM_UNINITIALIZED.
 - Verify user point counts after table activation and auto-mode writes.
 - Fall back to the stock table if firmware does not accept a table.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  15 ++
 drivers/gpu/drm/xe/xe_hwmon.c                 | 201 +++++++++++++++++-
 2 files changed, 207 insertions(+), 9 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index 24e9eb595746..be1c325fcc9b 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
@@ -354,4 +354,19 @@ Contact:	intel-xe@lists.freedesktop.org
 Description:	RW. Target fan PWM duty value in the range 0..255, where 255
 		is 100% fan speed.
 
+		Writing requires the corresponding pwm[1-3]_enable to be set to
+		1 (manual user-table mode) first; otherwise the write is
+		rejected with -EINVAL.
+
+		Only supported for particular Intel Xe graphics platforms.
+
+What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/pwm[1-3]_enable
+Date:		November 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:	RW. Fan control mode selector.
+
+		Use value 0 to run fans at full speed. Use value 1 to enable
+		user table control and value 2 to use firmware default behavior.
+
 		Only supported for particular Intel Xe graphics platforms.
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 1e375613fe18..f62a60e99140 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -139,6 +139,20 @@ struct xe_hwmon_energy_info {
 	long accum_energy;
 };
 
+/**
+ * enum xe_fan_pwm_enable_mode - fan PWM enable modes
+ */
+enum xe_fan_pwm_enable_mode {
+	/** @XE_FAN_PWM_FULL_SPEED: force all user table points to full speed */
+	XE_FAN_PWM_FULL_SPEED = 0,
+	/** @XE_FAN_PWM_MANUAL_USER_TABLE: use the driver-managed user table */
+	XE_FAN_PWM_MANUAL_USER_TABLE = 1,
+	/** @XE_FAN_PWM_AUTO_STOCK_TABLE: use the firmware stock table */
+	XE_FAN_PWM_AUTO_STOCK_TABLE = 2,
+	/** @XE_FAN_PWM_UNINITIALIZED: fan control mode is not established */
+	XE_FAN_PWM_UNINITIALIZED = 0xff,
+};
+
 /**
  * enum fan_table_type - fan table types tracked per fan
  */
@@ -190,6 +204,10 @@ struct xe_hwmon_fan_info {
 	} fan_table[FAN_TABLE_MAX];
 	/** @max_rps: maximum fan RPS */
 	u32 max_rps;
+	/** @pwm_enable_mode: current xe_fan_pwm_enable_mode */
+	u8 pwm_enable_mode;
+	/** @is_full_speed: flag indicating if fan is in full speed */
+	bool is_full_speed;
 };
 
 /**
@@ -926,7 +944,9 @@ static const struct hwmon_channel_info * const hwmon_info[] = {
 	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL),
 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_MAX, HWMON_F_INPUT | HWMON_F_MAX,
 			   HWMON_F_INPUT | HWMON_F_MAX),
-	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT, HWMON_PWM_INPUT, HWMON_PWM_INPUT),
+	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
 	NULL
 };
 
@@ -1176,6 +1196,7 @@ static int xe_hwmon_get_fan_point_count(struct xe_hwmon *hwmon, u8 fan, u32 *poi
 {
 	int ret;
 
+	*point_count = 0;
 	ret = xe_hwmon_pcode_read_fan_control(hwmon,
 					      (table_type == USER_FAN_TABLE) ?
 					      FSC_READ_USER_FAN_CONTROL_POINTS :
@@ -1211,9 +1232,88 @@ static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
 			     PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, fan),
 			     0);
 	if (ret)
-		return ret;
+		goto fail;
 
 	fi->fan_table[USER_FAN_TABLE].point_count = 0;
+	fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
+	fi->is_full_speed = false;
+	return 0;
+
+fail:
+	fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
+	fi->is_full_speed = false;
+	return ret;
+}
+
+static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
+					    bool is_full_speed, enum fan_table_type table_source)
+{
+	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+	struct xe_pcode_write writes[MAX_FAN_CONTROL_POINTS + 1];
+	u32 expected_count, point_count;
+	int ret, write_ret, verify_ret;
+	int point;
+	struct fan_table *source_table = &fi->fan_table[table_source];
+
+	xe_dbg(hwmon->xe, "activating fan %d user table from %s table\n", fan,
+	       (table_source == USER_FAN_TABLE) ? "user" : "stock");
+
+	point_count = source_table->point_count;
+	if (!point_count || point_count > MAX_FAN_CONTROL_POINTS) {
+		xe_err(hwmon->xe,
+		       "fan %d %s table point count %d is invalid, cannot activate user table\n",
+		       fan, (table_source == USER_FAN_TABLE) ? "user" : "stock", point_count);
+		return -EINVAL;
+	}
+	expected_count = point_count;
+
+	for (point = 0; point < point_count; point++) {
+		u8 temp = source_table->fcp[point].temp;
+		u8 speed = is_full_speed ? 100 : min_t(u8, source_table->fcp[point].speed, 100);
+
+		writes[point].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABLE, fan);
+		writes[point].data = FIELD_PREP(FAN_CONTROL_POINT_TEMP_MASK, temp) |
+					FIELD_PREP(FAN_CONTROL_POINT_SPEED_MASK, speed);
+	}
+
+	writes[point_count].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS,
+					      fan);
+	writes[point_count].data = point_count;
+	write_ret = xe_pcode_write_batch(xe_device_get_root_tile(hwmon->xe), writes,
+					 point_count + 1);
+	if (write_ret)
+		xe_dbg(hwmon->xe, "failed to write fan %d user table, ret=%d\n", fan, write_ret);
+
+	verify_ret = xe_hwmon_get_fan_point_count(hwmon, fan, &point_count, USER_FAN_TABLE);
+	if (write_ret || verify_ret || point_count != expected_count) {
+		int failure_ret = write_ret ?: verify_ret ?: -EIO;
+
+		fi->fan_table[USER_FAN_TABLE].point_count = 0;
+		if (verify_ret || point_count != expected_count)
+			xe_err(hwmon->xe,
+			       "fan %d user table count misatch: ret=%d, expected=%u, got=%u\n",
+			       fan, verify_ret, expected_count, point_count);
+		ret = xe_hwmon_fan_set_auto(hwmon, fan);
+		if (ret)
+			xe_err(hwmon->xe, "failed to restore fan %d to stock mode, ret=%d\n", fan,
+			       ret);
+		return failure_ret;
+	}
+
+	if (table_source == STOCK_FAN_TABLE) {
+		for (point = 0; point < point_count; point++) {
+			fi->fan_table[USER_FAN_TABLE].fcp[point].temp =
+				source_table->fcp[point].temp;
+			fi->fan_table[USER_FAN_TABLE].fcp[point].speed =
+				is_full_speed ? 100 : min_t(u8, source_table->fcp[point].speed,
+							    100);
+		}
+	}
+
+	fi->fan_table[USER_FAN_TABLE].point_count = point_count;
+	fi->pwm_enable_mode = is_full_speed ? XE_FAN_PWM_FULL_SPEED :
+					    XE_FAN_PWM_MANUAL_USER_TABLE;
+	fi->is_full_speed = is_full_speed;
 
 	return 0;
 }
@@ -1221,7 +1321,7 @@ static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
 /* Temperature to set when setting the fan PWM with pwm[1-3]. */
 #define FIXED_PWM_TEMP	1
 
-static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
+static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm, bool full_speed)
 {
 	struct xe_pcode_write writes[MIN_FAN_CONTROL_POINTS + 1];
 	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
@@ -1229,6 +1329,11 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
 	u32 point_count = 0;
 	u8 speed;
 
+	if (!full_speed && fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE) {
+		xe_dbg(hwmon->xe, "fan %d manual mode is not active, cannot set pwm\n", fan);
+		return -EINVAL;
+	}
+
 	/* Fan table speed are in percent. */
 	speed = DIV_ROUND_CLOSEST(pwm * 100, U8_MAX);
 	speed = min_t(u32, speed, 100);
@@ -1280,6 +1385,9 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
 	}
 	fi->fan_table[USER_FAN_TABLE].point_count = MIN_FAN_CONTROL_POINTS;
 
+	fi->pwm_enable_mode = full_speed ? XE_FAN_PWM_FULL_SPEED :
+					 XE_FAN_PWM_MANUAL_USER_TABLE;
+	fi->is_full_speed = full_speed;
 	return 0;
 }
 
@@ -1299,6 +1407,9 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 		if (!IS_FAN_ENABLED(hwmon, fan))
 			continue;
 
+		fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
+		fi->fan_table[STOCK_FAN_TABLE].point_count = 0;
+
 		ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_MAX_FAN_RPS, fan,
 						      &fi->max_rps);
 		if (ret) {
@@ -1352,6 +1463,8 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 		memcpy(fi->fan_table[STOCK_FAN_TABLE].fcp, stock_fcp,
 		       stock_fcp_count * sizeof(stock_fcp[0]));
 		fi->fan_table[STOCK_FAN_TABLE].point_count = stock_fcp_count;
+		if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
+			fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
 	}
 	return err;
 }
@@ -1913,6 +2026,7 @@ xe_hwmon_pwm_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 
 	switch (attr) {
 	case hwmon_pwm_input:
+	case hwmon_pwm_enable:
 		return 0644;
 	default:
 		return 0;
@@ -2018,7 +2132,7 @@ static int xe_hwmon_get_stock_table_pwm(struct xe_hwmon *hwmon, int channel, lon
 	}
 
 	*val = pwm;
-	xe_dbg(hwmon->xe, "fan %d stock table active PWM value: %ld\n", channel, pwm);
+	xe_dbg(hwmon->xe, "fan %d: active pwm value from stock table cache: %ld\n", channel, pwm);
 	return 0;
 }
 
@@ -2064,12 +2178,20 @@ static int xe_hwmon_get_pwm_from_pmt(struct xe_hwmon *hwmon, int channel, long *
 		return -EOPNOTSUPP;
 	}
 
-	xe_dbg(hwmon->xe, "fan %d PMT active PWM value: %u\n", channel, pwm);
+	xe_dbg(hwmon->xe, "fan %d: active pwm from PMT: %u\n", channel, pwm);
 
 	*val = pwm;
 	return 0;
 }
 
+static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
+{
+	if (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE)
+		return STOCK_FAN_TABLE;
+	else
+		return USER_FAN_TABLE;
+}
+
 static int
 xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 {
@@ -2084,8 +2206,9 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	switch (attr) {
 	case hwmon_pwm_input:
 		/* If the user fan table is not populated, get active stock pwm */
-		if (!fi->fan_table[USER_FAN_TABLE].point_count) {
-			xe_dbg(hwmon->xe, "user fan table not active for fan %d\n", channel);
+		if (xe_hwmon_get_active_fan_table(hwmon, channel) == STOCK_FAN_TABLE) {
+			xe_dbg(hwmon->xe, "getting active pwm for fan %d from stock table\n",
+			       channel);
 			switch (hwmon->xe->info.platform) {
 			case XE_BATTLEMAGE:
 				ret = xe_hwmon_get_pwm_from_pmt(hwmon, channel, &pwm);
@@ -2094,13 +2217,18 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 				ret = xe_hwmon_get_stock_table_pwm(hwmon, channel, &pwm);
 				break;
 			}
-			if (ret)
+			if (ret) {
+				xe_err(hwmon->xe, "failed to get active pwm for fan %d, ret=%d\n",
+				       channel, ret);
 				goto unlock;
+			}
 		} else {
 			/*
 			 * pwmN writes set every user-table point to the same speed,
 			 * so the first point gives the cached manual PWM percentage.
 			 */
+			xe_dbg(hwmon->xe, "getting active pwm for fan %d from user table cache\n",
+			       channel);
 			pwm = fi->fan_table[USER_FAN_TABLE].fcp[0].speed;
 		}
 		if (pwm < 0 || pwm > 100) {
@@ -2112,6 +2240,13 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 		*val = DIV_ROUND_CLOSEST(pwm * U8_MAX, 100);
 		xe_dbg(hwmon->xe, "pwm read for fan %d: %ld\n", channel, *val);
 		break;
+	case hwmon_pwm_enable:
+		if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED) {
+			ret = -ENODATA;
+			break;
+		}
+		*val = fi->is_full_speed ? XE_FAN_PWM_FULL_SPEED : fi->pwm_enable_mode;
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
@@ -2125,8 +2260,11 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 static int
 xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 {
+	struct xe_hwmon_fan_info *fi;
 	int ret = 0;
 
+	fi = &hwmon->fi[channel];
+
 	mutex_lock(&hwmon->hwmon_lock);
 
 	switch (attr) {
@@ -2135,7 +2273,48 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 			ret = -EINVAL;
 			break;
 		}
-		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val);
+		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val, false);
+		break;
+	case hwmon_pwm_enable:
+		if (val == XE_FAN_PWM_FULL_SPEED) {
+			ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, U8_MAX, true);
+			if (ret) {
+				xe_warn(hwmon->xe,
+					"full speed enable for fan %d failed, ret=%d\n",
+					channel, ret);
+				break;
+			}
+		} else if (val == XE_FAN_PWM_MANUAL_USER_TABLE) {
+			enum fan_table_type source = fi->fan_table[USER_FAN_TABLE].point_count ?
+				USER_FAN_TABLE : STOCK_FAN_TABLE;
+
+			/*
+			 * Manual fan speed control using user fan table.
+			 * If user fan table is not programmed, it would be initialized with
+			 * clamped stock points which allows manual control with some
+			 * default points.
+			 */
+			ret = xe_hwmon_activate_user_fan_table(hwmon, channel, false, source);
+			if (ret) {
+				xe_warn(hwmon->xe,
+					"failed to activate user fan table for fan %d, ret=%d\n",
+					channel, ret);
+				break;
+			}
+		} else if (val == XE_FAN_PWM_AUTO_STOCK_TABLE) {
+			ret = xe_hwmon_fan_set_auto(hwmon, channel);
+			if (ret) {
+				xe_warn(hwmon->xe,
+					"failed to switch fan %d to automatic stock control, ret=%d\n",
+					channel, ret);
+				break;
+			}
+			xe_dbg(hwmon->xe,
+			       "automatic control enabled for fan %d using stock table\n",
+			       channel);
+		} else {
+			ret = -EINVAL;
+		}
 		break;
 	default:
 		ret = -EOPNOTSUPP;
@@ -2350,6 +2529,7 @@ int xe_hwmon_register(struct xe_device *xe)
 {
 	struct device *dev = xe->drm.dev;
 	struct xe_hwmon *hwmon;
+	int fan;
 	int ret;
 
 	/* hwmon is available only for dGfx */
@@ -2364,6 +2544,9 @@ int xe_hwmon_register(struct xe_device *xe)
 	if (!hwmon)
 		return -ENOMEM;
 
+	for (fan = 0; fan < FAN_MAX; fan++)
+		hwmon->fi[fan].pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
+
 	ret = devm_mutex_init(dev, &hwmon->hwmon_lock);
 	if (ret)
 		return ret;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (4 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:10   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Expose curve-point temperature and pwm controls via
pwmF_auto_point[P]_temp and pwmF_auto_point[P]_pwm, where
F is the fan number and P is the fan curve point number.
Each fan curve point has a temperature and a pwm attribute.

Update Xe hwmon ABI documentation for pwm auto point temperature
attributes.

v2:
 - Use xe helpers for dmesg logs.
 - Update kernel version in Xe hwmon documentation.
 - Correct debug level of some dmesg logs.

v3:
 - Allow variable fan curve points from >=2 to MAX_FAN_CONTROL_POINTS.
 - Expose all MAX_FAN_CONTROL_POINTS fan-curve slots so users can extend
   a short fan table.
 - Stage temp/PWM pairs without changing the active curve. Commit at a
   (0, 0) end marker or after all MAX_FAN_CONTROL_POINTS pairs.
 - Write the points together, verify the firmware count, then update the
   active cache.

v4:
 - Use the two-argument IS_FAN_ENABLED() after rebase.
 - Fix kernel-doc for nested staged fan_table members.
 - Use "speed" for percent values and "pwm" only for the 0..255 sysfs
   value. (Soham)
 - Use u32 for the point index in xe_hwmon_commit_user_fan_table(). (Soham)
 - Fix comment to say curve points must not decrease. (Soham)
 - Add debug logs before -EINVAL returns. (Soham)
 - Drop inline from xe_hwmon_get_active_fan_table(). (Soham)
 - Use reverse xmas tree ordering for local variables. (Soham)
 - Rename xe_hwmon_get_u8_from_sysfs() to xe_hwmon_sysfs_to_hw_val().
   (Soham)
 - Use if/else if/else for value selection in pwm auto point show. (Soham)
 - Create the fan curve sensor attributes at probe time instead of 60
   static definitions. (Soham)
 - Clamp min PWM to 100% and reject pwm values above 100% speed.
 - Use xe helpers for logs in the auto point store functions.

v5:
 - Address review comments from Sashiko.
 - Remove min_pwm as it is not available.
 - Read back the user point count after curve programming and fall back
   to stock control when firmware does not accept it.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_hwmon.c | 429 ++++++++++++++++++++++++++++++++--
 1 file changed, 415 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index f62a60e99140..866e54c82d27 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -174,6 +174,11 @@ enum fan_table_type {
 /* Minimum number of points required for a user fan table. */
 #define MIN_FAN_CONTROL_POINTS	2
 
+/* pwmN_auto_pointM_temp and pwmN_auto_pointM_pwm for every curve point of a fan. */
+#define XE_CURVE_ATTRS_PER_FAN	(2 * MAX_FAN_CONTROL_POINTS)
+#define XE_CURVE_ATTRS		(FAN_MAX * XE_CURVE_ATTRS_PER_FAN)
+#define XE_CURVE_ATTR_NAME_LEN	sizeof("pwmN_auto_pointNN_temp")
+
 /* Fan control point index: bit31 selects fan table type; bits30:0 control point index. */
 #define FCP_INDEX(table_type, point_num) (((u32)(table_type) << 31) | \
 						((u32)(point_num) & GENMASK(30, 0)))
@@ -201,6 +206,12 @@ struct xe_hwmon_fan_info {
 			/** @fan_table.fcp.speed: fan speed in percentage */
 			u8 speed;
 		} fcp[MAX_FAN_CONTROL_POINTS];
+		/** @fan_table.staged_fcp: curve supplied via sysfs but not yet committed */
+		struct fan_control_point staged_fcp[MAX_FAN_CONTROL_POINTS];
+		/** @fan_table.temp_staged: bitmask of temperature fields supplied via sysfs */
+		u16 temp_staged;
+		/** @fan_table.speed_staged: bitmask of speed fields supplied via sysfs */
+		u16 speed_staged;
 	} fan_table[FAN_TABLE_MAX];
 	/** @max_rps: maximum fan RPS */
 	u32 max_rps;
@@ -268,6 +279,16 @@ struct xe_hwmon {
 	u32 pl2_on_boot[CHANNEL_MAX];
 	/** @temp: Temperature info */
 	struct xe_hwmon_thermal_info temp;
+	/** @curve_sda: fan curve sensor attributes */
+	struct sensor_device_attribute curve_sda[XE_CURVE_ATTRS];
+	/** @curve_names: names of the fan curve sensor attributes */
+	char curve_names[XE_CURVE_ATTRS][XE_CURVE_ATTR_NAME_LEN];
+	/** @curve_attrs: NULL-terminated list of fan curve attributes */
+	struct attribute *curve_attrs[XE_CURVE_ATTRS + 1];
+	/** @curve_group: fan curve attribute group */
+	struct attribute_group curve_group;
+	/** @groups: NULL-terminated list of extra attribute groups */
+	const struct attribute_group *groups[3];
 };
 
 static inline int prepare_power_limit_param2(const struct xe_hwmon *hwmon, int channel)
@@ -912,11 +933,6 @@ static const struct attribute_group hwmon_attrgroup = {
 	.is_visible = xe_hwmon_attributes_visible,
 };
 
-static const struct attribute_group *hwmon_groups[] = {
-	&hwmon_attrgroup,
-	NULL
-};
-
 static const u32 hwmon_temp_config[CHANNEL_MAX + 1] = {
 	[CHANNEL_CARD] = HWMON_T_LABEL,
 	[CHANNEL_PKG] = HWMON_T_CRIT | HWMON_T_EMERGENCY | HWMON_T_INPUT |
@@ -1245,6 +1261,79 @@ static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
 	return ret;
 }
 
+static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
+					  const struct fan_control_point *points, u32 point_count)
+{
+	/* The extra write is for the final write that sets the number of fan control points */
+	struct xe_pcode_write writes[MAX_FAN_CONTROL_POINTS + 1];
+	u32 accepted_count = 0;
+	u32 point;
+	int ret, write_ret, verify_ret;
+
+	if (!point_count) {
+		xe_err(hwmon->xe, "no fan control points provided for fan %d\n", fan);
+		return -ENODATA;
+	}
+
+	for (point = 0; point < point_count; point++) {
+		u8 temp = points[point].temp;
+		u8 speed = points[point].speed;
+
+		/* Temperature and speed must not decrease from one point to the next. */
+		if (point > 0) {
+			u8 prev_temp = points[point - 1].temp;
+			u8 prev_speed = points[point - 1].speed;
+
+			if (temp < prev_temp || speed < prev_speed) {
+				xe_dbg(hwmon->xe,
+				       "fan %d point %u decreases: %u/%u%% after %u/%u%%\n",
+				       fan, point, temp, speed, prev_temp, prev_speed);
+				return -EINVAL;
+			}
+		}
+
+		/* Prepare the write for this fan control point */
+		writes[point].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABLE,
+						fan);
+		writes[point].data = FIELD_PREP(FAN_CONTROL_POINT_TEMP_MASK, temp) |
+				FIELD_PREP(FAN_CONTROL_POINT_SPEED_MASK, speed);
+	}
+
+	xe_dbg(hwmon->xe, "committing fan %d user table with %u points\n", fan, point_count);
+
+	writes[point_count].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS,
+					      fan);
+	writes[point_count].data = point_count;
+
+	write_ret = xe_pcode_write_batch(xe_device_get_root_tile(hwmon->xe), writes,
+					 point_count + 1);
+	if (write_ret)
+		xe_dbg(hwmon->xe, "failed to write fan %d user table, ret=%d\n", fan, write_ret);
+
+	verify_ret = xe_hwmon_get_fan_point_count(hwmon, fan, &accepted_count, USER_FAN_TABLE);
+	if (write_ret || verify_ret || accepted_count != point_count) {
+		int failure_ret = write_ret ?: verify_ret ?: -EIO;
+
+		hwmon->fi[fan].fan_table[USER_FAN_TABLE].point_count = 0;
+		if (verify_ret || accepted_count != point_count)
+			xe_err(hwmon->xe,
+			       "fan %d table count verification failed: ret=%d, expected=%u, got=%u\n",
+			       fan, verify_ret, point_count, accepted_count);
+		ret = xe_hwmon_fan_set_auto(hwmon, fan);
+		if (ret) {
+			hwmon->fi[fan].pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
+			hwmon->fi[fan].is_full_speed = false;
+			xe_err(hwmon->xe, "failed to restore fan %d to stock mode, ret=%d\n", fan,
+			       ret);
+		}
+		return failure_ret;
+	}
+
+	xe_dbg(hwmon->xe, "fan %d user table committed successfully with %u points\n", fan,
+	       point_count);
+	return 0;
+}
+
 static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
 					    bool is_full_speed, enum fan_table_type table_source)
 {
@@ -1313,6 +1402,8 @@ static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
 	fi->fan_table[USER_FAN_TABLE].point_count = point_count;
 	fi->pwm_enable_mode = is_full_speed ? XE_FAN_PWM_FULL_SPEED :
 					    XE_FAN_PWM_MANUAL_USER_TABLE;
+	fi->fan_table[USER_FAN_TABLE].temp_staged = 0;
+	fi->fan_table[USER_FAN_TABLE].speed_staged = 0;
 	fi->is_full_speed = is_full_speed;
 
 	return 0;
@@ -1384,6 +1475,8 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm, boo
 		       fi->fan_table[USER_FAN_TABLE].fcp[i].speed);
 	}
 	fi->fan_table[USER_FAN_TABLE].point_count = MIN_FAN_CONTROL_POINTS;
+	fi->fan_table[USER_FAN_TABLE].temp_staged = 0;
+	fi->fan_table[USER_FAN_TABLE].speed_staged = 0;
 
 	fi->pwm_enable_mode = full_speed ? XE_FAN_PWM_FULL_SPEED :
 					 XE_FAN_PWM_MANUAL_USER_TABLE;
@@ -1391,6 +1484,312 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm, boo
 	return 0;
 }
 
+#define XE_CURVE_IDX(fan, point)	(((fan) << 8) | (point))
+#define XE_CURVE_FAN(idx)		((u8)(((idx) >> 8) & 0xff))
+#define XE_CURVE_POINT(idx)		((u8)((idx) & 0xff))
+
+static int
+xe_hwmon_decode_curve_index(struct xe_hwmon *hwmon, u32 index, u8 *fan, u8 *point)
+{
+	u8 local_fan = XE_CURVE_FAN(index);
+	u8 local_point = XE_CURVE_POINT(index);
+
+	if (local_fan >= FAN_MAX || local_point >= MAX_FAN_CONTROL_POINTS) {
+		xe_dbg(hwmon->xe, "invalid fan curve index 0x%x: fan %u point %u\n",
+		       index, local_fan, local_point);
+		return -EINVAL;
+	}
+
+	*fan = local_fan;
+	*point = local_point;
+
+	return 0;
+}
+
+static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
+{
+	if (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE)
+		return STOCK_FAN_TABLE;
+	else
+		return USER_FAN_TABLE;
+}
+
+static ssize_t
+xe_hwmon_pwm_auto_point_temp_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
+	enum fan_table_type active_table;
+	struct xe_hwmon_fan_info *fi;
+	u32 point_count;
+	u8 point;
+	u8 temp;
+	int ret;
+	u8 fan;
+
+	ret = xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&hwmon->hwmon_lock);
+
+	fi = &hwmon->fi[fan];
+	active_table = xe_hwmon_get_active_fan_table(hwmon, fan);
+
+	point_count = fi->fan_table[active_table].point_count;
+
+	if (point >= point_count)
+		temp = 0;
+	else
+		temp = fi->fan_table[active_table].fcp[point].temp;
+	return sysfs_emit(buf, "%u\n", temp * MILLIDEGREE_PER_DEGREE);
+}
+
+static int xe_hwmon_update_user_fan_point(struct xe_hwmon *hwmon, u8 fan, u32 point,
+					  bool is_temp, u8 value)
+{
+	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+	struct fan_table *table;
+	u32 point_count;
+	int ret;
+
+	table = &fi->fan_table[USER_FAN_TABLE];
+
+	if (is_temp) {
+		table->staged_fcp[point].temp = value;
+		table->temp_staged |= BIT(point);
+	} else {
+		table->staged_fcp[point].speed = value;
+		table->speed_staged |= BIT(point);
+	}
+
+	/* Wait for a complete prefix; (0, 0) ends the curve without becoming a point. */
+	for (point_count = 0; point_count < MAX_FAN_CONTROL_POINTS; point_count++) {
+		if (!(table->temp_staged & BIT(point_count)) ||
+		    !(table->speed_staged & BIT(point_count)))
+			return 0;
+		if (!table->staged_fcp[point_count].temp &&
+		    !table->staged_fcp[point_count].speed)
+			break;
+	}
+
+	if (point_count < MIN_FAN_CONTROL_POINTS) {
+		xe_dbg(hwmon->xe, "fan %d curve needs at least %d points, got %u\n",
+		       fan, MIN_FAN_CONTROL_POINTS, point_count);
+		ret = -EINVAL;
+		goto clear_staged;
+	}
+
+	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, table->staged_fcp, point_count);
+	if (ret)
+		goto clear_staged;
+
+	/* Publish the new curve only after firmware accepts its point count. */
+	memcpy(table->fcp, table->staged_fcp, point_count * sizeof(table->fcp[0]));
+	table->point_count = point_count;
+	fi->pwm_enable_mode = XE_FAN_PWM_MANUAL_USER_TABLE;
+	fi->is_full_speed = false;
+
+clear_staged:
+	table->temp_staged = 0;
+	table->speed_staged = 0;
+	return ret;
+}
+
+static int xe_hwmon_sysfs_to_hw_val(struct xe_hwmon *hwmon, long val, bool is_temp, u8 *out)
+{
+	long max = is_temp ? U8_MAX * MILLIDEGREE_PER_DEGREE : U8_MAX;
+	long hw_val;
+
+	if (val < 0 || val > max) {
+		xe_dbg(hwmon->xe, "%s value %ld out of range\n", is_temp ? "temp" : "pwm", val);
+		return -EINVAL;
+	}
+
+	/* Temperature is in degrees Celsius, speed in percent. */
+	if (is_temp)
+		hw_val = DIV_ROUND_CLOSEST(val, MILLIDEGREE_PER_DEGREE);
+	else
+		hw_val = DIV_ROUND_CLOSEST(val * 100, U8_MAX);
+
+	if (!is_temp && val && !hw_val)
+		hw_val = 1;
+
+	*out = hw_val;
+
+	return 0;
+}
+
+static ssize_t
+xe_hwmon_pwm_auto_point_temp_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
+	struct xe_hwmon_fan_info *fi;
+	long val;
+	u8 point;
+	u8 temp;
+	int ret;
+	u8 fan;
+
+	ret = xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point);
+	if (ret)
+		return ret;
+
+	ret = kstrtol(buf, 10, &val);
+	if (ret)
+		return ret;
+
+	ret = xe_hwmon_sysfs_to_hw_val(hwmon, val, true, &temp);
+	if (ret)
+		return ret;
+
+	guard(xe_pm_runtime)(hwmon->xe);
+
+	guard(mutex)(&hwmon->hwmon_lock);
+	fi = &hwmon->fi[fan];
+
+	if (fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE || fi->is_full_speed) {
+		xe_dbg(hwmon->xe, "pwm%d_enable not in manual mode; cannot update temp point %d\n",
+		       fan + 1, point + 1);
+		return -EINVAL;
+	}
+
+	xe_dbg(hwmon->xe, "staging fan %d point %d temp to %u\n", fan, point, temp);
+	ret = xe_hwmon_update_user_fan_point(hwmon, fan, point, true, temp);
+
+	return ret ? ret : count;
+}
+
+static ssize_t
+xe_hwmon_pwm_auto_point_pwm_show(struct device *dev,
+				 struct device_attribute *attr,
+				 char *buf)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
+	enum fan_table_type active_table;
+	struct xe_hwmon_fan_info *fi;
+	u32 point_count;
+	u8 point;
+	u8 speed;
+	int ret;
+	u8 fan;
+
+	ret = xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&hwmon->hwmon_lock);
+
+	fi = &hwmon->fi[fan];
+	active_table = xe_hwmon_get_active_fan_table(hwmon, fan);
+
+	point_count = fi->fan_table[active_table].point_count;
+
+	if (point >= point_count)
+		speed = 0;
+	else
+		speed = fi->fan_table[active_table].fcp[point].speed;
+	return sysfs_emit(buf, "%u\n", DIV_ROUND_CLOSEST(speed * U8_MAX, 100));
+}
+
+static ssize_t
+xe_hwmon_pwm_auto_point_pwm_store(struct device *dev,
+				  struct device_attribute *attr,
+				  const char *buf, size_t count)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
+	struct xe_hwmon_fan_info *fi;
+	long val;
+	u8 point;
+	u8 speed;
+	int ret;
+	u8 fan;
+
+	ret = xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point);
+	if (ret)
+		return ret;
+
+	ret = kstrtol(buf, 10, &val);
+	if (ret)
+		return ret;
+
+	ret = xe_hwmon_sysfs_to_hw_val(hwmon, val, false, &speed);
+	if (ret)
+		return ret;
+
+	guard(xe_pm_runtime)(hwmon->xe);
+
+	guard(mutex)(&hwmon->hwmon_lock);
+	fi = &hwmon->fi[fan];
+
+	if (fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE || fi->is_full_speed) {
+		xe_dbg(hwmon->xe,
+		       "pwm%d_enable is not in manual user mode, cannot update pwm point %d\n",
+		       fan + 1, point + 1);
+		return -EINVAL;
+	}
+
+	xe_dbg(hwmon->xe, "staging fan %d point %d speed to %u%%\n", fan, point, speed);
+	ret = xe_hwmon_update_user_fan_point(hwmon, fan, point, false, speed);
+
+	return ret ? ret : count;
+}
+
+static umode_t xe_hwmon_curve_attributes_visible(struct kobject *kobj,
+						 struct attribute *attr, int index)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
+	u32 fan = index / XE_CURVE_ATTRS_PER_FAN;
+
+	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, fan))
+		return 0;
+
+	/* All slots must exist at registration, before the user curve has been staged. */
+	return attr->mode;
+}
+
+static void xe_hwmon_init_curve_attrs(struct xe_hwmon *hwmon)
+{
+	int fan, kind, point, i = 0;
+
+	/* Per fan: all temp points then all pwm points; is_visible relies on this layout. */
+	for (fan = 0; fan < FAN_MAX; fan++) {
+		for (kind = 0; kind < 2; kind++) {
+			bool is_temp = !kind;
+
+			for (point = 0; point < MAX_FAN_CONTROL_POINTS; point++, i++) {
+				struct sensor_device_attribute *sda = &hwmon->curve_sda[i];
+
+				snprintf(hwmon->curve_names[i], XE_CURVE_ATTR_NAME_LEN,
+					 "pwm%d_auto_point%d_%s", fan + 1, point + 1,
+					 is_temp ? "temp" : "pwm");
+				sysfs_attr_init(&sda->dev_attr.attr);
+				sda->dev_attr.attr.name = hwmon->curve_names[i];
+				sda->dev_attr.attr.mode = 0644;
+				if (is_temp) {
+					sda->dev_attr.show = xe_hwmon_pwm_auto_point_temp_show;
+					sda->dev_attr.store = xe_hwmon_pwm_auto_point_temp_store;
+				} else {
+					sda->dev_attr.show = xe_hwmon_pwm_auto_point_pwm_show;
+					sda->dev_attr.store = xe_hwmon_pwm_auto_point_pwm_store;
+				}
+				sda->index = XE_CURVE_IDX(fan, point);
+				hwmon->curve_attrs[i] = &sda->dev_attr.attr;
+			}
+		}
+	}
+
+	hwmon->curve_group.attrs = hwmon->curve_attrs;
+	hwmon->curve_group.is_visible = xe_hwmon_curve_attributes_visible;
+}
+
 static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 {
 	int err = 0;
@@ -2184,14 +2583,6 @@ static int xe_hwmon_get_pwm_from_pmt(struct xe_hwmon *hwmon, int channel, long *
 	return 0;
 }
 
-static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
-{
-	if (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE)
-		return STOCK_FAN_TABLE;
-	else
-		return USER_FAN_TABLE;
-}
-
 static int
 xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 {
@@ -2301,6 +2692,8 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 					channel, ret);
 				break;
 			}
+			fi->fan_table[USER_FAN_TABLE].temp_staged = 0;
+			fi->fan_table[USER_FAN_TABLE].speed_staged = 0;
 		} else if (val == XE_FAN_PWM_AUTO_STOCK_TABLE) {
 			ret = xe_hwmon_fan_set_auto(hwmon, channel);
 			if (ret) {
@@ -2312,6 +2705,8 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 			xe_dbg(hwmon->xe,
 			       "automatic control enabled for fan %d using stock table\n",
 			       channel);
+			fi->fan_table[USER_FAN_TABLE].temp_staged = 0;
+			fi->fan_table[USER_FAN_TABLE].speed_staged = 0;
 		} else {
 			ret = -EINVAL;
 		}
@@ -2561,10 +2956,16 @@ int xe_hwmon_register(struct xe_device *xe)
 
 	drm_dbg(&xe->drm, "Register xe hwmon interface\n");
 
+	hwmon->groups[0] = &hwmon_attrgroup;
+	if (xe->info.has_fan_control) {
+		xe_hwmon_init_curve_attrs(hwmon);
+		hwmon->groups[1] = &hwmon->curve_group;
+	}
+
 	/*  hwmon_dev points to device hwmon<i> */
 	hwmon->hwmon_dev = hwmon_device_register_with_info(dev, "xe", hwmon,
 							   &hwmon_chip_info,
-							   hwmon_groups);
+							   hwmon->groups);
 	if (IS_ERR(hwmon->hwmon_dev)) {
 		ret = PTR_ERR(hwmon->hwmon_dev);
 		xe_log_err(xe, HWMON, ret, "Failed to register xe hwmon\n");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (5 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:15   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
                   ` (3 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Preserve active manual and full-speed fan tables across system suspend,
runtime D3cold, and FLR.

On resume, wait for late-bound fan-control firmware to reload before
restoring the cached table. If restoration fails, verify fallback to
stock fan control.

v2: Use xe_pcode_read() instead of xe_pcode_read_timeout().

v3:
 - Use u32 for the point index. (Soham)
 - Say "user table point" in suspend warnings. (Soham)
 - Sort the xe_hwmon.h include. (Soham)
 - Call the system PM hooks from xe_device_suspend() and
   xe_device_resume() after the upstream refactor, so FLR also
   restores the table.
 - Check d3cold.allowed only for runtime PM; system suspend always
   loses the table.
 - Pass hwmon to IS_FAN_ENABLED().

v4:
 - Avoid fan-table reads during suspend.
 - Fall back to the stock fan table on user-table restore failure.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_device.c       |   3 +
 drivers/gpu/drm/xe/xe_hwmon.c        | 109 +++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hwmon.h        |   4 +
 drivers/gpu/drm/xe/xe_late_bind_fw.c |   7 ++
 drivers/gpu/drm/xe/xe_pm.c           |  11 ++-
 5 files changed, 131 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 36693df65594..ed259b0fc3d6 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -1428,6 +1428,8 @@ int xe_device_suspend(struct xe_device *xe, bool hibernate, bool flr)
 
 	xe_late_bind_pm_suspend(&xe->late_bind);
 
+	xe_hwmon_suspend(xe, false);
+
 	for_each_gt(gt, xe, id)
 		xe_gt_suspend_prepare(gt);
 
@@ -1558,6 +1560,7 @@ int xe_device_resume(struct xe_device *xe, bool flr)
 	xe_pxp_pm_resume(xe->pxp);
 
 	xe_late_bind_pm_resume(&xe->late_bind);
+	xe_hwmon_resume(xe, false);
 
 	return 0;
 }
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 866e54c82d27..3e82a9236b10 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -219,6 +219,8 @@ struct xe_hwmon_fan_info {
 	u8 pwm_enable_mode;
 	/** @is_full_speed: flag indicating if fan is in full speed */
 	bool is_full_speed;
+	/** @restore_user_table: restore user fan table on next resume */
+	bool restore_user_table;
 };
 
 /**
@@ -2994,4 +2996,111 @@ void xe_hwmon_unregister(struct xe_device *xe)
 	hwmon_device_unregister(xe->hwmon->hwmon_dev);
 	xe->hwmon = NULL;
 }
+
+/**
+ * xe_hwmon_suspend - Save hwmon state before suspend
+ * @xe: xe device instance
+ * @runtime: true for runtime suspend, false for system suspend or FLR prepare
+ *
+ * Save any hwmon runtime state that must survive PM suspend transitions.
+ */
+void xe_hwmon_suspend(struct xe_device *xe, bool runtime)
+{
+	struct xe_hwmon *hwmon = xe->hwmon;
+	int fan;
+
+	if (!hwmon || !xe->info.has_fan_control)
+		return;
+
+	/* Runtime suspend keeps the fan tables unless the device enters d3cold. */
+	if (runtime && !xe->d3cold.allowed) {
+		xe_dbg(xe, "d3cold not allowed, skipping fan user-table snapshot\n");
+		return;
+	}
+
+	mutex_lock(&hwmon->hwmon_lock);
+
+	for (fan = 0; fan < FAN_MAX; fan++) {
+		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+		u32 point_count = fi->fan_table[USER_FAN_TABLE].point_count;
+
+		if (!IS_FAN_ENABLED(hwmon, fan))
+			continue;
+
+		fi->restore_user_table = false;
+		if (fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE &&
+		    fi->pwm_enable_mode != XE_FAN_PWM_FULL_SPEED)
+			continue;
+
+		if (point_count < MIN_FAN_CONTROL_POINTS ||
+		    point_count > MAX_FAN_CONTROL_POINTS)
+			continue;
+		fi->restore_user_table = true;
+		xe_dbg(hwmon->xe,
+		       "fan %d cached user table marked for restore, point_count=%u\n", fan,
+		       point_count);
+	}
+
+	mutex_unlock(&hwmon->hwmon_lock);
+}
+
+/**
+ * xe_hwmon_resume - Restore hwmon state after resume
+ * @xe: xe device instance
+ * @runtime: true for runtime resume, false for system resume or FLR done
+ *
+ * Restore any hwmon runtime state that was saved across suspend.
+ */
+void xe_hwmon_resume(struct xe_device *xe, bool runtime)
+{
+	struct xe_hwmon *hwmon = xe->hwmon;
+	int fan;
+
+	if (!hwmon || !xe->info.has_fan_control)
+		return;
+
+	if (runtime && !xe->d3cold.allowed) {
+		xe_dbg(xe, "d3cold not allowed, skipping fan user-table restore\n");
+		return;
+	}
+
+	mutex_lock(&hwmon->hwmon_lock);
+
+	for (fan = 0; fan < FAN_MAX; fan++) {
+		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+		int ret;
+
+		if (!IS_FAN_ENABLED(hwmon, fan))
+			continue;
+
+		if (!fi->restore_user_table) {
+			xe_dbg(xe, "fan %d user table restore not required\n", fan);
+			continue;
+		}
+
+		/* Re-program cached user-table settings for fans active before suspend. */
+		ret = xe_hwmon_activate_user_fan_table(hwmon, fan,
+						       fi->is_full_speed,
+						       USER_FAN_TABLE);
+		if (ret) {
+			xe_warn(xe, "failed to restore fan %d user table after resume, ret=%d\n",
+				fan, ret);
+			fi->fan_table[USER_FAN_TABLE].point_count = 0;
+			fi->restore_user_table = false;
+			ret = xe_hwmon_fan_set_auto(hwmon, fan);
+			if (ret) {
+				xe_warn(xe, "fan %d stock fallback failed, state unconfirmed, ret=%d\n",
+					fan, ret);
+				continue;
+			}
+			continue;
+		}
+
+		xe_dbg(xe, "fan %d user table restored\n", fan);
+		fi->restore_user_table = false;
+	}
+
+	mutex_unlock(&hwmon->hwmon_lock);
+}
+
 MODULE_IMPORT_NS("INTEL_PMT_TELEMETRY");
diff --git a/drivers/gpu/drm/xe/xe_hwmon.h b/drivers/gpu/drm/xe/xe_hwmon.h
index e67e0134f02a..50874b6b6527 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.h
+++ b/drivers/gpu/drm/xe/xe_hwmon.h
@@ -13,9 +13,13 @@ struct xe_device;
 #if IS_REACHABLE(CONFIG_HWMON)
 int xe_hwmon_register(struct xe_device *xe);
 void xe_hwmon_unregister(struct xe_device *xe);
+void xe_hwmon_suspend(struct xe_device *xe, bool runtime);
+void xe_hwmon_resume(struct xe_device *xe, bool runtime);
 #else
 static inline int xe_hwmon_register(struct xe_device *xe) { return 0; };
 static inline void xe_hwmon_unregister(struct xe_device *xe) { }
+static inline void xe_hwmon_suspend(struct xe_device *xe, bool runtime) {}
+static inline void xe_hwmon_resume(struct xe_device *xe, bool runtime) {}
 #endif
 
 #endif /* _XE_HWMON_H_ */
diff --git a/drivers/gpu/drm/xe/xe_late_bind_fw.c b/drivers/gpu/drm/xe/xe_late_bind_fw.c
index ad39383a7e41..c577e2d51620 100644
--- a/drivers/gpu/drm/xe/xe_late_bind_fw.c
+++ b/drivers/gpu/drm/xe/xe_late_bind_fw.c
@@ -517,10 +517,17 @@ void xe_late_bind_pm_suspend(struct xe_late_bind *late_bind)
  */
 void xe_late_bind_pm_resume(struct xe_late_bind *late_bind)
 {
+	struct xe_late_bind_fw *fan_control_fw;
+	bool fan_control_fw_pending;
+
 	if (!late_bind->component_added || late_bind->disable)
 		return;
 
+	fan_control_fw = &late_bind->late_bind_fw[XE_LB_FW_FAN_CONTROL];
+	fan_control_fw_pending = !!fan_control_fw->payload;
 	xe_late_bind_fw_load(late_bind);
+	if (fan_control_fw_pending)
+		flush_work(&fan_control_fw->work);
 }
 
 /**
diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
index f73f68bd449b..4d4b398cbeda 100644
--- a/drivers/gpu/drm/xe/xe_pm.c
+++ b/drivers/gpu/drm/xe/xe_pm.c
@@ -20,6 +20,7 @@
 #include "xe_ggtt.h"
 #include "xe_gt.h"
 #include "xe_gt_idle.h"
+#include "xe_hwmon.h"
 #include "xe_i2c.h"
 #include "xe_irq.h"
 #include "xe_late_bind_fw.h"
@@ -567,6 +568,8 @@ int xe_pm_runtime_suspend(struct xe_device *xe)
 	if (err)
 		goto out;
 
+	xe_hwmon_suspend(xe, true);
+
 	/*
 	 * Applying lock for entire list op as xe_ttm_bo_destroy and xe_bo_move_notify
 	 * also checks and deletes bo entry from user fault list.
@@ -683,12 +686,14 @@ int xe_pm_runtime_resume(struct xe_device *xe)
 
 	xe_pxp_pm_resume(xe->pxp);
 
-	if (IS_VF_CCS_READY(xe))
-		xe_sriov_vf_ccs_register_context(xe);
-
 	if (xe->d3cold.allowed)
 		xe_late_bind_pm_resume(&xe->late_bind);
 
+	xe_hwmon_resume(xe, true);
+
+	if (IS_VF_CCS_READY(xe))
+		xe_sriov_vf_ccs_register_context(xe);
+
 out:
 	xe_rpm_lockmap_release(xe);
 	xe_pm_write_callback_task(xe, NULL);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (6 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:27   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Refresh cached stock fan tables and maximum RPM for registered fans after
fan-control firmware loads.

Serialize the refresh with hwmon registration and teardown. Flush
late-bind work before releasing hwmon state, and protect fanN_max reads
against concurrent max_rps updates.

v2:
 - Compare lbfw->id with XE_LB_FW_FAN_CONTROL. (Soham)
 - Use xe already in scope. (Soham)
 - Spell out "late binding" in warnings. (Soham)
 - Return an error from xe_hwmon_get_fan_enable_mask() and keep the
   old mask if the read fails.
 - Start fans enabled after late binding in automatic mode.

v3:
 - Serialize the refresh with hwmon registration and teardown. Flush
   late-bind work before releasing hwmon state, and protect fanN_max reads
   against concurrent max_rps updates.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_hwmon.c        | 71 +++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_hwmon.h        |  2 +
 drivers/gpu/drm/xe/xe_late_bind_fw.c |  3 ++
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 3e82a9236b10..92eb0aba6d4a 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -16,6 +16,7 @@
 #include "regs/xe_pcode_regs.h"
 #include "xe_device.h"
 #include "xe_hwmon.h"
+#include "xe_late_bind_fw.h"
 #include "xe_log.h"
 #include "xe_mmio.h"
 #include "xe_pcode.h"
@@ -259,6 +260,8 @@ struct xe_hwmon {
 	struct device *hwmon_dev;
 	/** @xe: Xe device */
 	struct xe_device *xe;
+	/** @registration_lock: serialize registration with late-binding updates */
+	struct mutex registration_lock;
 	/** @hwmon_lock: lock for rw attributes*/
 	struct mutex hwmon_lock;
 	/** @scl_shift_power: pkg power unit */
@@ -293,6 +296,13 @@ struct xe_hwmon {
 	const struct attribute_group *groups[3];
 };
 
+static void xe_hwmon_flush_late_bind(void *arg)
+{
+	struct xe_device *xe = arg;
+
+	xe_late_bind_pm_suspend(&xe->late_bind);
+}
+
 static inline int prepare_power_limit_param2(const struct xe_hwmon *hwmon, int channel)
 {
 	if (hwmon->read_power_limit_from_pcode[channel]) {
@@ -1808,7 +1818,6 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 		if (!IS_FAN_ENABLED(hwmon, fan))
 			continue;
 
-		fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
 		fi->fan_table[STOCK_FAN_TABLE].point_count = 0;
 
 		ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_MAX_FAN_RPS, fan,
@@ -2480,7 +2489,9 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	case hwmon_fan_input:
 		return xe_hwmon_fan_input_read(hwmon, channel, val);
 	case hwmon_fan_max:
+		mutex_lock(&hwmon->hwmon_lock);
 		*val = mul_u32_u32(hwmon->fi[channel].max_rps, 60);
+		mutex_unlock(&hwmon->hwmon_lock);
 		return 0;
 	default:
 		return -EOPNOTSUPP;
@@ -2948,9 +2959,21 @@ int xe_hwmon_register(struct xe_device *xe)
 	if (ret)
 		return ret;
 
+	ret = devm_mutex_init(dev, &hwmon->registration_lock);
+	if (ret)
+		return ret;
+
+	/* Flush late-bind work before devm releases the hwmon state it may use. */
+	ret = devm_add_action_or_reset(dev, xe_hwmon_flush_late_bind, xe);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&hwmon->registration_lock);
+
 	/* There's only one instance of hwmon per device */
 	hwmon->xe = xe;
-	xe->hwmon = hwmon;
+	/* Pair with the acquire load in xe_hwmon_fan_update_post_lb(). */
+	smp_store_release(&xe->hwmon, hwmon);
 
 	ret = xe_hwmon_get_preregistration_info(hwmon);
 	if (ret)
@@ -2977,7 +3000,7 @@ int xe_hwmon_register(struct xe_device *xe)
 	return 0;
 
 err_null_hwmon:
-	xe->hwmon = NULL;
+	WRITE_ONCE(xe->hwmon, NULL);
 	return ret;
 }
 
@@ -2990,11 +3013,21 @@ int xe_hwmon_register(struct xe_device *xe)
  */
 void xe_hwmon_unregister(struct xe_device *xe)
 {
-	if (!xe->hwmon)
+	struct xe_hwmon *hwmon;
+
+	/* Stop updates before unregistering hwmon or unplugging the device. */
+	xe_late_bind_pm_suspend(&xe->late_bind);
+
+	hwmon = READ_ONCE(xe->hwmon);
+	if (!hwmon)
 		return;
 
-	hwmon_device_unregister(xe->hwmon->hwmon_dev);
-	xe->hwmon = NULL;
+	mutex_lock(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) == hwmon) {
+		hwmon_device_unregister(hwmon->hwmon_dev);
+		WRITE_ONCE(xe->hwmon, NULL);
+	}
+	mutex_unlock(&hwmon->registration_lock);
 }
 
 /**
@@ -3103,4 +3136,30 @@ void xe_hwmon_resume(struct xe_device *xe, bool runtime)
 	mutex_unlock(&hwmon->hwmon_lock);
 }
 
+/**
+ * xe_hwmon_fan_update_post_lb - Update fan info after late binding
+ * @xe: xe device instance
+ *
+ * This function reads the number of fans and their stock table after late binding.
+ */
+void xe_hwmon_fan_update_post_lb(struct xe_device *xe)
+{
+	/* Pair with the release store in xe_hwmon_register(). */
+	struct xe_hwmon *hwmon = smp_load_acquire(&xe->hwmon);
+
+	if (!hwmon || !xe->info.has_fan_control)
+		return;
+
+	guard(mutex)(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) != hwmon)
+		return;
+
+	mutex_lock(&hwmon->hwmon_lock);
+
+	if (xe_hwmon_read_fan_control_info(hwmon))
+		xe_warn(hwmon->xe, "failed to read stock fan tables after late binding\n");
+
+	mutex_unlock(&hwmon->hwmon_lock);
+}
+
 MODULE_IMPORT_NS("INTEL_PMT_TELEMETRY");
diff --git a/drivers/gpu/drm/xe/xe_hwmon.h b/drivers/gpu/drm/xe/xe_hwmon.h
index 50874b6b6527..4d46290e7a9d 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.h
+++ b/drivers/gpu/drm/xe/xe_hwmon.h
@@ -15,11 +15,13 @@ int xe_hwmon_register(struct xe_device *xe);
 void xe_hwmon_unregister(struct xe_device *xe);
 void xe_hwmon_suspend(struct xe_device *xe, bool runtime);
 void xe_hwmon_resume(struct xe_device *xe, bool runtime);
+void xe_hwmon_fan_update_post_lb(struct xe_device *xe);
 #else
 static inline int xe_hwmon_register(struct xe_device *xe) { return 0; };
 static inline void xe_hwmon_unregister(struct xe_device *xe) { }
 static inline void xe_hwmon_suspend(struct xe_device *xe, bool runtime) {}
 static inline void xe_hwmon_resume(struct xe_device *xe, bool runtime) {}
+static inline void xe_hwmon_fan_update_post_lb(struct xe_device *xe) {}
 #endif
 
 #endif /* _XE_HWMON_H_ */
diff --git a/drivers/gpu/drm/xe/xe_late_bind_fw.c b/drivers/gpu/drm/xe/xe_late_bind_fw.c
index c577e2d51620..6fe154ea01c3 100644
--- a/drivers/gpu/drm/xe/xe_late_bind_fw.c
+++ b/drivers/gpu/drm/xe/xe_late_bind_fw.c
@@ -14,6 +14,7 @@
 
 #include "xe_configfs.h"
 #include "xe_device.h"
+#include "xe_hwmon.h"
 #include "xe_late_bind_fw.h"
 #include "xe_pcode.h"
 #include "xe_pcode_api.h"
@@ -298,6 +299,8 @@ static void xe_late_bind_work(struct work_struct *work)
 	if (!ret) {
 		drm_dbg(&xe->drm, "Load %s firmware successful\n",
 			fw_id_to_name[lbfw->id]);
+		if (lbfw->id == XE_LB_FW_FAN_CONTROL)
+			xe_hwmon_fan_update_post_lb(xe);
 		goto out;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (7 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:12   ` sashiko-bot
  2026-11-28  5:59 ` [PATCH v5 10/10] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
  2026-11-28  6:14 ` ✗ CI.KUnit: failure for drm/xe/hwmon: Add fan control support (rev2) Patchwork
  10 siblings, 1 reply; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Add a device attribute to read and configure firmware fan control,
which forces maximum acoustic PWM as SoC VR temperatures
approach their critical thresholds.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 .../ABI/testing/sysfs-driver-intel-xe-gpu     | 15 ++++
 drivers/gpu/drm/xe/xe_device_sysfs.c          | 76 +++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode_api.h             |  2 +
 3 files changed, 93 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu b/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
index b563b7e8a9d9..b644a100dd6b 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
@@ -21,6 +21,21 @@ Description:
 		This sysfs file is present only on Crescent Island devices.
 		It is readable by all users.
 
+What:		/sys/bus/pci/drivers/xe/.../tjmax_fan_control_enable
+Date:		November 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:
+		RW. Firmware thermal fan control mode.
+
+		Write 1 to enable or 0 to disable the mode that forces maximum
+		acoustic PWM as SoC temperature approaches shutdown or GDDR/VR
+		temperature approaches its thermal maximum. Reads return the
+		status reported by firmware.
+
+		This sysfs file is present only on devices that support
+		this command.
+
 What:		/sys/bus/pci/drivers/xe/.../tile<N>/memory/max_freq
 Date:		October 2026
 KernelVersion:	7.5
diff --git a/drivers/gpu/drm/xe/xe_device_sysfs.c b/drivers/gpu/drm/xe/xe_device_sysfs.c
index f6275e74eb31..3a4861b82548 100644
--- a/drivers/gpu/drm/xe/xe_device_sysfs.c
+++ b/drivers/gpu/drm/xe/xe_device_sysfs.c
@@ -35,6 +35,9 @@
  * lb_voltage_regulator_version - Voltage regulator version provisioned by late
  * binding. Exposed only if supported by the device.
  *
+ * tjmax_fan_control_enable - Report/change firmware TjMax fan control mode.
+ * Exposed only if supported by the device.
+ *
  * shrinker_non_purgeable_seeks - Report/change the cost, relative to
  * purgeable buffer objects, of recreating non-purgeable buffer objects
  * reclaimed by the shrinker. Higher values reduce reclaim pressure on
@@ -203,6 +206,73 @@ static const struct attribute_group late_bind_attr_group = {
 	.is_visible = late_bind_attr_is_visible,
 };
 
+static ssize_t
+tjmax_fan_control_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
+	u32 val = 0;
+	int ret;
+
+	guard(xe_pm_runtime)(xe);
+	ret = xe_pcode_read(xe_device_get_root_tile(xe),
+			    PCODE_MBOX(FAN_SPEED_CONTROL, FSC_READ_TJMAX_FAN_CONTROL_MODE, 0),
+			    &val, NULL);
+	drm_dbg(&xe->drm, "pcode READ_TJMAX_FAN_CONTROL_MODE: val %#x, ret %d\n",
+		ret ? 0 : val, ret);
+
+	return ret ?: sysfs_emit(buf, "%u\n", val);
+}
+
+static ssize_t
+tjmax_fan_control_enable_store(struct device *dev, struct device_attribute *attr,
+			       const char *buf, size_t count)
+{
+	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
+	u32 val;
+	int ret;
+
+	ret = kstrtou32(buf, 0, &val);
+	if (ret)
+		return ret;
+	if (val > 1)
+		return -EINVAL;
+
+	guard(xe_pm_runtime)(xe);
+	ret = xe_pcode_write(xe_device_get_root_tile(xe),
+			     PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_TJMAX_FAN_CONTROL_MODE, 0),
+			     val);
+	drm_dbg(&xe->drm, "pcode WRITE_TJMAX_FAN_CONTROL_MODE: val %u, ret %d\n", val, ret);
+
+	return ret ?: count;
+}
+static DEVICE_ATTR_RW(tjmax_fan_control_enable);
+
+static struct attribute *fan_control_attrs[] = {
+	&dev_attr_tjmax_fan_control_enable.attr,
+	NULL
+};
+
+static umode_t fan_control_attr_is_visible(struct kobject *kobj,
+					   struct attribute *attr, int n)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
+	u32 val;
+	int ret;
+
+	guard(xe_pm_runtime)(xe);
+	ret = xe_pcode_read(xe_device_get_root_tile(xe),
+			    PCODE_MBOX(FAN_SPEED_CONTROL, FSC_READ_TJMAX_FAN_CONTROL_MODE, 0),
+			    &val, NULL);
+
+	return ret ? 0 : attr->mode;
+}
+
+static const struct attribute_group fan_control_attr_group = {
+	.attrs = fan_control_attrs,
+	.is_visible = fan_control_attr_is_visible,
+};
+
 /**
  * DOC: PCIe Gen5 Limitations
  *
@@ -428,5 +498,11 @@ int xe_device_sysfs_init(struct xe_device *xe)
 			return ret;
 	}
 
+	if (xe->info.has_fan_control) {
+		ret = devm_device_add_group(dev, &fan_control_attr_group);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
diff --git a/drivers/gpu/drm/xe/xe_pcode_api.h b/drivers/gpu/drm/xe/xe_pcode_api.h
index bf321ed19251..89dd5fba63c3 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -95,6 +95,8 @@
 #define     FSC_READ_FAN_TABLE			0x7
 #define     FAN_CONTROL_POINT_TEMP_MASK		REG_GENMASK(7, 0)
 #define     FAN_CONTROL_POINT_SPEED_MASK	REG_GENMASK(15, 8)
+#define     FSC_WRITE_TJMAX_FAN_CONTROL_MODE	0x8
+#define     FSC_READ_TJMAX_FAN_CONTROL_MODE	0x9
 
 #define PCODE_SCRATCH(x)		XE_REG(0x138320 + ((x) * 4))
 /* PCODE_SCRATCH0 */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v5 10/10] drm/xe/hwmon: Add kernel-doc for fan control
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (8 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
@ 2026-11-28  5:59 ` Karthik Poosa
  2026-11-28  6:14 ` ✗ CI.KUnit: failure for drm/xe/hwmon: Add fan control support (rev2) Patchwork
  10 siblings, 0 replies; 18+ messages in thread
From: Karthik Poosa @ 2026-11-28  5:59 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	riana.tauro, sk.anirban, mallesh.koujalagi, soham.purkait,
	ravi.kishore.koppuravuri, Karthik Poosa

Add kernel-doc for xe hwmon fan control and describe the fan control
model built around firmware stock tables and driver-managed user tables.

Document the exposed sysfs files for fan speed, maximum speed, pwm
control, pwm mode selection, and per-point fan curve temperature/PWM
programming.

This makes the hwmon fan control userspace interface easier to discover
and understand.

v2:
 - Clarify the registration-time visibility requirements.
 - Document how late binding affects existing fan attributes.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_hwmon.c | 56 +++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 92eb0aba6d4a..f5235dd33c57 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -140,6 +140,62 @@ struct xe_hwmon_energy_info {
 	long accum_energy;
 };
 
+/**
+ * DOC: Xe fan control
+ *
+ * Xe fan control is exposed through the hwmon pwm and fan interfaces when the
+ * platform advertises PCODE-backed fan management support.
+ *
+ * Attribute visibility is determined during hwmon registration. ``fanN_max``
+ * requires maximum fan speed data, while ``pwmN`` and ``pwmN_enable`` require a
+ * readable stock fan table. Late binding refreshes cached data for existing
+ * fans but does not create attributes that were hidden at registration.
+ *
+ * The implementation tracks two fan tables per fan:
+ *
+ * - A stock table read from firmware through PCODE.
+ * - A user table cached in the driver and programmed back through PCODE.
+ *
+ * The pwmN_enable sysfs value selects how those tables are used:
+ *
+ * - 0 (``XE_FAN_PWM_FULL_SPEED``) programs a two-point user table at 100
+ *   percent duty cycle as a full-speed override.
+ * - 1 (``XE_FAN_PWM_MANUAL_USER_TABLE``) enables the user table for manual
+ *   control.
+ * - 2 (``XE_FAN_PWM_AUTO_STOCK_TABLE``) restores automatic control using the
+ *   stock firmware table.
+ *
+ * The fan-control sysfs files are:
+ *
+ * - ``fanN_input`` reports the measured fan speed in RPM.
+ * - ``fanN_max`` reports the maximum supported fan speed in RPM.
+ * - ``pwmN`` reads or sets the manual PWM duty cycle in the hwmon 0..255
+ *   range.
+ * - ``pwmN_enable`` selects the mode described above.
+ * - ``pwmN_auto_pointM_temp`` exposes the temperature threshold for user-table
+ *   point ``M`` in millidegrees Celsius.
+ * - ``pwmN_auto_pointM_pwm`` exposes the PWM value for user-table point ``M``
+ *   in the hwmon 0..255 range.
+ *
+ * Writes to ``pwmN``, ``pwmN_auto_pointM_temp`` and ``pwmN_auto_pointM_pwm``
+ * are accepted only while ``pwmN_enable`` is 1; otherwise they fail with
+ * -EINVAL. Firmware stores duty cycle in percent, so PWM values read back
+ * rounded to the nearest percent step. Nonzero curve PWM writes are kept at
+ * least 1 percent so rounding cannot turn them into an end marker.
+ *
+ * Writes to ``pwmN_auto_pointM_temp`` and ``pwmN_auto_pointM_pwm`` are staged
+ * in the driver, so intermediate writes never reach firmware as a partial
+ * curve. The staged curve is committed once points 1..K all have both a
+ * temperature and a PWM value and either point K+1 is set to (0, 0) or K is
+ * the last supported point. The (0, 0) end marker is not part of the curve.
+ * A curve must have at least two points, and temperature and PWM must not
+ * decrease from one point to the next; otherwise the completing write fails
+ * with -EINVAL.
+ *
+ * Writes to ``pwmN`` replace the user table with a two-point table at a
+ * constant duty cycle.
+ */
+
 /**
  * enum xe_fan_pwm_enable_mode - fan PWM enable modes
  */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper
  2026-11-28  5:59 ` [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
@ 2026-11-28  6:04   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:04 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] drm/xe/pcode: missing rationale and lock impact for
  xe_pcode_write_batch()
--

commit 357f40b2588787b30829671c174b780a0ae2ed24
Author: Karthik Poosa <karthik.poosa@intel.com>

> drm/xe/pcode: Add ordered write batch helper
> 
> Add a pcode helper that issues an ordered sequence of mailbox writes
> while holding the pcode lock, so no other mailbox command can be
> interleaved.
> 
> This will be used to write fan tables atomically with respect to other
> pcode users.
> 
> Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>

> diff --git a/drivers/gpu/drm/xe/xe_pcode.c b/drivers/gpu/drm/xe/xe_pcode.c
> index fa9f45d565cb9..409b9ff372e99 100644
> --- a/drivers/gpu/drm/xe/xe_pcode.c
> +++ b/drivers/gpu/drm/xe/xe_pcode.c
> @@ -124,6 +124,45 @@ int xe_pcode_write_timeout(struct xe_tile *tile, u32 mbox, u32 data, int timeout
>  	return err;
>  }
>  
> +/**
> + * xe_pcode_write_batch - issue pcode writes in order under the mailbox lock
> + * @tile: tile
> + * @writes: mailbox/data pairs to write
> + * @count: number of entries in @writes
> + *
> + * Holds the pcode lock across all writes so no other mailbox command can be
> + * interleaved; the lock may be held for up to @count * PCODE_DEFAULT_TIMEOUT_MS.
> + * Stops at the first failure; earlier writes are not reverted.
> + *
> + * Return: 0 on success, negative error code otherwise.
> + */
> +int xe_pcode_write_batch(struct xe_tile *tile, const struct xe_pcode_write *writes,
> +			 unsigned int count)
> +{
> +	unsigned int i;
> +	int err = 0;
> +
> +	if (!count)
> +		return -EINVAL;
> +
> +	mutex_lock(&tile->pcode.lock);
> +	for (i = 0; i < count; i++) {
> +		u32 data = writes[i].data;
> +
> +		err = pcode_mailbox_rw(tile, writes[i].mbox, &data, NULL, PCODE_DEFAULT_TIMEOUT_MS,
> +				       false, false);

[Severity: Low]
As reported by Rodrigo Vivi on v4 [1], the commit message and kernel-doc
still do not explain why pcode requires these writes to be issued without
interleaved mailbox commands, or assess the risk of holding
tile->pcode.lock for up to count * PCODE_DEFAULT_TIMEOUT_MS (110 ms for
an 11-entry fan table write) and starving or causing timeouts on other
pcode paths.

[1] https://lore.kernel.org/r/awcpB6s_32zyhNBi@intel.com

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=3

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support
  2026-11-28  5:59 ` [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
@ 2026-11-28  6:10   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:10 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] drm/xe/hwmon: missing stock table check in
  xe_hwmon_curve_attributes_visible()
- [Medium] drm/xe/hwmon: xe_hwmon_pwm_read() returns fcp[0].speed on
  user fan curves
- [Low] drm/xe/hwmon: missing ABI documentation update claimed in commit
  message
- [Low] drm/xe/hwmon: unhandled XE_FAN_PWM_UNINITIALIZED in curve show
  callbacks
--

commit 51f62604f9e213b63207cde46fb9f696d1728daf
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe/hwmon: Enable fan curve control support

This commit exposes fan curve temperature and PWM control points in hwmon
sysfs via pwm[1-3]_auto_point[1-10]_temp and pwm[1-3]_auto_point[1-10]_pwm,
allowing userspace to stage and commit custom fan curves to PCODE.

> Update Xe hwmon ABI documentation for pwm auto point temperature
> attributes.
>
> v2:
>  - Use xe helpers for dmesg logs.
>  - Update kernel version in Xe hwmon documentation.

[Severity: Low]
Was the Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon update
dropped from this patch?  The commit message says the ABI documentation is
updated, but this commit only touches drivers/gpu/drm/xe/xe_hwmon.c and
leaves pwm[1-3]_auto_point[1-10]_temp and pwm[1-3]_auto_point[1-10]_pwm
undocumented.

> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index b6214ef933165..4d6eb6f6fbc2f 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c

[ ... ]

> @@ -1389,6 +1482,312 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm, boo
>  	return 0;
>  }
>  
> +#define XE_CURVE_IDX(fan, point)	(((fan) << 8) | (point))
> +#define XE_CURVE_FAN(idx)		((u8)(((idx) >> 8) & 0xff))
> +#define XE_CURVE_POINT(idx)		((u8)((idx) & 0xff))
> +
> +static int
> +xe_hwmon_decode_curve_index(struct xe_hwmon *hwmon, u32 index, u8 *fan, u8 *point)
> +{
> +	u8 local_fan = XE_CURVE_FAN(index);
> +	u8 local_point = XE_CURVE_POINT(index);
> +
> +	if (local_fan >= FAN_MAX || local_point >= MAX_FAN_CONTROL_POINTS) {
> +		xe_dbg(hwmon->xe, "invalid fan curve index 0x%x: fan %u point %u\n",
> +		       index, local_fan, local_point);
> +		return -EINVAL;
> +	}
> +
> +	*fan = local_fan;
> +	*point = local_point;
> +
> +	return 0;
> +}
> +
> +static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
> +{
> +	if (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE)
> +		return STOCK_FAN_TABLE;
> +	else
> +		return USER_FAN_TABLE;
> +}
> +
> +static ssize_t
> +xe_hwmon_pwm_auto_point_temp_show(struct device *dev,
> +				  struct device_attribute *attr, char *buf)
> +{
> +	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
> +	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
> +	enum fan_table_type active_table;
> +	struct xe_hwmon_fan_info *fi;
> +	u32 point_count;
> +	u8 point;
> +	u8 temp;
> +	int ret;
> +	u8 fan;
> +
> +	ret = xe_hwmon_decode_curve_index(hwmon, sattr->index, &fan, &point);
> +	if (ret)
> +		return ret;
> +
> +	guard(mutex)(&hwmon->hwmon_lock);
> +
> +	fi = &hwmon->fi[fan];
> +	active_table = xe_hwmon_get_active_fan_table(hwmon, fan);

[Severity: Low]
Should xe_hwmon_pwm_auto_point_temp_show() and
xe_hwmon_pwm_auto_point_pwm_show() return -ENODATA when
fi->pwm_enable_mode is XE_FAN_PWM_UNINITIALIZED, the same way
xe_hwmon_pwm_read() does for hwmon_pwm_enable?  Right now
xe_hwmon_get_active_fan_table() maps XE_FAN_PWM_UNINITIALIZED to
USER_FAN_TABLE, so after a failed xe_hwmon_fan_set_auto() call the show
callbacks report stale user-table points or 0 instead of an error.

> +
> +	point_count = fi->fan_table[active_table].point_count;
> +
> +	if (point >= point_count)
> +		temp = 0;
> +	else
> +		temp = fi->fan_table[active_table].fcp[point].temp;
> +	return sysfs_emit(buf, "%u\n", temp * MILLIDEGREE_PER_DEGREE);
> +}
> +
> +static int xe_hwmon_update_user_fan_point(struct xe_hwmon *hwmon, u8 fan, u32 point,
> +					  bool is_temp, u8 value)
> +{
> +	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> +	struct fan_table *table;
> +	u32 point_count;
> +	int ret;
> +
> +	table = &fi->fan_table[USER_FAN_TABLE];
> +
> +	if (is_temp) {
> +		table->staged_fcp[point].temp = value;
> +		table->temp_staged |= BIT(point);
> +	} else {
> +		table->staged_fcp[point].speed = value;
> +		table->speed_staged |= BIT(point);
> +	}
> +
> +	/* Wait for a complete prefix; (0, 0) ends the curve without becoming a point. */
> +	for (point_count = 0; point_count < MAX_FAN_CONTROL_POINTS; point_count++) {
> +		if (!(table->temp_staged & BIT(point_count)) ||
> +		    !(table->speed_staged & BIT(point_count)))
> +			return 0;
> +		if (!table->staged_fcp[point_count].temp &&
> +		    !table->staged_fcp[point_count].speed)
> +			break;
> +	}
> +
> +	if (point_count < MIN_FAN_CONTROL_POINTS) {
> +		xe_dbg(hwmon->xe, "fan %d curve needs at least %d points, got %u\n",
> +		       fan, MIN_FAN_CONTROL_POINTS, point_count);
> +		ret = -EINVAL;
> +		goto clear_staged;
> +	}
> +
> +	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, table->staged_fcp, point_count);
> +	if (ret)
> +		goto clear_staged;
> +
> +	/* Publish the new curve only after firmware accepts its point count. */
> +	memcpy(table->fcp, table->staged_fcp, point_count * sizeof(table->fcp[0]));
> +	table->point_count = point_count;
> +	fi->pwm_enable_mode = XE_FAN_PWM_MANUAL_USER_TABLE;
> +	fi->is_full_speed = false;

[Severity: Medium]
When a multi-point user fan curve is committed here, does reading pwmN in
xe_hwmon_pwm_read() return the wrong PWM value?

drivers/gpu/drm/xe/xe_hwmon.c:xe_hwmon_pwm_read() {
    ...
		if (xe_hwmon_get_active_fan_table(hwmon, channel) == STOCK_FAN_TABLE) {
			...
		} else {
			/*
			 * pwmN writes set every user-table point to the same speed,
			 * so the first point gives the cached manual PWM percentage.
			 */
			xe_dbg(hwmon->xe, "getting active pwm for fan %d from user table cache\n",
			       channel);
			pwm = fi->fan_table[USER_FAN_TABLE].fcp[0].speed;
		}
    ...
}

For a custom curve with different speeds across temperature points,
fcp[0].speed is only the speed at the lowest temperature point rather than
the active PWM from PMT telemetry (on Battlemage) or interpolation across
USER_FAN_TABLE.  Also, if xe_hwmon_commit_user_fan_table() and its
xe_hwmon_fan_set_auto() fallback both fail, point_count is reset to 0 and
pwm_enable_mode is set to XE_FAN_PWM_UNINITIALIZED, yet xe_hwmon_pwm_read()
still takes this USER_FAN_TABLE branch and returns stale fcp[0].speed.

[ ... ]

> +static umode_t xe_hwmon_curve_attributes_visible(struct kobject *kobj,
> +						 struct attribute *attr, int index)
> +{
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
> +	u32 fan = index / XE_CURVE_ATTRS_PER_FAN;
> +
> +	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, fan))
> +		return 0;
> +
> +	/* All slots must exist at registration, before the user curve has been staged. */
> +	return attr->mode;
> +}

[Severity: Medium]
Should xe_hwmon_curve_attributes_visible() also check
!hwmon->fi[fan].fan_table[STOCK_FAN_TABLE].point_count like
xe_hwmon_pwm_is_visible() does?

drivers/gpu/drm/xe/xe_hwmon.c:xe_hwmon_pwm_is_visible() {
	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) ||
	    !hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count)
		return 0;
    ...
}

When a fan channel is enabled in fan_enable_mask (such as on DG2, or when
reading the stock table in xe_hwmon_read_fan_control_info() fails or
returns 0 points), pwmN and pwmN_enable are hidden and fi->pwm_enable_mode
stays XE_FAN_PWM_UNINITIALIZED, but all 20 pwmN_auto_point* attributes are
still exposed even though every write to them will fail with -EINVAL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=6

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs
  2026-11-28  5:59 ` [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
@ 2026-11-28  6:12   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:12 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/xe: tjmax_fan_control_enable exposed as no-op on SR-IOV
  VFs
- [Medium] drm/xe: uninitialized val passed to xe_pcode_read() in
  fan_control_attr_is_visible()
--

commit f041e7b8d44d672ec8e023036136e0162ee46779
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe: Expose tjmax_fan_control_enable device sysfs

This commit adds a read-write tjmax_fan_control_enable sysfs attribute
to read and configure the firmware TjMax fan control mode via PCODE
mailbox commands.

> diff --git a/drivers/gpu/drm/xe/xe_device_sysfs.c b/drivers/gpu/drm/xe/xe_device_sysfs.c
> index f6275e74eb317..3a4861b825485 100644
> --- a/drivers/gpu/drm/xe/xe_device_sysfs.c
> +++ b/drivers/gpu/drm/xe/xe_device_sysfs.c

[ ... ]

> @@ -203,6 +206,73 @@ static const struct attribute_group late_bind_attr_group = {

[ ... ]

> +static umode_t fan_control_attr_is_visible(struct kobject *kobj,
> +					   struct attribute *attr, int n)
> +{
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
> +	u32 val;
> +	int ret;
> +
> +	guard(xe_pm_runtime)(xe);
> +	ret = xe_pcode_read(xe_device_get_root_tile(xe),
> +			    PCODE_MBOX(FAN_SPEED_CONTROL, FSC_READ_TJMAX_FAN_CONTROL_MODE, 0),
> +			    &val, NULL);
> +
> +	return ret ? 0 : attr->mode;
> +}

[Severity: Medium]
Should val be initialized to 0 before calling xe_pcode_read(), as in
tjmax_fan_control_enable_show()?

xe_pcode_read() -> pcode_mailbox_rw() -> __pcode_mailbox_rw() treats
val0 as an in/out argument and unconditionally writes *data0 to
PCODE_DATA0 before issuing the mailbox command:

drivers/gpu/drm/xe/xe_pcode.c:__pcode_mailbox_rw() {
	...
	xe_mmio_write32(mmio, PCODE_DATA0, *data0);
	xe_mmio_write32(mmio, PCODE_DATA1, data1 ? *data1 : 0);
	xe_mmio_write32(mmio, PCODE_MAILBOX, PCODE_READY | mbox);
	...
}

Leaving val uninitialized writes stack garbage into PCODE_DATA0 during
driver probe, triggering KMSAN uninit-value warnings and risking command
rejection if firmware checks the data register.

[ ... ]

> @@ -428,5 +498,11 @@ int xe_device_sysfs_init(struct xe_device *xe)
>  			return ret;
>  	}
>  
> +	if (xe->info.has_fan_control) {
> +		ret = devm_device_add_group(dev, &fan_control_attr_group);
> +		if (ret)
> +			return ret;
> +	}
> +
>  	return 0;
>  }

[Severity: Medium]
Can fan_control_attr_group be exposed on SR-IOV VFs where PCODE access
is disabled?

On platforms with both has_fan_control and has_sriov (such as
Battlemage), vf_update_device_info() sets xe->info.skip_pcode = 1 but
leaves xe->info.has_fan_control enabled.  Because pcode_mailbox_rw()
immediately returns 0 when skip_pcode is set,
fan_control_attr_is_visible() sees ret == 0 and exposes
tjmax_fan_control_enable on VFs, where reads report a fabricated value
and writes silently succeed as no-ops.  Guarding registration with
!IS_SRIOV_VF(xe) (or !xe->info.skip_pcode), like
auto_link_downgrade_attr_group and late_bind_attr_group above, would
avoid registering it on VFs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=9

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable
  2026-11-28  5:59 ` [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
@ 2026-11-28  6:13   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:13 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] drm/xe/hwmon: wrong pwm readback in xe_hwmon_pwm_read() for
  non-stock modes
- [Medium] drm/xe/hwmon: xe_hwmon_set_user_fan_pwm() clobbers user table
  in mode 0
- [Low] drm/xe/hwmon: missing point count check in
  xe_hwmon_fan_set_auto()
--

commit 562936f0b505f7da573995f262799f779e7aab25
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe/hwmon: Expose pwm[1-3]_enable

This commit exposes the pwm[1-3]_enable hwmon sysfs attributes to select
between full-speed, manual user-table, and automatic firmware fan control
modes, and documents them in the Xe hwmon ABI.

> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index 78ef926feffcb..b6214ef933165 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c

[ ... ]

> @@ -1209,9 +1230,88 @@ static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
>  			     PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, fan),
>  			     0);
>  	if (ret)
> -		return ret;
> +		goto fail;
>  
>  	fi->fan_table[USER_FAN_TABLE].point_count = 0;
> +	fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
> +	fi->is_full_speed = false;
> +	return 0;

[Severity: Low]
Should xe_hwmon_fan_set_auto() call xe_hwmon_get_fan_point_count() to verify
that firmware cleared the user point count to 0 before marking
XE_FAN_PWM_AUTO_STOCK_TABLE active?  The v5 changelog notes that user point
counts are verified after auto-mode writes as well as table activation, and
both xe_hwmon_activate_user_fan_table() and xe_hwmon_set_user_fan_pwm() do
this check.

[ ... ]

> @@ -2054,12 +2168,20 @@ static int xe_hwmon_get_pwm_from_pmt(struct xe_hwmon *hwmon, int channel, long *
>  		return -EOPNOTSUPP;
>  	}
>  
> -	xe_dbg(hwmon->xe, "fan %d PMT active PWM value: %u\n", channel, pwm);
> +	xe_dbg(hwmon->xe, "fan %d: active pwm from PMT: %u\n", channel, pwm);
>  
>  	*val = pwm;
>  	return 0;
>  }
>  
> +static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
> +{
> +	if (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE)
> +		return STOCK_FAN_TABLE;
> +	else
> +		return USER_FAN_TABLE;
> +}
> +
>  static int
>  xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
>  {
> @@ -2074,8 +2196,9 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
>  	switch (attr) {
>  	case hwmon_pwm_input:
>  		/* If the user fan table is not populated, get active stock pwm */
> -		if (!fi->fan_table[USER_FAN_TABLE].point_count) {
> -			xe_dbg(hwmon->xe, "user fan table not active for fan %d\n", channel);
> +		if (xe_hwmon_get_active_fan_table(hwmon, channel) == STOCK_FAN_TABLE) {
> +			xe_dbg(hwmon->xe, "getting active pwm for fan %d from stock table\n",
> +			       channel);
>  			switch (hwmon->xe->info.platform) {
>  			case XE_BATTLEMAGE:
>  				ret = xe_hwmon_get_pwm_from_pmt(hwmon, channel, &pwm);
> @@ -2084,13 +2207,18 @@ xe_hwmon_pwm_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
>  				ret = xe_hwmon_get_stock_table_pwm(hwmon, channel, &pwm);
>  				break;
>  			}
> -			if (ret)
> +			if (ret) {
> +				xe_err(hwmon->xe, "failed to get active pwm for fan %d, ret=%d\n",
> +				       channel, ret);
>  				goto unlock;
> +			}
>  		} else {
>  			/*
>  			 * pwmN writes set every user-table point to the same speed,
>  			 * so the first point gives the cached manual PWM percentage.
>  			 */
> +			xe_dbg(hwmon->xe, "getting active pwm for fan %d from user table cache\n",
> +			       channel);
>  			pwm = fi->fan_table[USER_FAN_TABLE].fcp[0].speed;
>  		}

[Severity: Medium]
Can reading hwmon_pwm_input return the wrong PWM duty value when
pwm_enable_mode is not XE_FAN_PWM_AUTO_STOCK_TABLE?

When pwm[1-3]_enable is switched from 2 to 1 before any flat write to
pwm[1-3], xe_hwmon_activate_user_fan_table() populates USER_FAN_TABLE from
STOCK_FAN_TABLE with a multi-point temperature curve.  In that state the
points in USER_FAN_TABLE do not share a single speed, so reading
fi->fan_table[USER_FAN_TABLE].fcp[0].speed always reports the lowest
temperature breakpoint speed instead of reading PMT on Battlemage or
interpolating the active table against package temperature.

In addition, xe_hwmon_get_active_fan_table() returns USER_FAN_TABLE when
fi->pwm_enable_mode is XE_FAN_PWM_UNINITIALIZED.  If a mode transition and
its xe_hwmon_fan_set_auto() fallback fail after clearing
fi->fan_table[USER_FAN_TABLE].point_count to 0, reading pwm[1-3] returns
stale or zero fcp[0].speed instead of -ENODATA.

[ ... ]

> @@ -2125,7 +2263,48 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
>  			ret = -EINVAL;
>  			break;
>  		}
> -		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val);
> +		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val, false);
> +		break;
> +	case hwmon_pwm_enable:
> +		if (val == XE_FAN_PWM_FULL_SPEED) {
> +			ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, U8_MAX, true);
> +			if (ret) {
> +				xe_warn(hwmon->xe,
> +					"full speed enable for fan %d failed, ret=%d\n",
> +					channel, ret);
> +				break;
> +			}
> +		} else if (val == XE_FAN_PWM_MANUAL_USER_TABLE) {
> +			enum fan_table_type source = fi->fan_table[USER_FAN_TABLE].point_count ?
> +				USER_FAN_TABLE : STOCK_FAN_TABLE;
> +
> +			/*
> +			 * Manual fan speed control using user fan table.
> +			 * If user fan table is not programmed, it would be initialized with
> +			 * clamped stock points which allows manual control with some
> +			 * default points.
> +			 */
> +			ret = xe_hwmon_activate_user_fan_table(hwmon, channel, false, source);

[Severity: Medium]
Does setting pwm[1-3]_enable to 0 (XE_FAN_PWM_FULL_SPEED) overwrite
fi->fan_table[USER_FAN_TABLE] and leave the fan pinned at 100% when
subsequently switching to mode 1 (XE_FAN_PWM_MANUAL_USER_TABLE)?

Calling xe_hwmon_set_user_fan_pwm(hwmon, channel, U8_MAX, true) replaces
fi->fan_table[USER_FAN_TABLE].fcp[0..1] with 100% speed points and sets
fi->fan_table[USER_FAN_TABLE].point_count to MIN_FAN_CONTROL_POINTS.  When
userspace later writes 1 to pwm[1-3]_enable, point_count is non-zero, so
source becomes USER_FAN_TABLE instead of STOCK_FAN_TABLE (or any previously
configured user table) and xe_hwmon_activate_user_fan_table() re-applies the
100% full-speed table.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=5

^ permalink raw reply	[flat|nested] 18+ messages in thread

* ✗ CI.KUnit: failure for drm/xe/hwmon: Add fan control support (rev2)
  2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (9 preceding siblings ...)
  2026-11-28  5:59 ` [PATCH v5 10/10] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
@ 2026-11-28  6:14 ` Patchwork
  10 siblings, 0 replies; 18+ messages in thread
From: Patchwork @ 2026-11-28  6:14 UTC (permalink / raw)
  To: Karthik Poosa; +Cc: intel-xe

== Series Details ==

Series: drm/xe/hwmon: Add fan control support (rev2)
URL   : https://patchwork.freedesktop.org/series/179435/
State : failure

== Summary ==

+ trap cleanup EXIT
+ kunitconfigs=('/kernel/drivers/gpu/tests/.kunitconfig' '/kernel/drivers/gpu/drm/xe/.kunitconfig' '/kernel/drivers/gpu/drm/tests/.kunitconfig' '/kernel/drivers/gpu/drm/ttm/tests/.kunitconfig' '/kernel/drivers/dma-buf/.kunitconfig')
+ for kcfg in "${kunitconfigs[@]}"
+ [[ ! -f /kernel/drivers/gpu/tests/.kunitconfig ]]
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/tests/.kunitconfig
[06:13:18] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
[06:13:23] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
[06:13:44] Starting KUnit Kernel (1/1)...
[06:13:44] ============================================================
Running tests with:
$ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
[06:13:44] ================= gpu_buddy (15 subtests) ==================
[06:13:44] [PASSED] gpu_test_buddy_alloc_limit
[06:13:44] [PASSED] gpu_test_buddy_alloc_optimistic
[06:13:44] [PASSED] gpu_test_buddy_alloc_pessimistic
[06:13:44] [PASSED] gpu_test_buddy_alloc_pathological
[06:13:44] [PASSED] gpu_test_buddy_alloc_contiguous
[06:13:44] [PASSED] gpu_test_buddy_alloc_clear
[06:13:44] [PASSED] gpu_test_buddy_alloc_range
[06:13:44] [PASSED] gpu_test_buddy_alloc_range_bias
[06:13:44] [PASSED] gpu_test_buddy_alloc_range_clear_guard
[06:13:45] [PASSED] gpu_test_buddy_fragmentation_performance
[06:13:46] [PASSED] gpu_test_buddy_dirty_tracker_performance
[06:13:46] [PASSED] gpu_test_buddy_alloc_exceeds_max_order
[06:13:46] [PASSED] gpu_test_buddy_offset_aligned_allocation
[06:13:46] [PASSED] gpu_test_buddy_subtree_offset_alignment_stress
[06:13:46] [PASSED] gpu_test_buddy_addr_to_block
[06:13:46] ==================== [PASSED] gpu_buddy ====================
[06:13:46] ============================================================
[06:13:46] Testing complete. Ran 15 tests: passed: 15
[06:13:46] Elapsed time: 27.769s total, 5.110s configuring, 20.641s building, 1.985s running

+ for kcfg in "${kunitconfigs[@]}"
+ [[ ! -f /kernel/drivers/gpu/drm/xe/.kunitconfig ]]
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig
ERROR:root:../drivers/gpu/drm/xe/xe_hwmon.c:3096:6: error: redefinition of ‘xe_hwmon_unregister’
 3096 | void xe_hwmon_unregister(struct xe_device *xe)
      |      ^~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_hwmon.c:3070:6: note: previous definition of ‘xe_hwmon_unregister’ with type ‘void(struct xe_device *)’
 3070 | void xe_hwmon_unregister(struct xe_device *xe)
      |      ^~~~~~~~~~~~~~~~~~~
make[7]: *** [../scripts/Makefile.build:290: drivers/gpu/drm/xe/xe_hwmon.o] Error 1
make[7]: *** Waiting for unfinished jobs....
make[6]: *** [../scripts/Makefile.build:572: drivers/gpu/drm/xe] Error 2
make[5]: *** [../scripts/Makefile.build:572: drivers/gpu/drm] Error 2
make[4]: *** [../scripts/Makefile.build:572: drivers/gpu] Error 2
make[3]: *** [../scripts/Makefile.build:572: drivers] Error 2
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [/kernel/Makefile:2244: .] Error 2
make[1]: *** [/kernel/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2

[06:13:46] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
[06:13:48] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume
  2026-11-28  5:59 ` [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
@ 2026-11-28  6:15   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:15 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/xe: synchronous flush_work() in xe_late_bind_pm_resume()
  deadlocks resume
- [Medium] drm/xe/hwmon: xe_hwmon_resume() clears staged fan curve
  points on resume
- [Low] drm/xe/hwmon: duplicate xe_hwmon_fan_set_auto() fallback in
  xe_hwmon_resume()
--

commit 487a525936d402c4983c294401d28e7b960944fa
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe/hwmon: Restore fan user tables after resume

This commit saves active manual and full-speed fan tables before system
suspend, runtime D3cold suspend, and FLR, and restores them on resume
after waiting for late-bound fan-control firmware to reload.

> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index 4d6eb6f6fbc2f..76c2b519a21f7 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c

[ ... ]

> +void xe_hwmon_resume(struct xe_device *xe, bool runtime)
> +{
> +	struct xe_hwmon *hwmon = xe->hwmon;
> +	int fan;
> +
> +	if (!hwmon || !xe->info.has_fan_control)
> +		return;
> +
> +	if (runtime && !xe->d3cold.allowed) {
> +		xe_dbg(xe, "d3cold not allowed, skipping fan user-table restore\n");
> +		return;
> +	}
> +
> +	mutex_lock(&hwmon->hwmon_lock);
> +
> +	for (fan = 0; fan < FAN_MAX; fan++) {
> +		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> +		int ret;
> +
> +		if (!IS_FAN_ENABLED(hwmon, fan))
> +			continue;
> +
> +		if (!fi->restore_user_table) {
> +			xe_dbg(xe, "fan %d user table restore not required\n", fan);
> +			continue;
> +		}
> +
> +		/* Re-program cached user-table settings for fans active before suspend. */
> +		ret = xe_hwmon_activate_user_fan_table(hwmon, fan,
> +						       fi->is_full_speed,
> +						       USER_FAN_TABLE);

[Severity: Medium]
Does calling xe_hwmon_activate_user_fan_table() here discard any
in-progress staged fan curve points across suspend and resume?

Each write to pwmN_auto_pointM_temp or pwmN_auto_pointM_pwm holds a
runtime PM reference only for that single sysfs write while recording
progress in temp_staged and speed_staged.  If the device enters runtime
D3cold (or system suspend) between sysfs writes while a user fan table is
already active, xe_hwmon_resume() calls xe_hwmon_activate_user_fan_table(),
which unconditionally clears both staged bitmasks:

drivers/gpu/drm/xe/xe_hwmon.c:xe_hwmon_activate_user_fan_table() {
    ...
	fi->fan_table[USER_FAN_TABLE].temp_staged = 0;
	fi->fan_table[USER_FAN_TABLE].speed_staged = 0;
    ...
}

Later sysfs writes to finish the curve then see missing prefix bits in
xe_hwmon_update_user_fan_point() and silently fail to commit the new
curve.

> +		if (ret) {
> +			xe_warn(xe, "failed to restore fan %d user table after resume, ret=%d\n",
> +				fan, ret);
> +			fi->fan_table[USER_FAN_TABLE].point_count = 0;
> +			fi->restore_user_table = false;
> +			ret = xe_hwmon_fan_set_auto(hwmon, fan);
> +			if (ret) {
> +				xe_warn(xe, "fan %d stock fallback failed, state unconfirmed, ret=%d\n",
> +					fan, ret);
> +				continue;
> +			}
> +			continue;
> +		}

[Severity: Low]
Does this error path duplicate the stock fallback that
xe_hwmon_activate_user_fan_table() already performs?

xe_hwmon_suspend() only sets restore_user_table when point_count is
within bounds, so xe_hwmon_activate_user_fan_table() only fails here if
writing or verifying the table fails, in which case it has already cleared
point_count and called xe_hwmon_fan_set_auto(hwmon, fan) before returning.

[ ... ]

> diff --git a/drivers/gpu/drm/xe/xe_late_bind_fw.c b/drivers/gpu/drm/xe/xe_late_bind_fw.c
> index ad39383a7e41f..c577e2d516200 100644
> --- a/drivers/gpu/drm/xe/xe_late_bind_fw.c
> +++ b/drivers/gpu/drm/xe/xe_late_bind_fw.c
> @@ -517,10 +517,17 @@ void xe_late_bind_pm_suspend(struct xe_late_bind *late_bind)
>   */
>  void xe_late_bind_pm_resume(struct xe_late_bind *late_bind)
>  {
> +	struct xe_late_bind_fw *fan_control_fw;
> +	bool fan_control_fw_pending;
> +
>  	if (!late_bind->component_added || late_bind->disable)
>  		return;
>  
> +	fan_control_fw = &late_bind->late_bind_fw[XE_LB_FW_FAN_CONTROL];
> +	fan_control_fw_pending = !!fan_control_fw->payload;
>  	xe_late_bind_fw_load(late_bind);
> +	if (fan_control_fw_pending)
> +		flush_work(&fan_control_fw->work);
>  }

[Severity: High]
Does flushing fan_control_fw->work inside xe_late_bind_pm_resume()
deadlock against the child MEI auxiliary device during both system resume
and runtime D3cold resume, ultimately timing out and freeing
fan_control_fw->payload?

On platforms such as Battlemage, heci_gsc_add_device() registers the
xe.mei-gscfi auxiliary device with aux_dev->dev.parent = &pdev->dev.
During system suspend, mei_gsc_pm_suspend() stops the MEI bus and unbinds
the late-bind component, clearing late_bind->component.ops = NULL.  During
system resume, the PM core runs the parent xe_device_resume() callback
before the child aux_dev->dev is allowed to resume:

PM resume context                 kworker (late_bind->wq)
xe_device_resume()
  xe_late_bind_pm_resume()
    xe_late_bind_fw_load()
      queue_work(..., &lbfw->work)
    flush_work(&fan_control_fw->work)
                                  xe_late_bind_work()
                                    /* waits up to 20s for component.ops */
                                    while (!late_bind->component.ops)
                                      msleep(100);
                                    /* mei_gsc_pm_resume() blocked on parent */
                                    drmm_kfree(..., lbfw->payload);
                                    lbfw->payload = NULL;  <-- firmware lost

Runtime resume from D3cold hits a similar dependency cycle:
xe_pm_runtime_resume() runs while pdev->dev.power.runtime_status is
RPM_RESUMING and blocks in flush_work(&fan_control_fw->work).
xe_late_bind_work() calls push_payload() -> mei_lb_push_payload() ->
mei_cldev_enable() -> mei_cl_connect(), which calls
pm_runtime_get(&aux_dev->dev) and waits 15 seconds for
mei_gsc_pm_runtime_resume(), while pm_wq cannot runtime-resume
aux_dev->dev until its parent pdev->dev finishes RPM_RESUMING.

In both paths, resume stalls for 15 to 20 seconds, xe_late_bind_work()
permanently frees fan_control_fw->payload until reboot, and
xe_hwmon_resume() runs without the fan-control firmware loaded.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=7

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding
  2026-11-28  5:59 ` [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
@ 2026-11-28  6:27   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-28  6:27 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] drm/xe/hwmon: refresh error in
  xe_hwmon_read_fan_control_info() wipes state
- [Medium] drm/xe/hwmon: circular locking dependency in
  xe_hwmon_unregister()
- [Low] drm/xe/hwmon: inaccurate kernel-doc on
  xe_hwmon_fan_update_post_lb()
--

commit 66dd9f8eb5bcfd27c5213f0a2c07a58daa0c1bb3
Author: Karthik Poosa <karthik.poosa@intel.com>

drm/xe/hwmon: Refresh fan data after late binding

This commit refreshes cached stock fan tables and maximum RPM after
fan-control firmware loads via late binding, and serializes the refresh
with hwmon registration and teardown.

> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index 76c2b519a21f7..be80717054c18 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c
[ ... ]
> @@ -1806,7 +1816,6 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
>  		if (!IS_FAN_ENABLED(hwmon, fan))
>  			continue;
>  
> -		fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
>  		fi->fan_table[STOCK_FAN_TABLE].point_count = 0;

[Severity: Medium]
When xe_hwmon_fan_update_post_lb() calls xe_hwmon_read_fan_control_info()
after hwmon registration, does a pcode failure wipe the previously cached
fi->fan_table[STOCK_FAN_TABLE].point_count and fi->max_rps?

Clearing point_count and overwriting fi->max_rps in place (which also
passes the old cached fi->max_rps as the PCODE_DATA0 input to
xe_pcode_read()) before verifying that the refresh succeeded leaves
fi->pwm_enable_mode at XE_FAN_PWM_AUTO_STOCK_TABLE and the sysfs
attributes visible, so subsequent reads of fanN_max return 0 and reads
of pwmN fail with -ENODATA.

[ ... ]
> @@ -2979,11 +3002,21 @@ int xe_hwmon_register(struct xe_device *xe)
>   */
>  void xe_hwmon_unregister(struct xe_device *xe)
>  {
> -	if (!xe->hwmon)
> +	struct xe_hwmon *hwmon;
> +
> +	/* Stop updates before unregistering hwmon or unplugging the device. */
> +	xe_late_bind_pm_suspend(&xe->late_bind);
> +
> +	hwmon = READ_ONCE(xe->hwmon);
> +	if (!hwmon)
>  		return;
>  
> -	hwmon_device_unregister(xe->hwmon->hwmon_dev);
> -	xe->hwmon = NULL;
> +	mutex_lock(&hwmon->registration_lock);
> +	if (READ_ONCE(xe->hwmon) == hwmon) {
> +		hwmon_device_unregister(hwmon->hwmon_dev);
> +		WRITE_ONCE(xe->hwmon, NULL);
> +	}
> +	mutex_unlock(&hwmon->registration_lock);
>  }

[Severity: Medium]
Does holding hwmon->registration_lock across hwmon_device_unregister()
create a circular locking dependency in lockdep?

hwmon_device_unregister() drains active sysfs attributes under
kn->dep_map, while hwmon sysfs callbacks (such as xe_hwmon_read())
acquire xe_pm_runtime_d3cold_map via xe_pm_runtime_get().  On resume,
xe_pm_runtime_resume() holds xe_pm_runtime_d3cold_map while calling
xe_late_bind_pm_resume() -> flush_work(&fan_control_fw->work), and
xe_late_bind_work() now calls xe_hwmon_fan_update_post_lb(), which takes
hwmon->registration_lock:

  hwmon->registration_lock -> kn->dep_map -> xe_pm_runtime_d3cold_map ->
  (&fan_control_fw->work) -> hwmon->registration_lock

[ ... ]
> +/**
> + * xe_hwmon_fan_update_post_lb - Update fan info after late binding
> + * @xe: xe device instance
> + *
> + * This function reads the number of fans and their stock table after late binding.
> + */
> +void xe_hwmon_fan_update_post_lb(struct xe_device *xe)
> +{
> +	/* Pair with the release store in xe_hwmon_register(). */
> +	struct xe_hwmon *hwmon = smp_load_acquire(&xe->hwmon);
> +
> +	if (!hwmon || !xe->info.has_fan_control)
> +		return;
> +
> +	guard(mutex)(&hwmon->registration_lock);
> +	if (READ_ONCE(xe->hwmon) != hwmon)
> +		return;
> +
> +	mutex_lock(&hwmon->hwmon_lock);
> +
> +	if (xe_hwmon_read_fan_control_info(hwmon))
> +		xe_warn(hwmon->xe, "failed to read stock fan tables after late binding\n");
> +
> +	mutex_unlock(&hwmon->hwmon_lock);
> +}

[Severity: Low]
This isn't a bug, but the kernel-doc above says
xe_hwmon_fan_update_post_lb() reads the number of fans, whereas it only
calls xe_hwmon_read_fan_control_info() and does not call
xe_hwmon_get_fan_enable_mask().  The v2 changelog in the commit message
also mentions returning an error from xe_hwmon_get_fan_enable_mask() and
keeping the old mask on failure, which is not part of this patch.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128055923.3316956-1-karthik.poosa@intel.com?part=8

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2026-11-28  6:27 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  5:59 [PATCH v5 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
2026-11-28  5:59 ` [PATCH v5 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
2026-11-28  5:59 ` [PATCH v5 02/10] drm/xe/hwmon: Expose fanN_max Karthik Poosa
2026-11-28  5:59 ` [PATCH v5 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
2026-11-28  6:04   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 04/10] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
2026-11-28  5:59 ` [PATCH v5 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
2026-11-28  6:13   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
2026-11-28  6:10   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 07/10] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
2026-11-28  6:15   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 08/10] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
2026-11-28  6:27   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
2026-11-28  6:12   ` sashiko-bot
2026-11-28  5:59 ` [PATCH v5 10/10] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
2026-11-28  6:14 ` ✗ CI.KUnit: failure for drm/xe/hwmon: Add fan control support (rev2) Patchwork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox