Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Rodrigo Vivi <rodrigo.vivi@intel.com>
To: Karthik Poosa <karthik.poosa@intel.com>
Cc: <intel-xe@lists.freedesktop.org>, <anshuman.gupta@intel.com>,
	<badal.nilawar@intel.com>, <raag.jadav@intel.com>,
	<riana.tauro@intel.com>, <sk.anirban@intel.com>,
	<mallesh.koujalagi@intel.com>, <soham.purkait@intel.com>,
	<ravi.kishore.koppuravuri@intel.com>
Subject: Re: [PATCH v6 06/10] drm/xe/hwmon: Enable fan curve control support
Date: Mon, 30 Nov 2026 16:00:36 -0500	[thread overview]
Message-ID: <aw3j9I7wcN6Jq3Nk@intel.com> (raw)
In-Reply-To: <20261130185050.36979-7-karthik.poosa@intel.com>

On Tue, Dec 01, 2026 at 12:20:46AM +0530, Karthik Poosa wrote:
> Expose curve-point temperature and pwm controls via
> pwmF_auto_point[P]_temp and pwmF_auto_point[P]_pwm, where
> F is the fan number and P is the fan curve point number.
> Each fan curve point has a temperature and a pwm attribute.
> 
> Update Xe hwmon ABI documentation for pwm auto point temperature
> attributes.
> 
> 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)
> 
>  .../ABI/testing/sysfs-driver-intel-xe-hwmon   |  46 ++
>  drivers/gpu/drm/xe/xe_hwmon.c                 | 513 +++++++++++++++---
>  2 files changed, 475 insertions(+), 84 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

This will have to change if we miss this round.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>

