All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] hwmon (coretemp): Add support for dynamic tjmax/ttarget
@ 2022-11-08  7:50 Zhang Rui
  2022-11-08  7:50 ` [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid Zhang Rui
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Zhang Rui @ 2022-11-08  7:50 UTC (permalink / raw)
  To: linux, jdelvare, fenghua.yu; +Cc: linux-hwmon, srinivas.pandruvada

Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at
runtime when the Intel SST-PP (Intel Speed Select Technology - Performance
Profile) level is changed. As a result, the ttarget temperature also changes.

Enhance the coretemp driver to always use updated tjmax when it can be
retrieved from MSR_IA32_TEMPERATURE_TARGET.

When tjmax can not be retrieved from MSR_IA32_TEMPERATURE_TARGET, stick with
previous behavior and always use static value. Plus that ttarget is not
available in this case.

The same dynaimic tjmax enhancement, for driver/thermal drivers can be found
at https://patchwork.kernel.org/project/linux-pm/list/?series=693050

thanks,
rui

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

* [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid
  2022-11-08  7:50 [PATCH 0/3] hwmon (coretemp): Add support for dynamic tjmax/ttarget Zhang Rui
@ 2022-11-08  7:50 ` Zhang Rui
  2022-11-11 21:14   ` Guenter Roeck
  2022-11-08  7:50 ` [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax Zhang Rui
  2022-11-08  7:50 ` [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget Zhang Rui
  2 siblings, 1 reply; 9+ messages in thread
From: Zhang Rui @ 2022-11-08  7:50 UTC (permalink / raw)
  To: linux, jdelvare, fenghua.yu; +Cc: linux-hwmon, srinivas.pandruvada

Checking for the valid bit of IA32_THERM_STATUS is removed in commit
bf6ea084ebb5 ("hwmon: (coretemp) Do not return -EAGAIN for low
temperatures"), and temp_data->valid is set and never cleared when the
temperature has been read once.

Remove the obsolete temp_data->valid field.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
---
 drivers/hwmon/coretemp.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
index 8bf32c6c85d9..ec35ada68455 100644
--- a/drivers/hwmon/coretemp.c
+++ b/drivers/hwmon/coretemp.c
@@ -64,7 +64,6 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
  * @attr_size:  Total number of pre-core attrs displayed in the sysfs.
  * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
  *		Otherwise, temp_data holds coretemp data.
- * @valid: If this is 1, the current temperature is valid.
  */
 struct temp_data {
 	int temp;
@@ -76,7 +75,6 @@ struct temp_data {
 	u32 status_reg;
 	int attr_size;
 	bool is_pkg_data;
-	bool valid;
 	struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
 	char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
 	struct attribute *attrs[TOTAL_ATTRS + 1];
@@ -157,7 +155,7 @@ static ssize_t show_temp(struct device *dev,
 	mutex_lock(&tdata->update_lock);
 
 	/* Check whether the time interval has elapsed */
-	if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
+	if (time_after(jiffies, tdata->last_updated + HZ)) {
 		rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
 		/*
 		 * Ignore the valid bit. In all observed cases the register
@@ -166,7 +164,6 @@ static ssize_t show_temp(struct device *dev,
 		 * really help at all.
 		 */
 		tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
-		tdata->valid = true;
 		tdata->last_updated = jiffies;
 	}
 
-- 
2.25.1


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

* [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax
  2022-11-08  7:50 [PATCH 0/3] hwmon (coretemp): Add support for dynamic tjmax/ttarget Zhang Rui
  2022-11-08  7:50 ` [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid Zhang Rui
@ 2022-11-08  7:50 ` Zhang Rui
  2022-11-11 21:26   ` Guenter Roeck
  2022-11-08  7:50 ` [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget Zhang Rui
  2 siblings, 1 reply; 9+ messages in thread
From: Zhang Rui @ 2022-11-08  7:50 UTC (permalink / raw)
  To: linux, jdelvare, fenghua.yu; +Cc: linux-hwmon, srinivas.pandruvada

Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at
runtime when the Intel SST-PP (Intel Speed Select Technology -
Performance Profile) level is changed.

Improve the code to always use updated tjmax when it can be retrieved
from MSR_IA32_TEMPERATURE_TARGET.

When tjmax can not be retrieved from MSR_IA32_TEMPERATURE_TARGET, still
follow the previous logic and always use a static tjmax value.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
---
 drivers/hwmon/coretemp.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
index ec35ada68455..5292f7844860 100644
--- a/drivers/hwmon/coretemp.c
+++ b/drivers/hwmon/coretemp.c
@@ -55,6 +55,8 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
 
 /*
  * Per-Core Temperature Data
+ * @tjmax: The static tjmax value when tjmax cannot be retrieved from
+ *		IA32_TEMPERATURE_TARGET MSR.
  * @last_updated: The time when the current temperature value was updated
  *		earlier (in jiffies).
  * @cpu_core_id: The CPU Core from which temperature values should be read
@@ -93,6 +95,8 @@ struct platform_data {
 	struct device_attribute name_attr;
 };
 
+static int get_tjmax(struct temp_data *tdata, struct device *dev);
+
 /* Keep track of how many zone pointers we allocated in init() */
 static int max_zones __read_mostly;
 /* Array of zone pointers. Serialized by cpu hotplug lock */
@@ -131,8 +135,14 @@ static ssize_t show_tjmax(struct device *dev,
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 	struct platform_data *pdata = dev_get_drvdata(dev);
+	struct temp_data *tdata = pdata->core_data[attr->index];
+	int tjmax;
+
+	mutex_lock(&tdata->update_lock);
+	tjmax = get_tjmax(tdata, dev);
+	mutex_unlock(&tdata->update_lock);
 
-	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
+	return sprintf(buf, "%d\n", tjmax);
 }
 
 static ssize_t show_ttarget(struct device *dev,
@@ -151,9 +161,11 @@ static ssize_t show_temp(struct device *dev,
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 	struct platform_data *pdata = dev_get_drvdata(dev);
 	struct temp_data *tdata = pdata->core_data[attr->index];
+	int tjmax;
 
 	mutex_lock(&tdata->update_lock);
 
+	tjmax = get_tjmax(tdata, dev);
 	/* Check whether the time interval has elapsed */
 	if (time_after(jiffies, tdata->last_updated + HZ)) {
 		rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
@@ -163,7 +175,7 @@ static ssize_t show_temp(struct device *dev,
 		 * Return it instead of reporting an error which doesn't
 		 * really help at all.
 		 */
-		tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
+		tdata->temp = tjmax - ((eax >> 16) & 0x7f) * 1000;
 		tdata->last_updated = jiffies;
 	}
 
@@ -334,20 +346,25 @@ static bool cpu_has_tjmax(struct cpuinfo_x86 *c)
 	       model != 0x36;
 }
 
-static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
+static int get_tjmax(struct temp_data *tdata, struct device *dev)
 {
+	struct cpuinfo_x86 *c = &cpu_data(tdata->cpu);
 	int err;
 	u32 eax, edx;
 	u32 val;
 
+	/* use static tjmax once it is set */
+	if (tdata->tjmax)
+		return tdata->tjmax;
+
 	/*
 	 * A new feature of current Intel(R) processors, the
 	 * IA32_TEMPERATURE_TARGET contains the TjMax value
 	 */
-	err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
+	err = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
 	if (err) {
 		if (cpu_has_tjmax(c))
-			dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
+			dev_warn(dev, "Unable to read TjMax from CPU %u\n", tdata->cpu);
 	} else {
 		val = (eax >> 16) & 0xff;
 		/*
@@ -363,14 +380,17 @@ static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
 	if (force_tjmax) {
 		dev_notice(dev, "TjMax forced to %d degrees C by user\n",
 			   force_tjmax);
-		return force_tjmax * 1000;
+		tdata->tjmax = force_tjmax * 1000;
+		goto end;
 	}
 
 	/*
 	 * An assumption is made for early CPUs and unreadable MSR.
 	 * NOTE: the calculated value may not be correct.
 	 */
-	return adjust_tjmax(c, id, dev);
+	tdata->tjmax = adjust_tjmax(c, tdata->cpu, dev);
+end:
+	return tdata->tjmax;
 }
 
 static int create_core_attrs(struct temp_data *tdata, struct device *dev,
@@ -450,7 +470,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
 	struct platform_data *pdata = platform_get_drvdata(pdev);
 	struct cpuinfo_x86 *c = &cpu_data(cpu);
 	u32 eax, edx;
-	int err, index, attr_no;
+	int err, index, attr_no, tjmax;
 
 	/*
 	 * Find attr number for sysfs:
@@ -485,7 +505,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
 		goto exit_free;
 
 	/* We can access status register. Get Critical Temperature */
-	tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
+	tjmax = get_tjmax(tdata, &pdev->dev);
 
 	/*
 	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
@@ -497,7 +517,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
 					&eax, &edx);
 		if (!err) {
 			tdata->ttarget
-			  = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
+			  = tjmax - ((eax >> 8) & 0xff) * 1000;
 			tdata->attr_size++;
 		}
 	}
-- 
2.25.1


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

* [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget
  2022-11-08  7:50 [PATCH 0/3] hwmon (coretemp): Add support for dynamic tjmax/ttarget Zhang Rui
  2022-11-08  7:50 ` [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid Zhang Rui
  2022-11-08  7:50 ` [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax Zhang Rui
@ 2022-11-08  7:50 ` Zhang Rui
  2022-11-11 21:34   ` Guenter Roeck
  2 siblings, 1 reply; 9+ messages in thread
From: Zhang Rui @ 2022-11-08  7:50 UTC (permalink / raw)
  To: linux, jdelvare, fenghua.yu; +Cc: linux-hwmon, srinivas.pandruvada

Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at
runtime when the Intel SST-PP (Intel Speed Select Technology -
Performance Profile) level is changed. As a result, the ttarget value
also becomes dyamic.

Improve the code to always get updated ttarget value.

Signed-off-by: Zhang Rui <rui.zhang@intel.com>
---
 drivers/hwmon/coretemp.c | 69 ++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 21 deletions(-)

diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
index 5292f7844860..d6084600862f 100644
--- a/drivers/hwmon/coretemp.c
+++ b/drivers/hwmon/coretemp.c
@@ -69,7 +69,6 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
  */
 struct temp_data {
 	int temp;
-	int ttarget;
 	int tjmax;
 	unsigned long last_updated;
 	unsigned int cpu;
@@ -96,6 +95,7 @@ struct platform_data {
 };
 
 static int get_tjmax(struct temp_data *tdata, struct device *dev);
+static int get_ttarget(struct temp_data *tdata, struct device *dev);
 
 /* Keep track of how many zone pointers we allocated in init() */
 static int max_zones __read_mostly;
@@ -150,8 +150,17 @@ static ssize_t show_ttarget(struct device *dev,
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 	struct platform_data *pdata = dev_get_drvdata(dev);
+	struct temp_data *tdata = pdata->core_data[attr->index];
+	int ttarget;
+
+	mutex_lock(&tdata->update_lock);
+	ttarget = get_ttarget(tdata, dev);
+	mutex_unlock(&tdata->update_lock);
 
-	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
+	if (ttarget >= 0)
+		return sprintf(buf, "%d\n", ttarget);
+	else
+		return ttarget;
 }
 
 static ssize_t show_temp(struct device *dev,
@@ -393,6 +402,38 @@ static int get_tjmax(struct temp_data *tdata, struct device *dev)
 	return tdata->tjmax;
 }
 
+static int get_ttarget(struct temp_data *tdata, struct device *dev)
+{
+	u32 eax, edx;
+	struct cpuinfo_x86 *c = &cpu_data(tdata->cpu);
+	int tj_max, ttarget_offset, ret;
+
+	/*
+	 * ttarget is valid only if tjmax can be retrieved from
+	 * MSR_IA32_TEMPERATURE_TARGET
+	 */
+	if (tdata->tjmax)
+		return -ENODEV;
+
+	if (c->x86_model <= 0xe || c->x86_model == 0x1c)
+		return -ENODEV;
+
+	/*
+	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
+	 * The target temperature is available on older CPUs but not in this
+	 * register. Atoms don't have the register at all.
+	 */
+	ret = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET,
+					&eax, &edx);
+	if (ret)
+		return ret;
+
+	tj_max = (eax >> 16) & 0xff;
+	ttarget_offset = (eax >> 8) & 0xff;
+
+	return (tj_max - ttarget_offset) * 1000;
+}
+
 static int create_core_attrs(struct temp_data *tdata, struct device *dev,
 			     int attr_no)
 {
@@ -468,9 +509,8 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
 {
 	struct temp_data *tdata;
 	struct platform_data *pdata = platform_get_drvdata(pdev);
-	struct cpuinfo_x86 *c = &cpu_data(cpu);
 	u32 eax, edx;
-	int err, index, attr_no, tjmax;
+	int err, index, attr_no;
 
 	/*
 	 * Find attr number for sysfs:
@@ -504,23 +544,10 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
 	if (err)
 		goto exit_free;
 
-	/* We can access status register. Get Critical Temperature */
-	tjmax = get_tjmax(tdata, &pdev->dev);
-
-	/*
-	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
-	 * The target temperature is available on older CPUs but not in this
-	 * register. Atoms don't have the register at all.
-	 */
-	if (c->x86_model > 0xe && c->x86_model != 0x1c) {
-		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
-					&eax, &edx);
-		if (!err) {
-			tdata->ttarget
-			  = tjmax - ((eax >> 8) & 0xff) * 1000;
-			tdata->attr_size++;
-		}
-	}
+	/* Make sure tdata->tjmax is a valid indicator for dynamic/static tjmax */
+	get_tjmax(tdata, &pdev->dev);
+	if (get_ttarget(tdata, &pdev->dev) >= 0)
+		tdata->attr_size++;
 
 	pdata->core_data[attr_no] = tdata;
 
-- 
2.25.1


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

* Re: [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid
  2022-11-08  7:50 ` [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid Zhang Rui
@ 2022-11-11 21:14   ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2022-11-11 21:14 UTC (permalink / raw)
  To: Zhang Rui; +Cc: jdelvare, fenghua.yu, linux-hwmon, srinivas.pandruvada

On Tue, Nov 08, 2022 at 03:50:49PM +0800, Zhang Rui wrote:
> Checking for the valid bit of IA32_THERM_STATUS is removed in commit
> bf6ea084ebb5 ("hwmon: (coretemp) Do not return -EAGAIN for low
> temperatures"), and temp_data->valid is set and never cleared when the
> temperature has been read once.
> 
> Remove the obsolete temp_data->valid field.
> 
> Signed-off-by: Zhang Rui <rui.zhang@intel.com>

Applied to hwmon-next.

Thanks,
Guenter

> ---
>  drivers/hwmon/coretemp.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
> index 8bf32c6c85d9..ec35ada68455 100644
> --- a/drivers/hwmon/coretemp.c
> +++ b/drivers/hwmon/coretemp.c
> @@ -64,7 +64,6 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
>   * @attr_size:  Total number of pre-core attrs displayed in the sysfs.
>   * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
>   *		Otherwise, temp_data holds coretemp data.
> - * @valid: If this is 1, the current temperature is valid.
>   */
>  struct temp_data {
>  	int temp;
> @@ -76,7 +75,6 @@ struct temp_data {
>  	u32 status_reg;
>  	int attr_size;
>  	bool is_pkg_data;
> -	bool valid;
>  	struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
>  	char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
>  	struct attribute *attrs[TOTAL_ATTRS + 1];
> @@ -157,7 +155,7 @@ static ssize_t show_temp(struct device *dev,
>  	mutex_lock(&tdata->update_lock);
>  
>  	/* Check whether the time interval has elapsed */
> -	if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
> +	if (time_after(jiffies, tdata->last_updated + HZ)) {
>  		rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
>  		/*
>  		 * Ignore the valid bit. In all observed cases the register
> @@ -166,7 +164,6 @@ static ssize_t show_temp(struct device *dev,
>  		 * really help at all.
>  		 */
>  		tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
> -		tdata->valid = true;
>  		tdata->last_updated = jiffies;
>  	}
>  

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

* Re: [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax
  2022-11-08  7:50 ` [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax Zhang Rui
@ 2022-11-11 21:26   ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2022-11-11 21:26 UTC (permalink / raw)
  To: Zhang Rui; +Cc: jdelvare, fenghua.yu, linux-hwmon, srinivas.pandruvada

On Tue, Nov 08, 2022 at 03:50:50PM +0800, Zhang Rui wrote:
> Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at
> runtime when the Intel SST-PP (Intel Speed Select Technology -
> Performance Profile) level is changed.
> 
> Improve the code to always use updated tjmax when it can be retrieved
> from MSR_IA32_TEMPERATURE_TARGET.
> 
> When tjmax can not be retrieved from MSR_IA32_TEMPERATURE_TARGET, still
> follow the previous logic and always use a static tjmax value.
> 
> Signed-off-by: Zhang Rui <rui.zhang@intel.com>
> ---
>  drivers/hwmon/coretemp.c | 40 ++++++++++++++++++++++++++++++----------
>  1 file changed, 30 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
> index ec35ada68455..5292f7844860 100644
> --- a/drivers/hwmon/coretemp.c
> +++ b/drivers/hwmon/coretemp.c
> @@ -55,6 +55,8 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
>  
>  /*
>   * Per-Core Temperature Data
> + * @tjmax: The static tjmax value when tjmax cannot be retrieved from
> + *		IA32_TEMPERATURE_TARGET MSR.
>   * @last_updated: The time when the current temperature value was updated
>   *		earlier (in jiffies).
>   * @cpu_core_id: The CPU Core from which temperature values should be read
> @@ -93,6 +95,8 @@ struct platform_data {
>  	struct device_attribute name_attr;
>  };
>  
> +static int get_tjmax(struct temp_data *tdata, struct device *dev);
> +

Please rearrange the code to avoid the forward declaration.

>  /* Keep track of how many zone pointers we allocated in init() */
>  static int max_zones __read_mostly;
>  /* Array of zone pointers. Serialized by cpu hotplug lock */
> @@ -131,8 +135,14 @@ static ssize_t show_tjmax(struct device *dev,
>  {
>  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
>  	struct platform_data *pdata = dev_get_drvdata(dev);
> +	struct temp_data *tdata = pdata->core_data[attr->index];
> +	int tjmax;
> +
> +	mutex_lock(&tdata->update_lock);
> +	tjmax = get_tjmax(tdata, dev);
> +	mutex_unlock(&tdata->update_lock);
>  
> -	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
> +	return sprintf(buf, "%d\n", tjmax);
>  }
>  
>  static ssize_t show_ttarget(struct device *dev,
> @@ -151,9 +161,11 @@ static ssize_t show_temp(struct device *dev,
>  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
>  	struct platform_data *pdata = dev_get_drvdata(dev);
>  	struct temp_data *tdata = pdata->core_data[attr->index];
> +	int tjmax;
>  
>  	mutex_lock(&tdata->update_lock);
>  
> +	tjmax = get_tjmax(tdata, dev);
>  	/* Check whether the time interval has elapsed */
>  	if (time_after(jiffies, tdata->last_updated + HZ)) {
>  		rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
> @@ -163,7 +175,7 @@ static ssize_t show_temp(struct device *dev,
>  		 * Return it instead of reporting an error which doesn't
>  		 * really help at all.
>  		 */
> -		tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
> +		tdata->temp = tjmax - ((eax >> 16) & 0x7f) * 1000;
>  		tdata->last_updated = jiffies;
>  	}
>  
> @@ -334,20 +346,25 @@ static bool cpu_has_tjmax(struct cpuinfo_x86 *c)
>  	       model != 0x36;
>  }
>  
> -static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
> +static int get_tjmax(struct temp_data *tdata, struct device *dev)
>  {
> +	struct cpuinfo_x86 *c = &cpu_data(tdata->cpu);
>  	int err;
>  	u32 eax, edx;
>  	u32 val;
>  
> +	/* use static tjmax once it is set */
> +	if (tdata->tjmax)
> +		return tdata->tjmax;
> +
>  	/*
>  	 * A new feature of current Intel(R) processors, the
>  	 * IA32_TEMPERATURE_TARGET contains the TjMax value
>  	 */
> -	err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
> +	err = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
>  	if (err) {
>  		if (cpu_has_tjmax(c))
> -			dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
> +			dev_warn(dev, "Unable to read TjMax from CPU %u\n", tdata->cpu);
>  	} else {
>  		val = (eax >> 16) & 0xff;
>  		/*
> @@ -363,14 +380,17 @@ static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
>  	if (force_tjmax) {
>  		dev_notice(dev, "TjMax forced to %d degrees C by user\n",
>  			   force_tjmax);
> -		return force_tjmax * 1000;
> +		tdata->tjmax = force_tjmax * 1000;
> +		goto end;

This added goto doesn't add any value and is just a manual replacement
for if/else. Please drop and return immediately, or use if/else.

	if (force_tjmax) {
		...
		tdata->tjmax = force_tjmax * 1000;
	} else {
		/*
		 * An assumption is made for early CPUs and unreadable MSR.
		 * NOTE: the calculated value may not be correct.
		 */
		tdata->tjmax = adjust_tjmax(c, tdata->cpu, dev);
	}
	return tdata->tjmax;

Thanks,
Guenter

>  	}
>  
>  	/*
>  	 * An assumption is made for early CPUs and unreadable MSR.
>  	 * NOTE: the calculated value may not be correct.
>  	 */
> -	return adjust_tjmax(c, id, dev);
> +	tdata->tjmax = adjust_tjmax(c, tdata->cpu, dev);
> +end:
> +	return tdata->tjmax;
>  }
>  
>  static int create_core_attrs(struct temp_data *tdata, struct device *dev,
> @@ -450,7 +470,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
>  	struct platform_data *pdata = platform_get_drvdata(pdev);
>  	struct cpuinfo_x86 *c = &cpu_data(cpu);
>  	u32 eax, edx;
> -	int err, index, attr_no;
> +	int err, index, attr_no, tjmax;
>  
>  	/*
>  	 * Find attr number for sysfs:
> @@ -485,7 +505,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
>  		goto exit_free;
>  
>  	/* We can access status register. Get Critical Temperature */
> -	tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
> +	tjmax = get_tjmax(tdata, &pdev->dev);
>  
>  	/*
>  	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
> @@ -497,7 +517,7 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
>  					&eax, &edx);
>  		if (!err) {
>  			tdata->ttarget
> -			  = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
> +			  = tjmax - ((eax >> 8) & 0xff) * 1000;
>  			tdata->attr_size++;
>  		}
>  	}

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

* Re: [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget
  2022-11-08  7:50 ` [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget Zhang Rui
@ 2022-11-11 21:34   ` Guenter Roeck
  2022-11-12  8:37     ` Zhang Rui
  0 siblings, 1 reply; 9+ messages in thread
From: Guenter Roeck @ 2022-11-11 21:34 UTC (permalink / raw)
  To: Zhang Rui; +Cc: jdelvare, fenghua.yu, linux-hwmon, srinivas.pandruvada

On Tue, Nov 08, 2022 at 03:50:51PM +0800, Zhang Rui wrote:
> Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at
> runtime when the Intel SST-PP (Intel Speed Select Technology -
> Performance Profile) level is changed. As a result, the ttarget value
> also becomes dyamic.
> 
> Improve the code to always get updated ttarget value.
> 
> Signed-off-by: Zhang Rui <rui.zhang@intel.com>
> ---
>  drivers/hwmon/coretemp.c | 69 ++++++++++++++++++++++++++++------------
>  1 file changed, 48 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/hwmon/coretemp.c b/drivers/hwmon/coretemp.c
> index 5292f7844860..d6084600862f 100644
> --- a/drivers/hwmon/coretemp.c
> +++ b/drivers/hwmon/coretemp.c
> @@ -69,7 +69,6 @@ MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
>   */
>  struct temp_data {
>  	int temp;
> -	int ttarget;
>  	int tjmax;
>  	unsigned long last_updated;
>  	unsigned int cpu;
> @@ -96,6 +95,7 @@ struct platform_data {
>  };
>  
>  static int get_tjmax(struct temp_data *tdata, struct device *dev);
> +static int get_ttarget(struct temp_data *tdata, struct device *dev);

Please rearrange to code to avoid forward declarations.

>  
>  /* Keep track of how many zone pointers we allocated in init() */
>  static int max_zones __read_mostly;
> @@ -150,8 +150,17 @@ static ssize_t show_ttarget(struct device *dev,
>  {
>  	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
>  	struct platform_data *pdata = dev_get_drvdata(dev);
> +	struct temp_data *tdata = pdata->core_data[attr->index];
> +	int ttarget;
> +
> +	mutex_lock(&tdata->update_lock);

Is that mutex really necessary ? I don't immediately see the need
for it.

> +	ttarget = get_ttarget(tdata, dev);
> +	mutex_unlock(&tdata->update_lock);
>  
> -	return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
> +	if (ttarget >= 0)
> +		return sprintf(buf, "%d\n", ttarget);
> +	else
> +		return ttarget;

else after return is unnecessary. Much better would be

	if (ttarget < 0)
		return ttarget;
	return sprintf(buf, "%d\n", ttarget);

>  }
>  
>  static ssize_t show_temp(struct device *dev,
> @@ -393,6 +402,38 @@ static int get_tjmax(struct temp_data *tdata, struct device *dev)
>  	return tdata->tjmax;
>  }
>  
> +static int get_ttarget(struct temp_data *tdata, struct device *dev)
> +{
> +	u32 eax, edx;
> +	struct cpuinfo_x86 *c = &cpu_data(tdata->cpu);
> +	int tj_max, ttarget_offset, ret;

Please use tjmax for consistency.

> +
> +	/*
> +	 * ttarget is valid only if tjmax can be retrieved from
> +	 * MSR_IA32_TEMPERATURE_TARGET
> +	 */
> +	if (tdata->tjmax)
> +		return -ENODEV;
> +
> +	if (c->x86_model <= 0xe || c->x86_model == 0x1c)
> +		return -ENODEV;
> +

Does it really make sense to re-check this each time the target temperature
is read ?

> +	/*
> +	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
> +	 * The target temperature is available on older CPUs but not in this
> +	 * register. Atoms don't have the register at all.
> +	 */
> +	ret = rdmsr_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET,
> +					&eax, &edx);

Please watch multi-line alignment.

> +	if (ret)
> +		return ret;
> +
> +	tj_max = (eax >> 16) & 0xff;
> +	ttarget_offset = (eax >> 8) & 0xff;
> +
> +	return (tj_max - ttarget_offset) * 1000;
> +}
> +
>  static int create_core_attrs(struct temp_data *tdata, struct device *dev,
>  			     int attr_no)
>  {
> @@ -468,9 +509,8 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
>  {
>  	struct temp_data *tdata;
>  	struct platform_data *pdata = platform_get_drvdata(pdev);
> -	struct cpuinfo_x86 *c = &cpu_data(cpu);
>  	u32 eax, edx;
> -	int err, index, attr_no, tjmax;
> +	int err, index, attr_no;
>  
>  	/*
>  	 * Find attr number for sysfs:
> @@ -504,23 +544,10 @@ static int create_core_data(struct platform_device *pdev, unsigned int cpu,
>  	if (err)
>  		goto exit_free;
>  
> -	/* We can access status register. Get Critical Temperature */
> -	tjmax = get_tjmax(tdata, &pdev->dev);
> -
> -	/*
> -	 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
> -	 * The target temperature is available on older CPUs but not in this
> -	 * register. Atoms don't have the register at all.
> -	 */
> -	if (c->x86_model > 0xe && c->x86_model != 0x1c) {
> -		err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
> -					&eax, &edx);
> -		if (!err) {
> -			tdata->ttarget
> -			  = tjmax - ((eax >> 8) & 0xff) * 1000;
> -			tdata->attr_size++;
> -		}
> -	}
> +	/* Make sure tdata->tjmax is a valid indicator for dynamic/static tjmax */
> +	get_tjmax(tdata, &pdev->dev);
> +	if (get_ttarget(tdata, &pdev->dev) >= 0)
> +		tdata->attr_size++;
>  
>  	pdata->core_data[attr_no] = tdata;
>  

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

* Re: [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget
  2022-11-11 21:34   ` Guenter Roeck
@ 2022-11-12  8:37     ` Zhang Rui
  2022-11-12 15:10       ` Guenter Roeck
  0 siblings, 1 reply; 9+ messages in thread
From: Zhang Rui @ 2022-11-12  8:37 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: jdelvare, fenghua.yu, linux-hwmon, srinivas.pandruvada

Hi, Guenter,

Thanks for reviewing the patches.
I will address the comments in next version. And also some comments
below.

On Fri, 2022-11-11 at 13:34 -0800, Guenter Roeck wrote:
> >  
> >  /* Keep track of how many zone pointers we allocated in init() */
> >  static int max_zones __read_mostly;
> > @@ -150,8 +150,17 @@ static ssize_t show_ttarget(struct device
> > *dev,
> >  {
> >  	struct sensor_device_attribute *attr =
> > to_sensor_dev_attr(devattr);
> >  	struct platform_data *pdata = dev_get_drvdata(dev);
> > +	struct temp_data *tdata = pdata->core_data[attr->index];
> > +	int ttarget;
> > +
> > +	mutex_lock(&tdata->update_lock);
> 
> Is that mutex really necessary ? I don't immediately see the need
> for it.

I just followed the same pattern as show_crit_alarm().
I checked the history and it was introduced by commit 723f573433b2
("hwmon: (coretemp) Fixup target cpu for package when cpu is
offlined"), to make sure the msr is not running on an offlined cpu.

> > +
> > +	/*
> > +	 * ttarget is valid only if tjmax can be retrieved from
> > +	 * MSR_IA32_TEMPERATURE_TARGET
> > +	 */
> > +	if (tdata->tjmax)
> > +		return -ENODEV;
> > +
> > +	if (c->x86_model <= 0xe || c->x86_model == 0x1c)
> > +		return -ENODEV;
> > +
> 
> Does it really make sense to re-check this each time the target
> temperature
> is read ?

You're right. We can keep this as it was, when deciding whether to
create this sysfs attr or not. Then the check in get_ttarget() can be
removed.

thanks,
rui


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

* Re: [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget
  2022-11-12  8:37     ` Zhang Rui
@ 2022-11-12 15:10       ` Guenter Roeck
  0 siblings, 0 replies; 9+ messages in thread
From: Guenter Roeck @ 2022-11-12 15:10 UTC (permalink / raw)
  To: Zhang Rui; +Cc: jdelvare, fenghua.yu, linux-hwmon, srinivas.pandruvada

On Sat, Nov 12, 2022 at 04:37:50PM +0800, Zhang Rui wrote:
> Hi, Guenter,
> 
> Thanks for reviewing the patches.
> I will address the comments in next version. And also some comments
> below.
> 
> On Fri, 2022-11-11 at 13:34 -0800, Guenter Roeck wrote:
> > >  
> > >  /* Keep track of how many zone pointers we allocated in init() */
> > >  static int max_zones __read_mostly;
> > > @@ -150,8 +150,17 @@ static ssize_t show_ttarget(struct device
> > > *dev,
> > >  {
> > >  	struct sensor_device_attribute *attr =
> > > to_sensor_dev_attr(devattr);
> > >  	struct platform_data *pdata = dev_get_drvdata(dev);
> > > +	struct temp_data *tdata = pdata->core_data[attr->index];
> > > +	int ttarget;
> > > +
> > > +	mutex_lock(&tdata->update_lock);
> > 
> > Is that mutex really necessary ? I don't immediately see the need
> > for it.
> 
> I just followed the same pattern as show_crit_alarm().
> I checked the history and it was introduced by commit 723f573433b2
> ("hwmon: (coretemp) Fixup target cpu for package when cpu is
> offlined"), to make sure the msr is not running on an offlined cpu.
> 
Good point. I am not sure if it matters at that point if the code
uses the old or the new CPU, but I guess it is safer.

Thanks for the clarification,

Guenter

> > > +
> > > +	/*
> > > +	 * ttarget is valid only if tjmax can be retrieved from
> > > +	 * MSR_IA32_TEMPERATURE_TARGET
> > > +	 */
> > > +	if (tdata->tjmax)
> > > +		return -ENODEV;
> > > +
> > > +	if (c->x86_model <= 0xe || c->x86_model == 0x1c)
> > > +		return -ENODEV;
> > > +
> > 
> > Does it really make sense to re-check this each time the target
> > temperature
> > is read ?
> 
> You're right. We can keep this as it was, when deciding whether to
> create this sysfs attr or not. Then the check in get_ttarget() can be
> removed.
> 
> thanks,
> rui
> 

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

end of thread, other threads:[~2022-11-12 15:10 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-08  7:50 [PATCH 0/3] hwmon (coretemp): Add support for dynamic tjmax/ttarget Zhang Rui
2022-11-08  7:50 ` [PATCH 1/3] hwmon (coretemp): Remove obsolete temp_data->valid Zhang Rui
2022-11-11 21:14   ` Guenter Roeck
2022-11-08  7:50 ` [PATCH 2/3] hwmon (coretemp): Add support for dynamic tjmax Zhang Rui
2022-11-11 21:26   ` Guenter Roeck
2022-11-08  7:50 ` [PATCH 3/3] hwmon (coretemp): Add support for dynamic ttarget Zhang Rui
2022-11-11 21:34   ` Guenter Roeck
2022-11-12  8:37     ` Zhang Rui
2022-11-12 15:10       ` Guenter Roeck

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.