All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v9 00/11] drm/xe/hwmon: Add fan control support
@ 2026-12-02 19:52 Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 01/11] drm/xe/hwmon: Rename has_fan_control to has_fan Karthik Poosa
                   ` (10 more replies)
  0 siblings, 11 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

Fan control flows across system and runtime suspend-resume and late-binding
flows also handled.

Series overview

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

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

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.
 - Fans start in pwmN_enable = 2 after probe. pwmN and curve point
   writes need pwmN_enable = 1.
 - A curve has 2 to 10 points. Temperature must increase 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. A completing write that fails drops all
   staged points.
 - The user curve is kept across pwmN_enable 0 and 2 and re-applied
   when switching back to 1.
 - If firmware does not accept a user table, the fan falls back to
   pwmN_enable = 2.

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.

v6:
 - Address v5 https://patchwork.freedesktop.org/series/179435/ review
   comments. (Soham, sashiko)
 - Report the active PWM from pwmN: PMT on BMG, otherwise interpolated
   from the active fan table; 100 in full speed mode; -ENODATA while
   the fan control mode is not established.
 - Keep the cached user curve across pwmN_enable 0 and 2; full speed
   no longer overwrites it. Remove the redundant is_full_speed flag.
 - Write and verify all user tables through one helper that falls back
   to stock control on failure. Reject tables with fewer than two
   points or non-increasing temperatures before writing to firmware.
 - Hide pwmN_auto_point* without a stock table, return -ENODATA from
   them while the mode is not established, and document them in the
   ABI. Keep staged curve points when the user table is re-applied.
 - Explain why fan table writes must not be interleaved with other
   mailbox commands, and the pcode lock hold time.
 - Do not flush the late binding work from resume, which could deadlock
   against the MEI device resume.
 - Keep the previous stock table and max RPM if the refresh after late
   binding fails; re-program user tables of fans in manual or full
   speed mode after late binding.
 - Unregister the hwmon device outside registration_lock to avoid a
   deadlock with late binding work.
 - tjmax_fan_control_enable: do not expose on SR-IOV VFs, report only
   the enable bit, drop the probe-time visibility read and use one
   wording in the commit message, ABI and DOC comment.
 - Align enum kernel-doc with the ABI, add debug logs before -EINVAL
   returns and use "late binding" consistently.
 - Update the fan ABI Date to December 2026.

v7:
 - Rebase.
 - Reorder the TjMax sysfs, suspend/resume, late-binding, and documentation
   patches.
 - 01: Return an error only if no fan has a readable stock table; skip
   unsupported fan-control reads on DG2 and remove the stale min-PWM note.
 - 04: Move fan-table batch construction and verification into the shared
   commit helper; reduce invalid PWM read logging to debug and require a
   usable stock table for pwm visibility. (Badal)
 - 05: Reuse the commit helper for mode changes; return -ENODATA for
   uninitialized BMG PWM reads; simplify user-table cache updates. (Badal)
 - 06: Refactor curve control to use the table helpers moved into earlier
   patches. (Badal)
 - 08: Preserve fan TjMax control across suspend and resume. (Soham)
 - 09: Decouple xe_hwmon and xe_late_bind_fw using a worker. (Badal)
 - 10: Update documentation as per the above changes.
 
v8:
 - Address review comments from sashiko.
 - Add new patch at the start to rename .has_fan_control to .has_fan.
   .has_fan_control will be now used only for fan control only.
 
v9:
 - Rebase on drm-xe-next. Resolve sashiko patch apply error.

Karthik Poosa (11):
  drm/xe/hwmon: Rename has_fan_control to has_fan
  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: Expose tjmax_fan_control_enable device sysfs
  drm/xe/hwmon: Restore fan user tables after resume
  drm/xe/hwmon: Refresh fan data after late binding
  drm/xe/hwmon: Add kernel-doc for fan control

 .../ABI/testing/sysfs-driver-intel-xe-gpu     |   20 +
 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |   82 +
 drivers/gpu/drm/xe/regs/xe_pmt.h              |    5 +
 drivers/gpu/drm/xe/xe_device.c                |    6 +
 drivers/gpu/drm/xe/xe_device_sysfs.c          |   63 +
 drivers/gpu/drm/xe/xe_device_types.h          |    2 +
 drivers/gpu/drm/xe/xe_hwmon.c                 | 1746 +++++++++++++++--
 drivers/gpu/drm/xe/xe_hwmon.h                 |    6 +
 drivers/gpu/drm/xe/xe_pci.c                   |    4 +-
 drivers/gpu/drm/xe/xe_pci_types.h             |    1 +
 drivers/gpu/drm/xe/xe_pcode.c                 |   49 +
 drivers/gpu/drm/xe/xe_pcode.h                 |   12 +
 drivers/gpu/drm/xe/xe_pcode_api.h             |   11 +
 drivers/gpu/drm/xe/xe_pm.c                    |   12 +-
 14 files changed, 1838 insertions(+), 181 deletions(-)

-- 
2.34.1


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

* [PATCH v9 01/11] drm/xe/hwmon: Rename has_fan_control to has_fan
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 02/11] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, Karthik Poosa

The existing has_fan_control flag currently indicates that the device has
a fan and is used to expose the corresponding hwmon fanX_input attribute.
It does not specifically indicate support for controlling the fan.

Rename has_fan_control to has_fan to reflect its actual purpose and avoid
conflating fan presence with fan control capability.

The has_fan_control flag will be added in later patches where fan
control support is introduced.

No functional change.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---
 drivers/gpu/drm/xe/xe_device_types.h | 4 ++--
 drivers/gpu/drm/xe/xe_hwmon.c        | 2 +-
 drivers/gpu/drm/xe/xe_pci.c          | 6 +++---
 drivers/gpu/drm/xe/xe_pci_types.h    | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h
index e38bc28404ff..30ee2248f7fb 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -175,8 +175,8 @@ struct xe_device {
 		u8 has_driver_cper:1;
 		/** @info.has_drm_ras: Device supports drm_ras (Reliability, Availability, Serviceability) */
 		u8 has_drm_ras:1;
-		/** @info.has_fan_control: Device supports fan control */
-		u8 has_fan_control:1;
+		/** @info.has_fan: Device supports fan */
+		u8 has_fan:1;
 		/** @info.has_fixed_vram_channels: Device has fixed VRAM temperature channels */
 		u8 has_fixed_vram_channels:1;
 		/** @info.has_flat_ccs: Whether flat CCS metadata is used */
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 717845b7e6a1..2ff294f43259 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -1634,7 +1634,7 @@ xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 {
 	u32 uval = 0;
 
-	if (!hwmon->xe->info.has_fan_control)
+	if (!hwmon->xe->info.has_fan)
 		return 0;
 
 	switch (attr) {
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index abf0ef80fcb8..2c0ac48c8eeb 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -373,7 +373,7 @@ static const struct xe_device_desc dg2_desc = {
 
 	DG2_FEATURES,
 	.has_display = true,
-	.has_fan_control = true,
+	.has_fan = true,
 	.has_mbx_power_limits = false,
 };
 
@@ -430,7 +430,7 @@ static const struct xe_device_desc bmg_desc = {
 	PLATFORM(BATTLEMAGE),
 	.dma_mask_size = 46,
 	.has_display = true,
-	.has_fan_control = true,
+	.has_fan = true,
 	.has_fixed_vram_channels = true,
 	.has_flat_ccs = 1,
 	.has_mbx_power_limits = true,
@@ -825,7 +825,7 @@ static int xe_info_init_early(struct xe_device *xe,
 	xe->info.has_device_uid = desc->has_device_uid;
 	xe->info.has_driver_cper = desc->has_driver_cper;
 	xe->info.has_drm_ras = desc->has_drm_ras;
-	xe->info.has_fan_control = desc->has_fan_control;
+	xe->info.has_fan = desc->has_fan;
 	xe->info.has_fixed_vram_channels = desc->has_fixed_vram_channels;
 	/* runtime fusing may force flat_ccs to disabled later */
 	xe->info.has_flat_ccs = desc->has_flat_ccs;
diff --git a/drivers/gpu/drm/xe/xe_pci_types.h b/drivers/gpu/drm/xe/xe_pci_types.h
index 177b963bfc5a..aab3812db1ae 100644
--- a/drivers/gpu/drm/xe/xe_pci_types.h
+++ b/drivers/gpu/drm/xe/xe_pci_types.h
@@ -44,7 +44,7 @@ struct xe_device_desc {
 	u8 has_device_uid:1;
 	u8 has_driver_cper:1;
 	u8 has_drm_ras:1;
-	u8 has_fan_control:1;
+	u8 has_fan:1;
 	u8 has_fixed_vram_channels:1;
 	u8 has_flat_ccs:1;
 	u8 has_gsc_nvm:1;
-- 
2.34.1


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

* [PATCH v9 02/11] drm/xe/hwmon: Initialize fan-control backend and table cache
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 01/11] drm/xe/hwmon: Rename has_fan_control to has_fan Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 03/11] drm/xe/hwmon: Expose fanN_max Karthik Poosa
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, Karthik Poosa

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

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

Add and use has_fan_control flag fan control support.

Reviewed-by: Soham Purkait <soham.purkait@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

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)
  - 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.
 - 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.

v6:
 - Avoid the err variable in xe_hwmon_read_fan_control_info(). (Soham)
 - Maintain alphabetical ordering for #include "xe_printk.h". (Soham)
 - Fix parenthesis alignment in xe_hwmon_pcode_read_fan_control(). (Soham)

v7:
 - Return an error from xe_hwmon_read_fan_control_info() only when no
   fans has a stock table, not from the last fan read. (sashiko)
 - Drop stale min PWM mention from the commit message. (sashiko)
 - Avoid fan control reads on DG2 to prevent unsupported-operation
   warnings.

 drivers/gpu/drm/xe/xe_device_types.h |   2 +
 drivers/gpu/drm/xe/xe_hwmon.c        | 168 ++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_pci.c          |   2 +
 drivers/gpu/drm/xe/xe_pci_types.h    |   1 +
 drivers/gpu/drm/xe/xe_pcode_api.h    |   4 +
 5 files changed, 162 insertions(+), 15 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h
index 30ee2248f7fb..39499d48f70f 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -177,6 +177,8 @@ struct xe_device {
 		u8 has_drm_ras:1;
 		/** @info.has_fan: Device supports fan */
 		u8 has_fan:1;
+		/** @info.has_fan_control: Device supports fan control */
+		u8 has_fan_control:1;
 		/** @info.has_fixed_vram_channels: Device has fixed VRAM temperature channels */
 		u8 has_fixed_vram_channels:1;
 		/** @info.has_flat_ccs: Whether flat CCS metadata is used */
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 2ff294f43259..f363ccd9f5e6 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -20,6 +20,7 @@
 #include "xe_mmio.h"
 #include "xe_pcode.h"
 #include "xe_pcode_api.h"
+#include "xe_printk.h"
 #include "xe_sriov.h"
 #include "xe_pm.h"
 #include "xe_vsec.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,112 @@ 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)
+{
+	bool found = false;
+	int point;
+	int ret;
+	int fan;
+
+	if (hwmon->xe->info.platform == XE_DG2)
+		return -ENODATA;
+
+	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);
+			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);
+				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) {
+			xe_warn(hwmon->xe, "fan %d stock table incomplete, expected %u points, got %d\n",
+				fan, stock_fcp_count, point);
+			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;
+		found = true;
+	}
+
+	/* Succeed if any enabled fan's stock table was read. */
+	return found ? 0 : -ENODATA;
 }
 
 static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, int channel,
@@ -1632,17 +1768,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)
 		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 +2007,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) {
+		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 (hwmon->xe->info.has_fan_control && 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_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index 2c0ac48c8eeb..b9552a373d6d 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -431,6 +431,7 @@ static const struct xe_device_desc bmg_desc = {
 	.dma_mask_size = 46,
 	.has_display = true,
 	.has_fan = true,
+	.has_fan_control = true,
 	.has_fixed_vram_channels = true,
 	.has_flat_ccs = 1,
 	.has_mbx_power_limits = true,
@@ -826,6 +827,7 @@ static int xe_info_init_early(struct xe_device *xe,
 	xe->info.has_driver_cper = desc->has_driver_cper;
 	xe->info.has_drm_ras = desc->has_drm_ras;
 	xe->info.has_fan = desc->has_fan;
+	xe->info.has_fan_control = desc->has_fan_control;
 	xe->info.has_fixed_vram_channels = desc->has_fixed_vram_channels;
 	/* runtime fusing may force flat_ccs to disabled later */
 	xe->info.has_flat_ccs = desc->has_flat_ccs;
diff --git a/drivers/gpu/drm/xe/xe_pci_types.h b/drivers/gpu/drm/xe/xe_pci_types.h
index aab3812db1ae..2213cd9fe47d 100644
--- a/drivers/gpu/drm/xe/xe_pci_types.h
+++ b/drivers/gpu/drm/xe/xe_pci_types.h
@@ -45,6 +45,7 @@ struct xe_device_desc {
 	u8 has_driver_cper:1;
 	u8 has_drm_ras:1;
 	u8 has_fan:1;
+	u8 has_fan_control:1;
 	u8 has_fixed_vram_channels:1;
 	u8 has_flat_ccs:1;
 	u8 has_gsc_nvm:1;
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] 15+ messages in thread

* [PATCH v9 03/11] drm/xe/hwmon: Expose fanN_max
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 01/11] drm/xe/hwmon: Rename has_fan_control to has_fan Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 02/11] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 04/11] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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>
Reviewed-by: Soham Purkait <soham.purkait@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                 | 21 ++++++++++++++++++-
 drivers/gpu/drm/xe/xe_pcode_api.h             |  1 +
 3 files changed, 29 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..ac9ccfd47eab 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:		December 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 f363ccd9f5e6..1d7ba1149917 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
 };
 
@@ -1173,14 +1176,25 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 	if (hwmon->xe->info.platform == XE_DG2)
 		return -ENODATA;
 
+	/* 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];
 		u32 stock_fcp_count = 0;
+		u32 max_rps = 0;
 
 		if (!IS_FAN_ENABLED(hwmon, fan))
 			continue;
 
+		ret = xe_hwmon_pcode_read_fan_control(hwmon, FSC_READ_MAX_FAN_RPS, fan,
+						      &max_rps);
+		if (ret) {
+			xe_warn(hwmon->xe, "failed to read fan %d max RPS, ret=%d\n", fan, ret);
+		} else {
+			fi->max_rps = max_rps;
+			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);
@@ -1774,6 +1788,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;
 	}
@@ -1824,6 +1840,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] 15+ messages in thread

* [PATCH v9 04/11] drm/xe/pcode: Add ordered write batch helper
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (2 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 03/11] drm/xe/hwmon: Expose fanN_max Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

Some pcode interfaces keep state across mailbox commands. A fan table is
written one point at a time with FSC_WRITE_FAN_TABLE and loaded by a
final FSC_WRITE_NUM_FAN_CONTROL_POINTS write. pcode does not tie the
staged points to a requester, so fan table writes from another caller
in the middle of the sequence can be merged into it, and the commit then
either fails with illegal data or loads a wrong table.

Serialize all fan control table writes, for every fan, on the pcode
lock rather than per fan. Caller-side locks such as the hwmon lock only
order callers that take them; the pcode lock orders every mailbox user
in the driver.

Lock hold time is the sum of the pcode response times of the batch. Each
write is bounded by PCODE_DEFAULT_TIMEOUT_MS, and a write that times out
ends the batch, so the worst case is count * PCODE_DEFAULT_TIMEOUT_MS
(110 ms for the largest fan table of 10 points plus the count write).
Other pcode users wait on the lock with mutex_lock() and start their own
mailbox timeout only after acquiring it, so a batch delays them but does
not make them time out. This is in line with existing pcode lock users:
xe_pcode_request() holds the lock for up to its base timeout plus 50 ms,
and xe_pcode_init_min_freq_table() holds it across a loop of writes.

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

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Reviewed-by: Soham Purkait <soham.purkait@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

v2:
 - Explain why pcode needs the writes without interleaving, and the
   lock hold time impact on other pcode users. (sashiko)

 drivers/gpu/drm/xe/xe_pcode.c | 49 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode.h | 12 +++++++++
 2 files changed, 61 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_pcode.c b/drivers/gpu/drm/xe/xe_pcode.c
index fa9f45d565cb..f465decbf946 100644
--- a/drivers/gpu/drm/xe/xe_pcode.c
+++ b/drivers/gpu/drm/xe/xe_pcode.c
@@ -124,6 +124,55 @@ 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
+ *
+ * Some pcode interfaces keep state across mailbox commands; e.g. a fan table
+ * is written point by point and loaded by a final point count write. Holding
+ * the pcode lock across all writes keeps each such sequence atomic with
+ * respect to every other mailbox user, including fan table writes for any
+ * other fan, independent of caller-side locks.
+ *
+ * The lock is held for the sum of the pcode response times, bounded by
+ * @count * PCODE_DEFAULT_TIMEOUT_MS; a write that times out ends the batch.
+ * Other pcode users sleep on the lock and start their own mailbox timeout only
+ * once they own it, so they are delayed but do not time out. Keep @count small.
+ *
+ * Stops at the first failure; earlier writes are not reverted.
+ *
+ * Context: Process context. Takes and releases the pcode lock.
+ * 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] 15+ messages in thread

* [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3]
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (3 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 04/11] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:53   ` Purkait, Soham
  2026-12-02 19:52 ` [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

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.

v6:
 - Add couple of warnings. (Soham)

v7:
 - Move user fan-table batch construction and verification into
   new function xe_hwmon_commit_user_fan_table(). (Badal)
 - Keep xe_hwmon_set_user_fan_pwm() responsible for preparing the
   fixed two-point curve and updating the cached user table. (Badal)
 - Log an out-of-range PWM read at debug level, as pollers reading pwmN
   after resume flood dmesg. (Rodrigo)
 - Drop Soham Reviewed-by as refactor occurred.
 - Make pwm node visible only if stock points > MIN_FAN_CONTROL_POINTS.

 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |   9 +
 drivers/gpu/drm/xe/regs/xe_pmt.h              |   5 +
 drivers/gpu/drm/xe/xe_hwmon.c                 | 362 ++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode_api.h             |   3 +
 4 files changed, 379 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index ac9ccfd47eab..34c50f13f0b4 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:		December 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 1d7ba1149917..e77ab0685bb7 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
 };
 
@@ -1166,6 +1170,162 @@ 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;
+
+	*point_count = 0;
+	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;
+}
+
+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 < MIN_FAN_CONTROL_POINTS ||
+	    point_count > MAX_FAN_CONTROL_POINTS) {
+		xe_err(hwmon->xe, "fan %d table needs %d-%d points, got %u\n", fan,
+		       MIN_FAN_CONTROL_POINTS, MAX_FAN_CONTROL_POINTS, point_count);
+		return -EINVAL;
+	}
+
+	for (point = 0; point < point_count; point++) {
+		u8 temp = points[point].temp;
+		u8 speed = points[point].speed;
+
+		/* Firmware requires increasing temperature and non-decreasing speed. */
+		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 not monotonic: %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);
+
+	/* pcode fills the user table from point 0 in write order; the count write commits it. */
+	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_err(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;
+
+		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);
+		/* Firmware may have accepted only part of the table; fall back to stock. */
+		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;
+	}
+
+	xe_dbg(hwmon->xe, "fan %d user table committed successfully with %u points\n", fan,
+	       point_count);
+	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_hwmon_fan_info *fi = &hwmon->fi[fan];
+	struct fan_table *user_table = &fi->fan_table[USER_FAN_TABLE];
+	struct fan_control_point points[MIN_FAN_CONTROL_POINTS];
+	int ret, i;
+	u8 speed;
+
+	/* Fan table speed are in percent. */
+	speed = min_t(u32, DIV_ROUND_CLOSEST(pwm * 100, U8_MAX), 100);
+
+	/* Fan control needs at least two points; program them all at the same speed. */
+	for (i = 0; i < MIN_FAN_CONTROL_POINTS; i++) {
+		points[i].temp = FIXED_PWM_TEMP + i;
+		points[i].speed = speed;
+	}
+
+	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, points, MIN_FAN_CONTROL_POINTS);
+	if (ret) {
+		xe_err(hwmon->xe, "failed to set pwm %u (%u%%) for fan %d, ret=%d\n",
+		       pwm, speed, fan, ret);
+		return ret;
+	}
+
+	/* Update the user fan table cache. */
+	memcpy(user_table->fcp, points, sizeof(points));
+	user_table->point_count = MIN_FAN_CONTROL_POINTS;
+	xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan);
+
+	return 0;
+}
+
 static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 {
 	bool found = false;
@@ -1795,6 +1955,22 @@ 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)
+{
+	/* Manual control starts from the stock table, so it needs a usable curve. */
+	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) ||
+	    hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONTROL_POINTS)
+		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)
 {
@@ -1848,6 +2024,185 @@ 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) {
+		xe_warn(hwmon->xe, "failed to read pwm, PMT unavailable: %d\n", 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)) {
+		xe_warn(hwmon->xe, "pwm read from PMT failed: %d\n", ret);
+		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: active pwm from PMT: %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_dbg(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) {
+			xe_dbg(hwmon->xe, "pwm %ld out of range for fan %d\n", val, channel);
+			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)
@@ -1874,6 +2229,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;
@@ -1903,6 +2261,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;
 	}
@@ -1921,6 +2281,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] 15+ messages in thread

* [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (4 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:53   ` Purkait, Soham
  2026-12-02 19:52 ` [PATCH v9 07/11] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

pwm[1-3] writes now require pwm[1-3]_enable to be 1 first. Since fans
start in mode 2 after probe, a pwm[1-3] write without selecting manual
mode is rejected with -EINVAL.

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

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

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.
 - Fall back to the stock table if firmware does not accept a table.

v6:
- Remove is_full_speed as it is redundant with pwm_enable_mode now.
- Rename xe_hwmon_get_stock_table_pwm() to xe_hwmon_get_active_table_pwm().
- Avoid overwriting user table cache for full speed.
- Align pwm_enable mode kernel-doc with the ABI wording. (Soham)
- Add debug logs before -EINVAL returns. (Soham)
- Note the pwm[1-3] write precondition in the commit message. (Soham)

v7:
 - Use xe_hwmon_commit_user_fan_table() helper in
   xe_hwmon_activate_user_fan_table() for table update. (Badal)
 - Return -ENODATA from pwm[1-3] reads on BMG too while the fan control
   mode is not established. (sashiko)
 - Add debug log if fan control mode is uninitialized. (Rodrigo)
 - Made source_table const in xe_hwmon_commit_user_fan_table().
 - Added a user_table pointer for stock-table cache updates.
 - Avoided redundant cache updates when the source is already
   USER_FAN_TABLE.

 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  19 ++
 drivers/gpu/drm/xe/xe_hwmon.c                 | 196 +++++++++++++++---
 2 files changed, 187 insertions(+), 28 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index 34c50f13f0b4..dbf2c6d01376 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
@@ -354,4 +354,23 @@ 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:		December 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.
+
+		The user fan curve is kept across modes 0 and 2, and re-applied
+		when switching back to 1. Without a user curve, mode 1 starts
+		from the firmware stock curve.
+
 		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 e77ab0685bb7..3561e3d271eb 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: run the fan at full speed */
+	XE_FAN_PWM_FULL_SPEED = 0,
+	/** @XE_FAN_PWM_MANUAL_USER_TABLE: user table control */
+	XE_FAN_PWM_MANUAL_USER_TABLE = 1,
+	/** @XE_FAN_PWM_AUTO_STOCK_TABLE: firmware default behavior using the 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,8 @@ 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;
 };
 
 /**
@@ -926,7 +942,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
 };
 
@@ -1211,11 +1229,14 @@ 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;
-
-	fi->fan_table[USER_FAN_TABLE].point_count = 0;
+		goto fail;
 
+	fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
 	return 0;
+
+fail:
+	fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
+	return ret;
 }
 
 static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
@@ -1291,6 +1312,56 @@ static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
 	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)
+{
+	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+	struct fan_control_point points[MAX_FAN_CONTROL_POINTS];
+	const struct fan_table *source_table;
+	u32 point_count, point;
+	int ret;
+
+	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 %u is invalid, cannot activate user table\n",
+		       fan, (table_source == USER_FAN_TABLE) ? "user" : "stock", point_count);
+		return -EINVAL;
+	}
+
+	for (point = 0; point < point_count; point++) {
+		points[point].temp = source_table->fcp[point].temp;
+		points[point].speed = is_full_speed ? 100 :
+				      min_t(u8, source_table->fcp[point].speed, 100);
+	}
+
+	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, points, point_count);
+	if (ret)
+		return ret;
+
+	/* Full speed only overrides firmware; the cache keeps the manual curve. */
+	if (table_source == STOCK_FAN_TABLE) {
+		struct fan_table *user_table = &fi->fan_table[USER_FAN_TABLE];
+
+		for (point = 0; point < point_count; point++) {
+			user_table->fcp[point].temp = source_table->fcp[point].temp;
+			user_table->fcp[point].speed =
+				min_t(u8, source_table->fcp[point].speed, 100);
+		}
+		user_table->point_count = point_count;
+	}
+
+	fi->pwm_enable_mode = is_full_speed ? XE_FAN_PWM_FULL_SPEED :
+					    XE_FAN_PWM_MANUAL_USER_TABLE;
+
+	return 0;
+}
+
 /* Temperature to set when setting the fan PWM with pwm[1-3]. */
 #define FIXED_PWM_TEMP	1
 
@@ -1302,6 +1373,11 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
 	int ret, i;
 	u8 speed;
 
+	if (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 = min_t(u32, DIV_ROUND_CLOSEST(pwm * 100, U8_MAX), 100);
 
@@ -1401,6 +1477,8 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
 		       stock_fcp_count * sizeof(stock_fcp[0]));
 		fi->fan_table[STOCK_FAN_TABLE].point_count = stock_fcp_count;
 		found = true;
+		if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
+			fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
 	}
 
 	/* Succeed if any enabled fan's stock table was read. */
@@ -1965,6 +2043,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;
@@ -2024,25 +2103,43 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	}
 }
 
+static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
+{
+	return (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE) ? STOCK_FAN_TABLE :
+			USER_FAN_TABLE;
+}
+
 /**
- * xe_hwmon_get_stock_table_pwm - Calculate PWM from the stock fan curve
+ * xe_hwmon_get_active_table_pwm - Calculate PWM from the active 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.
+ * Read package temperature and linearly interpolate the fan table selected by
+ * pwm_enable. Temperatures outside the table range use 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)
+static int xe_hwmon_get_active_table_pwm(struct xe_hwmon *hwmon, int channel, long *val)
 {
-	const struct fan_table *table = &hwmon->fi[channel].fan_table[STOCK_FAN_TABLE];
+	const struct fan_table *table;
 	long temperature, pwm;
 	int point, ret;
 
 	lockdep_assert_held(&hwmon->hwmon_lock);
 
+	switch (hwmon->fi[channel].pwm_enable_mode) {
+	case XE_FAN_PWM_UNINITIALIZED:
+		return -ENODATA;
+	case XE_FAN_PWM_FULL_SPEED:
+		*val = 100;
+		return 0;
+	default:
+		break;
+	}
+
+	table = &hwmon->fi[channel].fan_table[xe_hwmon_get_active_fan_table(hwmon, channel)];
+
 	if (!table->point_count)
 		return -ENODATA;
 
@@ -2070,7 +2167,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: pwm value from active table cache: %ld\n", channel, pwm);
 	return 0;
 }
 
@@ -2139,25 +2236,25 @@ 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);
-			switch (hwmon->xe->info.platform) {
-			case XE_BATTLEMAGE:
+		switch (hwmon->xe->info.platform) {
+		case XE_BATTLEMAGE:
+			if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
+				ret = -ENODATA;
+			else
 				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;
+			break;
+		default:
+			ret = xe_hwmon_get_active_table_pwm(hwmon, channel, &pwm);
+			break;
+		}
+		if (ret == -ENODATA) {
+			xe_dbg(hwmon->xe, "fan %d control mode not established\n", channel);
+			goto unlock;
+		}
+		if (ret) {
+			xe_err(hwmon->xe, "failed to get active pwm for fan %d, ret=%d\n",
+			       channel, ret);
+			goto unlock;
 		}
 		if (pwm < 0 || pwm > 100) {
 			xe_dbg(hwmon->xe, "invalid PWM value %ld read for fan %d\n", pwm, channel);
@@ -2168,6 +2265,14 @@ 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) {
+			xe_dbg(hwmon->xe, "fan %d control mode is not initialized\n", channel);
+			ret = -ENODATA;
+			goto unlock;
+		}
+		*val = fi->pwm_enable_mode;
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
@@ -2181,8 +2286,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) {
@@ -2194,6 +2302,33 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 		}
 		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val);
 		break;
+	case hwmon_pwm_enable:
+		if (val == XE_FAN_PWM_FULL_SPEED || val == XE_FAN_PWM_MANUAL_USER_TABLE) {
+			bool full_speed = val == XE_FAN_PWM_FULL_SPEED;
+			/* Without a user curve, start from the clamped stock table. */
+			enum fan_table_type source = fi->fan_table[USER_FAN_TABLE].point_count ?
+				USER_FAN_TABLE : STOCK_FAN_TABLE;
+
+			ret = xe_hwmon_activate_user_fan_table(hwmon, channel, full_speed, source);
+			if (ret)
+				xe_warn(hwmon->xe, "failed to set fan %d pwm_enable %ld, ret=%d\n",
+					channel, val, ret);
+		} 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 {
+			xe_dbg(hwmon->xe, "invalid pwm%d_enable value %ld\n", channel + 1, val);
+			ret = -EINVAL;
+		}
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
@@ -2407,6 +2542,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 */
@@ -2421,6 +2557,10 @@ int xe_hwmon_register(struct xe_device *xe)
 	if (!hwmon)
 		return -ENOMEM;
 
+	/* Zero is XE_FAN_PWM_FULL_SPEED; stay unset until the stock table is read. */
+	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] 15+ messages in thread

* [PATCH v9 07/11] drm/xe/hwmon: Enable fan curve control support
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (5 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 08/11] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

Stage writes until the curve ends with a (0, 0) marker or all points are
supplied. Validate the curve before programming it; on firmware rejection,
attempt to restore stock mode and return an error.

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

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

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.

v6:
 - Require strictly increasing temperatures across curve points, as
   firmware rejects equal temperatures.
 - Reject tables with fewer than MIN_FAN_CONTROL_POINTS before writing
   to firmware.
 - Expose pwmN_auto_point* only when the fan has a stock table, like
   pwmN_enable.
 - Return -ENODATA from pwmN_auto_point* reads while the fan control
   mode is not established, like pwmN and pwmN_enable.
 - Add the missing ABI documentation for pwm[1-3]_auto_point[1-10]_temp
   and pwm[1-3]_auto_point[1-10]_pwm.
 - Use reverse xmas tree ordering in xe_hwmon_activate_user_fan_table().
   (Soham)
 - Drop the else after return in xe_hwmon_get_active_fan_table().
   (Soham)
 - Keep staged curve points when the user table is re-applied on
   resume or after late binding; clear them only on pwm_enable writes.
   (sashiko)
 - Document that a failed completing curve write drops all staged
   points. (Soham)

v7:
 - xe_hwmon_commit_user_fan_table() and xe_hwmon_activate_user_fan_table()
   are moved to previous patches. Refactor this patch accordingly.

 .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  46 +++
 drivers/gpu/drm/xe/xe_hwmon.c                 | 361 +++++++++++++++++-
 2 files changed, 394 insertions(+), 13 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
index dbf2c6d01376..81c0c8217d4a 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
@@ -374,3 +374,49 @@ Description:	RW. Fan control mode selector.
 		from the firmware stock curve.
 
 		Only supported for particular Intel Xe graphics platforms.
+
+What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/pwm[1-3]_auto_point[1-10]_temp
+Date:		December 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:	RW. Temperature of fan curve point in millidegree Celsius.
+
+		Reads return the point of the stock fan curve when
+		pwm[1-3]_enable is 2 and of the user fan curve otherwise. Unused
+		points read 0. Reads fail with -ENODATA while the fan control
+		mode is not established.
+
+		Writes stage the user curve point and require pwm[1-3]_enable
+		to be 1; otherwise they are rejected with -EINVAL. See
+		pwm[1-3]_auto_point[1-10]_pwm for how the curve is committed.
+
+		Only supported for particular Intel Xe graphics platforms.
+
+What:		/sys/bus/pci/drivers/xe/.../hwmon/hwmon<i>/pwm[1-3]_auto_point[1-10]_pwm
+Date:		December 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:	RW. Fan PWM duty of fan curve point in the range 0..255, where
+		255 is 100% fan speed. Firmware stores the duty in percent, so
+		reads return the value rounded to the nearest percent step.
+
+		Reads return the point of the stock fan curve when
+		pwm[1-3]_enable is 2 and of the user fan curve otherwise. Unused
+		points read 0. Reads fail with -ENODATA while the fan control
+		mode is not established.
+
+		Writes stage the user curve point and require pwm[1-3]_enable
+		to be 1; otherwise they are rejected with -EINVAL. Nonzero
+		values are kept at least 1%.
+
+		The staged curve is programmed once points 1..K all have both a
+		temperature and a PWM value and either point K+1 is set to
+		temperature 0 and PWM 0, or K is 10. The end marker is not part
+		of the curve. The curve needs at least two points, temperature
+		must increase and PWM must not decrease from one point to the
+		next; otherwise the completing write fails with -EINVAL. A
+		completing write that fails drops all staged points. If
+		firmware does not accept the curve, the fan falls back to
+		pwm[1-3]_enable 2 and the write fails.
+
+		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 3561e3d271eb..e0a0c0ba41b7 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;
@@ -266,6 +277,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)
@@ -910,11 +931,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 |
@@ -1397,11 +1413,324 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
 	/* Update the user fan table cache. */
 	memcpy(user_table->fcp, points, sizeof(points));
 	user_table->point_count = MIN_FAN_CONTROL_POINTS;
+	user_table->temp_staged = 0;
+	user_table->speed_staged = 0;
 	xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan);
 
 	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)
+{
+	return (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE) ? STOCK_FAN_TABLE :
+			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];
+	if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
+		return -ENODATA;
+
+	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;
+
+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) {
+		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];
+	if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
+		return -ENODATA;
+
+	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) {
+		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;
+
+	/* Manual mode, and thus curve writes, need a stock table, as for pwmN_enable. */
+	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, fan) ||
+	    hwmon->fi[fan].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONTROL_POINTS)
+		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)
 {
 	bool found = false;
@@ -2103,12 +2432,6 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
 	}
 }
 
-static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
-{
-	return (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE) ? STOCK_FAN_TABLE :
-			USER_FAN_TABLE;
-}
-
 /**
  * xe_hwmon_get_active_table_pwm - Calculate PWM from the active fan curve
  * @hwmon: hwmon instance, with hwmon_lock held and runtime PM active
@@ -2310,9 +2633,13 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
 				USER_FAN_TABLE : STOCK_FAN_TABLE;
 
 			ret = xe_hwmon_activate_user_fan_table(hwmon, channel, full_speed, source);
-			if (ret)
+			if (ret) {
 				xe_warn(hwmon->xe, "failed to set fan %d pwm_enable %ld, ret=%d\n",
 					channel, val, 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) {
@@ -2324,6 +2651,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 {
 			xe_dbg(hwmon->xe, "invalid pwm%d_enable value %ld\n", channel + 1, val);
 			ret = -EINVAL;
@@ -2575,10 +2904,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] 15+ messages in thread

* [PATCH v9 08/11] drm/xe: Expose tjmax_fan_control_enable device sysfs
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (6 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 07/11] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 09/11] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, Karthik Poosa

Add a device attribute to read and configure firmware TjMax fan control,
which forces maximum fan PWM as SoC temperature approaches TjMax or
GDDR/VR temperature approaches its thermal maximum.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

v2:
 - Expose this attribute only if CONFIG_HWMON is defined.(sashiko)
 - Change month to December in sysfs ABI documentation.
 - Remove wrong comment for fan_control_attr_group. Tjmax fan control
   support cannot be enable with late binding.

 .../ABI/testing/sysfs-driver-intel-xe-gpu     | 20 ++++++
 drivers/gpu/drm/xe/xe_device_sysfs.c          | 63 +++++++++++++++++++
 drivers/gpu/drm/xe/xe_pcode_api.h             |  3 +
 3 files changed, 86 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu b/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
index b563b7e8a9d9..2ff5939af249 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-gpu
@@ -21,6 +21,26 @@ 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:		December 2026
+KernelVersion:	7.5
+Contact:	intel-xe@lists.freedesktop.org
+Description:
+		RW. Firmware TjMax fan control.
+
+		Write 1 to enable or 0 to disable firmware TjMax fan control,
+		which forces maximum fan PWM as SoC temperature approaches
+		TjMax or GDDR/VR temperature approaches its thermal maximum.
+		Reads return 1 if enabled and 0 otherwise.
+
+		This mode is applied by firmware on top of the hwmon
+		pwm[1-3]_enable selection; hwmon pwm[1-3] and
+		pwm[1-3]_enable do not reflect the override.
+
+		This sysfs file is present only on devices with fan control
+		support, except SR-IOV VFs. Accesses fail with the firmware
+		error if the command is not supported.
+
 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 9b734aeefd33..29bdb6f08ae7 100644
--- a/drivers/gpu/drm/xe/xe_device_sysfs.c
+++ b/drivers/gpu/drm/xe/xe_device_sysfs.c
@@ -35,6 +35,11 @@
  * 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, which
+ * forces maximum fan PWM as SoC temperature approaches TjMax or GDDR/VR
+ * temperature approaches its thermal maximum. Exposed on devices with fan
+ * control support, except SR-IOV VFs.
+ *
  * 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
@@ -209,6 +214,58 @@ 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 & TJMAX_FAN_CONTROL_ENABLE));
+}
+
+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[] = {
+#if IS_REACHABLE(CONFIG_HWMON)
+	&dev_attr_tjmax_fan_control_enable.attr,
+#endif
+	NULL
+};
+
+static const struct attribute_group fan_control_attr_group = {
+	.attrs = fan_control_attrs,
+};
+
 /**
  * DOC: PCIe Gen5 Limitations
  *
@@ -463,5 +520,11 @@ int xe_device_sysfs_init(struct xe_device *xe)
 			return ret;
 	}
 
+	if (xe->info.has_fan_control && !IS_SRIOV_VF(xe)) {
+		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..a217256f9e9d 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -95,6 +95,9 @@
 #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       TJMAX_FAN_CONTROL_ENABLE		REG_BIT(0)
 
 #define PCODE_SCRATCH(x)		XE_REG(0x138320 + ((x) * 4))
 /* PCODE_SCRATCH0 */
-- 
2.34.1


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

* [PATCH v9 09/11] drm/xe/hwmon: Restore fan user tables after resume
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (7 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 08/11] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 10/11] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, Karthik Poosa

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

On resume, restore the cached table without waiting for the late-bound
fan-control firmware, which reloads asynchronously. If restoration fails,
fall back to stock fan control.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

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.

v5:
 - Do not flush the late-binding work from xe_late_bind_pm_resume();
   it can deadlock against the MEI device resume. (sashiko)
 - Drop the duplicate stock fallback in xe_hwmon_resume(). (sashiko)

v6: Preserve fan TjMax control across suspend and resume. (Soham)

 drivers/gpu/drm/xe/xe_device.c    |   3 +
 drivers/gpu/drm/xe/xe_hwmon.c     | 145 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hwmon.h     |   4 +
 drivers/gpu/drm/xe/xe_pcode_api.h |   6 +-
 drivers/gpu/drm/xe/xe_pm.c        |  11 ++-
 5 files changed, 163 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 86de5f183c0a..d11e01ce50ca 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -1425,6 +1425,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);
 
@@ -1555,6 +1557,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 e0a0c0ba41b7..94825d1f2928 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -153,6 +153,18 @@ enum xe_fan_pwm_enable_mode {
 	XE_FAN_PWM_UNINITIALIZED = 0xff,
 };
 
+/**
+ * enum xe_tjmax_fan_control_state - saved TjMax fan-control state
+ */
+enum xe_tjmax_fan_control_state {
+	/** @XE_TJMAX_FAN_CONTROL_UNKNOWN: no state to restore */
+	XE_TJMAX_FAN_CONTROL_UNKNOWN,
+	/** @XE_TJMAX_FAN_CONTROL_DISABLED: state was disabled before suspend */
+	XE_TJMAX_FAN_CONTROL_DISABLED,
+	/** @XE_TJMAX_FAN_CONTROL_ENABLED: state was enabled before suspend */
+	XE_TJMAX_FAN_CONTROL_ENABLED,
+};
+
 /**
  * enum fan_table_type - fan table types tracked per fan
  */
@@ -217,6 +229,8 @@ struct xe_hwmon_fan_info {
 	u32 max_rps;
 	/** @pwm_enable_mode: current xe_fan_pwm_enable_mode */
 	u8 pwm_enable_mode;
+	/** @restore_user_table: restore user fan table on next resume */
+	bool restore_user_table;
 };
 
 /**
@@ -277,6 +291,8 @@ struct xe_hwmon {
 	u32 pl2_on_boot[CHANNEL_MAX];
 	/** @temp: Temperature info */
 	struct xe_hwmon_thermal_info temp;
+	/** @tjmax_fan_control_state: saved TjMax fan-control state */
+	enum xe_tjmax_fan_control_state tjmax_fan_control_state;
 	/** @curve_sda: fan curve sensor attributes */
 	struct sensor_device_attribute curve_sda[XE_CURVE_ATTRS];
 	/** @curve_names: names of the fan curve sensor attributes */
@@ -2886,6 +2902,8 @@ int xe_hwmon_register(struct xe_device *xe)
 	if (!hwmon)
 		return -ENOMEM;
 
+	hwmon->tjmax_fan_control_state = XE_TJMAX_FAN_CONTROL_UNKNOWN;
+
 	/* Zero is XE_FAN_PWM_FULL_SPEED; stay unset until the stock table is read. */
 	for (fan = 0; fan < FAN_MAX; fan++)
 		hwmon->fi[fan].pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
@@ -2942,4 +2960,131 @@ 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 = READ_ONCE(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 state snapshot\n");
+		return;
+	}
+
+	guard(mutex)(&hwmon->hwmon_lock);
+
+	if (!runtime || xe->d3cold.allowed) {
+		u32 val = 0;
+		int ret;
+
+		hwmon->tjmax_fan_control_state = XE_TJMAX_FAN_CONTROL_UNKNOWN;
+
+		ret = xe_pcode_read(xe_device_get_root_tile(xe),
+				    PCODE_MBOX(FAN_SPEED_CONTROL,
+					       FSC_READ_TJMAX_FAN_CONTROL_MODE, 0),
+				    &val, NULL);
+		if (ret) {
+			xe_warn(xe, "failed to save fan tjmax control, ret=%d\n", ret);
+		} else {
+			hwmon->tjmax_fan_control_state =
+				(val & TJMAX_FAN_CONTROL_ENABLE) ?
+				XE_TJMAX_FAN_CONTROL_ENABLED :
+				XE_TJMAX_FAN_CONTROL_DISABLED;
+		}
+	}
+
+	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);
+	}
+}
+
+/**
+ * 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 = READ_ONCE(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 state restore\n");
+		return;
+	}
+
+	guard(mutex)(&hwmon->hwmon_lock);
+
+	if (hwmon->tjmax_fan_control_state != XE_TJMAX_FAN_CONTROL_UNKNOWN) {
+		bool enable = hwmon->tjmax_fan_control_state == XE_TJMAX_FAN_CONTROL_ENABLED;
+		int ret = xe_pcode_write(xe_device_get_root_tile(xe),
+					 PCODE_MBOX(FAN_SPEED_CONTROL,
+						    FSC_WRITE_TJMAX_FAN_CONTROL_MODE, 0),
+					 enable);
+		if (ret)
+			xe_warn(xe, "failed to restore fan tjmax control, ret=%d\n", ret);
+		else
+			hwmon->tjmax_fan_control_state = XE_TJMAX_FAN_CONTROL_UNKNOWN;
+	}
+
+	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->pwm_enable_mode == XE_FAN_PWM_FULL_SPEED,
+						       USER_FAN_TABLE);
+		fi->restore_user_table = false;
+		if (ret) {
+			xe_warn(xe, "failed to restore fan %d user table after resume, ret=%d\n",
+				fan, ret);
+			continue;
+		}
+
+		xe_dbg(xe, "fan %d user table restored\n", fan);
+	}
+}
+
 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_pcode_api.h b/drivers/gpu/drm/xe/xe_pcode_api.h
index a217256f9e9d..b950bb064b01 100644
--- a/drivers/gpu/drm/xe/xe_pcode_api.h
+++ b/drivers/gpu/drm/xe/xe_pcode_api.h
@@ -93,11 +93,11 @@
 #define     FSC_READ_STOCK_FAN_CONTROL_POINTS	0x5
 #define     FSC_READ_USER_FAN_CONTROL_POINTS	0x6
 #define     FSC_READ_FAN_TABLE			0x7
+#define     FSC_WRITE_TJMAX_FAN_CONTROL_MODE	0x8
+#define     FSC_READ_TJMAX_FAN_CONTROL_MODE		0x9
+#define     TJMAX_FAN_CONTROL_ENABLE		REG_BIT(0)
 #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       TJMAX_FAN_CONTROL_ENABLE		REG_BIT(0)
 
 #define PCODE_SCRATCH(x)		XE_REG(0x138320 + ((x) * 4))
 /* PCODE_SCRATCH0 */
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] 15+ messages in thread

* [PATCH v9 10/11] drm/xe/hwmon: Refresh fan data after late binding
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (8 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 09/11] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:52 ` [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
  10 siblings, 0 replies; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, Karthik Poosa

Refresh cached stock fan tables and maximum RPM for registered fans after
fan-control firmware loads. Keep the previous stock table if a complete
table cannot be read, and keep the previous maximum RPM if its read fails.
Re-program user tables for fans in manual or full-speed mode.

Serialize refresh with hwmon registration and teardown. Protect cached
fan data reads against concurrent late-binding updates. Unregister hwmon
outside registration_lock to avoid deadlocking sysfs callbacks with
late-binding work. Use drmm for hwmon state shared with the late-binding
worker so it remains valid until DRM device release.

Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

v2:
 - Compare lbfw->id with XE_LB_FW_FAN_CONTROL. (Soham)
 - Use xe already in scope. (Soham)
 - Spell out "late binding" in warnings. (Soham)
 - Start fans enabled after late binding in automatic mode.

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

v4:
 - Keep the previous stock table if the refresh does not read a
   complete one.
 - Re-program user tables of fans in manual or full-speed mode after
   late binding.
 - Unregister the hwmon device outside registration_lock, so sysfs
   callbacks resuming the device cannot deadlock with late binding work.
 - Keep the previous max RPM if the refresh read fails.

v5:
 - Fix xe_hwmon_fan_late_binding_update() kernel-doc; it does not read
   the number of fans. (sashiko)
 - Drop a v2 note that does not belong to this patch. (sashiko)
 - Use "late binding" consistently and rename
   xe_hwmon_fan_update_post_lb() to xe_hwmon_fan_late_binding_update().
   (Soham)
 - Use guards for hwmon_lock. (Soham)

v6:
 - Move late-binding status polling and fan-data refresh into hwmon-owned
   delayed work.
 - Poll V1_FAN_PROVISIONED after hwmon registration and resume; avoid
   waking a runtime-suspended GPU and cancel the work during unregister.
 - Remove the direct dependency from xe_late_bind_fw.c to xe_hwmon.c.
 - Read maximum RPM into a temporary value and publish it only after
   success, preserving the previous cache on mailbox read failure.
 - Allocate shared hwmon state and locks with drmm so they remain valid
   until DRM device release.
 - Protect fan cache visibility checks with hwmon_lock.

v7:
 - Address review comments form sashiko.
 - Gate fan provisioning polling on the fan late-binding capability.
 - Cancel polling synchronously before suspend/FLR state snapshots.
 - Restart polling before the D3hot resume early return.
 - Delay the first resume check and retry inactive runtime-PM acquisitions
   within the existing retry limit; release only acquired PM references.
 - Replace deprecated system_wq with system_dfl_wq.
 - Remove the stale acquire/release pairing comment and use WRITE_ONCE()
   to publish xe->hwmon.
 - Add few debug logs in fan late binding flow.

 drivers/gpu/drm/xe/xe_device.c |   3 +
 drivers/gpu/drm/xe/xe_hwmon.c  | 188 +++++++++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_hwmon.h  |   2 +
 drivers/gpu/drm/xe/xe_pm.c     |   1 +
 4 files changed, 186 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index d11e01ce50ca..5decebc0361a 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -1152,6 +1152,8 @@ void xe_device_shutdown(struct xe_device *xe)
 
 	drm_dbg(&xe->drm, "Shutting down device\n");
 
+	xe_hwmon_unregister(xe);
+
 	xe_display_shutdown(xe);
 
 	xe_irq_suspend(xe);
@@ -1465,6 +1467,7 @@ int xe_device_suspend(struct xe_device *xe, bool hibernate, bool flr)
 err_display:
 	xe_display_pm_resume(xe);
 	xe_pxp_pm_resume(xe->pxp);
+	xe_hwmon_suspend_abort(xe);
 	return err;
 }
 
diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 94825d1f2928..0407f6f1ab45 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -9,6 +9,7 @@
 #include <linux/overflow.h>
 #include <linux/types.h>
 #include <linux/units.h>
+#include <linux/workqueue.h>
 
 #include <drm/drm_managed.h>
 #include "regs/xe_gt_regs.h"
@@ -16,6 +17,7 @@
 #include "regs/xe_pcode_regs.h"
 #include "xe_device.h"
 #include "xe_hwmon.h"
+#include "xe_late_bind_fw_types.h"
 #include "xe_log.h"
 #include "xe_mmio.h"
 #include "xe_pcode.h"
@@ -183,6 +185,9 @@ enum fan_table_type {
 /* Maximum number of fan control points supported by each fan. */
 #define MAX_FAN_CONTROL_POINTS	10
 
+#define XE_FAN_LATE_BIND_POLL_INTERVAL	msecs_to_jiffies(1000)
+#define XE_FAN_LATE_BIND_POLL_RETRIES	30
+
 /* Minimum number of points required for a user fan table. */
 #define MIN_FAN_CONTROL_POINTS	2
 
@@ -269,6 +274,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 +300,10 @@ struct xe_hwmon {
 	struct xe_hwmon_thermal_info temp;
 	/** @tjmax_fan_control_state: saved TjMax fan-control state */
 	enum xe_tjmax_fan_control_state tjmax_fan_control_state;
+	/** @fan_late_bind_work: polls for fan firmware provisioning */
+	struct delayed_work fan_late_bind_work;
+	/** @fan_late_bind_poll_count: polls since the fan firmware check was queued */
+	u8 fan_late_bind_poll_count;
 	/** @curve_sda: fan curve sensor attributes */
 	struct sensor_device_attribute curve_sda[XE_CURVE_ATTRS];
 	/** @curve_names: names of the fan curve sensor attributes */
@@ -1703,6 +1714,8 @@ static umode_t xe_hwmon_curve_attributes_visible(struct kobject *kobj,
 	struct xe_hwmon *hwmon = dev_get_drvdata(dev);
 	u32 fan = index / XE_CURVE_ATTRS_PER_FAN;
 
+	guard(mutex)(&hwmon->hwmon_lock);
+
 	/* Manual mode, and thus curve writes, need a stock table, as for pwmN_enable. */
 	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, fan) ||
 	    hwmon->fi[fan].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONTROL_POINTS)
@@ -2372,7 +2385,10 @@ xe_hwmon_fan_is_visible(struct xe_hwmon *hwmon, u32 attr, int channel)
 	case hwmon_fan_input:
 		return IS_FAN_ENABLED(hwmon, channel) ? 0444 : 0;
 	case hwmon_fan_max:
+	{
+		guard(mutex)(&hwmon->hwmon_lock);
 		return IS_FAN_ENABLED(hwmon, channel) && hwmon->fi[channel].max_rps ? 0444 : 0;
+	}
 	default:
 		return 0;
 	}
@@ -2381,6 +2397,8 @@ 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)
 {
+	guard(mutex)(&hwmon->hwmon_lock);
+
 	/* Manual control starts from the stock table, so it needs a usable curve. */
 	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) ||
 	    hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONTROL_POINTS)
@@ -2441,7 +2459,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:
-		*val = mul_u32_u32(hwmon->fi[channel].max_rps, 60);
+		scoped_guard(mutex, &hwmon->hwmon_lock) {
+			*val = mul_u32_u32(hwmon->fi[channel].max_rps, 60);
+		}
 		return 0;
 	default:
 		return -EOPNOTSUPP;
@@ -2883,6 +2903,8 @@ xe_hwmon_get_preregistration_info(struct xe_hwmon *hwmon)
 	return xe_hwmon_init_temp_info(hwmon);
 }
 
+static void xe_hwmon_fan_late_bind_work(struct work_struct *work);
+
 int xe_hwmon_register(struct xe_device *xe)
 {
 	struct device *dev = xe->drm.dev;
@@ -2898,9 +2920,11 @@ int xe_hwmon_register(struct xe_device *xe)
 	if (IS_SRIOV_VF(xe))
 		return 0;
 
-	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
+	/* drmm keeps this state alive until late binding work is gone at device release. */
+	hwmon = drmm_kzalloc(&xe->drm, sizeof(*hwmon), GFP_KERNEL);
 	if (!hwmon)
 		return -ENOMEM;
+	INIT_DELAYED_WORK(&hwmon->fan_late_bind_work, xe_hwmon_fan_late_bind_work);
 
 	hwmon->tjmax_fan_control_state = XE_TJMAX_FAN_CONTROL_UNKNOWN;
 
@@ -2908,13 +2932,19 @@ int xe_hwmon_register(struct xe_device *xe)
 	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);
+	ret = drmm_mutex_init(&xe->drm, &hwmon->hwmon_lock);
 	if (ret)
 		return ret;
 
+	ret = drmm_mutex_init(&xe->drm, &hwmon->registration_lock);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&hwmon->registration_lock);
+
 	/* There's only one instance of hwmon per device */
 	hwmon->xe = xe;
-	xe->hwmon = hwmon;
+	WRITE_ONCE(xe->hwmon, hwmon);
 
 	ret = xe_hwmon_get_preregistration_info(hwmon);
 	if (ret)
@@ -2937,11 +2967,17 @@ int xe_hwmon_register(struct xe_device *xe)
 		xe_log_err(xe, HWMON, ret, "Failed to register xe hwmon\n");
 		goto err_null_hwmon;
 	}
+	if (xe->info.has_fan_control &&
+	    (xe->info.late_bind_mask & BIT(XE_LB_FW_FAN_CONTROL))) {
+		hwmon->fan_late_bind_poll_count = 0;
+		xe_dbg(xe, "queue fan firmware provisioning check\n");
+		mod_delayed_work(system_dfl_wq, &hwmon->fan_late_bind_work, 0);
+	}
 
 	return 0;
 
 err_null_hwmon:
-	xe->hwmon = NULL;
+	WRITE_ONCE(xe->hwmon, NULL);
 	return ret;
 }
 
@@ -2954,11 +2990,27 @@ int xe_hwmon_register(struct xe_device *xe)
  */
 void xe_hwmon_unregister(struct xe_device *xe)
 {
-	if (!xe->hwmon)
+	struct xe_hwmon *hwmon;
+
+	hwmon = READ_ONCE(xe->hwmon);
+	if (!hwmon)
+		return;
+
+	/* An in-flight late binding update finishes before xe->hwmon is cleared. */
+	mutex_lock(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) != hwmon) {
+		mutex_unlock(&hwmon->registration_lock);
 		return;
+	}
+	WRITE_ONCE(xe->hwmon, NULL);
+	mutex_unlock(&hwmon->registration_lock);
+	cancel_delayed_work_sync(&hwmon->fan_late_bind_work);
 
-	hwmon_device_unregister(xe->hwmon->hwmon_dev);
-	xe->hwmon = NULL;
+	/*
+	 * Sysfs callbacks may wait on hwmon_lock held by a late-bind update
+	 * that needs registration_lock.
+	 */
+	hwmon_device_unregister(hwmon->hwmon_dev);
 }
 
 /**
@@ -2976,6 +3028,8 @@ void xe_hwmon_suspend(struct xe_device *xe, bool runtime)
 	if (!hwmon || !xe->info.has_fan_control)
 		return;
 
+	cancel_delayed_work_sync(&hwmon->fan_late_bind_work);
+
 	/* Runtime suspend keeps the fan tables unless the device enters d3cold. */
 	if (runtime && !xe->d3cold.allowed) {
 		xe_dbg(xe, "d3cold not allowed, skipping fan state snapshot\n");
@@ -3026,6 +3080,35 @@ void xe_hwmon_suspend(struct xe_device *xe, bool runtime)
 	}
 }
 
+static void xe_hwmon_fan_late_bind_restart(struct xe_hwmon *hwmon)
+{
+	struct xe_device *xe = hwmon->xe;
+
+	mutex_lock(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) == hwmon &&
+	    (xe->info.late_bind_mask & BIT(XE_LB_FW_FAN_CONTROL))) {
+		hwmon->fan_late_bind_poll_count = 0;
+		xe_dbg(xe, "restart fan firmware provisioning checks\n");
+		mod_delayed_work(system_dfl_wq, &hwmon->fan_late_bind_work,
+				 XE_FAN_LATE_BIND_POLL_INTERVAL);
+	}
+	mutex_unlock(&hwmon->registration_lock);
+}
+
+/**
+ * xe_hwmon_suspend_abort - Restart fan provisioning checks after failed suspend
+ * @xe: xe device instance
+ */
+void xe_hwmon_suspend_abort(struct xe_device *xe)
+{
+	struct xe_hwmon *hwmon = READ_ONCE(xe->hwmon);
+
+	if (!hwmon || !xe->info.has_fan_control)
+		return;
+
+	xe_hwmon_fan_late_bind_restart(hwmon);
+}
+
 /**
  * xe_hwmon_resume - Restore hwmon state after resume
  * @xe: xe device instance
@@ -3041,6 +3124,8 @@ void xe_hwmon_resume(struct xe_device *xe, bool runtime)
 	if (!hwmon || !xe->info.has_fan_control)
 		return;
 
+	xe_hwmon_fan_late_bind_restart(hwmon);
+
 	if (runtime && !xe->d3cold.allowed) {
 		xe_dbg(xe, "d3cold not allowed, skipping fan state restore\n");
 		return;
@@ -3087,4 +3172,91 @@ void xe_hwmon_resume(struct xe_device *xe, bool runtime)
 	}
 }
 
+static void xe_hwmon_refresh_fan_data(struct xe_hwmon *hwmon)
+{
+	struct xe_device *xe = hwmon->xe;
+	int fan;
+
+	guard(mutex)(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) != hwmon)
+		return;
+
+	guard(mutex)(&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");
+
+	for (fan = 0; fan < FAN_MAX; fan++) {
+		struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
+		int ret;
+
+		if (!IS_FAN_ENABLED(hwmon, fan) ||
+		    (fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE &&
+		     fi->pwm_enable_mode != XE_FAN_PWM_FULL_SPEED))
+			continue;
+
+		ret = xe_hwmon_activate_user_fan_table(hwmon, fan,
+						       fi->pwm_enable_mode == XE_FAN_PWM_FULL_SPEED,
+						       USER_FAN_TABLE);
+		if (ret)
+			xe_warn(hwmon->xe,
+				"failed to restore fan %d user table after late binding, ret=%d\n",
+				fan, ret);
+	}
+}
+
+static void xe_hwmon_fan_late_bind_work(struct work_struct *work)
+{
+	struct xe_hwmon *hwmon = container_of(to_delayed_work(work),
+						      struct xe_hwmon, fan_late_bind_work);
+	struct xe_device *xe = hwmon->xe;
+	const char *retry_reason;
+	u32 status = 0;
+	bool active;
+	int ret = 0;
+
+	active = xe_pm_runtime_get_if_active(xe);
+	if (!active) {
+		retry_reason = "device is not runtime active";
+		goto retry;
+	}
+
+	ret = xe_pcode_read(xe_device_get_root_tile(xe),
+			    PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
+			    &status, NULL);
+	if (ret) {
+		retry_reason = "failed to read fan firmware provisioning status";
+		goto retry;
+	}
+
+	if (!(status & V1_FAN_SUPPORTED)) {
+		xe_dbg(xe, "fan firmware does not support fan control (status=%#x)\n", status);
+		goto out;
+	}
+
+	if (status & V1_FAN_PROVISIONED) {
+		xe_dbg(xe, "fan firmware provisioned (status=%#x); refreshing fan data\n", status);
+		xe_hwmon_refresh_fan_data(hwmon);
+		goto out;
+	}
+
+	retry_reason = "fan firmware not provisioned yet";
+retry:
+	mutex_lock(&hwmon->registration_lock);
+	if (READ_ONCE(xe->hwmon) == hwmon &&
+	    ++hwmon->fan_late_bind_poll_count < XE_FAN_LATE_BIND_POLL_RETRIES) {
+		xe_dbg(xe, "retry fan firmware provisioning check: %s (ret=%d, status=%#x, poll %u/%u)\n",
+		       retry_reason, ret, status, hwmon->fan_late_bind_poll_count,
+		       XE_FAN_LATE_BIND_POLL_RETRIES);
+		mod_delayed_work(system_dfl_wq, &hwmon->fan_late_bind_work,
+				 XE_FAN_LATE_BIND_POLL_INTERVAL);
+	} else if (READ_ONCE(xe->hwmon) == hwmon) {
+		xe_dbg(xe, "timed out waiting for fan firmware provisioning\n");
+	}
+	mutex_unlock(&hwmon->registration_lock);
+out:
+	if (active)
+		xe_pm_runtime_put(xe);
+}
+
 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..4eba2bbcc469 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.h
+++ b/drivers/gpu/drm/xe/xe_hwmon.h
@@ -14,11 +14,13 @@ struct xe_device;
 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_suspend_abort(struct xe_device *xe);
 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_suspend_abort(struct xe_device *xe) {}
 static inline void xe_hwmon_resume(struct xe_device *xe, bool runtime) {}
 #endif
 
diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
index 4d4b398cbeda..65728995cb81 100644
--- a/drivers/gpu/drm/xe/xe_pm.c
+++ b/drivers/gpu/drm/xe/xe_pm.c
@@ -608,6 +608,7 @@ int xe_pm_runtime_suspend(struct xe_device *xe)
 	xe_pm_update_pme_enabled(xe, false);
 	xe_display_pm_runtime_resume(xe);
 	xe_pxp_pm_resume(xe->pxp);
+	xe_hwmon_suspend_abort(xe);
 out:
 	xe_rpm_lockmap_release(xe);
 	xe_pm_write_callback_task(xe, NULL);
-- 
2.34.1


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

* [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control
  2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
                   ` (9 preceding siblings ...)
  2026-12-02 19:52 ` [PATCH v9 10/11] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
@ 2026-12-02 19:52 ` Karthik Poosa
  2026-12-02 19:55   ` Purkait, Soham
  10 siblings, 1 reply; 15+ messages in thread
From: Karthik Poosa @ 2026-12-02 19:52 UTC (permalink / raw)
  To: intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav,
	soham.purkait, 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.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
---

v2:
 -Update the fan-control documentation to describe late-binding visibility
  refresh, staged point-prefix curves, suspended and restored user tables,
  and TjMax fan-control preservation across suspend and resume.

v3:
- Update pwmN that it reports active pwm duty cycle.
- Update pwmN_enable that it return -ENODATA when control mode is
  uninitialized.

 drivers/gpu/drm/xe/xe_hwmon.c | 81 +++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
index 0407f6f1ab45..3e9a5d2e47c9 100644
--- a/drivers/gpu/drm/xe/xe_hwmon.c
+++ b/drivers/gpu/drm/xe/xe_hwmon.c
@@ -141,6 +141,87 @@ 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``, ``pwmN_enable`` and the
+ * ``pwmN_auto_pointM_*`` curve files require a readable stock fan table with at
+ * least two points. Late binding refreshes cached data for existing fans but
+ * does not create attributes that were hidden at registration. The stock table
+ * can change with the late bound firmware; if a complete table cannot be read,
+ * the previous one is kept.
+ *
+ * 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.
+ *
+ * Late-bound fan firmware refreshes the stock table and maximum fan speed.
+ * Active manual and full-speed user tables are re-programmed after the refresh,
+ * while the cached user curve is preserved. Active manual and full-speed fan
+ * state is also restored across system suspend, runtime D3cold, and FLR. TjMax
+ * fan control is independently saved and restored across those state-loss
+ * transitions. D3hot runtime suspend does not lose firmware state and does not
+ * require TjMax or fan-table restoration.
+ *
+ * The pwmN_enable sysfs value selects how those tables are used:
+ *
+ * - 0 (``XE_FAN_PWM_FULL_SPEED``) programs the user table at 100 percent duty
+ *   cycle as a full-speed override. The cached user curve is kept.
+ * - 1 (``XE_FAN_PWM_MANUAL_USER_TABLE``) enables the user table for manual
+ *   control. Without a cached user curve, it starts from the stock table.
+ * - 2 (``XE_FAN_PWM_AUTO_STOCK_TABLE``) restores automatic control using the
+ *   stock firmware table. The cached user curve is kept for a later switch to
+ *   mode 1.
+ *
+ * Any table programmed by modes 0 and 1 must meet the curve rules below;
+ * otherwise the mode switch fails with -EINVAL.
+ *
+ * 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`` reports the active PWM duty cycle in the hwmon 0..255 range.
+ *   Writes set a fixed duty cycle in manual mode.
+ * - ``pwmN_enable`` selects the mode described above.
+ * - ``pwmN_auto_pointM_temp`` reports the temperature of point ``M`` of the
+ *   stock table in mode 2 or of the user table otherwise, in millidegrees
+ *   Celsius. Writes stage the user-table point.
+ * - ``pwmN_auto_pointM_pwm`` reports the PWM value of point ``M`` of the same
+ *   table in the hwmon 0..255 range. Writes stage the user-table point.
+ *
+ * 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. Each write fills the selected point, and the staged curve is committed
+ * once a complete point prefix ends with (0, 0) or reaches the last supported
+ * point. The (0, 0) end marker is not included in the committed curve.
+ * A curve must have at least two points, temperature must increase and PWM
+ * must not decrease from one point to the next; otherwise the completing write
+ * fails with -EINVAL. Staged temperature and PWM values are cleared after the
+ * completing write, whether firmware accepts or rejects the curve.
+ * Firmware acceptance publishes the staged curve in the cached user table;
+ * rejection keeps the previously cached user curve.
+ *
+ * Writes to ``pwmN`` replace the user table with a two-point table at a
+ * constant duty cycle.
+ *
+ * If a user-table write is not accepted by firmware, the fan falls back to
+ * mode 2 and the previously cached user curve is kept. If that fallback also
+ * fails, the fan control mode is not established and reads of ``pwmN``,
+ * ``pwmN_enable`` and ``pwmN_auto_pointM_*`` fail with -ENODATA while the
+ * control mode is uninitialized.
+ */
+
 /**
  * enum xe_fan_pwm_enable_mode - fan PWM enable modes
  */
-- 
2.34.1


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

* Re: [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3]
  2026-12-02 19:52 ` [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
@ 2026-12-02 19:53   ` Purkait, Soham
  0 siblings, 0 replies; 15+ messages in thread
From: Purkait, Soham @ 2026-12-02 19:53 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav


On 03-12-2026 01:22, Karthik Poosa wrote:
> 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.
>
> Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
> Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
> ---
>
> 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.
>
> v6:
>   - Add couple of warnings. (Soham)
>
> v7:
>   - Move user fan-table batch construction and verification into
>     new function xe_hwmon_commit_user_fan_table(). (Badal)
>   - Keep xe_hwmon_set_user_fan_pwm() responsible for preparing the
>     fixed two-point curve and updating the cached user table. (Badal)
>   - Log an out-of-range PWM read at debug level, as pollers reading pwmN
>     after resume flood dmesg. (Rodrigo)
>   - Drop Soham Reviewed-by as refactor occurred.
>   - Make pwm node visible only if stock points > MIN_FAN_CONTROL_POINTS.
>
>   .../ABI/testing/sysfs-driver-intel-xe-hwmon   |   9 +
>   drivers/gpu/drm/xe/regs/xe_pmt.h              |   5 +
>   drivers/gpu/drm/xe/xe_hwmon.c                 | 362 ++++++++++++++++++
>   drivers/gpu/drm/xe/xe_pcode_api.h             |   3 +
>   4 files changed, 379 insertions(+)
>
> diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
> index ac9ccfd47eab..34c50f13f0b4 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:		December 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 1d7ba1149917..e77ab0685bb7 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
>   };
>   
> @@ -1166,6 +1170,162 @@ 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;
> +
> +	*point_count = 0;
> +	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;
> +}
> +
> +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 < MIN_FAN_CONTROL_POINTS ||
> +	    point_count > MAX_FAN_CONTROL_POINTS) {
> +		xe_err(hwmon->xe, "fan %d table needs %d-%d points, got %u\n", fan,
> +		       MIN_FAN_CONTROL_POINTS, MAX_FAN_CONTROL_POINTS, point_count);
> +		return -EINVAL;
> +	}
> +
> +	for (point = 0; point < point_count; point++) {
> +		u8 temp = points[point].temp;
> +		u8 speed = points[point].speed;
> +
> +		/* Firmware requires increasing temperature and non-decreasing speed. */
> +		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 not monotonic: %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);
> +
> +	/* pcode fills the user table from point 0 in write order; the count write commits it. */
> +	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_err(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;
> +
> +		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);
> +		/* Firmware may have accepted only part of the table; fall back to stock. */
> +		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;
> +	}
> +
> +	xe_dbg(hwmon->xe, "fan %d user table committed successfully with %u points\n", fan,
> +	       point_count);
> +	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_hwmon_fan_info *fi = &hwmon->fi[fan];
> +	struct fan_table *user_table = &fi->fan_table[USER_FAN_TABLE];
> +	struct fan_control_point points[MIN_FAN_CONTROL_POINTS];
> +	int ret, i;
> +	u8 speed;
> +
> +	/* Fan table speed are in percent. */
> +	speed = min_t(u32, DIV_ROUND_CLOSEST(pwm * 100, U8_MAX), 100);
> +
> +	/* Fan control needs at least two points; program them all at the same speed. */
> +	for (i = 0; i < MIN_FAN_CONTROL_POINTS; i++) {
> +		points[i].temp = FIXED_PWM_TEMP + i;
> +		points[i].speed = speed;
> +	}
> +
> +	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, points, MIN_FAN_CONTROL_POINTS);
> +	if (ret) {
> +		xe_err(hwmon->xe, "failed to set pwm %u (%u%%) for fan %d, ret=%d\n",
> +		       pwm, speed, fan, ret);
> +		return ret;
> +	}
> +
> +	/* Update the user fan table cache. */
> +	memcpy(user_table->fcp, points, sizeof(points));
> +	user_table->point_count = MIN_FAN_CONTROL_POINTS;
> +	xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan);
> +
> +	return 0;
> +}
> +
>   static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
>   {
>   	bool found = false;
> @@ -1795,6 +1955,22 @@ 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)
> +{
> +	/* Manual control starts from the stock table, so it needs a usable curve. */
> +	if (!hwmon->xe->info.has_fan_control || !IS_FAN_ENABLED(hwmon, channel) ||
> +	    hwmon->fi[channel].fan_table[STOCK_FAN_TABLE].point_count < MIN_FAN_CONTROL_POINTS)
> +		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)
>   {
> @@ -1848,6 +2024,185 @@ 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) {
> +		xe_warn(hwmon->xe, "failed to read pwm, PMT unavailable: %d\n", 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)) {
> +		xe_warn(hwmon->xe, "pwm read from PMT failed: %d\n", ret);
> +		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: active pwm from PMT: %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_dbg(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) {
> +			xe_dbg(hwmon->xe, "pwm %ld out of range for fan %d\n", val, channel);
> +			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)
> @@ -1874,6 +2229,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;
> @@ -1903,6 +2261,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;
>   	}
> @@ -1921,6 +2281,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)
LGTM:

Reviewed-by: Soham Purkait <soham.purkait@intel.com>

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

* Re: [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable
  2026-12-02 19:52 ` [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
@ 2026-12-02 19:53   ` Purkait, Soham
  0 siblings, 0 replies; 15+ messages in thread
From: Purkait, Soham @ 2026-12-02 19:53 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav


On 03-12-2026 01:22, Karthik Poosa wrote:
> 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.
>
> pwm[1-3] writes now require pwm[1-3]_enable to be 1 first. Since fans
> start in mode 2 after probe, a pwm[1-3] write without selecting manual
> mode is rejected with -EINVAL.
>
> Document pwm[1-3]_enable in the Xe hwmon ABI.
>
> Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
> Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
> ---
>
> 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.
>   - Fall back to the stock table if firmware does not accept a table.
>
> v6:
> - Remove is_full_speed as it is redundant with pwm_enable_mode now.
> - Rename xe_hwmon_get_stock_table_pwm() to xe_hwmon_get_active_table_pwm().
> - Avoid overwriting user table cache for full speed.
> - Align pwm_enable mode kernel-doc with the ABI wording. (Soham)
> - Add debug logs before -EINVAL returns. (Soham)
> - Note the pwm[1-3] write precondition in the commit message. (Soham)
>
> v7:
>   - Use xe_hwmon_commit_user_fan_table() helper in
>     xe_hwmon_activate_user_fan_table() for table update. (Badal)
>   - Return -ENODATA from pwm[1-3] reads on BMG too while the fan control
>     mode is not established. (sashiko)
>   - Add debug log if fan control mode is uninitialized. (Rodrigo)
>   - Made source_table const in xe_hwmon_commit_user_fan_table().
>   - Added a user_table pointer for stock-table cache updates.
>   - Avoided redundant cache updates when the source is already
>     USER_FAN_TABLE.
>
>   .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  19 ++
>   drivers/gpu/drm/xe/xe_hwmon.c                 | 196 +++++++++++++++---
>   2 files changed, 187 insertions(+), 28 deletions(-)
>
> diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
> index 34c50f13f0b4..dbf2c6d01376 100644
> --- a/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
> +++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-hwmon
> @@ -354,4 +354,23 @@ 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:		December 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.
> +
> +		The user fan curve is kept across modes 0 and 2, and re-applied
> +		when switching back to 1. Without a user curve, mode 1 starts
> +		from the firmware stock curve.
> +
>   		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 e77ab0685bb7..3561e3d271eb 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: run the fan at full speed */
> +	XE_FAN_PWM_FULL_SPEED = 0,
> +	/** @XE_FAN_PWM_MANUAL_USER_TABLE: user table control */
> +	XE_FAN_PWM_MANUAL_USER_TABLE = 1,
> +	/** @XE_FAN_PWM_AUTO_STOCK_TABLE: firmware default behavior using the 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,8 @@ 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;
>   };
>   
>   /**
> @@ -926,7 +942,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
>   };
>   
> @@ -1211,11 +1229,14 @@ 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;
> -
> -	fi->fan_table[USER_FAN_TABLE].point_count = 0;
> +		goto fail;
>   
> +	fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
>   	return 0;
> +
> +fail:
> +	fi->pwm_enable_mode = XE_FAN_PWM_UNINITIALIZED;
> +	return ret;
>   }
>   
>   static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
> @@ -1291,6 +1312,56 @@ static int xe_hwmon_commit_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
>   	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)
> +{
> +	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> +	struct fan_control_point points[MAX_FAN_CONTROL_POINTS];
> +	const struct fan_table *source_table;
> +	u32 point_count, point;
> +	int ret;
> +
> +	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 %u is invalid, cannot activate user table\n",
> +		       fan, (table_source == USER_FAN_TABLE) ? "user" : "stock", point_count);
> +		return -EINVAL;
> +	}
> +
> +	for (point = 0; point < point_count; point++) {
> +		points[point].temp = source_table->fcp[point].temp;
> +		points[point].speed = is_full_speed ? 100 :
> +				      min_t(u8, source_table->fcp[point].speed, 100);
> +	}
> +
> +	ret = xe_hwmon_commit_user_fan_table(hwmon, fan, points, point_count);
> +	if (ret)
> +		return ret;
> +
> +	/* Full speed only overrides firmware; the cache keeps the manual curve. */
> +	if (table_source == STOCK_FAN_TABLE) {
> +		struct fan_table *user_table = &fi->fan_table[USER_FAN_TABLE];
> +
> +		for (point = 0; point < point_count; point++) {
> +			user_table->fcp[point].temp = source_table->fcp[point].temp;
> +			user_table->fcp[point].speed =
> +				min_t(u8, source_table->fcp[point].speed, 100);
> +		}
> +		user_table->point_count = point_count;
> +	}
> +
> +	fi->pwm_enable_mode = is_full_speed ? XE_FAN_PWM_FULL_SPEED :
> +					    XE_FAN_PWM_MANUAL_USER_TABLE;
> +
> +	return 0;
> +}
> +
>   /* Temperature to set when setting the fan PWM with pwm[1-3]. */
>   #define FIXED_PWM_TEMP	1
>   
> @@ -1302,6 +1373,11 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
>   	int ret, i;
>   	u8 speed;
>   
> +	if (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 = min_t(u32, DIV_ROUND_CLOSEST(pwm * 100, U8_MAX), 100);
>   
> @@ -1401,6 +1477,8 @@ static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
>   		       stock_fcp_count * sizeof(stock_fcp[0]));
>   		fi->fan_table[STOCK_FAN_TABLE].point_count = stock_fcp_count;
>   		found = true;
> +		if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
> +			fi->pwm_enable_mode = XE_FAN_PWM_AUTO_STOCK_TABLE;
>   	}
>   
>   	/* Succeed if any enabled fan's stock table was read. */
> @@ -1965,6 +2043,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;
> @@ -2024,25 +2103,43 @@ xe_hwmon_fan_read(struct xe_hwmon *hwmon, u32 attr, int channel, long *val)
>   	}
>   }
>   
> +static enum fan_table_type xe_hwmon_get_active_fan_table(struct xe_hwmon *hwmon, u8 fan)
> +{
> +	return (hwmon->fi[fan].pwm_enable_mode == XE_FAN_PWM_AUTO_STOCK_TABLE) ? STOCK_FAN_TABLE :
> +			USER_FAN_TABLE;
> +}
> +
>   /**
> - * xe_hwmon_get_stock_table_pwm - Calculate PWM from the stock fan curve
> + * xe_hwmon_get_active_table_pwm - Calculate PWM from the active 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.
> + * Read package temperature and linearly interpolate the fan table selected by
> + * pwm_enable. Temperatures outside the table range use 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)
> +static int xe_hwmon_get_active_table_pwm(struct xe_hwmon *hwmon, int channel, long *val)
>   {
> -	const struct fan_table *table = &hwmon->fi[channel].fan_table[STOCK_FAN_TABLE];
> +	const struct fan_table *table;
>   	long temperature, pwm;
>   	int point, ret;
>   
>   	lockdep_assert_held(&hwmon->hwmon_lock);
>   
> +	switch (hwmon->fi[channel].pwm_enable_mode) {
> +	case XE_FAN_PWM_UNINITIALIZED:
> +		return -ENODATA;
> +	case XE_FAN_PWM_FULL_SPEED:
> +		*val = 100;
> +		return 0;
> +	default:
> +		break;
> +	}
> +
> +	table = &hwmon->fi[channel].fan_table[xe_hwmon_get_active_fan_table(hwmon, channel)];
> +
>   	if (!table->point_count)
>   		return -ENODATA;
>   
> @@ -2070,7 +2167,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: pwm value from active table cache: %ld\n", channel, pwm);
>   	return 0;
>   }
>   
> @@ -2139,25 +2236,25 @@ 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);
> -			switch (hwmon->xe->info.platform) {
> -			case XE_BATTLEMAGE:
> +		switch (hwmon->xe->info.platform) {
> +		case XE_BATTLEMAGE:
> +			if (fi->pwm_enable_mode == XE_FAN_PWM_UNINITIALIZED)
> +				ret = -ENODATA;
> +			else
>   				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;
> +			break;
> +		default:
> +			ret = xe_hwmon_get_active_table_pwm(hwmon, channel, &pwm);
> +			break;
> +		}
> +		if (ret == -ENODATA) {
> +			xe_dbg(hwmon->xe, "fan %d control mode not established\n", channel);
> +			goto unlock;
> +		}
> +		if (ret) {
> +			xe_err(hwmon->xe, "failed to get active pwm for fan %d, ret=%d\n",
> +			       channel, ret);
> +			goto unlock;
>   		}
>   		if (pwm < 0 || pwm > 100) {
>   			xe_dbg(hwmon->xe, "invalid PWM value %ld read for fan %d\n", pwm, channel);
> @@ -2168,6 +2265,14 @@ 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) {
> +			xe_dbg(hwmon->xe, "fan %d control mode is not initialized\n", channel);
> +			ret = -ENODATA;
> +			goto unlock;
> +		}
> +		*val = fi->pwm_enable_mode;
> +		break;
>   	default:
>   		ret = -EOPNOTSUPP;
>   		break;
> @@ -2181,8 +2286,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) {
> @@ -2194,6 +2302,33 @@ xe_hwmon_pwm_write(struct xe_hwmon *hwmon, u32 attr, int channel, long val)
>   		}
>   		ret = xe_hwmon_set_user_fan_pwm(hwmon, channel, (u8)val);
>   		break;
> +	case hwmon_pwm_enable:
> +		if (val == XE_FAN_PWM_FULL_SPEED || val == XE_FAN_PWM_MANUAL_USER_TABLE) {
> +			bool full_speed = val == XE_FAN_PWM_FULL_SPEED;
> +			/* Without a user curve, start from the clamped stock table. */
> +			enum fan_table_type source = fi->fan_table[USER_FAN_TABLE].point_count ?
> +				USER_FAN_TABLE : STOCK_FAN_TABLE;
> +
> +			ret = xe_hwmon_activate_user_fan_table(hwmon, channel, full_speed, source);
> +			if (ret)
> +				xe_warn(hwmon->xe, "failed to set fan %d pwm_enable %ld, ret=%d\n",
> +					channel, val, ret);
> +		} 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 {
> +			xe_dbg(hwmon->xe, "invalid pwm%d_enable value %ld\n", channel + 1, val);
> +			ret = -EINVAL;
> +		}
> +		break;
>   	default:
>   		ret = -EOPNOTSUPP;
>   		break;
> @@ -2407,6 +2542,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 */
> @@ -2421,6 +2557,10 @@ int xe_hwmon_register(struct xe_device *xe)
>   	if (!hwmon)
>   		return -ENOMEM;
>   
> +	/* Zero is XE_FAN_PWM_FULL_SPEED; stay unset until the stock table is read. */
> +	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;

LGTM:

Reviewed-by: Soham Purkait <soham.purkait@intel.com>


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

* Re: [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control
  2026-12-02 19:52 ` [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
@ 2026-12-02 19:55   ` Purkait, Soham
  0 siblings, 0 replies; 15+ messages in thread
From: Purkait, Soham @ 2026-12-02 19:55 UTC (permalink / raw)
  To: Karthik Poosa, intel-xe
  Cc: rodrigo.vivi, anshuman.gupta, badal.nilawar, raag.jadav


On 03-12-2026 01:22, Karthik Poosa wrote:
> 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.
>
> Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
> Signed-off-by: Karthik Poosa <karthik.poosa@intel.com>
> ---
>
> v2:
>   -Update the fan-control documentation to describe late-binding visibility
>    refresh, staged point-prefix curves, suspended and restored user tables,
>    and TjMax fan-control preservation across suspend and resume.
>
> v3:
> - Update pwmN that it reports active pwm duty cycle.
> - Update pwmN_enable that it return -ENODATA when control mode is
>    uninitialized.
>
>   drivers/gpu/drm/xe/xe_hwmon.c | 81 +++++++++++++++++++++++++++++++++++
>   1 file changed, 81 insertions(+)
>
> diff --git a/drivers/gpu/drm/xe/xe_hwmon.c b/drivers/gpu/drm/xe/xe_hwmon.c
> index 0407f6f1ab45..3e9a5d2e47c9 100644
> --- a/drivers/gpu/drm/xe/xe_hwmon.c
> +++ b/drivers/gpu/drm/xe/xe_hwmon.c
> @@ -141,6 +141,87 @@ 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``, ``pwmN_enable`` and the
> + * ``pwmN_auto_pointM_*`` curve files require a readable stock fan table with at
> + * least two points. Late binding refreshes cached data for existing fans but
> + * does not create attributes that were hidden at registration. The stock table
> + * can change with the late bound firmware; if a complete table cannot be read,
> + * the previous one is kept.
> + *
> + * 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.
> + *
> + * Late-bound fan firmware refreshes the stock table and maximum fan speed.
> + * Active manual and full-speed user tables are re-programmed after the refresh,
> + * while the cached user curve is preserved. Active manual and full-speed fan
> + * state is also restored across system suspend, runtime D3cold, and FLR. TjMax
> + * fan control is independently saved and restored across those state-loss
> + * transitions. D3hot runtime suspend does not lose firmware state and does not
> + * require TjMax or fan-table restoration.
> + *
> + * The pwmN_enable sysfs value selects how those tables are used:
> + *
> + * - 0 (``XE_FAN_PWM_FULL_SPEED``) programs the user table at 100 percent duty
> + *   cycle as a full-speed override. The cached user curve is kept.
> + * - 1 (``XE_FAN_PWM_MANUAL_USER_TABLE``) enables the user table for manual
> + *   control. Without a cached user curve, it starts from the stock table.
> + * - 2 (``XE_FAN_PWM_AUTO_STOCK_TABLE``) restores automatic control using the
> + *   stock firmware table. The cached user curve is kept for a later switch to
> + *   mode 1.
> + *
> + * Any table programmed by modes 0 and 1 must meet the curve rules below;
> + * otherwise the mode switch fails with -EINVAL.
> + *
> + * 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`` reports the active PWM duty cycle in the hwmon 0..255 range.
> + *   Writes set a fixed duty cycle in manual mode.
> + * - ``pwmN_enable`` selects the mode described above.
> + * - ``pwmN_auto_pointM_temp`` reports the temperature of point ``M`` of the
> + *   stock table in mode 2 or of the user table otherwise, in millidegrees
> + *   Celsius. Writes stage the user-table point.
> + * - ``pwmN_auto_pointM_pwm`` reports the PWM value of point ``M`` of the same
> + *   table in the hwmon 0..255 range. Writes stage the user-table point.
> + *
> + * 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. Each write fills the selected point, and the staged curve is committed
> + * once a complete point prefix ends with (0, 0) or reaches the last supported
> + * point. The (0, 0) end marker is not included in the committed curve.
> + * A curve must have at least two points, temperature must increase and PWM
> + * must not decrease from one point to the next; otherwise the completing write
> + * fails with -EINVAL. Staged temperature and PWM values are cleared after the
> + * completing write, whether firmware accepts or rejects the curve.
> + * Firmware acceptance publishes the staged curve in the cached user table;
> + * rejection keeps the previously cached user curve.
> + *
> + * Writes to ``pwmN`` replace the user table with a two-point table at a
> + * constant duty cycle.
> + *
> + * If a user-table write is not accepted by firmware, the fan falls back to
> + * mode 2 and the previously cached user curve is kept. If that fallback also
> + * fails, the fan control mode is not established and reads of ``pwmN``,
> + * ``pwmN_enable`` and ``pwmN_auto_pointM_*`` fail with -ENODATA while the
> + * control mode is uninitialized.
> + */
> +
>   /**
>    * enum xe_fan_pwm_enable_mode - fan PWM enable modes
>    */
LGTM:

Reviewed-by: Soham Purkait <soham.purkait@intel.com>

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

end of thread, other threads:[~2026-12-02 19:55 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 19:52 [PATCH v9 00/11] drm/xe/hwmon: Add fan control support Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 01/11] drm/xe/hwmon: Rename has_fan_control to has_fan Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 02/11] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 03/11] drm/xe/hwmon: Expose fanN_max Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 04/11] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 05/11] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
2026-12-02 19:53   ` Purkait, Soham
2026-12-02 19:52 ` [PATCH v9 06/11] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
2026-12-02 19:53   ` Purkait, Soham
2026-12-02 19:52 ` [PATCH v9 07/11] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 08/11] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 09/11] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 10/11] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
2026-12-02 19:52 ` [PATCH v9 11/11] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
2026-12-02 19:55   ` Purkait, Soham

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.