> +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 1ef9c12b7110..857c9d293f09 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 |
> @@ -1240,55 +1256,65 @@ static int xe_hwmon_fan_set_auto(struct xe_hwmon *hwmon, u8 fan)
>  	return ret;
>  }
>  
> -static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
> -					    bool is_full_speed, enum fan_table_type table_source)
> +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];
> -	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> -	struct fan_table *source_table;
> -	u32 expected_count, point_count;
> -	int ret, write_ret, verify_ret;
> +	u32 accepted_count = 0;
>  	u32 point;
> +	int ret, write_ret, verify_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 %d is invalid, cannot activate user table\n",
> -		       fan, (table_source == USER_FAN_TABLE) ? "user" : "stock", point_count);
> +	if (point_count < MIN_FAN_CONTROL_POINTS) {
> +		xe_err(hwmon->xe, "fan %d table needs at least %d points, got %u\n", fan,
> +		       MIN_FAN_CONTROL_POINTS, point_count);
>  		return -EINVAL;
>  	}
> -	expected_count = point_count;
>  
>  	for (point = 0; point < point_count; point++) {
> -		u8 temp = source_table->fcp[point].temp;
> -		u8 speed = is_full_speed ? 100 : min_t(u8, source_table->fcp[point].speed, 100);
> +		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;
> +			}
> +		}
>  
> -		writes[point].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABLE, fan);
> +		/* 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);
> +				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_dbg(hwmon->xe, "failed to write fan %d user table, ret=%d\n", fan, write_ret);
>  
> -	verify_ret = xe_hwmon_get_fan_point_count(hwmon, fan, &point_count, USER_FAN_TABLE);
> -	if (write_ret || verify_ret || point_count != expected_count) {
> +	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 || point_count != expected_count)
> +		if (verify_ret || accepted_count != point_count)
>  			xe_err(hwmon->xe,
> -			       "fan %d user table count mismatch: ret=%d, expected=%u, got=%u\n",
> -			       fan, verify_ret, expected_count, point_count);
> +			       "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)
> @@ -1297,15 +1323,45 @@ static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
>  		return failure_ret;
>  	}
>  
> +	xe_dbg(hwmon->xe, "fan %d user table committed successfully with %u points\n", fan,
> +	       point_count);
> +	return 0;
> +}
> +
> +static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
> +					    bool is_full_speed, enum fan_table_type table_source)
> +{
> +	struct fan_control_point points[MAX_FAN_CONTROL_POINTS];
> +	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> +	struct fan_table *source_table, *user_table;
> +	u32 point_count, point;
> +	int ret;
> +
> +	source_table = &fi->fan_table[table_source];
> +	user_table = &fi->fan_table[USER_FAN_TABLE];
> +	point_count = source_table->point_count;
> +
> +	xe_dbg(hwmon->xe, "activating fan %d user table from %s table\n", fan,
> +	       (table_source == USER_FAN_TABLE) ? "user" : "stock");
> +
> +	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) {
>  		for (point = 0; point < point_count; point++) {
> -			fi->fan_table[USER_FAN_TABLE].fcp[point].temp =
> -				source_table->fcp[point].temp;
> -			fi->fan_table[USER_FAN_TABLE].fcp[point].speed =
> -				min_t(u8, source_table->fcp[point].speed, 100);
> +			user_table->fcp[point].temp = source_table->fcp[point].temp;
> +			user_table->fcp[point].speed = min_t(u8, source_table->fcp[point].speed,
> +							     100);
>  		}
> -		fi->fan_table[USER_FAN_TABLE].point_count = point_count;
> +		user_table->point_count = point_count;
>  	}
>  
>  	fi->pwm_enable_mode = is_full_speed ? XE_FAN_PWM_FULL_SPEED :
> @@ -1319,10 +1375,10 @@ static int xe_hwmon_activate_user_fan_table(struct xe_hwmon *hwmon, u8 fan,
>  
>  static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
>  {
> -	struct xe_pcode_write writes[MIN_FAN_CONTROL_POINTS + 1];
>  	struct xe_hwmon_fan_info *fi = &hwmon->fi[fan];
> -	int ret, write_ret, verify_ret, i;
> -	u32 point_count = 0;
> +	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;
>  
>  	if (fi->pwm_enable_mode != XE_FAN_PWM_MANUAL_USER_TABLE) {
> @@ -1331,58 +1387,341 @@ static int xe_hwmon_set_user_fan_pwm(struct xe_hwmon *hwmon, u8 fan, u8 pwm)
>  	}
>  
>  	/* Fan table speed are in percent. */
> -	speed = DIV_ROUND_CLOSEST(pwm * 100, U8_MAX);
> -	speed = min_t(u32, speed, 100);
> +	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++) {
> -		writes[i].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_FAN_TABLE, fan);
> -		writes[i].data = FIELD_PREP(FAN_CONTROL_POINT_TEMP_MASK, FIXED_PWM_TEMP + i) |
> -				 FIELD_PREP(FAN_CONTROL_POINT_SPEED_MASK, speed);
> -		xe_dbg(hwmon->xe, "prepared fan control point %d with temp=%d, speed=%u%%\n", i,
> -		       FIXED_PWM_TEMP + i, speed);
> +		points[i].temp = FIXED_PWM_TEMP + i;
> +		points[i].speed = speed;
>  	}
>  
> -	/* pcode fills the user table from point 0 in write order; the count write commits it. */
> -	writes[i].mbox = PCODE_MBOX(FAN_SPEED_CONTROL, FSC_WRITE_NUM_FAN_CONTROL_POINTS, fan);
> -	writes[i].data = MIN_FAN_CONTROL_POINTS;
> -	write_ret = xe_pcode_write_batch(xe_device_get_root_tile(hwmon->xe), writes,
> -					 ARRAY_SIZE(writes));
> -	if (write_ret)
> -		xe_err(hwmon->xe, "failed to write pwm %u (%u%%) for fan %d, ret=%d\n",
> -		       pwm, speed, fan, write_ret);
> +	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;
> +	}
> +
> +	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
> -		xe_dbg(hwmon->xe, "applied pwm %u (%u%%) for fan %d\n", pwm, speed, fan);
> +		temp = fi->fan_table[active_table].fcp[point].temp;
> +	return sysfs_emit(buf, "%u\n", temp * MILLIDEGREE_PER_DEGREE);
> +}
>  
> -	verify_ret = xe_hwmon_get_fan_point_count(hwmon, fan, &point_count, USER_FAN_TABLE);
> -	if (write_ret || verify_ret || point_count != MIN_FAN_CONTROL_POINTS) {
> -		int failure_ret = write_ret ?: verify_ret ?: -EIO;
> +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;
>  
> -		if (verify_ret || point_count != MIN_FAN_CONTROL_POINTS)
> -			xe_err(hwmon->xe,
> -			       "fan %d PWM table count verification failed: ret=%d, count=%u\n",
> -			       fan, verify_ret, point_count);
> -		/* 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 auto mode ret=%d\n",
> -			       fan, ret);
> -		return failure_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);
>  	}
>  
> -	/* Update the user fan table cache. */
> -	for (i = 0; i < MIN_FAN_CONTROL_POINTS; i++) {
> -		fi->fan_table[USER_FAN_TABLE].fcp[i].temp = FIXED_PWM_TEMP + i;
> -		fi->fan_table[USER_FAN_TABLE].fcp[i].speed = speed;
> -		xe_dbg(hwmon->xe, "fan %d user table cache point %d with temp=%d, speed=%u%%\n",
> -		       fan, i, fi->fan_table[USER_FAN_TABLE].fcp[i].temp,
> -		       fi->fan_table[USER_FAN_TABLE].fcp[i].speed);
> +	/* 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;
>  	}
> -	fi->fan_table[USER_FAN_TABLE].point_count = MIN_FAN_CONTROL_POINTS;
> +
> +	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)
> +		return 0;
> +
> +	/* All slots must exist at registration, before the user curve has been staged. */
> +	return attr->mode;
> +}
> +
> +static void xe_hwmon_init_curve_attrs(struct xe_hwmon *hwmon)
> +{
> +	int fan, kind, point, i = 0;
> +
> +	/* Per fan: all temp points then all pwm points; is_visible relies on this layout. */
> +	for (fan = 0; fan < FAN_MAX; fan++) {
> +		for (kind = 0; kind < 2; kind++) {
> +			bool is_temp = !kind;
> +
> +			for (point = 0; point < MAX_FAN_CONTROL_POINTS; point++, i++) {
> +				struct sensor_device_attribute *sda = &hwmon->curve_sda[i];
> +
> +				snprintf(hwmon->curve_names[i], XE_CURVE_ATTR_NAME_LEN,
> +					 "pwm%d_auto_point%d_%s", fan + 1, point + 1,
> +					 is_temp ? "temp" : "pwm");
> +				sysfs_attr_init(&sda->dev_attr.attr);
> +				sda->dev_attr.attr.name = hwmon->curve_names[i];
> +				sda->dev_attr.attr.mode = 0644;
> +				if (is_temp) {
> +					sda->dev_attr.show = xe_hwmon_pwm_auto_point_temp_show;
> +					sda->dev_attr.store = xe_hwmon_pwm_auto_point_temp_store;
> +				} else {
> +					sda->dev_attr.show = xe_hwmon_pwm_auto_point_pwm_show;
> +					sda->dev_attr.store = xe_hwmon_pwm_auto_point_pwm_store;
> +				}
> +				sda->index = XE_CURVE_IDX(fan, point);
> +				hwmon->curve_attrs[i] = &sda->dev_attr.attr;
> +			}
> +		}
> +	}
> +
> +	hwmon->curve_group.attrs = hwmon->curve_attrs;
> +	hwmon->curve_group.is_visible = xe_hwmon_curve_attributes_visible;
> +}
> +
>  static int xe_hwmon_read_fan_control_info(struct xe_hwmon *hwmon)
>  {
>  	int point;
> @@ -2075,12 +2414,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
> @@ -2278,9 +2611,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) {
> @@ -2292,6 +2629,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;
> @@ -2543,10 +2882,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
> 

  reply	other threads:[~2026-11-30 21:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:50 [PATCH v6 00/10] drm/xe/hwmon: Add fan control support Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 01/10] drm/xe/hwmon: Initialize fan-control backend and table cache Karthik Poosa
2026-11-30 19:04   ` sashiko-bot
2026-11-30 18:50 ` [PATCH v6 02/10] drm/xe/hwmon: Expose fanN_max Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 03/10] drm/xe/pcode: Add ordered write batch helper Karthik Poosa
2026-11-30 20:59   ` Rodrigo Vivi
2026-11-30 18:50 ` [PATCH v6 04/10] drm/xe/hwmon: Expose pwm[1-3] Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 05/10] drm/xe/hwmon: Expose pwm[1-3]_enable Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 06/10] drm/xe/hwmon: Enable fan curve control support Karthik Poosa
2026-11-30 21:00   ` Rodrigo Vivi [this message]
2026-11-30 18:50 ` [PATCH v6 07/10] drm/xe/hwmon: Restore fan user tables after resume Karthik Poosa
2026-11-30 21:03   ` Rodrigo Vivi
2026-11-30 18:50 ` [PATCH v6 08/10] drm/xe/hwmon: Refresh fan data after late binding Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 09/10] drm/xe: Expose tjmax_fan_control_enable device sysfs Karthik Poosa
2026-11-30 18:50 ` [PATCH v6 10/10] drm/xe/hwmon: Add kernel-doc for fan control Karthik Poosa
2026-11-30 21:04   ` Rodrigo Vivi
2026-11-30 20:52 ` ✓ CI.KUnit: success for drm/xe/hwmon: Add fan control support (rev3) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw3j9I7wcN6Jq3Nk@intel.com \
    --to=rodrigo.vivi@intel.com \
    --cc=anshuman.gupta@intel.com \
    --cc=badal.nilawar@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=karthik.poosa@intel.com \
    --cc=mallesh.koujalagi@intel.com \
    --cc=raag.jadav@intel.com \
    --cc=ravi.kishore.koppuravuri@intel.com \
    --cc=riana.tauro@intel.com \
    --cc=sk.anirban@intel.com \
    --cc=soham.purkait@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox