Skip to contentSkip to conversation

thermal/intel: Introduce intel-tcc lib and enhance tjmax handling

PATCH In review Zhang Rui 2 participants 8 messages Dec 19, 2022 – Dec 30, 2022 Thermal

lore
Share & tools
lore

Zhang Rui

author reviewer
#0 lore raw

v2 cover thermal/intel: Introduce intel-tcc lib and enhance tjmax handling

Currently, all the thermal drivers that access the Intel CPU TCC (thermal control circuitry) MSRs have two problems,
1. they have their own implementation for the same TCC functionalities, including getting the tjmax/temperature, getting/setting the TCC offset. This introduces a lot of duplicate code.
2. they get TjMax value once and always use the cached value later. But Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at runtime for cases like the Intel SST-PP (Intel Speed Select Technology- Performance Profile) level change.

The intel-tcc library is introduced in patch 1/6 for cleaning up the duplicate code among these drivers, and it also ensures the temperature is alway got based on the updated tjmax value.

Patch 2 ~ 5 cleans up the drivers by using the new Intel TCC lib APIs.

Patch 6/6 enhances the x86_pkg_temp driver to handle dynamic tjmax when progamming the thermal interrupt threshold.
Actually, the thermal interrupt threshold programming can also be part of the TCC library, but I didn't do it in this version because x86_pkg_temp is the only user for now.

thanks,
rui

---
Changes since V1:
- remove unnecessary API parameters
- avoid reading MSR on a remote CPU when possible
- coding format improvements

author reviewer
#1 lore raw

PATCH v2 1/6 thermal/intel: Introduce Intel TCC library

There are several different drivers that accesses the Intel TCC (thermal control circuitry) MSRs, and each of them has its own implementation for the same functionalities, e.g. getting the current temperature, getting the tj_max, and getting/setting the tj_max offset.

Introduce a library to unify the code for Intel CPU TCC MSR access.

At the same time, ensure the temperature is got based on the updated tjmax value because tjmax can be changed at runtime for cases like the Intel SST-PP (Intel Speed Select Technology - Performance Profile) level change.

---
 drivers/thermal/intel/Kconfig     |   4 +
 drivers/thermal/intel/Makefile    |   1 +
 drivers/thermal/intel/intel_tcc.c | 139 ++++++++++++++++++++++++++++++
 include/linux/intel_tcc.h         |  18 ++++
 4 files changed, 162 insertions(+)
 create mode 100644 drivers/thermal/intel/intel_tcc.c
 create mode 100644 include/linux/intel_tcc.h
drivers/thermal/intel/Kconfig +4 -0
index f0c845679250..6b938c040d6e 100644
@@ -12,6 +12,10 @@ config X86_THERMAL_VECTOR
 	def_bool y
 	depends on X86 && CPU_SUP_INTEL && X86_LOCAL_APIC
 
+config INTEL_TCC
+	bool
+	depends on X86
+
 config X86_PKG_TEMP_THERMAL
 	tristate "X86 package temperature thermal driver"
 	depends on X86_THERMAL_VECTOR
drivers/thermal/intel/Makefile +1 -0
index 9a8d8054f316..5d8833c82ab6 100644
@@ -2,6 +2,7 @@
 #
 # Makefile for various Intel thermal drivers.
 
+obj-$(CONFIG_INTEL_TCC)	+= intel_tcc.o
 obj-$(CONFIG_INTEL_POWERCLAMP)	+= intel_powerclamp.o
 obj-$(CONFIG_X86_PKG_TEMP_THERMAL)	+= x86_pkg_temp_thermal.o
 obj-$(CONFIG_INTEL_SOC_DTS_IOSF_CORE)	+= intel_soc_dts_iosf.o
drivers/thermal/intel/intel_tcc.c new file +139 -0

139 lines not on this page · raw message

include/linux/intel_tcc.h new file +18 -0
new file mode 100644
index 000000000000..f422612c28d6
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  header for Intel TCC (thermal control circuitry) library
+ *
+ *  Copyright (C) 2022  Intel Corporation.
+ */
+
+#ifndef __INTEL_TCC_H__
+#define __INTEL_TCC_H__
+
+#include <linux/types.h>
+
+int intel_tcc_get_tjmax(int cpu);
+int intel_tcc_get_offset(int cpu);
+int intel_tcc_set_offset(int cpu, int offset);
+int intel_tcc_get_temp(int cpu, bool pkg);
+
+#endif /* __INTEL_TCC_H__ */
Signature · 1 line
2.25.1
author reviewer
#2 lore raw

PATCH v2 2/6 thermal/int340x/processor_thermal: Use Intel TCC library

In reply to Zhang Rui

Cleanup the code by using Intel TCC library for TCC (Thermal Control Circuitry) MSR access.

---
 drivers/thermal/intel/int340x_thermal/Kconfig |   1 +
 .../processor_thermal_device.c                | 119 ++++--------------
 2 files changed, 22 insertions(+), 98 deletions(-)
drivers/thermal/intel/int340x_thermal/Kconfig +1 -0
index 5d046de96a5d..0f511917e0e1 100644
@@ -10,6 +10,7 @@ config INT340X_THERMAL
 	select ACPI_THERMAL_REL
 	select ACPI_FAN
 	select INTEL_SOC_DTS_IOSF_CORE
+	select INTEL_TCC
 	select PROC_THERMAL_MMIO_RAPL if POWERCAP
 	help
 	  Newer laptops and tablets that use ACPI may have thermal sensors and
drivers/thermal/intel/int340x_thermal/processor_thermal_device.c +21 -98

182 lines not on this page · raw message

Signature · 1 line
2.25.1
author reviewer
#3 lore raw

PATCH v2 3/6 thermal/intel/intel_soc_dts_iosf: Use Intel TCC library

In reply to Zhang Rui

Cleanup the code by using Intel TCC library for TCC (Thermal Control Circuitry) MSR access.

---
 drivers/thermal/intel/Kconfig              |  1 +
 drivers/thermal/intel/intel_soc_dts_iosf.c | 33 ++++------------------
 2 files changed, 6 insertions(+), 28 deletions(-)
drivers/thermal/intel/Kconfig +1 -0
index 6b938c040d6e..329c0ee934c4 100644
@@ -32,6 +32,7 @@ config INTEL_SOC_DTS_IOSF_CORE
 	tristate
 	depends on X86 && PCI
 	select IOSF_MBI
+	select INTEL_TCC
 	help
 	  This is becoming a common feature for Intel SoCs to expose the additional
 	  digital temperature sensors (DTSs) using side band interface (IOSF). This
drivers/thermal/intel/intel_soc_dts_iosf.c +5 -28
index 342b0bb5a56d..2138693d8afd 100644
@@ -7,6 +7,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/bitops.h>
+#include <linux/intel_tcc.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
@@ -45,32 +46,6 @@
 /* DTS0 and DTS 1 */
 #define SOC_MAX_DTS_SENSORS		2
 
-static int get_tj_max(u32 *tj_max)
-{
-	u32 eax, edx;
-	u32 val;
-	int err;
-
-	err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
-	if (err)
-		goto err_ret;
-	else {
-		val = (eax >> 16) & 0xff;
-		if (val)
-			*tj_max = val * 1000;
-		else {
-			err = -EINVAL;
-			goto err_ret;
-		}
-	}
-
-	return 0;
-err_ret:
-	*tj_max = 0;
-
-	return err;
-}
-
 static int sys_get_trip_temp(struct thermal_zone_device *tzd, int trip,
 			     int *temp)
 {
@@ -415,8 +390,9 @@ struct intel_soc_dts_sensors *intel_soc_dts_iosf_init(
 	if (!trip_count || read_only_trip_count > trip_count)
 		return ERR_PTR(-EINVAL);
 
-	if (get_tj_max(&tj_max))
-		return ERR_PTR(-EINVAL);
+	tj_max = intel_tcc_get_tjmax(-1);
+	if (tj_max < 0)
+		return ERR_PTR(tj_max);
 
 	sensors = kzalloc(sizeof(*sensors), GFP_KERNEL);
 	if (!sensors)
@@ -475,4 +451,5 @@ void intel_soc_dts_iosf_exit(struct intel_soc_dts_sensors *sensors)
 }
 EXPORT_SYMBOL_GPL(intel_soc_dts_iosf_exit);
 
+MODULE_IMPORT_NS(INTEL_TCC);
 MODULE_LICENSE("GPL v2");
Signature · 1 line
2.25.1
author reviewer
#4 lore raw

PATCH v2 4/6 thermal/intel/intel_tcc_cooling: Use Intel TCC library

In reply to Zhang Rui

Cleanup the code by using Intel TCC library for TCC (Thermal Control Circuitry) MSR access.

---
 drivers/thermal/intel/Kconfig             |  1 +
 drivers/thermal/intel/intel_tcc_cooling.c | 37 +++++------------------
 2 files changed, 9 insertions(+), 29 deletions(-)
drivers/thermal/intel/Kconfig +1 -0
index 329c0ee934c4..dafdb3dd3fc7 100644
@@ -88,6 +88,7 @@ config INTEL_PCH_THERMAL
 config INTEL_TCC_COOLING
 	tristate "Intel TCC offset cooling Driver"
 	depends on X86
+	select INTEL_TCC
 	help
 	  Enable this to support system cooling by adjusting the effective TCC
 	  activation temperature via the TCC Offset register, which is widely
drivers/thermal/intel/intel_tcc_cooling.c +8 -29
index 95adac427b6f..c5b559a123fd 100644
@@ -7,12 +7,11 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/device.h>
+#include <linux/intel_tcc.h>
 #include <linux/module.h>
 #include <linux/thermal.h>
 #include <asm/cpu_device_id.h>
 
-#define TCC_SHIFT 24
-#define TCC_MASK	(0x3fULL<<24)
 #define TCC_PROGRAMMABLE	BIT(30)
 
 static struct thermal_cooling_device *tcc_cdev;
@@ -20,47 +19,26 @@ static struct thermal_cooling_device *tcc_cdev;
 static int tcc_get_max_state(struct thermal_cooling_device *cdev, unsigned long
 			     *state)
 {
-	*state = TCC_MASK >> TCC_SHIFT;
-	return 0;
-}
-
-static int tcc_offset_update(int tcc)
-{
-	u64 val;
-	int err;
-
-	err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
-	if (err)
-		return err;
-
-	val &= ~TCC_MASK;
-	val |= tcc << TCC_SHIFT;
-
-	err = wrmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, val);
-	if (err)
-		return err;
-
+	*state = 0x3f;
 	return 0;
 }
 
 static int tcc_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
 			     *state)
 {
-	u64 val;
-	int err;
+	int offset = intel_tcc_get_offset(-1);
 
-	err = rdmsrl_safe(MSR_IA32_TEMPERATURE_TARGET, &val);
-	if (err)
-		return err;
+	if (offset < 0)
+		return offset;
 
-	*state = (val & TCC_MASK) >> TCC_SHIFT;
+	*state = offset;
 	return 0;
 }
 
 static int tcc_set_cur_state(struct thermal_cooling_device *cdev, unsigned long
 			     state)
 {
-	return tcc_offset_update(state);
+	return intel_tcc_set_offset(-1, (int)state);
 }
 
 static const struct thermal_cooling_device_ops tcc_cooling_ops = {
@@ -129,6 +107,7 @@ static void __exit tcc_cooling_exit(void)
 
 module_exit(tcc_cooling_exit)
 
+MODULE_IMPORT_NS(INTEL_TCC);
 MODULE_DESCRIPTION("TCC offset cooling device Driver");
 MODULE_AUTHOR("Zhang Rui <[email protected]>");
 MODULE_LICENSE("GPL v2");
Signature · 1 line
2.25.1
author reviewer
#5 lore raw

PATCH v2 5/6 thermal/x86_pkg_temp_thermal: Use Intel TCC library

In reply to Zhang Rui

Cleanup the code by using Intel TCC library for TCC (Thermal Control Circuitry) MSR access.

---
 drivers/thermal/intel/Kconfig                |  1 +
 drivers/thermal/intel/x86_pkg_temp_thermal.c | 44 ++++++--------------
 2 files changed, 14 insertions(+), 31 deletions(-)
drivers/thermal/intel/Kconfig +1 -0
index dafdb3dd3fc7..fd41c810629b 100644
@@ -21,6 +21,7 @@ config X86_PKG_TEMP_THERMAL
 	depends on X86_THERMAL_VECTOR
 	select THERMAL_GOV_USER_SPACE
 	select THERMAL_WRITABLE_TRIPS
+	select INTEL_TCC
 	default m
 	help
 	  Enable this to register CPU digital sensor for package temperature as
drivers/thermal/intel/x86_pkg_temp_thermal.c +13 -31

72 lines not on this page · raw message

Signature · 1 line
2.25.1
author reviewer
#6 lore raw

PATCH v2 6/6 thermal/x86_pkg_temp_thermal: Add support for handling dynamic tjmax

In reply to Zhang Rui

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.

Enhance the code to use updated tjmax when programming the thermal interrupt thresholds.

---
 drivers/thermal/intel/x86_pkg_temp_thermal.c | 30 +++++++++++++-------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/thermal/intel/x86_pkg_temp_thermal.c +19 -11
index 416ff23da4de..954cbceaf786 100644
@@ -49,7 +49,6 @@ MODULE_PARM_DESC(notify_delay_ms,
 struct zone_device {
 	int				cpu;
 	bool				work_scheduled;
-	u32				tj_max;
 	u32				msr_pkg_therm_low;
 	u32				msr_pkg_therm_high;
 	struct delayed_work		work;
@@ -125,7 +124,7 @@ static int sys_get_trip_temp(struct thermal_zone_device *tzd,
 	struct zone_device *zonedev = tzd->devdata;
 	unsigned long thres_reg_value;
 	u32 mask, shift, eax, edx;
-	int ret;
+	int tj_max, ret;
 
 	if (trip >= MAX_NUMBER_OF_TRIPS)
 		return -EINVAL;
@@ -138,6 +137,11 @@ static int sys_get_trip_temp(struct thermal_zone_device *tzd,
 		shift = THERM_SHIFT_THRESHOLD0;
 	}
 
+	tj_max = intel_tcc_get_tjmax(zonedev->cpu);
+	if (tj_max < 0)
+		return tj_max;
+	tj_max *= 1000;
+
 	ret = rdmsr_on_cpu(zonedev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
 			   &eax, &edx);
 	if (ret < 0)
@@ -145,7 +149,7 @@ static int sys_get_trip_temp(struct thermal_zone_device *tzd,
 
 	thres_reg_value = (eax & mask) >> shift;
 	if (thres_reg_value)
-		*temp = zonedev->tj_max - thres_reg_value * 1000;
+		*temp = tj_max - thres_reg_value * 1000;
 	else
 		*temp = THERMAL_TEMP_INVALID;
 	pr_debug("sys_get_trip_temp %d\n", *temp);
@@ -158,9 +162,14 @@ sys_set_trip_temp(struct thermal_zone_device *tzd, int trip, int temp)
 {
 	struct zone_device *zonedev = tzd->devdata;
 	u32 l, h, mask, shift, intr;
-	int ret;
+	int tj_max, ret;
 
-	if (trip >= MAX_NUMBER_OF_TRIPS || temp >= zonedev->tj_max)
+	tj_max = intel_tcc_get_tjmax(zonedev->cpu);
+	if (tj_max < 0)
+		return tj_max;
+	tj_max *= 1000;
+
+	if (trip >= MAX_NUMBER_OF_TRIPS || temp >= tj_max)
 		return -EINVAL;
 
 	ret = rdmsr_on_cpu(zonedev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
@@ -185,7 +194,7 @@ sys_set_trip_temp(struct thermal_zone_device *tzd, int trip, int temp)
 	if (!temp) {
 		l &= ~intr;
 	} else {
-		l |= (zonedev->tj_max - temp)/1000 << shift;
+		l |= (tj_max - temp)/1000 << shift;
 		l |= intr;
 	}
 
@@ -312,7 +321,7 @@ static int pkg_thermal_notify(u64 msr_val)
 static int pkg_temp_thermal_device_add(unsigned int cpu)
 {
 	int id = topology_logical_die_id(cpu);
-	u32 tj_max, eax, ebx, ecx, edx;
+	u32 eax, ebx, ecx, edx;
 	struct zone_device *zonedev;
 	int thres_count, err;
 
@@ -326,9 +335,9 @@ static int pkg_temp_thermal_device_add(unsigned int cpu)
 
 	thres_count = clamp_val(thres_count, 0, MAX_NUMBER_OF_TRIPS);
 
-	tj_max = intel_tcc_get_tjmax(cpu);
-	if (tj_max < 0)
-		return tj_max;
+	err = intel_tcc_get_tjmax(cpu);
+	if (err < 0)
+		return err;
 
 	zonedev = kzalloc(sizeof(*zonedev), GFP_KERNEL);
 	if (!zonedev)
@@ -336,7 +345,6 @@ static int pkg_temp_thermal_device_add(unsigned int cpu)
 
 	INIT_DELAYED_WORK(&zonedev->work, pkg_temp_thermal_threshold_work_fn);
 	zonedev->cpu = cpu;
-	zonedev->tj_max = tj_max;
 	zonedev->tzone = thermal_zone_device_register("x86_pkg_temp",
 			thres_count,
 			(thres_count == MAX_NUMBER_OF_TRIPS) ? 0x03 : 0x01,
Signature · 1 line
2.25.1
#7 lore raw
In reply to Zhang Rui
Quoted 15 lines · Zhang Rui

Currently, all the thermal drivers that access the Intel CPU TCC (thermal control circuitry) MSRs have two problems,
1. they have their own implementation for the same TCC functionalities, including getting the tjmax/temperature, getting/setting the TCC offset. This introduces a lot of duplicate code.
2. they get TjMax value once and always use the cached value later. But Tjmax value retrieved from MSR_IA32_TEMPERATURE_TARGET can be changed at runtime for cases like the Intel SST-PP (Intel Speed Select Technology- Performance Profile) level change.

The intel-tcc library is introduced in patch 1/6 for cleaning up the duplicate code among these drivers, and it also ensures the temperature is alway got based on the updated tjmax value.

Quoted 7 lines

Patch 2 ~ 5 cleans up the drivers by using the new Intel TCC lib APIs.

Patch 6/6 enhances the x86_pkg_temp driver to handle dynamic tjmax when progamming the thermal interrupt threshold.
Actually, the thermal interrupt threshold programming can also be part of the TCC library, but I didn't do it in this version because x86_pkg_temp is the only user for now.

All patches applied as 6.3 material, thanks!